diff --git a/src/platform/qt/CheatsModel.cpp b/src/platform/qt/CheatsModel.cpp index 913f86394..93124667c 100644 --- a/src/platform/qt/CheatsModel.cpp +++ b/src/platform/qt/CheatsModel.cpp @@ -27,6 +27,7 @@ QVariant CheatsModel::data(const QModelIndex& index, int role) const { const GBACheatSet* cheats = static_cast(index.internalPointer()); switch (role) { case Qt::DisplayRole: + case Qt::EditRole: return cheats->name ? cheats->name : tr("(untitled)"); case Qt::CheckStateRole: return Qt::Checked; @@ -35,6 +36,29 @@ QVariant CheatsModel::data(const QModelIndex& index, int role) const { } } +bool CheatsModel::setData(const QModelIndex& index, const QVariant& value, int role) { + if (!index.isValid()) { + return false; + } + + int row = index.row(); + GBACheatSet* cheats = static_cast(index.internalPointer()); + switch (role) { + case Qt::DisplayRole: + case Qt::EditRole: + if (cheats->name) { + free(cheats->name); + cheats->name = nullptr; + } + cheats->name = strdup(value.toString().toLocal8Bit().constData()); + return true; + case Qt::CheckStateRole: + return false; + default: + return false; + } +} + QModelIndex CheatsModel::index(int row, int column, const QModelIndex& parent) const { return createIndex(row, column, *GBACheatSetsGetPointer(&m_device->cheats, row)); } @@ -43,6 +67,14 @@ QModelIndex CheatsModel::parent(const QModelIndex& index) const { return QModelIndex(); } +Qt::ItemFlags CheatsModel::flags(const QModelIndex &index) const { + if (!index.isValid()) { + return 0; + } + + return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} + int CheatsModel::columnCount(const QModelIndex& parent) const { return 1; } @@ -54,6 +86,13 @@ int CheatsModel::rowCount(const QModelIndex& parent) const { return GBACheatSetsSize(&m_device->cheats); } +GBACheatSet* CheatsModel::itemAt(const QModelIndex& index) { + if (!index.isValid()) { + return nullptr; + } + return static_cast(index.internalPointer()); +} + void CheatsModel::loadFile(const QString& path) { VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY); if (!vf) { @@ -63,4 +102,10 @@ void CheatsModel::loadFile(const QString& path) { GBACheatParseFile(m_device, vf); endResetModel(); vf->close(vf); +} + +void CheatsModel::addSet(GBACheatSet* set) { + beginInsertRows(QModelIndex(), GBACheatSetsSize(&m_device->cheats), GBACheatSetsSize(&m_device->cheats) + 1); + *GBACheatSetsAppend(&m_device->cheats) = set; + endInsertRows(); } \ No newline at end of file diff --git a/src/platform/qt/CheatsModel.h b/src/platform/qt/CheatsModel.h index 6d8ec1dc2..cada0b81e 100644 --- a/src/platform/qt/CheatsModel.h +++ b/src/platform/qt/CheatsModel.h @@ -9,6 +9,7 @@ #include struct GBACheatDevice; +struct GBACheatSet; namespace QGBA { @@ -19,14 +20,19 @@ public: CheatsModel(GBACheatDevice* m_device, QObject* parent = nullptr); virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; + virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override; virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override; virtual QModelIndex parent(const QModelIndex& index) const override; virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override; virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override; + virtual Qt::ItemFlags flags(const QModelIndex &index) const override; + + GBACheatSet* itemAt(const QModelIndex& index); void loadFile(const QString& path); + void addSet(GBACheatSet* set); private: GBACheatDevice* m_device; diff --git a/src/platform/qt/CheatsView.cpp b/src/platform/qt/CheatsView.cpp index a9f4f6bd1..92ce07d66 100644 --- a/src/platform/qt/CheatsView.cpp +++ b/src/platform/qt/CheatsView.cpp @@ -7,6 +7,10 @@ #include +extern "C" { +#include "gba/cheats.h" +} + using namespace QGBA; CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent) @@ -18,6 +22,19 @@ CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent) m_ui.cheatList->setModel(&m_model); connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load())); + connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet())); + + connect(m_ui.add, &QPushButton::clicked, [this]() { + enterCheat(GBACheatAddLine); + }); + + connect(m_ui.addGSA, &QPushButton::clicked, [this]() { + enterCheat(GBACheatAddGameSharkLine); + }); + + connect(m_ui.addCB, &QPushButton::clicked, [this]() { + enterCheat(GBACheatAddCodeBreakerLine); + }); } void CheatsView::load() { @@ -25,4 +42,24 @@ void CheatsView::load() { if (!filename.isEmpty()) { m_model.loadFile(filename); } +} + +void CheatsView::addSet() { + GBACheatSet* set = new GBACheatSet; + GBACheatSetInit(set, nullptr); + m_model.addSet(set); +} + +void CheatsView::enterCheat(std::function callback) { + GBACheatSet* set; + QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes(); + if (selection.count() != 1) { + return; + } + set = m_model.itemAt(selection[0]); + QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts); + for (const QString& string : cheats) { + callback(set, string.toLocal8Bit().constData()); + } + m_ui.codeEntry->clear(); } \ No newline at end of file diff --git a/src/platform/qt/CheatsView.h b/src/platform/qt/CheatsView.h index f247b37f1..7c276a7d8 100644 --- a/src/platform/qt/CheatsView.h +++ b/src/platform/qt/CheatsView.h @@ -8,6 +8,8 @@ #include +#include + #include "CheatsModel.h" #include "ui_CheatsView.h" @@ -24,8 +26,11 @@ public: private slots: void load(); + void addSet(); private: + void enterCheat(std::function callback); + Ui::CheatsView m_ui; CheatsModel m_model; }; diff --git a/src/platform/qt/CheatsView.ui b/src/platform/qt/CheatsView.ui index e98646e36..ac89715a8 100644 --- a/src/platform/qt/CheatsView.ui +++ b/src/platform/qt/CheatsView.ui @@ -26,9 +26,6 @@ - - false - Add New Set @@ -36,9 +33,6 @@ - - false - Add GameShark @@ -56,9 +50,6 @@ - - false - Add CodeBreaker @@ -73,9 +64,6 @@ - - false - Add @@ -115,10 +103,7 @@ - - - false - + 0 @@ -131,6 +116,11 @@ 16777215 + + + Courier New + +