Qt: Start cheat input
Jeffrey Pfau jeffrey@endrift.com
Sat, 14 Feb 2015 17:45:46 -0800
5 files changed,
99 insertions(+),
16 deletions(-)
M
src/platform/qt/CheatsModel.cpp
→
src/platform/qt/CheatsModel.cpp
@@ -27,6 +27,7 @@ int row = index.row();
const GBACheatSet* cheats = static_cast<const GBACheatSet*>(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 @@ return QVariant();
} } +bool CheatsModel::setData(const QModelIndex& index, const QVariant& value, int role) { + if (!index.isValid()) { + return false; + } + + int row = index.row(); + GBACheatSet* cheats = static_cast<GBACheatSet*>(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 @@ }
return GBACheatSetsSize(&m_device->cheats); } +GBACheatSet* CheatsModel::itemAt(const QModelIndex& index) { + if (!index.isValid()) { + return nullptr; + } + return static_cast<GBACheatSet*>(index.internalPointer()); +} + void CheatsModel::loadFile(const QString& path) { VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY); if (!vf) {@@ -63,4 +102,10 @@ beginResetModel();
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(); }
M
src/platform/qt/CheatsModel.h
→
src/platform/qt/CheatsModel.h
@@ -9,6 +9,7 @@
#include <QAbstractItemModel> 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;
M
src/platform/qt/CheatsView.cpp
→
src/platform/qt/CheatsView.cpp
@@ -7,6 +7,10 @@ #include "CheatsView.h"
#include <QFileDialog> +extern "C" { +#include "gba/cheats.h" +} + using namespace QGBA; CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent)@@ -18,6 +22,19 @@
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 @@ QString filename = QFileDialog::getOpenFileName(this, tr("Select cheats file"));
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<bool(GBACheatSet*, const char*)> 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(); }
M
src/platform/qt/CheatsView.h
→
src/platform/qt/CheatsView.h
@@ -8,6 +8,8 @@ #define QGBA_CHEATS_VIEW
#include <QWidget> +#include <functional> + #include "CheatsModel.h" #include "ui_CheatsView.h"@@ -24,8 +26,11 @@ CheatsView(GBACheatDevice* device, QWidget* parent = nullptr);
private slots: void load(); + void addSet(); private: + void enterCheat(std::function<bool(GBACheatSet*, const char*)> callback); + Ui::CheatsView m_ui; CheatsModel m_model; };
M
src/platform/qt/CheatsView.ui
→
src/platform/qt/CheatsView.ui
@@ -26,9 +26,6 @@ </widget>
</item> <item row="0" column="1" colspan="2"> <widget class="QPushButton" name="addSet"> - <property name="enabled"> - <bool>false</bool> - </property> <property name="text"> <string>Add New Set</string> </property>@@ -36,9 +33,6 @@ </widget>
</item> <item row="7" column="1" colspan="2"> <widget class="QPushButton" name="addGSA"> - <property name="enabled"> - <bool>false</bool> - </property> <property name="text"> <string>Add GameShark</string> </property>@@ -56,9 +50,6 @@ </widget>
</item> <item row="9" column="1" colspan="2"> <widget class="QPushButton" name="addCB"> - <property name="enabled"> - <bool>false</bool> - </property> <property name="text"> <string>Add CodeBreaker</string> </property>@@ -73,9 +64,6 @@ </widget>
</item> <item row="6" column="1" colspan="2"> <widget class="QPushButton" name="add"> - <property name="enabled"> - <bool>false</bool> - </property> <property name="text"> <string>Add</string> </property>@@ -115,10 +103,7 @@ </property>
</widget> </item> <item row="4" column="1" rowspan="2" colspan="2"> - <widget class="QTextEdit" name="codeEntry"> - <property name="enabled"> - <bool>false</bool> - </property> + <widget class="QPlainTextEdit" name="codeEntry"> <property name="sizePolicy"> <sizepolicy hsizetype="Maximum" vsizetype="Expanding"> <horstretch>0</horstretch>@@ -130,6 +115,11 @@ <size>
<width>180</width> <height>16777215</height> </size> + </property> + <property name="font"> + <font> + <family>Courier New</family> + </font> </property> </widget> </item>