Skip to content

Commit

Permalink
Qt/GeckoCodeWidget: Support drag and drop reordering
Browse files Browse the repository at this point in the history
  • Loading branch information
spycrab committed Jun 22, 2019
1 parent 981925a commit 389351c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
34 changes: 30 additions & 4 deletions Source/Core/DolphinQt/Config/GeckoCodeWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down Expand Up @@ -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<Gecko::GeckoCode> 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();
Expand All @@ -239,12 +262,15 @@ void GeckoCodeWidget::UpdateList()
.replace(QStringLiteral("&lt;"), QStringLiteral("<"))
.replace(QStringLiteral("&gt;"), 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<int>(i));

m_code_list->addItem(item);
}

m_code_list->setDragDropMode(QAbstractItemView::InternalMove);
}

void GeckoCodeWidget::DownloadCodes()
Expand Down
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/Config/GeckoCodeWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class GeckoCodeWidget : public QWidget
private:
void OnSelectionChanged();
void OnItemChanged(QListWidgetItem* item);
void OnListReordered();

void CreateWidgets();
void ConnectWidgets();
Expand Down

0 comments on commit 389351c

Please sign in to comment.