Qt: MemoryModel scrolling

This commit is contained in:
Jeffrey Pfau 2015-04-29 00:23:25 -07:00
parent 0bc9d18bc7
commit e3cc98e034
2 changed files with 24 additions and 3 deletions

View File

@ -11,6 +11,7 @@
#include <QPainter>
#include <QScrollBar>
#include <QSlider>
#include <QWheelEvent>
extern "C" {
#include "gba/memory.h"
@ -33,9 +34,10 @@ MemoryModel::MemoryModel(QWidget* parent)
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_margins = QMargins(metrics.width("FFFFFF ") + 3, m_cellHeight + 1, metrics.width(" AAAAAAAAAAAAAAAA") + 3, 0);
verticalScrollBar()->setRange(0, 0x01000000 - viewport()->size().height() / m_cellHeight);
verticalScrollBar()->setRange(0, 0x01000001 - viewport()->size().height() / m_cellHeight);
connect(verticalScrollBar(), &QSlider::sliderMoved, [this](int position) {
m_top = position;
update();
});
update();
}
@ -45,7 +47,8 @@ void MemoryModel::setController(GameController* controller) {
}
void MemoryModel::resizeEvent(QResizeEvent*) {
verticalScrollBar()->setRange(0, 0x01000000 - viewport()->size().height() / m_cellHeight);
verticalScrollBar()->setRange(0, 0x01000001 - viewport()->size().height() / m_cellHeight);
boundsCheck();
}
void MemoryModel::paintEvent(QPaintEvent* event) {
@ -59,7 +62,7 @@ void MemoryModel::paintEvent(QPaintEvent* event) {
for (int x = 0; x < 16; ++x) {
painter.drawText(QRectF(QPointF(cellSize.width() * x + m_margins.left(), 0), cellSize), Qt::AlignHCenter, QString::number(x, 16).toUpper());
}
int height = (viewport()->size().height() - m_cellHeight + 1) / m_cellHeight;
int height = (viewport()->size().height() - m_cellHeight) / m_cellHeight;
for (int y = 0; y < height; ++y) {
int yp = m_cellHeight * y + m_margins.top();
QString data = QString("%0").arg(y + m_top, 6, 16, c0).toUpper();
@ -79,3 +82,19 @@ void MemoryModel::paintEvent(QPaintEvent* event) {
painter.drawLine(viewport()->size().width() - m_margins.right(), 0, viewport()->size().width() - m_margins.right(), viewport()->size().height());
painter.drawLine(0, m_margins.top(), viewport()->size().width(), m_margins.top());
}
void MemoryModel::wheelEvent(QWheelEvent* event) {
m_top -= event->angleDelta().y() / 8;
boundsCheck();
event->accept();
update();
QAbstractScrollArea::wheelEvent(event);
}
void MemoryModel::boundsCheck() {
if (m_top < 0) {
m_top = 0;
} else if (m_top > 0x01000001 - viewport()->size().height() / m_cellHeight) {
m_top = 0x01000001 - viewport()->size().height() / m_cellHeight;
}
}

View File

@ -30,6 +30,8 @@ protected:
void wheelEvent(QWheelEvent*) override;
private:
void boundsCheck();
ARMCore* m_cpu;
QFont m_font;
int m_cellHeight;