Qt: Tile viewer improvements, magnification

This commit is contained in:
Jeffrey Pfau 2016-08-23 10:31:01 -07:00
parent f491196bc4
commit 0241952133
4 changed files with 82 additions and 16 deletions

View File

@ -13,10 +13,12 @@ using namespace QGBA;
TilePainter::TilePainter(QWidget* parent) TilePainter::TilePainter(QWidget* parent)
: QWidget(parent) : QWidget(parent)
, m_size(8)
{ {
m_backing = QPixmap(256, 768); m_backing = QPixmap(256, 768);
m_backing.fill(Qt::transparent); m_backing.fill(Qt::transparent);
resize(256, 768); resize(256, 768);
setTileCount(3072);
} }
void TilePainter::paintEvent(QPaintEvent* event) { void TilePainter::paintEvent(QPaintEvent* event) {
@ -25,30 +27,37 @@ void TilePainter::paintEvent(QPaintEvent* event) {
} }
void TilePainter::resizeEvent(QResizeEvent* event) { void TilePainter::resizeEvent(QResizeEvent* event) {
if (width() / 8 != m_backing.width() / 8) { int calculatedHeight = (m_tileCount * m_size) / (width() / m_size) + m_size / 2;
m_backing = QPixmap(width(), (3072 * 8) / (width() / 8)); if (width() / m_size != m_backing.width() / m_size || m_backing.height() != calculatedHeight) {
m_backing = QPixmap(width(), calculatedHeight);
m_backing.fill(Qt::transparent); m_backing.fill(Qt::transparent);
} }
} }
void TilePainter::mousePressEvent(QMouseEvent* event) { void TilePainter::mousePressEvent(QMouseEvent* event) {
int x = event->x() / 8; int x = event->x() / m_size;
int y = event->y() / 8; int y = event->y() / m_size;
emit indexPressed(y * (width() / 8) + x); emit indexPressed(y * (width() / m_size) + x);
} }
void TilePainter::setTile(int index, const uint16_t* data) { void TilePainter::setTile(int index, const uint16_t* data) {
QPainter painter(&m_backing); QPainter painter(&m_backing);
int w = width() / 8; int w = width() / m_size;
int x = index % w; int x = index % w;
int y = index / w; int y = index / w;
QRect r(x * 8, y * 8, 8, 8); QRect r(x * m_size, y * m_size, m_size, m_size);
QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_RGB555); QImage tile(reinterpret_cast<const uchar*>(data), 8, 8, QImage::Format_RGB555);
painter.fillRect(r, tile.rgbSwapped()); painter.drawImage(r, tile.rgbSwapped());
update(r); update(r);
} }
void TilePainter::setTileCount(int tiles) { void TilePainter::setTileCount(int tiles) {
setMinimumSize(16, (tiles * 8) / (width() / 8)); m_tileCount = tiles;
setMinimumSize(16, (tiles * m_size) / (width() / m_size));
resizeEvent(nullptr); resizeEvent(nullptr);
} }
void TilePainter::setTileMagnification(int mag) {
m_size = mag * 8;
setTileCount(m_tileCount);
}

View File

@ -21,6 +21,7 @@ public:
public slots: public slots:
void setTile(int index, const uint16_t*); void setTile(int index, const uint16_t*);
void setTileCount(int tiles); void setTileCount(int tiles);
void setTileMagnification(int mag);
signals: signals:
void indexPressed(int index); void indexPressed(int index);
@ -32,6 +33,8 @@ protected:
private: private:
QPixmap m_backing; QPixmap m_backing;
int m_size;
int m_tileCount;
}; };
} }

View File

@ -26,7 +26,7 @@ TileView::TileView(GameController* controller, QWidget* parent)
m_ui.preview->setDimensions(QSize(8, 8)); m_ui.preview->setDimensions(QSize(8, 8));
m_updateTimer.setSingleShot(true); m_updateTimer.setSingleShot(true);
m_updateTimer.setInterval(10); m_updateTimer.setInterval(1);
connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTiles())); connect(&m_updateTimer, SIGNAL(timeout()), this, SLOT(updateTiles()));
const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont); const QFont font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
@ -38,7 +38,12 @@ TileView::TileView(GameController* controller, QWidget* parent)
connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close())); connect(m_controller, SIGNAL(gameStopped(mCoreThread*)), this, SLOT(close()));
connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int))); connect(m_ui.tiles, SIGNAL(indexPressed(int)), this, SLOT(selectIndex(int)));
connect(m_ui.paletteId, SIGNAL(valueChanged(int)), this, SLOT(updatePalette(int))); connect(m_ui.paletteId, SIGNAL(valueChanged(int)), this, SLOT(updatePalette(int)));
connect(m_ui.palette256, SIGNAL(toggled(bool)), this, SLOT(updateTiles())); connect(m_ui.palette256, &QAbstractButton::toggled, [this]() {
updateTiles(true);
});
connect(m_ui.magnification, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [this]() {
updateTiles(true);
});
} }
TileView::~TileView() { TileView::~TileView() {
@ -131,5 +136,5 @@ void TileView::resizeEvent(QResizeEvent*) {
} }
void TileView::showEvent(QShowEvent*) { void TileView::showEvent(QShowEvent*) {
m_updateTimer.start(); updateTiles(true);
} }

View File

@ -21,7 +21,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QGroupBox" name="groupBox"> <widget class="QGroupBox" name="groupBox">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred"> <sizepolicy hsizetype="Fixed" vsizetype="Preferred">
@ -116,7 +116,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<spacer name="verticalSpacer"> <spacer name="verticalSpacer">
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
@ -129,7 +129,7 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item row="0" column="1" rowspan="4"> <item row="0" column="1" rowspan="5">
<widget class="QScrollArea" name="scrollArea"> <widget class="QScrollArea" name="scrollArea">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Expanding"> <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
@ -145,7 +145,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>271</width> <width>290</width>
<height>768</height> <height>768</height>
</rect> </rect>
</property> </property>
@ -185,6 +185,36 @@
</widget> </widget>
</widget> </widget>
</item> </item>
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QSpinBox" name="magnification">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="suffix">
<string>×</string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>4</number>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Magnification</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>
@ -193,6 +223,9 @@
<extends>QWidget</extends> <extends>QWidget</extends>
<header>TilePainter.h</header> <header>TilePainter.h</header>
<container>1</container> <container>1</container>
<slots>
<slot>setTileMagnification(int)</slot>
</slots>
</customwidget> </customwidget>
<customwidget> <customwidget>
<class>QGBA::Swatch</class> <class>QGBA::Swatch</class>
@ -219,5 +252,21 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>magnification</sender>
<signal>valueChanged(int)</signal>
<receiver>tiles</receiver>
<slot>setTileMagnification(int)</slot>
<hints>
<hint type="sourcelabel">
<x>36</x>
<y>83</y>
</hint>
<hint type="destinationlabel">
<x>339</x>
<y>396</y>
</hint>
</hints>
</connection>
</connections> </connections>
</ui> </ui>