From 475c7790c5ddd94127eaa0f4b6b569e7beae2f79 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 21 Feb 2019 19:13:50 -0800 Subject: [PATCH] Qt: Add BattleChip "deck" save/load --- src/platform/qt/BattleChipView.cpp | 58 +++++- src/platform/qt/BattleChipView.h | 4 + src/platform/qt/BattleChipView.ui | 307 ++++++++++++++++++----------- 3 files changed, 246 insertions(+), 123 deletions(-) diff --git a/src/platform/qt/BattleChipView.cpp b/src/platform/qt/BattleChipView.cpp index e05785f4f..53c9a3024 100644 --- a/src/platform/qt/BattleChipView.cpp +++ b/src/platform/qt/BattleChipView.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include using namespace QGBA; @@ -53,6 +54,8 @@ BattleChipView::BattleChipView(std::shared_ptr controller, QWidg 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 @@ void BattleChipView::addChip() { 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); } @@ -184,4 +191,49 @@ void BattleChipView::advanceFrameCounter() { 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); + } + } } \ No newline at end of file diff --git a/src/platform/qt/BattleChipView.h b/src/platform/qt/BattleChipView.h index 6b4582c36..423c937c8 100644 --- a/src/platform/qt/BattleChipView.h +++ b/src/platform/qt/BattleChipView.h @@ -32,8 +32,12 @@ public slots: private slots: void advanceFrameCounter(); void addChip(); + void addChipId(int); void removeChip(); + void saveDeck(); + void loadDeck(); + private: static const int UNINSERTED_TIME = 10; diff --git a/src/platform/qt/BattleChipView.ui b/src/platform/qt/BattleChipView.ui index 10e8000d8..a31513ced 100644 --- a/src/platform/qt/BattleChipView.ui +++ b/src/platform/qt/BattleChipView.ui @@ -6,121 +6,15 @@ 0 0 - 794 - 893 + 630 + 722 BattleChip Gate - - - - - - - Gate type - - - - - - - Ba&ttleChip Gate - - - true - - - - - - - Progress &Gate - - - - - - - Beast &Link Gate - - - - - - - - - - - Chip ID - - - - - - - 65535 - - - - - - - Inserted - - - - - - - - - Qt::Vertical - - - - - - - - - - - - - - Chip name - - - - - - - - - Add - - - - - - - Remove - - - - - - - Insert - - - - - - + + true @@ -160,30 +54,203 @@ - - - - QDialogButtonBox::Close - + + + + + + + + + + + Chip name + + + + + + + + + Insert + + + + + + + + + + + Save + + + + + + + Load + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Add + + + + + + + Remove + + + + + + + + + false + + + + + + + + Gate type + + + + + + + Ba&ttleChip Gate + + + true + + + + + + + Progress &Gate + + + + + + + Beast &Link Gate + + + + + + + + + Qt::Vertical + + + + + + + + + Chip ID + + + + + + + 65535 + + + + + + + Inserted + + + + + + + + + + + + Show advanced + + + + + + + QDialogButtonBox::Close + + + + + + + showAdvanced + toggled(bool) + advanced + setVisible(bool) + + + 109 + 34 + + + 396 + 654 + + + buttonBox rejected() BattleChipView - close() + reject() - 310 - 632 + 416 + 690 - 310 - 331 + 314 + 360