From 389351c6c0d58a8d45abfd33598b0ff7875be051 Mon Sep 17 00:00:00 2001 From: spycrab Date: Fri, 21 Jun 2019 01:40:37 +0200 Subject: [PATCH] Qt/GeckoCodeWidget: Support drag and drop reordering --- .../Core/DolphinQt/Config/GeckoCodeWidget.cpp | 34 ++++++++++++++++--- .../Core/DolphinQt/Config/GeckoCodeWidget.h | 1 + 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp b/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp index 69295680d0..c82b127235 100644 --- a/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp +++ b/Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp @@ -116,12 +116,13 @@ void GeckoCodeWidget::ConnectWidgets() connect(m_code_list, &QListWidget::itemSelectionChanged, this, &GeckoCodeWidget::OnSelectionChanged); connect(m_code_list, &QListWidget::itemChanged, this, &GeckoCodeWidget::OnItemChanged); + connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this, + &GeckoCodeWidget::OnListReordered); connect(m_add_code, &QPushButton::pressed, this, &GeckoCodeWidget::AddCode); connect(m_remove_code, &QPushButton::pressed, this, &GeckoCodeWidget::RemoveCode); connect(m_edit_code, &QPushButton::pressed, this, &GeckoCodeWidget::EditCode); connect(m_download_codes, &QPushButton::pressed, this, &GeckoCodeWidget::DownloadCodes); - connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this, &GeckoCodeWidget::OpenGeneralSettings); } @@ -130,8 +131,10 @@ void GeckoCodeWidget::OnSelectionChanged() { auto items = m_code_list->selectedItems(); - m_edit_code->setEnabled(!items.empty()); - m_remove_code->setEnabled(!items.empty()); + const bool empty = items.empty(); + + m_edit_code->setEnabled(!empty); + m_remove_code->setEnabled(!empty); if (items.empty()) return; @@ -222,11 +225,31 @@ void GeckoCodeWidget::SaveCodes() { IniFile game_ini_local; game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"); + Gecko::SaveCodes(game_ini_local, m_gecko_codes); game_ini_local.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"); } +void GeckoCodeWidget::OnListReordered() +{ + // Reorder codes based on the indices of table item + std::vector codes; + codes.reserve(m_gecko_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_gecko_codes[index])); + } + + m_gecko_codes = std::move(codes); + + UpdateList(); + SaveCodes(); +} + void GeckoCodeWidget::UpdateList() { m_code_list->clear(); @@ -239,12 +262,15 @@ void GeckoCodeWidget::UpdateList() .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(code.enabled ? Qt::Checked : Qt::Unchecked); item->setData(Qt::UserRole, static_cast(i)); m_code_list->addItem(item); } + + m_code_list->setDragDropMode(QAbstractItemView::InternalMove); } void GeckoCodeWidget::DownloadCodes() diff --git a/Source/Core/DolphinQt/Config/GeckoCodeWidget.h b/Source/Core/DolphinQt/Config/GeckoCodeWidget.h index 430cc0d15a..1c3619b2a8 100644 --- a/Source/Core/DolphinQt/Config/GeckoCodeWidget.h +++ b/Source/Core/DolphinQt/Config/GeckoCodeWidget.h @@ -36,6 +36,7 @@ signals: private: void OnSelectionChanged(); void OnItemChanged(QListWidgetItem* item); + void OnListReordered(); void CreateWidgets(); void ConnectWidgets();