From 9b2de68000a19cb69807d6032416d13bab362860 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Mon, 11 Oct 2021 16:38:45 +0200 Subject: [PATCH] Sketcher: Constraint widget associated constraints filter ========================================================= A new special filter, that filters the constraint list so as to show the constraints associated with the currently selected geometries. --- src/Mod/Sketcher/Gui/ConstraintFilters.h | 1 + .../Sketcher/Gui/TaskSketcherConstrains.cpp | 108 +++++++++++++++++- src/Mod/Sketcher/Gui/TaskSketcherConstrains.h | 6 + .../Sketcher/Gui/TaskSketcherConstrains.ui | 7 +- 4 files changed, 116 insertions(+), 6 deletions(-) diff --git a/src/Mod/Sketcher/Gui/ConstraintFilters.h b/src/Mod/Sketcher/Gui/ConstraintFilters.h index 4c8a292948..68370e2c71 100644 --- a/src/Mod/Sketcher/Gui/ConstraintFilters.h +++ b/src/Mod/Sketcher/Gui/ConstraintFilters.h @@ -61,6 +61,7 @@ namespace ConstraintFilter { enum SpecialFilterValue { Multiple = 24, Selection = 25, + AssociatedConstraints = 26, NumSpecialFilterValue }; diff --git a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp index 1f04614880..cd67fc5f72 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp +++ b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.cpp @@ -752,6 +752,47 @@ void TaskSketcherConstrains::updateSelectionFilter() selectionFilter.push_back(static_cast(item)->ConstraintNbr); } +void TaskSketcherConstrains::updateAssociatedConstraintsFilter() +{ + associatedConstraintsFilter.clear(); + + assert(sketchView); + + std::vector selection; + selection = Gui::Selection().getSelectionEx(0, Sketcher::SketchObject::getClassTypeId()); + + // only one sketch with its subelements are allowed to be selected + if (selection.size() != 1) { + return; + } + + // get the needed lists and objects + const std::vector &SubNames = selection[0].getSubNames(); + const Sketcher::SketchObject * Obj = sketchView->getSketchObject(); + const std::vector< Sketcher::Constraint * > &vals = Obj->Constraints.getValues(); + + std::vector constraintSubNames; + // go through the selected subelements + for (std::vector::const_iterator it=SubNames.begin(); it != SubNames.end(); ++it) { + // only handle edges + if (it->size() > 4 && it->substr(0,4) == "Edge") { + int GeoId = std::atoi(it->substr(4,4000).c_str()) - 1; + + // push all the constraints + int i = 0; + for (std::vector< Sketcher::Constraint * >::const_iterator it= vals.begin(); + it != vals.end(); ++it,++i) + { + if ((*it)->First == GeoId || (*it)->Second == GeoId || (*it)->Third == GeoId) { + associatedConstraintsFilter.push_back(i); + } + } + } + } + + updateList(); +} + void TaskSketcherConstrains::updateList() { // enforce constraint visibility @@ -899,6 +940,8 @@ void TaskSketcherConstrains::on_listWidgetConstraints_emitShowSelection3DVisibil void TaskSketcherConstrains::onSelectionChanged(const Gui::SelectionChanges& msg) { + assert(sketchView); + std::string temp; if (msg.Type == Gui::SelectionChanges::ClrSelection) { ui->listWidgetConstraints->blockSignals(true); @@ -912,6 +955,10 @@ void TaskSketcherConstrains::onSelectionChanged(const Gui::SelectionChanges& msg updateList(); this->blockConnection(block); } + else if (isFilter(ConstraintFilter::SpecialFilterValue::AssociatedConstraints)) { + associatedConstraintsFilter.clear(); + updateList(); + } } else if (msg.Type == Gui::SelectionChanges::AddSelection || msg.Type == Gui::SelectionChanges::RmvSelection) { @@ -923,7 +970,7 @@ void TaskSketcherConstrains::onSelectionChanged(const Gui::SelectionChanges& msg QRegExp rx(QString::fromLatin1("^Constraint(\\d+)$")); QString expr = QString::fromLatin1(msg.pSubName); int pos = expr.indexOf(rx); - if (pos > -1) { + if (pos > -1) { // is a constraint bool ok; int ConstrId = rx.cap(1).toInt(&ok) - 1; if (ok) { @@ -938,10 +985,25 @@ void TaskSketcherConstrains::onSelectionChanged(const Gui::SelectionChanges& msg break; } } - updateSelectionFilter(); - bool block = this->blockConnection(true); // avoid to be notified by itself - updateList(); - this->blockConnection(block); + + if(isFilter(ConstraintFilter::SpecialFilterValue::Selection)) { + updateSelectionFilter(); + bool block = this->blockConnection(true); // avoid to be notified by itself + updateList(); + this->blockConnection(block); + } + } + } + else if(isFilter(ConstraintFilter::SpecialFilterValue::AssociatedConstraints)) { // is NOT a constraint + int geoid = Sketcher::Constraint::GeoUndef; + Sketcher::PointPos pointpos = Sketcher::none; + getSelectionGeoId(expr, geoid, pointpos); + + if(geoid != Sketcher::Constraint::GeoUndef && pointpos == Sketcher::none){ + // It is not possible to update on single addition/removal of a geometric element, + // as one removal may imply removing a constraint that should be added by a different element + // that is still selected. The necessary checks outweight a full rebuild of the filter. + updateAssociatedConstraintsFilter(); } } } @@ -952,14 +1014,46 @@ void TaskSketcherConstrains::onSelectionChanged(const Gui::SelectionChanges& msg } } +void TaskSketcherConstrains::getSelectionGeoId(QString expr, int & geoid, Sketcher::PointPos & pointpos) +{ + QRegExp rxEdge(QString::fromLatin1("^Edge(\\d+)$")); + int pos = expr.indexOf(rxEdge); + geoid = Sketcher::Constraint::GeoUndef; + pointpos = Sketcher::none; + + if (pos > -1) { + bool ok; + int edgeId = rxEdge.cap(1).toInt(&ok) - 1; + if (ok) { + geoid = edgeId; + } + } + else { + QRegExp rxVertex(QString::fromLatin1("^Vertex(\\d+)$")); + pos = expr.indexOf(rxVertex); + + if (pos > -1) { + bool ok; + int vertexId = rxVertex.cap(1).toInt(&ok) - 1; + if (ok) { + const Sketcher::SketchObject * sketch = sketchView->getSketchObject(); + sketch->getGeoVertexIndex(vertexId, geoid, pointpos); + } + } + } +} void TaskSketcherConstrains::on_comboBoxFilter_currentIndexChanged(int filterindex) { selectionFilter.clear(); // reset the stored selection filter + associatedConstraintsFilter.clear(); if(filterindex == ConstraintFilter::SpecialFilterValue::Selection) { updateSelectionFilter(); } + else if(filterindex == ConstraintFilter::SpecialFilterValue::AssociatedConstraints) { + updateAssociatedConstraintsFilter(); + } updateList(); } @@ -1313,6 +1407,10 @@ bool TaskSketcherConstrains::isConstraintFiltered(QListWidgetItem * item) visible = visible || (Filter == SpecialFilterValue::Selection && std::find(selectionFilter.begin(), selectionFilter.end(), it->ConstraintNbr) != selectionFilter.end()); + // Constraint Type independent, associated Constraints Filter + visible = visible || (Filter == SpecialFilterValue::AssociatedConstraints && + std::find(associatedConstraintsFilter.begin(), associatedConstraintsFilter.end(), it->ConstraintNbr) != associatedConstraintsFilter.end()); + return !visible; } diff --git a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.h b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.h index 7965f60fbf..00e9d1715d 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.h +++ b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.h @@ -29,6 +29,8 @@ #include #include +#include + #include "ConstraintFilters.h" namespace App { @@ -93,12 +95,15 @@ private: void change3DViewVisibilityToTrackFilter(); void changeFilteredVisibility(bool show, ActionTarget target = ActionTarget::All); void updateSelectionFilter(); + void updateAssociatedConstraintsFilter(); void updateList(); void createVisibilityButtonActions(); template bool isFilter(T filterValue); + void getSelectionGeoId(QString expr, int & geoid, Sketcher::PointPos & pos); + public Q_SLOTS: void on_comboBoxFilter_currentIndexChanged(int); void on_listWidgetConstraints_itemSelectionChanged(void); @@ -131,6 +136,7 @@ private: std::unique_ptr ui; std::bitset multiFilterStatus; std::vector selectionFilter; + std::vector associatedConstraintsFilter; }; } //namespace SketcherGui diff --git a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.ui b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.ui index dca26d8b8a..7730655b4a 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherConstrains.ui +++ b/src/Mod/Sketcher/Gui/TaskSketcherConstrains.ui @@ -6,7 +6,7 @@ 0 0 - 405 + 417 388 @@ -182,6 +182,11 @@ Selection Filter + + + Associated Constraint Filter + +