Qt: Start cheat input

This commit is contained in:
Jeffrey Pfau 2015-02-14 17:45:46 -08:00
parent c8d3488804
commit 297551a5be
5 changed files with 99 additions and 16 deletions

View File

@ -27,6 +27,7 @@ QVariant CheatsModel::data(const QModelIndex& index, int role) const {
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 @@ 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<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 @@ 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<GBACheatSet*>(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();
}

View File

@ -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;

View File

@ -7,6 +7,10 @@
#include <QFileDialog>
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<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();
}

View File

@ -8,6 +8,8 @@
#include <QWidget>
#include <functional>
#include "CheatsModel.h"
#include "ui_CheatsView.h"
@ -24,8 +26,11 @@ public:
private slots:
void load();
void addSet();
private:
void enterCheat(std::function<bool(GBACheatSet*, const char*)> callback);
Ui::CheatsView m_ui;
CheatsModel m_model;
};

View File

@ -26,9 +26,6 @@
</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 @@
</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 @@
</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 @@
</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 @@
</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>
@ -131,6 +116,11 @@
<height>16777215</height>
</size>
</property>
<property name="font">
<font>
<family>Courier New</family>
</font>
</property>
</widget>
</item>
</layout>