mirror of https://github.com/mgba-emu/mgba.git
Qt: Ability to properly copy cheats
This commit is contained in:
parent
cc214e0f44
commit
d3ebcda24b
|
@ -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()));
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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()) {
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue