Qt: Ability to properly copy cheats
Jeffrey Pfau jeffrey@endrift.com
Tue, 17 Feb 2015 01:17:29 -0800
4 files changed,
54 insertions(+),
0 deletions(-)
M
src/platform/qt/CheatsModel.cpp
→
src/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.h
→
src/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.cpp
→
src/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.h
→
src/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();