Qt/ARCodeWidget: Support drag and drop reordering

This commit is contained in:
spycrab 2019-06-21 02:25:30 +02:00
parent 389351c6c0
commit 93393a288c
2 changed files with 30 additions and 2 deletions

View File

@ -70,6 +70,8 @@ void ARCodeWidget::ConnectWidgets()
&ARCodeWidget::OpenGeneralSettings); &ARCodeWidget::OpenGeneralSettings);
connect(m_code_list, &QListWidget::itemChanged, this, &ARCodeWidget::OnItemChanged); connect(m_code_list, &QListWidget::itemChanged, this, &ARCodeWidget::OnItemChanged);
connect(m_code_list, &QListWidget::itemSelectionChanged, this, &ARCodeWidget::OnSelectionChanged); 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_add, &QPushButton::pressed, this, &ARCodeWidget::OnCodeAddPressed);
connect(m_code_edit, &QPushButton::pressed, this, &ARCodeWidget::OnCodeEditPressed); connect(m_code_edit, &QPushButton::pressed, this, &ARCodeWidget::OnCodeEditPressed);
@ -83,6 +85,25 @@ void ARCodeWidget::OnItemChanged(QListWidgetItem* item)
if (!m_restart_required) if (!m_restart_required)
ActionReplay::ApplyCodes(m_ar_codes); 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(); SaveCodes();
} }
@ -105,17 +126,22 @@ void ARCodeWidget::UpdateList()
{ {
m_code_list->clear(); 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) auto* item = new QListWidgetItem(QString::fromStdString(ar.name)
.replace(QStringLiteral("&lt;"), QStringLiteral("<")) .replace(QStringLiteral("&lt;"), QStringLiteral("<"))
.replace(QStringLiteral("&gt;"), 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->setCheckState(ar.active ? Qt::Checked : Qt::Unchecked);
item->setData(Qt::UserRole, static_cast<int>(i));
m_code_list->addItem(item); m_code_list->addItem(item);
} }
m_code_list->setDragDropMode(QAbstractItemView::InternalMove);
} }
void ARCodeWidget::SaveCodes() void ARCodeWidget::SaveCodes()

View File

@ -47,6 +47,8 @@ private:
void OnCodeEditPressed(); void OnCodeEditPressed();
void OnCodeRemovePressed(); void OnCodeRemovePressed();
void OnListReordered();
const UICommon::GameFile& m_game; const UICommon::GameFile& m_game;
std::string m_game_id; std::string m_game_id;
u16 m_game_revision; u16 m_game_revision;