From 0a7d82b0bb370d378811bebe116501e5eb3153de Mon Sep 17 00:00:00 2001 From: donovaly Date: Sun, 16 Feb 2020 21:31:56 +0100 Subject: [PATCH] enable selection in the dialog item list now the user can use the Ctrl and/or Shift key to select multiple items to be deleted --- .../PartDesign/Gui/TaskChamferParameters.cpp | 40 ++++++++++++++---- .../PartDesign/Gui/TaskChamferParameters.ui | 3 ++ .../PartDesign/Gui/TaskDraftParameters.cpp | 41 ++++++++++++++---- src/Mod/PartDesign/Gui/TaskDraftParameters.ui | 3 ++ .../PartDesign/Gui/TaskDressUpParameters.cpp | 2 +- .../PartDesign/Gui/TaskFilletParameters.cpp | 41 ++++++++++++++---- .../PartDesign/Gui/TaskFilletParameters.ui | 3 ++ .../Gui/TaskThicknessParameters.cpp | 42 ++++++++++++++----- .../PartDesign/Gui/TaskThicknessParameters.ui | 3 ++ 9 files changed, 139 insertions(+), 39 deletions(-) diff --git a/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp b/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp index 893ec7713c..1088b05a65 100644 --- a/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskChamferParameters.cpp @@ -25,6 +25,7 @@ #ifndef _PreComp_ # include +# include #endif #include "ui_TaskChamferParameters.h" @@ -109,6 +110,9 @@ TaskChamferParameters::TaskChamferParameters(ViewProviderDressUp *DressUpView, Q void TaskChamferParameters::onSelectionChanged(const Gui::SelectionChanges& msg) { + // executed when the user selected something in the CAD object + // adds/deletes the selection accordingly + if (selectionMode == none) return; @@ -155,21 +159,39 @@ void TaskChamferParameters::clearButtons(const selectionModes notThis) void TaskChamferParameters::onRefDeleted(void) { + // get vector of selected objects of active document to assure we have a valid selection + std::vector selection = Gui::Selection().getSelectionEx(); + if (selection.size() == 0) { + QMessageBox::warning(this, tr("Selection error"), tr("Nothing selected!")); + return; + } // assure we we are not in selection mode exitSelectionMode(); clearButtons(none); - // delete any selections since the reference might be highlighted + // delete any selections since the reference(s) might be highlighted Gui::Selection().clearSelection(); DressUpView->highlightReferences(false); - PartDesign::Chamfer* pcChamfer = static_cast(DressUpView->getObject()); - App::DocumentObject* base = pcChamfer->Base.getValue(); - std::vector refs = pcChamfer->Base.getSubValues(); - refs.erase(refs.begin() + ui->listWidgetReferences->currentRow()); - setupTransaction(); - pcChamfer->Base.setValue(base, refs); - ui->listWidgetReferences->model()->removeRow(ui->listWidgetReferences->currentRow()); - pcChamfer->getDocument()->recomputeFeature(pcChamfer); + // get the list of items to be deleted + QList selectedList = ui->listWidgetReferences->selectedItems(); + + // delete the selection backwards to assure the list index keeps valid for the deletion + for (int i = selectedList.count() - 1; i > -1; i--) { + QListWidgetItem* item = selectedList.at(i); + // get the fillet object + PartDesign::Chamfer* pcChamfer = static_cast(DressUpView->getObject()); + App::DocumentObject* base = pcChamfer->Base.getValue(); + // get all fillet references + std::vector refs = pcChamfer->Base.getSubValues(); + // the ref index is the same as the listWidgetReferences index + // so we can erase using the row number of the element to be deleted + int rowNumber = ui->listWidgetReferences->row(selectedList.at(i)); + refs.erase(refs.begin() + rowNumber); + setupTransaction(); + pcChamfer->Base.setValue(base, refs); + ui->listWidgetReferences->model()->removeRow(rowNumber); + pcChamfer->getDocument()->recomputeFeature(pcChamfer); + } // if there is only one item left, it cannot be deleted if (ui->listWidgetReferences->count() == 1) { diff --git a/src/Mod/PartDesign/Gui/TaskChamferParameters.ui b/src/Mod/PartDesign/Gui/TaskChamferParameters.ui index b48df1ea7b..f35e2ce402 100644 --- a/src/Mod/PartDesign/Gui/TaskChamferParameters.ui +++ b/src/Mod/PartDesign/Gui/TaskChamferParameters.ui @@ -52,6 +52,9 @@ click again to end selection - select an item to highlight it - double-click on an item to see the chamfers + + QAbstractItemView::ExtendedSelection + diff --git a/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp b/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp index 7e1d0dd51f..5c79d99801 100644 --- a/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskDraftParameters.cpp @@ -27,6 +27,7 @@ #ifndef _PreComp_ # include # include +# include #endif #include "ui_TaskDraftParameters.h" @@ -131,6 +132,9 @@ TaskDraftParameters::TaskDraftParameters(ViewProviderDressUp *DressUpView, QWidg void TaskDraftParameters::onSelectionChanged(const Gui::SelectionChanges& msg) { + // executed when the user selected something in the CAD object + // adds/deletes the selection accordingly + if (selectionMode == none) return; @@ -229,21 +233,40 @@ void TaskDraftParameters::onButtonLine(bool checked) void TaskDraftParameters::onRefDeleted(void) { + // get vector of selected objects of active document to assure we have a valid selection + std::vector selection = Gui::Selection().getSelectionEx(); + if (selection.size() == 0) { + QMessageBox::warning(this, tr("Selection error"), tr("Nothing selected!")); + return; + } // assure we we are not in selection mode exitSelectionMode(); clearButtons(none); - // delete any selections since the reference might be highlighted + // delete any selections since the reference(s) might be highlighted Gui::Selection().clearSelection(); DressUpView->highlightReferences(false); - PartDesign::Draft* pcDraft = static_cast(DressUpView->getObject()); - App::DocumentObject* base = pcDraft->Base.getValue(); - std::vector faces = pcDraft->Base.getSubValues(); - faces.erase(faces.begin() + ui->listWidgetReferences->currentRow()); - setupTransaction(); - pcDraft->Base.setValue(base, faces); - ui->listWidgetReferences->model()->removeRow(ui->listWidgetReferences->currentRow()); - pcDraft->getDocument()->recomputeFeature(pcDraft); + // get the list of items to be deleted + QList selectedList = ui->listWidgetReferences->selectedItems(); + + // delete the selection backwards to assure the list index keeps valid for the deletion + for (int i = selectedList.count() - 1; i > -1; i--) { + //QMessageBox::warning(this, tr("i"), QString::number(i)); + QListWidgetItem* item = selectedList.at(i); + // get the fillet object + PartDesign::Draft* pcDraft = static_cast(DressUpView->getObject()); + App::DocumentObject* base = pcDraft->Base.getValue(); + // get all fillet references + std::vector refs = pcDraft->Base.getSubValues(); + // the ref index is the same as the listWidgetReferences index + // so we can erase using the row number of the element to be deleted + int rowNumber = ui->listWidgetReferences->row(selectedList.at(i)); + refs.erase(refs.begin() + rowNumber); + setupTransaction(); + pcDraft->Base.setValue(base, refs); + ui->listWidgetReferences->model()->removeRow(rowNumber); + pcDraft->getDocument()->recomputeFeature(pcDraft); + } // if there is only one item left, it cannot be deleted if (ui->listWidgetReferences->count() == 1) { diff --git a/src/Mod/PartDesign/Gui/TaskDraftParameters.ui b/src/Mod/PartDesign/Gui/TaskDraftParameters.ui index 6e91bc509a..9bb7cc073c 100644 --- a/src/Mod/PartDesign/Gui/TaskDraftParameters.ui +++ b/src/Mod/PartDesign/Gui/TaskDraftParameters.ui @@ -52,6 +52,9 @@ click again to end selection - select an item to highlight it - double-click on an item to see the drafts + + QAbstractItemView::ExtendedSelection + diff --git a/src/Mod/PartDesign/Gui/TaskDressUpParameters.cpp b/src/Mod/PartDesign/Gui/TaskDressUpParameters.cpp index 08689c6dae..8c379ce612 100644 --- a/src/Mod/PartDesign/Gui/TaskDressUpParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskDressUpParameters.cpp @@ -197,7 +197,7 @@ void TaskDressUpParameters::setSelection(QListWidgetItem* current) { hideObject(); // highlight all objects in the list DressUpView->highlightReferences(true); - // clear existing selections + // clear existing selection because only the current item is highlighted, not all selected ones to keep the overview Gui::Selection().clearSelection(); // highligh the selected item Gui::Selection().addSelection(docName.c_str(), objName.c_str(), subName.c_str(), 0, 0, 0); diff --git a/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp b/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp index c145223b53..ed4664f38f 100644 --- a/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskFilletParameters.cpp @@ -26,6 +26,7 @@ #ifndef _PreComp_ # include # include +# include #endif #include "ui_TaskFilletParameters.h" @@ -110,6 +111,9 @@ TaskFilletParameters::TaskFilletParameters(ViewProviderDressUp *DressUpView, QWi void TaskFilletParameters::onSelectionChanged(const Gui::SelectionChanges& msg) { + // executed when the user selected something in the CAD object + // adds/deletes the selection accordingly + if (selectionMode == none) return; @@ -155,21 +159,40 @@ void TaskFilletParameters::clearButtons(const selectionModes notThis) void TaskFilletParameters::onRefDeleted(void) { + // get vector of selected objects of active document to assure we have a valid selection + std::vector selection = Gui::Selection().getSelectionEx(); + if (selection.size() == 0) { + QMessageBox::warning(this, tr("Selection error"), tr("Nothing selected!")); + return; + } // assure we we are not in selection mode exitSelectionMode(); clearButtons(none); - // delete any selections since the reference might be highlighted + // delete any selections since the reference(s) might be highlighted Gui::Selection().clearSelection(); DressUpView->highlightReferences(false); - PartDesign::Fillet* pcFillet = static_cast(DressUpView->getObject()); - App::DocumentObject* base = pcFillet->Base.getValue(); - std::vector refs = pcFillet->Base.getSubValues(); - refs.erase(refs.begin() + ui->listWidgetReferences->currentRow()); - setupTransaction(); - pcFillet->Base.setValue(base, refs); - ui->listWidgetReferences->model()->removeRow(ui->listWidgetReferences->currentRow()); - pcFillet->getDocument()->recomputeFeature(pcFillet); + // get the list of items to be deleted + QList selectedList = ui->listWidgetReferences->selectedItems(); + + // delete the selection backwards to assure the list index keeps valid for the deletion + for (int i = selectedList.count()-1; i > -1; i--) { + //QMessageBox::warning(this, tr("i"), QString::number(i)); + QListWidgetItem* item = selectedList.at(i); + // get the fillet object + PartDesign::Fillet* pcFillet = static_cast(DressUpView->getObject()); + App::DocumentObject* base = pcFillet->Base.getValue(); + // get all fillet references + std::vector refs = pcFillet->Base.getSubValues(); + // the ref index is the same as the listWidgetReferences index + // so we can erase using the row number of the element to be deleted + int rowNumber = ui->listWidgetReferences->row(selectedList.at(i)); + refs.erase(refs.begin() + rowNumber); + setupTransaction(); + pcFillet->Base.setValue(base, refs); + ui->listWidgetReferences->model()->removeRow(rowNumber); + pcFillet->getDocument()->recomputeFeature(pcFillet); + } // if there is only one item left, it cannot be deleted if (ui->listWidgetReferences->count() == 1) { diff --git a/src/Mod/PartDesign/Gui/TaskFilletParameters.ui b/src/Mod/PartDesign/Gui/TaskFilletParameters.ui index 716a97b0f1..e8018c3a08 100644 --- a/src/Mod/PartDesign/Gui/TaskFilletParameters.ui +++ b/src/Mod/PartDesign/Gui/TaskFilletParameters.ui @@ -52,6 +52,9 @@ click again to end selection - select an item to highlight it - double-click on an item to see the fillets + + QAbstractItemView::ExtendedSelection + diff --git a/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp b/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp index e1edc2ed89..c48d5f9e50 100644 --- a/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp +++ b/src/Mod/PartDesign/Gui/TaskThicknessParameters.cpp @@ -25,6 +25,7 @@ #ifndef _PreComp_ # include +# include #endif #include "ui_TaskThicknessParameters.h" @@ -131,6 +132,9 @@ TaskThicknessParameters::TaskThicknessParameters(ViewProviderDressUp *DressUpVie void TaskThicknessParameters::onSelectionChanged(const Gui::SelectionChanges& msg) { + // executed when the user selected something in the CAD object + // adds/deletes the selection accordingly + if (selectionMode == none) return; @@ -177,23 +181,39 @@ void TaskThicknessParameters::clearButtons(const selectionModes notThis) void TaskThicknessParameters::onRefDeleted(void) { + // get vector of selected objects of active document to assure we have a valid selection + std::vector selection = Gui::Selection().getSelectionEx(); + if (selection.size() == 0) { + QMessageBox::warning(this, tr("Selection error"), tr("Nothing selected!")); + return; + } // assure we we are not in selection mode exitSelectionMode(); clearButtons(none); - // delete any selections since the reference might be highlighted + // delete any selections since the reference(s) might be highlighted Gui::Selection().clearSelection(); DressUpView->highlightReferences(false); - PartDesign::Thickness* pcThickness = static_cast(DressUpView->getObject()); - App::DocumentObject* base = pcThickness->Base.getValue(); - std::vector faces = pcThickness->Base.getSubValues(); - faces.erase(faces.begin() + ui->listWidgetReferences->currentRow()); - setupTransaction(); - pcThickness->Base.setValue(base, faces); - ui->listWidgetReferences->model()->removeRow(ui->listWidgetReferences->currentRow()); - pcThickness->getDocument()->recomputeFeature(pcThickness); - clearButtons(none); - exitSelectionMode(); + // get the list of items to be deleted + QList selectedList = ui->listWidgetReferences->selectedItems(); + + // delete the selection backwards to assure the list index keeps valid for the deletion + for (int i = selectedList.count() - 1; i > -1; i--) { + QListWidgetItem* item = selectedList.at(i); + // get the fillet object + PartDesign::Thickness* pcThickness = static_cast(DressUpView->getObject()); + App::DocumentObject* base = pcThickness->Base.getValue(); + // get all fillet references + std::vector refs = pcThickness->Base.getSubValues(); + // the ref index is the same as the listWidgetReferences index + // so we can erase using the row number of the element to be deleted + int rowNumber = ui->listWidgetReferences->row(selectedList.at(i)); + refs.erase(refs.begin() + rowNumber); + setupTransaction(); + pcThickness->Base.setValue(base, refs); + ui->listWidgetReferences->model()->removeRow(rowNumber); + pcThickness->getDocument()->recomputeFeature(pcThickness); + } // if there is only one item left, it cannot be deleted if (ui->listWidgetReferences->count() == 1) { diff --git a/src/Mod/PartDesign/Gui/TaskThicknessParameters.ui b/src/Mod/PartDesign/Gui/TaskThicknessParameters.ui index faf42a3a00..ec49cc53a5 100644 --- a/src/Mod/PartDesign/Gui/TaskThicknessParameters.ui +++ b/src/Mod/PartDesign/Gui/TaskThicknessParameters.ui @@ -52,6 +52,9 @@ click again to end selection - select an item to highlight it - double-click on an item to see the features + + QAbstractItemView::MultiSelection +