Skip to content

Commit 4be7b30

Browse files
committed
Support drag and drop of categories. Fixes #994.
1 parent c7f2b1f commit 4be7b30

File tree

3 files changed

+72
-43
lines changed

3 files changed

+72
-43
lines changed

qt/lc_qpreferencesdialog.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ lcQPreferencesDialog::lcQPreferencesDialog(QWidget* Parent, lcPreferencesDialogO
6464
connect(ui->ControlPointColorButton, &QToolButton::clicked, this, &lcQPreferencesDialog::ColorButtonClicked);
6565
connect(ui->ControlPointFocusedColorButton, &QToolButton::clicked, this, &lcQPreferencesDialog::ColorButtonClicked);
6666
connect(ui->categoriesTree, &QTreeWidget::itemSelectionChanged, this, &lcQPreferencesDialog::updateParts);
67+
connect(ui->categoriesTree->model(), &QAbstractItemModel::rowsInserted, this, &lcQPreferencesDialog::CategoriesDropped);
6768
ui->shortcutEdit->installEventFilter(this);
6869
connect(ui->commandList, &QTreeWidget::currentItemChanged, this, &lcQPreferencesDialog::commandChanged);
6970
connect(ui->mouseTree, &QTreeWidget::currentItemChanged, this, &lcQPreferencesDialog::MouseTreeItemChanged);
@@ -689,17 +690,20 @@ void lcQPreferencesDialog::updateCategories()
689690
{
690691
QTreeWidgetItem* CategoryItem;
691692
QTreeWidget* CategoriesTree = ui->categoriesTree;
693+
QSignalBlocker Blocker(CategoriesTree->model());
692694

693695
CategoriesTree->clear();
694696

695697
for (int CategoryIndex = 0; CategoryIndex < static_cast<int>(mOptions->Categories.size()); CategoryIndex++)
696698
{
697699
CategoryItem = new QTreeWidgetItem(CategoriesTree, QStringList(mOptions->Categories[CategoryIndex].Name));
698700
CategoryItem->setData(0, CategoryRole, QVariant(CategoryIndex));
701+
CategoryItem->setFlags(CategoryItem->flags() & ~Qt::ItemFlag::ItemIsDropEnabled);
699702
}
700703

701704
CategoryItem = new QTreeWidgetItem(CategoriesTree, QStringList(tr("Unassigned")));
702705
CategoryItem->setData(0, CategoryRole, QVariant(-1));
706+
CategoryItem->setFlags(CategoryItem->flags() & ~Qt::ItemFlag::ItemIsDropEnabled);
703707
}
704708

705709
void lcQPreferencesDialog::updateParts()
@@ -757,6 +761,27 @@ void lcQPreferencesDialog::updateParts()
757761
tree->resizeColumnToContents(1);
758762
}
759763

764+
void lcQPreferencesDialog::CategoriesDropped(const QModelIndex& Parent, int First, int Last)
765+
{
766+
std::vector<lcLibraryCategory> Categories;
767+
768+
for (int Row = 0; Row < ui->categoriesTree->topLevelItemCount(); Row++)
769+
{
770+
QTreeWidgetItem* CategoryItem = ui->categoriesTree->topLevelItem(Row);
771+
size_t CategoryIndex = CategoryItem->data(0, CategoryRole).toInt();
772+
773+
if (CategoryIndex >= 0 && CategoryIndex < mOptions->Categories.size())
774+
{
775+
Categories.emplace_back(mOptions->Categories[CategoryIndex]);
776+
CategoryItem->setData(0, CategoryRole, Row);
777+
}
778+
}
779+
780+
mOptions->CategoriesModified = true;
781+
mOptions->CategoriesDefault = false;
782+
mOptions->Categories = std::move(Categories);
783+
}
784+
760785
void lcQPreferencesDialog::on_newCategory_clicked()
761786
{
762787
lcLibraryCategory category;

qt/lc_qpreferencesdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public slots:
8181
void on_ViewSphereSizeCombo_currentIndexChanged(int Index);
8282
void on_PreviewViewSphereSizeCombo_currentIndexChanged(int Index);
8383
void updateParts();
84+
void CategoriesDropped(const QModelIndex& Parent, int First, int Last);
8485
void on_newCategory_clicked();
8586
void on_editCategory_clicked();
8687
void on_deleteCategory_clicked();

0 commit comments

Comments
 (0)