Qt: Add BattleChip "deck" save/load
Vicki Pfau vi@endrift.com
Thu, 21 Feb 2019 19:13:50 -0800
3 files changed,
246 insertions(+),
123 deletions(-)
M
src/platform/qt/BattleChipView.cpp
→
src/platform/qt/BattleChipView.cpp
@@ -13,6 +13,7 @@ #include <QtAlgorithms>
#include <QFile> #include <QFontMetrics> #include <QResource> +#include <QSettings> #include <QStringList> using namespace QGBA;@@ -53,6 +54,8 @@ connect(m_ui.insert, &QAbstractButton::clicked, this, &BattleChipView::reinsert);
connect(m_ui.add, &QAbstractButton::clicked, this, &BattleChipView::addChip); connect(m_ui.remove, &QAbstractButton::clicked, this, &BattleChipView::removeChip); connect(controller.get(), &CoreController::stopping, this, &QWidget::close); + connect(m_ui.save, &QAbstractButton::clicked, this, &BattleChipView::saveDeck); + connect(m_ui.load, &QAbstractButton::clicked, this, &BattleChipView::loadDeck); connect(m_ui.gateBattleChip, &QAbstractButton::toggled, this, [this](bool on) { if (on) {@@ -130,9 +133,13 @@ int insertedChip = m_ui.chipId->value();
if (insertedChip < 1) { return; } - QListWidgetItem* add = new QListWidgetItem(m_chipIdToName[insertedChip]); - add->setData(Qt::UserRole, insertedChip); - QString path = QString(":/res/exe%1/%2.png").arg(m_flavor).arg(insertedChip, 3, 10, QLatin1Char('0')); + addChipId(insertedChip); +} + +void BattleChipView::addChipId(int id) { + QListWidgetItem* add = new QListWidgetItem(m_chipIdToName[id]); + add->setData(Qt::UserRole, id); + QString path = QString(":/res/exe%1/%2.png").arg(m_flavor).arg(id, 3, 10, QLatin1Char('0')); if (!QFile(path).exists()) { path = QString(":/res/exe%1/placeholder.png").arg(m_flavor); }@@ -183,5 +190,50 @@ insertChip(m_next);
} if (m_frameCounter >= 0) { --m_frameCounter; + } +} + +void BattleChipView::saveDeck() { + QString filename = GBAApp::app()->getSaveFileName(this, tr("Select deck file"), tr(("BattleChip deck file (*.deck)"))); + if (filename.isEmpty()) { + return; + } + + QStringList deck; + for (int i = 0; i < m_ui.chipList->count(); ++i) { + deck.append(m_ui.chipList->item(i)->data(Qt::UserRole).toString()); + } + + QSettings ini(filename, QSettings::IniFormat); + ini.clear(); + ini.beginGroup("BattleChipDeck"); + ini.setValue("version", m_flavor); + ini.setValue("deck", deck.join(',')); + ini.sync(); +} + +void BattleChipView::loadDeck() { + QString filename = GBAApp::app()->getOpenFileName(this, tr("Select deck file"), tr(("BattleChip deck file (*.deck)"))); + if (filename.isEmpty()) { + return; + } + + QSettings ini(filename, QSettings::IniFormat); + ini.beginGroup("BattleChipDeck"); + int flavor = ini.value("version").toInt(); + if (flavor != m_flavor) { + return; + } + + while (m_ui.chipList->count()) { + delete m_ui.chipList->takeItem(m_ui.chipList->count() - 1); + } + QStringList deck = ini.value("deck").toString().split(','); + for (const auto& item : deck) { + bool ok; + int id = item.toInt(&ok); + if (ok) { + addChipId(id); + } } }
M
src/platform/qt/BattleChipView.h
→
src/platform/qt/BattleChipView.h
@@ -32,7 +32,11 @@
private slots: void advanceFrameCounter(); void addChip(); + void addChipId(int); void removeChip(); + + void saveDeck(); + void loadDeck(); private: static const int UNINSERTED_TIME = 10;
M
src/platform/qt/BattleChipView.ui
→
src/platform/qt/BattleChipView.ui
@@ -6,121 +6,15 @@ <property name="geometry">
<rect> <x>0</x> <y>0</y> - <width>794</width> - <height>893</height> + <width>630</width> + <height>722</height> </rect> </property> <property name="windowTitle"> <string>BattleChip Gate</string> </property> - <layout class="QGridLayout" name="gridLayout" rowstretch="1,0,0,0,0" columnstretch="1,0,1"> - <item row="2" column="0"> - <layout class="QFormLayout" name="formLayout_2"> - <item row="1" column="0"> - <widget class="QLabel" name="label_3"> - <property name="text"> - <string>Gate type</string> - </property> - </widget> - </item> - <item row="1" column="1"> - <widget class="QRadioButton" name="gateBattleChip"> - <property name="text"> - <string>Ba&ttleChip Gate</string> - </property> - <property name="checked"> - <bool>true</bool> - </property> - </widget> - </item> - <item row="2" column="1"> - <widget class="QRadioButton" name="gateProgress"> - <property name="text"> - <string>Progress &Gate</string> - </property> - </widget> - </item> - <item row="3" column="1"> - <widget class="QRadioButton" name="gateBeastLink"> - <property name="text"> - <string>Beast &Link Gate</string> - </property> - </widget> - </item> - </layout> - </item> - <item row="2" column="2"> - <layout class="QFormLayout" name="formLayout_3"> - <item row="0" column="0"> - <widget class="QLabel" name="label"> - <property name="text"> - <string>Chip ID</string> - </property> - </widget> - </item> - <item row="0" column="1"> - <widget class="QSpinBox" name="chipId"> - <property name="maximum"> - <number>65535</number> - </property> - </widget> - </item> - <item row="1" column="1"> - <widget class="QCheckBox" name="inserted"> - <property name="text"> - <string>Inserted</string> - </property> - </widget> - </item> - </layout> - </item> - <item row="2" column="1"> - <widget class="Line" name="line"> - <property name="orientation"> - <enum>Qt::Vertical</enum> - </property> - </widget> - </item> - <item row="1" column="0" colspan="3"> - <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,0,0,0"> - <item> - <layout class="QFormLayout" name="formLayout_4"> - <item row="0" column="1"> - <widget class="QComboBox" name="chipName"/> - </item> - <item row="0" column="0"> - <widget class="QLabel" name="label_2"> - <property name="text"> - <string>Chip name</string> - </property> - </widget> - </item> - </layout> - </item> - <item> - <widget class="QPushButton" name="add"> - <property name="text"> - <string>Add</string> - </property> - </widget> - </item> - <item> - <widget class="QPushButton" name="remove"> - <property name="text"> - <string>Remove</string> - </property> - </widget> - </item> - <item> - <widget class="QPushButton" name="insert"> - <property name="text"> - <string>Insert</string> - </property> - </widget> - </item> - </layout> - </item> - <item row="0" column="0" colspan="3"> + <layout class="QVBoxLayout" name="verticalLayout" stretch="1,0,0,0,0"> + <item> <widget class="QListWidget" name="chipList"> <property name="acceptDrops"> <bool>true</bool>@@ -160,30 +54,203 @@ <enum>QListView::IconMode</enum>
</property> </widget> </item> - <item row="4" column="0" colspan="3"> - <widget class="QDialogButtonBox" name="buttonBox"> - <property name="standardButtons"> - <set>QDialogButtonBox::Close</set> - </property> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_2" stretch="1,0"> + <item> + <layout class="QFormLayout" name="formLayout_4"> + <item row="0" column="1"> + <widget class="QComboBox" name="chipName"/> + </item> + <item row="0" column="0"> + <widget class="QLabel" name="label_2"> + <property name="text"> + <string>Chip name</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QPushButton" name="insert"> + <property name="text"> + <string>Insert</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_3"> + <item> + <widget class="QPushButton" name="save"> + <property name="text"> + <string>Save</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="load"> + <property name="text"> + <string>Load</string> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QPushButton" name="add"> + <property name="text"> + <string>Add</string> + </property> + </widget> + </item> + <item> + <widget class="QPushButton" name="remove"> + <property name="text"> + <string>Remove</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="QWidget" name="advanced" native="true"> + <property name="visible"> + <bool>false</bool> + </property> + <layout class="QHBoxLayout" name="horizontalLayout_4"> + <item> + <layout class="QFormLayout" name="formLayout_2"> + <item row="1" column="0"> + <widget class="QLabel" name="label_3"> + <property name="text"> + <string>Gate type</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QRadioButton" name="gateBattleChip"> + <property name="text"> + <string>Ba&ttleChip Gate</string> + </property> + <property name="checked"> + <bool>true</bool> + </property> + </widget> + </item> + <item row="2" column="1"> + <widget class="QRadioButton" name="gateProgress"> + <property name="text"> + <string>Progress &Gate</string> + </property> + </widget> + </item> + <item row="3" column="1"> + <widget class="QRadioButton" name="gateBeastLink"> + <property name="text"> + <string>Beast &Link Gate</string> + </property> + </widget> + </item> + </layout> + </item> + <item> + <widget class="Line" name="line"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + </widget> + </item> + <item> + <layout class="QFormLayout" name="formLayout_5"> + <item row="0" column="0"> + <widget class="QLabel" name="label_4"> + <property name="text"> + <string>Chip ID</string> + </property> + </widget> + </item> + <item row="0" column="1"> + <widget class="QSpinBox" name="chipId"> + <property name="maximum"> + <number>65535</number> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QCheckBox" name="inserted"> + <property name="text"> + <string>Inserted</string> + </property> + </widget> + </item> + </layout> + </item> + </layout> </widget> </item> + <item> + <layout class="QHBoxLayout" name="horizontalLayout_5"> + <item> + <widget class="QCheckBox" name="showAdvanced"> + <property name="text"> + <string>Show advanced</string> + </property> + </widget> + </item> + <item> + <widget class="QDialogButtonBox" name="buttonBox"> + <property name="standardButtons"> + <set>QDialogButtonBox::Close</set> + </property> + </widget> + </item> + </layout> + </item> </layout> </widget> <resources/> <connections> <connection> + <sender>showAdvanced</sender> + <signal>toggled(bool)</signal> + <receiver>advanced</receiver> + <slot>setVisible(bool)</slot> + <hints> + <hint type="sourcelabel"> + <x>109</x> + <y>34</y> + </hint> + <hint type="destinationlabel"> + <x>396</x> + <y>654</y> + </hint> + </hints> + </connection> + <connection> <sender>buttonBox</sender> <signal>rejected()</signal> <receiver>BattleChipView</receiver> - <slot>close()</slot> + <slot>reject()</slot> <hints> <hint type="sourcelabel"> - <x>310</x> - <y>632</y> + <x>416</x> + <y>690</y> </hint> <hint type="destinationlabel"> - <x>310</x> - <y>331</y> + <x>314</x> + <y>360</y> </hint> </hints> </connection>