all repos — mgba @ d3ebcda24b2515111d1a75c9717ef87c91b31558

mGBA Game Boy Advance Emulator

Qt: Ability to properly copy cheats
Jeffrey Pfau jeffrey@endrift.com
Tue, 17 Feb 2015 01:17:29 -0800
commit

d3ebcda24b2515111d1a75c9717ef87c91b31558

parent

cc214e0f44b51498bf2fc6db21909b1bbf12d733

M src/platform/qt/CheatsModel.cppsrc/platform/qt/CheatsModel.cpp

@@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "CheatsModel.h" #include <QFont> +#include <QSet> extern "C" { #include "gba/cheats.h"

@@ -151,6 +152,40 @@ GBACheatSetDeinit(set);

delete set; endInsertRows(); +} + +QString CheatsModel::toString(const QModelIndexList& indices) const { + QMap<int, GBACheatSet*> setOrder; + QMap<GBACheatSet*, QSet<size_t>> setIndices; + for (const QModelIndex& index : indices) { + GBACheatSet* set = static_cast<GBACheatSet*>(index.internalPointer()); + if (!set) { + set = *GBACheatSetsGetPointer(&m_device->cheats, index.row()); + setOrder[index.row()] = set; + QSet<size_t> range; + for (size_t i = 0; i < StringListSize(&set->lines); ++i) { + range.insert(i); + } + setIndices[set] = range; + } else { + setOrder[index.parent().row()] = set; + setIndices[set].insert(index.row()); + } + } + + QStringList strings; + QList<int> order = setOrder.keys(); + std::sort(order.begin(), order.end()); + for (int i : order) { + GBACheatSet* set = setOrder[i]; + QList<size_t> indexOrdex = setIndices[set].toList(); + std::sort(indexOrdex.begin(), indexOrdex.end()); + for (size_t j : indexOrdex) { + strings.append(*StringListGetPointer(&set->lines, j)); + } + } + + return strings.join('\n'); } void CheatsModel::beginAppendRow(const QModelIndex& index) {
M src/platform/qt/CheatsModel.hsrc/platform/qt/CheatsModel.h

@@ -31,6 +31,7 @@ virtual Qt::ItemFlags flags(const QModelIndex &index) const override;

GBACheatSet* itemAt(const QModelIndex& index); void removeAt(const QModelIndex& index); + QString toString(const QModelIndexList& indices) const; void beginAppendRow(const QModelIndex& index); void endAppendRow();
M src/platform/qt/CheatsView.cppsrc/platform/qt/CheatsView.cpp

@@ -7,6 +7,7 @@ #include "CheatsView.h"

#include "GameController.h" +#include <QClipboard> #include <QFileDialog> extern "C" {

@@ -22,6 +23,7 @@ , m_model(controller->cheatDevice())

{ m_ui.setupUi(this); + m_ui.cheatList->installEventFilter(this); m_ui.cheatList->setModel(&m_model); connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));

@@ -41,6 +43,20 @@

connect(m_ui.addCB, &QPushButton::clicked, [this]() { enterCheat(GBACheatAddCodeBreakerLine); }); +} + +bool CheatsView::eventFilter(QObject* object, QEvent* event) { + if (object != m_ui.cheatList) { + return false; + } + if (event->type() != QEvent::KeyPress) { + return false; + } + if (static_cast<QKeyEvent*>(event) == QKeySequence::Copy) { + QApplication::clipboard()->setText(m_model.toString(m_ui.cheatList->selectionModel()->selectedIndexes())); + return true; + } + return false; } void CheatsView::load() {
M src/platform/qt/CheatsView.hsrc/platform/qt/CheatsView.h

@@ -26,6 +26,8 @@

public: CheatsView(GameController* controller, QWidget* parent = nullptr); + virtual bool eventFilter(QObject*, QEvent*) override; + private slots: void load(); void save();