src/platform/qt/CheatsView.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 "CheatsView.h"
7
8#include <QFileDialog>
9
10extern "C" {
11#include "gba/cheats.h"
12}
13
14using namespace QGBA;
15
16CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent)
17 : QWidget(parent)
18 , m_model(device)
19{
20 m_ui.setupUi(this);
21
22 m_ui.cheatList->setModel(&m_model);
23
24 connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
25 connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet()));
26
27 connect(m_ui.add, &QPushButton::clicked, [this]() {
28 enterCheat(GBACheatAddLine);
29 });
30
31 connect(m_ui.addGSA, &QPushButton::clicked, [this]() {
32 enterCheat(GBACheatAddGameSharkLine);
33 });
34
35 connect(m_ui.addCB, &QPushButton::clicked, [this]() {
36 enterCheat(GBACheatAddCodeBreakerLine);
37 });
38}
39
40void CheatsView::load() {
41 QString filename = QFileDialog::getOpenFileName(this, tr("Select cheats file"));
42 if (!filename.isEmpty()) {
43 m_model.loadFile(filename);
44 }
45}
46
47void CheatsView::addSet() {
48 GBACheatSet* set = new GBACheatSet;
49 GBACheatSetInit(set, nullptr);
50 m_model.addSet(set);
51}
52
53void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) {
54 GBACheatSet* set;
55 QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
56 if (selection.count() != 1) {
57 return;
58 }
59 set = m_model.itemAt(selection[0]);
60 QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts);
61 for (const QString& string : cheats) {
62 callback(set, string.toLocal8Bit().constData());
63 }
64 m_ui.codeEntry->clear();
65}