all repos — mgba @ c8d348880497cbb357f66f9a3c1c3a4e2cdb41cb

mGBA Game Boy Advance Emulator

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		return cheats->name ? cheats->name : tr("(untitled)");
31	case Qt::CheckStateRole:
32		return Qt::Checked;
33	default:
34		return QVariant();
35	}
36}
37
38QModelIndex CheatsModel::index(int row, int column, const QModelIndex& parent) const {
39	return createIndex(row, column, *GBACheatSetsGetPointer(&m_device->cheats, row));
40}
41
42QModelIndex CheatsModel::parent(const QModelIndex& index) const {
43	return QModelIndex();
44}
45
46int CheatsModel::columnCount(const QModelIndex& parent) const {
47	return 1;
48}
49
50int CheatsModel::rowCount(const QModelIndex& parent) const {
51	if (parent.isValid()) {
52		return 0;
53	}
54	return GBACheatSetsSize(&m_device->cheats);
55}
56
57void CheatsModel::loadFile(const QString& path) {
58	VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
59	if (!vf) {
60		return;
61	}
62	beginResetModel();
63	GBACheatParseFile(m_device, vf);
64	endResetModel();
65	vf->close(vf);
66}