From 93393a288cc62ffecfb44dc200dca51408bb8eff Mon Sep 17 00:00:00 2001 From: spycrab Date: Fri, 21 Jun 2019 02:25:30 +0200 Subject: [PATCH] Qt/ARCodeWidget: Support drag and drop reordering --- Source/Core/DolphinQt/Config/ARCodeWidget.cpp | 30 +++++++++++++++++-- Source/Core/DolphinQt/Config/ARCodeWidget.h | 2 ++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Source/Core/DolphinQt/Config/ARCodeWidget.cpp b/Source/Core/DolphinQt/Config/ARCodeWidget.cpp index c2e192e798..661c722df1 100644 --- a/Source/Core/DolphinQt/Config/ARCodeWidget.cpp +++ b/Source/Core/DolphinQt/Config/ARCodeWidget.cpp @@ -70,6 +70,8 @@ void ARCodeWidget::ConnectWidgets() &ARCodeWidget::OpenGeneralSettings); connect(m_code_list, &QListWidget::itemChanged, this, &ARCodeWidget::OnItemChanged); connect(m_code_list, &QListWidget::itemSelectionChanged, this, &ARCodeWidget::OnSelectionChanged); + connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this, + &ARCodeWidget::OnListReordered); connect(m_code_add, &QPushButton::pressed, this, &ARCodeWidget::OnCodeAddPressed); connect(m_code_edit, &QPushButton::pressed, this, &ARCodeWidget::OnCodeEditPressed); @@ -83,6 +85,25 @@ void ARCodeWidget::OnItemChanged(QListWidgetItem* item) if (!m_restart_required) ActionReplay::ApplyCodes(m_ar_codes); + UpdateList(); + SaveCodes(); +} + +void ARCodeWidget::OnListReordered() +{ + // Reorder codes based on the indices of table item + std::vector codes; + codes.reserve(m_ar_codes.size()); + + for (int i = 0; i < m_code_list->count(); i++) + { + const int index = m_code_list->item(i)->data(Qt::UserRole).toInt(); + + codes.push_back(std::move(m_ar_codes[index])); + } + + m_ar_codes = std::move(codes); + SaveCodes(); } @@ -105,17 +126,22 @@ void ARCodeWidget::UpdateList() { m_code_list->clear(); - for (const auto& ar : m_ar_codes) + for (size_t i = 0; i < m_ar_codes.size(); i++) { + const auto& ar = m_ar_codes[i]; auto* item = new QListWidgetItem(QString::fromStdString(ar.name) .replace(QStringLiteral("<"), QStringLiteral("<")) .replace(QStringLiteral(">"), QStringLiteral(">"))); - item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable); + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | + Qt::ItemIsDragEnabled); item->setCheckState(ar.active ? Qt::Checked : Qt::Unchecked); + item->setData(Qt::UserRole, static_cast(i)); m_code_list->addItem(item); } + + m_code_list->setDragDropMode(QAbstractItemView::InternalMove); } void ARCodeWidget::SaveCodes() diff --git a/Source/Core/DolphinQt/Config/ARCodeWidget.h b/Source/Core/DolphinQt/Config/ARCodeWidget.h index 3f7617ff60..9e84d59bd3 100644 --- a/Source/Core/DolphinQt/Config/ARCodeWidget.h +++ b/Source/Core/DolphinQt/Config/ARCodeWidget.h @@ -47,6 +47,8 @@ private: void OnCodeEditPressed(); void OnCodeRemovePressed(); + void OnListReordered(); + const UICommon::GameFile& m_game; std::string m_game_id; u16 m_game_revision;