all repos — mgba @ 9dc49df0bca23925cfcc8193a1770463fd9916cf

mGBA Game Boy Advance Emulator

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 "GameController.h"
  9
 10#include <QClipboard>
 11#include <QFileDialog>
 12
 13extern "C" {
 14#include "gba/cheats.h"
 15}
 16
 17using namespace QGBA;
 18
 19CheatsView::CheatsView(GameController* controller, QWidget* parent)
 20	: QWidget(parent)
 21	, m_controller(controller)
 22	, m_model(controller->cheatDevice())
 23{
 24	m_ui.setupUi(this);
 25
 26	m_ui.cheatList->installEventFilter(this);
 27	m_ui.cheatList->setModel(&m_model);
 28
 29	connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
 30	connect(m_ui.save, SIGNAL(clicked()), this, SLOT(save()));
 31	connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet()));
 32	connect(m_ui.remove, SIGNAL(clicked()), this, SLOT(removeSet()));
 33	connect(controller, SIGNAL(gameStopped(GBAThread*)), &m_model, SLOT(invalidated()));
 34
 35	connect(m_ui.add, &QPushButton::clicked, [this]() {
 36		enterCheat(GBACheatAddLine);
 37	});
 38
 39	connect(m_ui.addGSA, &QPushButton::clicked, [this]() {
 40		enterCheat(GBACheatAddGameSharkLine);
 41	});
 42
 43	connect(m_ui.addCB, &QPushButton::clicked, [this]() {
 44		enterCheat(GBACheatAddCodeBreakerLine);
 45	});
 46}
 47
 48bool CheatsView::eventFilter(QObject* object, QEvent* event) {
 49	if (object != m_ui.cheatList) {
 50		return false;
 51	}
 52	if (event->type() != QEvent::KeyPress) {
 53		return false;
 54	}
 55	if (static_cast<QKeyEvent*>(event) == QKeySequence::Copy) {
 56		QApplication::clipboard()->setText(m_model.toString(m_ui.cheatList->selectionModel()->selectedIndexes()));
 57		return true;
 58	}
 59	return false;
 60}
 61
 62void CheatsView::load() {
 63	QString filename = QFileDialog::getOpenFileName(this, tr("Select cheats file"));
 64	if (!filename.isEmpty()) {
 65		m_model.loadFile(filename);
 66	}
 67}
 68
 69void CheatsView::save() {
 70	QString filename = QFileDialog::getSaveFileName(this, tr("Select cheats file"));
 71	if (!filename.isEmpty()) {
 72		m_model.saveFile(filename);
 73	}
 74}
 75
 76void CheatsView::addSet() {
 77	GBACheatSet* set = new GBACheatSet;
 78	GBACheatSetInit(set, nullptr);
 79	m_controller->threadInterrupt();
 80	m_model.addSet(set);
 81	m_controller->threadContinue();
 82}
 83
 84void CheatsView::removeSet() {
 85	GBACheatSet* set;
 86	QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
 87	if (selection.count() < 1) {
 88		return;
 89	}
 90	m_controller->threadInterrupt();
 91	for (const QModelIndex& index : selection) {
 92		m_model.removeAt(selection[0]);
 93	}
 94	m_controller->threadContinue();
 95}
 96
 97void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) {
 98	GBACheatSet* set;
 99	QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
100	if (selection.count() != 1) {
101		return;
102	}
103	set = m_model.itemAt(selection[0]);
104	if (!set) {
105		return;
106	}
107	m_controller->threadInterrupt();
108	QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts);
109	for (const QString& string : cheats) {
110		m_model.beginAppendRow(selection[0]);
111		callback(set, string.toLocal8Bit().constData());
112		m_model.endAppendRow();
113	}
114	m_controller->threadContinue();
115	m_ui.codeEntry->clear();
116}