From 4be7b308f5b96bdf272e9ea8a45581f68ac1d6e4 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Wed, 8 Apr 2026 23:30:07 -0700 Subject: [PATCH] Support drag and drop of categories. Fixes #994. --- qt/lc_qpreferencesdialog.cpp | 25 ++++++++++ qt/lc_qpreferencesdialog.h | 1 + qt/lc_qpreferencesdialog.ui | 89 +++++++++++++++++++----------------- 3 files changed, 72 insertions(+), 43 deletions(-) diff --git a/qt/lc_qpreferencesdialog.cpp b/qt/lc_qpreferencesdialog.cpp index 61d16fc6..43a578d1 100644 --- a/qt/lc_qpreferencesdialog.cpp +++ b/qt/lc_qpreferencesdialog.cpp @@ -64,6 +64,7 @@ lcQPreferencesDialog::lcQPreferencesDialog(QWidget* Parent, lcPreferencesDialogO connect(ui->ControlPointColorButton, &QToolButton::clicked, this, &lcQPreferencesDialog::ColorButtonClicked); connect(ui->ControlPointFocusedColorButton, &QToolButton::clicked, this, &lcQPreferencesDialog::ColorButtonClicked); connect(ui->categoriesTree, &QTreeWidget::itemSelectionChanged, this, &lcQPreferencesDialog::updateParts); + connect(ui->categoriesTree->model(), &QAbstractItemModel::rowsInserted, this, &lcQPreferencesDialog::CategoriesDropped); ui->shortcutEdit->installEventFilter(this); connect(ui->commandList, &QTreeWidget::currentItemChanged, this, &lcQPreferencesDialog::commandChanged); connect(ui->mouseTree, &QTreeWidget::currentItemChanged, this, &lcQPreferencesDialog::MouseTreeItemChanged); @@ -689,6 +690,7 @@ void lcQPreferencesDialog::updateCategories() { QTreeWidgetItem* CategoryItem; QTreeWidget* CategoriesTree = ui->categoriesTree; + QSignalBlocker Blocker(CategoriesTree->model()); CategoriesTree->clear(); @@ -696,10 +698,12 @@ void lcQPreferencesDialog::updateCategories() { CategoryItem = new QTreeWidgetItem(CategoriesTree, QStringList(mOptions->Categories[CategoryIndex].Name)); CategoryItem->setData(0, CategoryRole, QVariant(CategoryIndex)); + CategoryItem->setFlags(CategoryItem->flags() & ~Qt::ItemFlag::ItemIsDropEnabled); } CategoryItem = new QTreeWidgetItem(CategoriesTree, QStringList(tr("Unassigned"))); CategoryItem->setData(0, CategoryRole, QVariant(-1)); + CategoryItem->setFlags(CategoryItem->flags() & ~Qt::ItemFlag::ItemIsDropEnabled); } void lcQPreferencesDialog::updateParts() @@ -757,6 +761,27 @@ void lcQPreferencesDialog::updateParts() tree->resizeColumnToContents(1); } +void lcQPreferencesDialog::CategoriesDropped(const QModelIndex& Parent, int First, int Last) +{ + std::vector Categories; + + for (int Row = 0; Row < ui->categoriesTree->topLevelItemCount(); Row++) + { + QTreeWidgetItem* CategoryItem = ui->categoriesTree->topLevelItem(Row); + size_t CategoryIndex = CategoryItem->data(0, CategoryRole).toInt(); + + if (CategoryIndex >= 0 && CategoryIndex < mOptions->Categories.size()) + { + Categories.emplace_back(mOptions->Categories[CategoryIndex]); + CategoryItem->setData(0, CategoryRole, Row); + } + } + + mOptions->CategoriesModified = true; + mOptions->CategoriesDefault = false; + mOptions->Categories = std::move(Categories); +} + void lcQPreferencesDialog::on_newCategory_clicked() { lcLibraryCategory category; diff --git a/qt/lc_qpreferencesdialog.h b/qt/lc_qpreferencesdialog.h index c7f23ce2..75c42f4c 100644 --- a/qt/lc_qpreferencesdialog.h +++ b/qt/lc_qpreferencesdialog.h @@ -81,6 +81,7 @@ public slots: void on_ViewSphereSizeCombo_currentIndexChanged(int Index); void on_PreviewViewSphereSizeCombo_currentIndexChanged(int Index); void updateParts(); + void CategoriesDropped(const QModelIndex& Parent, int First, int Last); void on_newCategory_clicked(); void on_editCategory_clicked(); void on_deleteCategory_clicked(); diff --git a/qt/lc_qpreferencesdialog.ui b/qt/lc_qpreferencesdialog.ui index 9fa2d883..d28579a3 100644 --- a/qt/lc_qpreferencesdialog.ui +++ b/qt/lc_qpreferencesdialog.ui @@ -6,8 +6,8 @@ 0 0 - 621 - 503 + 726 + 638 @@ -25,7 +25,7 @@ - QFormLayout::ExpandingFieldsGrow + QFormLayout::FieldGrowthPolicy::ExpandingFieldsGrow @@ -84,7 +84,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -352,10 +352,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSlider::NoTicks + QSlider::TickPosition::NoTicks @@ -448,7 +448,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -524,7 +524,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -550,10 +550,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSlider::NoTicks + QSlider::TickPosition::NoTicks @@ -584,7 +584,7 @@ - Qt::Vertical + Qt::Orientation::Vertical @@ -679,7 +679,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -739,7 +739,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -861,7 +861,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -877,7 +877,7 @@ - Qt::Vertical + Qt::Orientation::Vertical @@ -994,7 +994,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1014,10 +1014,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::Fixed + QSizePolicy::Policy::Fixed @@ -1053,10 +1053,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::Fixed + QSizePolicy::Policy::Fixed @@ -1083,10 +1083,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::Fixed + QSizePolicy::Policy::Fixed @@ -1113,7 +1113,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1129,7 +1129,7 @@ - Qt::Vertical + Qt::Orientation::Vertical @@ -1162,10 +1162,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::Fixed + QSizePolicy::Policy::Fixed @@ -1192,7 +1192,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1230,10 +1230,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::Fixed + QSizePolicy::Policy::Fixed @@ -1260,7 +1260,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1298,7 +1298,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1336,10 +1336,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QSizePolicy::Fixed + QSizePolicy::Policy::Fixed @@ -1422,7 +1422,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1457,7 +1457,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1466,6 +1466,9 @@ 0 + + QAbstractItemView::DragDropMode::InternalMove + false @@ -1492,7 +1495,7 @@ - QAbstractItemView::NoSelection + QAbstractItemView::SelectionMode::NoSelection false @@ -1551,7 +1554,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1596,7 +1599,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1679,7 +1682,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1812,7 +1815,7 @@ - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1938,7 +1941,7 @@ 5 - Qt::Horizontal + Qt::Orientation::Horizontal @@ -1951,10 +1954,10 @@ - Qt::Horizontal + Qt::Orientation::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok