Skip to content
Permalink
Browse files

Qt/ARCodeWidget: Support drag and drop reordering

  • Loading branch information...
spycrab committed Jun 21, 2019
1 parent 389351c commit 93393a288cc62ffecfb44dc200dca51408bb8eff
Showing with 30 additions and 2 deletions.
  1. +28 −2 Source/Core/DolphinQt/Config/ARCodeWidget.cpp
  2. +2 −0 Source/Core/DolphinQt/Config/ARCodeWidget.h
@@ -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<ActionReplay::ARCode> 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("&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(ar.active ? Qt::Checked : Qt::Unchecked);
item->setData(Qt::UserRole, static_cast<int>(i));

m_code_list->addItem(item);
}

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

void ARCodeWidget::SaveCodes()
@@ -47,6 +47,8 @@ class ARCodeWidget : public QWidget
void OnCodeEditPressed();
void OnCodeRemovePressed();

void OnListReordered();

const UICommon::GameFile& m_game;
std::string m_game_id;
u16 m_game_revision;

0 comments on commit 93393a2

Please sign in to comment.
You can’t perform that action at this time.