Qt: Ability to properly copy cheats

This commit is contained in:
Jeffrey Pfau 2015-02-17 01:17:29 -08:00
parent cc214e0f44
commit d3ebcda24b
4 changed files with 54 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include "CheatsModel.h"
#include <QFont>
#include <QSet>
extern "C" {
#include "gba/cheats.h"
@ -153,6 +154,40 @@ void CheatsModel::removeAt(const QModelIndex& index) {
}
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) {
if (index.parent().isValid()) {
beginInsertRows(index.parent(), rowCount(index.parent()), rowCount(index.parent()));

View File

@ -31,6 +31,7 @@ public:
GBACheatSet* itemAt(const QModelIndex& index);
void removeAt(const QModelIndex& index);
QString toString(const QModelIndexList& indices) const;
void beginAppendRow(const QModelIndex& index);
void endAppendRow();

View File

@ -7,6 +7,7 @@
#include "GameController.h"
#include <QClipboard>
#include <QFileDialog>
extern "C" {
@ -22,6 +23,7 @@ CheatsView::CheatsView(GameController* controller, QWidget* parent)
{
m_ui.setupUi(this);
m_ui.cheatList->installEventFilter(this);
m_ui.cheatList->setModel(&m_model);
connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
@ -43,6 +45,20 @@ CheatsView::CheatsView(GameController* controller, QWidget* parent)
});
}
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() {
QString filename = QFileDialog::getOpenFileName(this, tr("Select cheats file"));
if (!filename.isEmpty()) {

View File

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