src/platform/qt/CheatsModel.cpp (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "CheatsModel.h"
7
8extern "C" {
9#include "gba/cheats.h"
10#include "util/vfs.h"
11}
12
13using namespace QGBA;
14
15CheatsModel::CheatsModel(GBACheatDevice* device, QObject* parent)
16 : QAbstractItemModel(parent)
17 , m_device(device)
18{
19}
20
21QVariant CheatsModel::data(const QModelIndex& index, int role) const {
22 if (!index.isValid()) {
23 return QVariant();
24 }
25
26 int row = index.row();
27 const GBACheatSet* cheats = static_cast<const GBACheatSet*>(index.internalPointer());
28 switch (role) {
29 case Qt::DisplayRole:
30 case Qt::EditRole:
31 return cheats->name ? cheats->name : tr("(untitled)");
32 case Qt::CheckStateRole:
33 return Qt::Checked;
34 default:
35 return QVariant();
36 }
37}
38
39bool CheatsModel::setData(const QModelIndex& index, const QVariant& value, int role) {
40 if (!index.isValid()) {
41 return false;
42 }
43
44 int row = index.row();
45 GBACheatSet* cheats = static_cast<GBACheatSet*>(index.internalPointer());
46 switch (role) {
47 case Qt::DisplayRole:
48 case Qt::EditRole:
49 if (cheats->name) {
50 free(cheats->name);
51 cheats->name = nullptr;
52 }
53 cheats->name = strdup(value.toString().toLocal8Bit().constData());
54 return true;
55 case Qt::CheckStateRole:
56 return false;
57 default:
58 return false;
59 }
60}
61
62QModelIndex CheatsModel::index(int row, int column, const QModelIndex& parent) const {
63 return createIndex(row, column, *GBACheatSetsGetPointer(&m_device->cheats, row));
64}
65
66QModelIndex CheatsModel::parent(const QModelIndex& index) const {
67 return QModelIndex();
68}
69
70Qt::ItemFlags CheatsModel::flags(const QModelIndex &index) const {
71 if (!index.isValid()) {
72 return 0;
73 }
74
75 return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
76}
77
78int CheatsModel::columnCount(const QModelIndex& parent) const {
79 return 1;
80}
81
82int CheatsModel::rowCount(const QModelIndex& parent) const {
83 if (parent.isValid()) {
84 return 0;
85 }
86 return GBACheatSetsSize(&m_device->cheats);
87}
88
89GBACheatSet* CheatsModel::itemAt(const QModelIndex& index) {
90 if (!index.isValid()) {
91 return nullptr;
92 }
93 return static_cast<GBACheatSet*>(index.internalPointer());
94}
95
96void CheatsModel::loadFile(const QString& path) {
97 VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
98 if (!vf) {
99 return;
100 }
101 beginResetModel();
102 GBACheatParseFile(m_device, vf);
103 endResetModel();
104 vf->close(vf);
105}
106
107void CheatsModel::addSet(GBACheatSet* set) {
108 beginInsertRows(QModelIndex(), GBACheatSetsSize(&m_device->cheats), GBACheatSetsSize(&m_device->cheats) + 1);
109 *GBACheatSetsAppend(&m_device->cheats) = set;
110 endInsertRows();
111}
112
113void CheatsModel::invalidated() {
114 beginResetModel();
115 endResetModel();
116}