1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <ModuleBase_WidgetMultiSelector.h>
22 #include <GeomAPI_AISObject.h>
24 #include <ModuleBase_ActionIntParameter.h>
25 #include <ModuleBase_Definitions.h>
26 #include <ModuleBase_Events.h>
27 #include <ModuleBase_IconFactory.h>
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_ISelection.h>
30 #include <ModuleBase_ISelectionActivate.h>
31 #include <ModuleBase_IPropertyPanel.h>
32 #include <ModuleBase_IViewer.h>
33 #include <ModuleBase_IWorkshop.h>
34 #include <ModuleBase_ListView.h>
35 #include <ModuleBase_ResultPrs.h>
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_ViewerPrs.h>
38 #include <ModuleBase_WidgetShapeSelector.h>
39 #include <ModuleBase_ChoiceCtrl.h>
40 #include <ModuleBase_WidgetSelectionFilter.h>
42 #include <ModelAPI_Data.h>
43 #include <ModelAPI_Object.h>
44 #include <ModelAPI_AttributeSelectionList.h>
45 #include <ModelAPI_AttributeRefList.h>
46 #include <ModelAPI_AttributeRefAttrList.h>
47 #include <ModelAPI_Tools.h>
48 #include <ModelAPI_Events.h>
50 #include <Config_WidgetAPI.h>
52 #include <AIS_InteractiveObject.hxx>
54 #include <QGridLayout>
56 #include <QListWidget>
61 #include <QApplication>
64 #include <QMainWindow>
66 #include <QPushButton>
71 //#define DEBUG_UNDO_REDO
73 #ifdef DEBUG_UNDO_REDO
74 void printHistoryInfo(const QString& theMethodName, int theCurrentHistoryIndex,
75 QList<QList<std::shared_ptr<ModuleBase_ViewerPrs> > > theSelectedHistoryValues)
78 for (int i = 0; i < theSelectedHistoryValues.size(); i++)
79 aSizes.append(QString::number(theSelectedHistoryValues[i].size()));
81 std::cout << theMethodName.toStdString()
82 << " current = " << theCurrentHistoryIndex
83 << " size(history) = " << theSelectedHistoryValues.size()
84 << " (" << aSizes.join(", ").toStdString() << ")"
90 QStringList getIconsList(const QStringList& theNames)
93 foreach (QString aName, theNames) {
94 QString aUName = aName.toUpper();
95 if ((aUName == "VERTICES") || (aUName == "VERTEX"))
96 aIcons << ":pictures/vertex32.png";
97 else if ((aUName == "EDGES") || (aUName == "EDGE"))
98 aIcons << ":pictures/edge32.png";
99 else if ((aUName == "FACES") || (aUName == "FACE"))
100 aIcons << ":pictures/face32.png";
101 else if ((aUName == "SOLIDS") || (aUName == "SOLID"))
102 aIcons << ":pictures/solid32.png";
107 /// Stores default values of selected option (selection mode)
108 /// It is used only in case if myTypeCtrl is used
109 static QMap<std::string, std::string> defaultValues;
112 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
113 ModuleBase_IWorkshop* theWorkshop,
114 const Config_WidgetAPI* theData)
115 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
116 myIsSetSelectionBlocked(false), myCurrentHistoryIndex(-1),
117 myIsFirst(true), myFiltersWgt(0)
119 std::string aPropertyTypes = theData->getProperty("shape_types");
120 QString aTypesStr = aPropertyTypes.c_str();
121 myShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
122 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
124 QString aAllowedList(theData->getProperty("allow_objects").c_str());
125 if (!aAllowedList.isEmpty())
126 myAllowedObjects = aAllowedList.split(' ', QString::SkipEmptyParts);
128 QVBoxLayout* aMainLay = new QVBoxLayout(this);
129 ModuleBase_Tools::adjustMargins(aMainLay);
131 QStringList aIconsList = getIconsList(myShapeTypes);
132 myTypeCtrl = new ModuleBase_ChoiceCtrl(this, myShapeTypes, aIconsList);
133 myTypeCtrl->setLabel(tr("Type"));
134 if (!myShapeTypes.empty()) {
135 myTypeCtrl->setValue(0);
136 myDefMode = myShapeTypes.first().toStdString();
138 aMainLay->addWidget(myTypeCtrl);
140 // There is no sense to parameterize list of types while we can not parameterize selection mode
141 // if the xml definition contains one type, the controls to select a type should not be shown
142 if (myShapeTypes.size() <= 1 || !myIsUseChoice) {
143 myTypeCtrl->setVisible(false);
146 QString aLabelText = translate(theData->getProperty("label"));
147 if (aLabelText.size() > 0) {
148 QWidget* aLabelWgt = new QWidget(this);
149 QHBoxLayout* aLabelLayout = new QHBoxLayout(aLabelWgt);
150 aLabelLayout->setContentsMargins(0, 0, 0, 0);
151 aMainLay->addWidget(aLabelWgt);
153 QLabel* aListLabel = new QLabel(aLabelText, this);
154 aLabelLayout->addWidget(aListLabel);
155 // if the xml definition contains one type, an information label
156 // should be shown near to the latest
157 if (myShapeTypes.size() <= 1) {
158 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
159 if (!aLabelIcon.isEmpty()) {
160 QLabel* aSelectedLabel = new QLabel("", this);
161 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
162 aLabelLayout->addWidget(aSelectedLabel);
163 aLabelLayout->addStretch(1);
168 QString aToolTip = translate(theData->widgetTooltip());
169 QString anObjName = QString::fromStdString(attributeID());
170 myListView = new ModuleBase_ListView(this, anObjName, aToolTip);
171 connect(myListView->getControl(), SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
172 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
173 connect(myListView, SIGNAL(listActivated()), SLOT(onListActivated()));
175 aMainLay->addWidget(myListView->getControl());
176 connect(myTypeCtrl, SIGNAL(valueChanged(int)), this, SLOT(onSelectionTypeChanged()));
178 std::string aUseFilters = theData->getProperty("use_filters");
179 if (aUseFilters.length() > 0) {
180 QWidget* aFltrWgt = new QWidget(this);
181 QHBoxLayout* aFltrLayout = new QHBoxLayout(aFltrWgt);
183 myFiltersWgt = new ModuleBase_FilterStarter(aUseFilters.c_str(), aFltrWgt, theWorkshop);
184 aFltrLayout->addWidget(myFiltersWgt);
186 aFltrLayout->addStretch();
188 QPushButton* aShowBtn = new QPushButton(tr("Show only"), aFltrWgt);
189 aShowBtn->setCheckable(true);
190 aShowBtn->setChecked(false);
191 connect(aShowBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
192 aFltrLayout->addWidget(aShowBtn);
194 aMainLay->addWidget(aFltrWgt);
197 bool aSameTop = theData->getBooleanAttribute("same_topology", false);
199 myGeomCheck = new QCheckBox(tr("Add elements that share the same topology"), this);
200 aMainLay->addWidget(myGeomCheck);
201 connect(myGeomCheck, SIGNAL(toggled(bool)), SLOT(onSameTopology(bool)));
206 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
207 if (myShapeTypes.size() > 1 || myIsUseChoice) {
208 if (defaultValues.contains(myFeatureId + attributeID())) {
209 myDefMode = defaultValues[myFeatureId + attributeID()];
210 myTypeCtrl->setValue(myDefMode.c_str());
215 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
219 //********************************************************************
220 void ModuleBase_WidgetMultiSelector::activateCustom()
222 ModuleBase_WidgetSelector::activateCustom();
224 ModuleBase_IModule* aModule = myWorkshop->module();
225 aModule->activateCustomPrs(myFeature,
226 ModuleBase_IModule::CustomizeHighlightedObjects, true);
227 clearSelectedHistory();
228 if (myAllowedObjects.length() > 0) {
229 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_GlobalFilter);
230 if (!aFilter.IsNull()) {
231 Handle(ModuleBase_ShapeDocumentFilter) aDocFilter =
232 Handle(ModuleBase_ShapeDocumentFilter)::DownCast(aFilter);
233 if (!aDocFilter.IsNull()) {
234 QStringList aSelFilters = aDocFilter->nonSelectableTypes();
235 foreach(QString aType, aSelFilters) {
236 if (aSelFilters.contains(aType)) {
237 aDocFilter->removeNonSelectableType(aType);
238 myTmpAllowed.append(aType);
246 //********************************************************************
247 void ModuleBase_WidgetMultiSelector::deactivate()
249 myWorkshop->module()->enableCustomModes();
251 ModuleBase_WidgetSelector::deactivate();
252 if (myVisibleObjects.size())
255 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
256 clearSelectedHistory();
257 if (myTmpAllowed.length() > 0) {
258 ModuleBase_IModule* aModule = myWorkshop->module();
259 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_GlobalFilter);
260 if (!aFilter.IsNull()) {
261 Handle(ModuleBase_ShapeDocumentFilter) aDocFilter =
262 Handle(ModuleBase_ShapeDocumentFilter)::DownCast(aFilter);
263 if (!aDocFilter.IsNull()) {
264 foreach(QString aType, myTmpAllowed) {
265 aDocFilter->addNonSelectableType(aType);
269 myTmpAllowed.clear();
273 //********************************************************************
274 void ModuleBase_WidgetMultiSelector::updateAfterDeactivation()
276 // restore previous Undo/Redo workshop state
277 myWorkshop->updateCommandStatus();
280 //********************************************************************
281 void ModuleBase_WidgetMultiSelector::updateAfterActivation()
283 // fill Undo/Redo actions with current information
284 myWorkshop->updateCommandStatus();
287 //********************************************************************
288 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
290 // the value is stored on the selection changed signal processing
291 // A rare case when plugin was not loaded.
295 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
296 std::string aType = anAttribute->attributeType();
297 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
298 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
299 if (myTypeCtrl->isVisible()) {
300 std::string aMode = myTypeCtrl->textValue().toStdString();
301 if (myIsFirst && (!myDefMode.empty()))
304 aSelectionListAttr->setSelectionType(aMode);
306 } else { // no type, set the type as a first element of the list shape type when it is appeared
307 if (aSelectionListAttr->size()) {
308 AttributeSelectionPtr aSel = aSelectionListAttr->value(0);
309 GeomShapePtr aFirstVal = aSel->value();
310 if (!aFirstVal.get() && aSel->context().get())
311 aFirstVal = aSel->context()->shape();
312 if (aFirstVal.get() && !aFirstVal->isNull())
313 aSelectionListAttr->setSelectionType(aFirstVal->shapeTypeStr());
320 //********************************************************************
321 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
323 // A rare case when plugin was not loaded.
327 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
328 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
329 std::string aType = anAttribute->attributeType();
330 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
331 // Restore shape type
332 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
333 if (!aSelectionType.empty()) {
334 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
335 myDefMode = aSelectionType;
340 myGeomCheck->setChecked(aSelectionListAttr->isGeometricalSelection());
341 updateSelectionList();
345 //********************************************************************
346 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
347 const bool theToValidate)
349 if (myIsSetSelectionBlocked)
352 AttributeSelectionListPtr aSelectionListAttr;
353 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
354 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
355 if (aSelectionListAttr.get())
356 aSelectionListAttr->cashValues(true);
358 /// remove unused objects from the model attribute.
359 /// It should be performed before new attributes append.
360 bool isDone = removeUnusedAttributeObjects(theValues);
362 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
363 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
364 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
365 for (; anIt != aLast; anIt++) {
366 ModuleBase_ViewerPrsPtr aValue = *anIt;
367 // do not validate and append to attribute selection presentation if it exists in the attribute
370 getGeomSelection(aValue, anObject, aShape);
371 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
372 anAttributeValues.append(aValue);
375 if (theToValidate && !isValidInFilters(aValue))
376 anInvalidValues.append(aValue);
378 bool aHasInvalidValues = anInvalidValues.size() > 0;
380 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
381 ModuleBase_ViewerPrsPtr aValue = *anIt;
382 bool aProcessed = false;
383 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
384 anAttributeValues.contains(aValue))
386 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
387 // if there is at least one set, the result is true
388 isDone = isDone || aProcessed;
390 // Check the selection with validators
391 QString aError = getError();
392 if (aError.length() > 0) {
393 aSelectionListAttr->clear();
396 // updateObject - to update/redisplay feature
397 // it is commented in order to perfom it outside the method
399 //updateObject(myFeature);
400 // this emit is necessary to call store/restore method an restore type of selection
401 //emit valuesChanged();
404 if (aSelectionListAttr.get())
405 aSelectionListAttr->cashValues(false);
408 if (!anInvalidValues.empty())
409 theValues.append(anInvalidValues);
411 if (isDone) // may be the feature's result is not displayed, but attributes should be
412 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
413 true);/// hope that something is redisplayed by object updated
418 //********************************************************************
419 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
421 std::set<int> anAttributeIds;
422 getSelectedAttributeIndices(anAttributeIds);
423 if (!anAttributeIds.empty())
424 convertIndicesToViewerSelection(anAttributeIds, theValues);
427 //********************************************************************
428 bool ModuleBase_WidgetMultiSelector::canProcessAction(ModuleBase_ActionType theActionType,
429 bool& isActionEnabled)
431 isActionEnabled = false;
432 bool aCanProcess = false;
433 switch (theActionType) {
437 isActionEnabled = theActionType == ActionUndo ? myCurrentHistoryIndex > 0
438 : (mySelectedHistoryValues.size() > 0 &&
439 myCurrentHistoryIndex < mySelectedHistoryValues.size() - 1);
443 aCanProcess = ModuleBase_WidgetSelector::canProcessAction(theActionType, isActionEnabled);
449 //********************************************************************
450 bool ModuleBase_WidgetMultiSelector::processAction(ModuleBase_ActionType theActionType,
451 const ActionParamPtr& theParam)
453 switch (theActionType) {
456 ActionIntParamPtr aParam =
457 std::dynamic_pointer_cast<ModuleBase_ActionIntParameter>(theParam);
458 int aNb = aParam->value();
459 if (theActionType == ActionUndo)
460 myCurrentHistoryIndex -= aNb;
462 myCurrentHistoryIndex += aNb;
463 QList<ModuleBase_ViewerPrsPtr> aSelected = mySelectedHistoryValues[myCurrentHistoryIndex];
464 // equal vertices should not be used here
465 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
466 bool isDone = setSelection(aSelected,
467 false /*need not validate because values already was in list*/);
468 updateOnSelectionChanged(isDone);
470 myWorkshop->updateCommandStatus();
471 #ifdef DEBUG_UNDO_REDO
472 printHistoryInfo(QString("processAction %1").arg(theActionType == ActionUndo ? "Undo"
473 : "Redo"), myCurrentHistoryIndex, mySelectedHistoryValues);
478 return ModuleBase_ModelWidget::processAction(theActionType, theParam);
482 //********************************************************************
483 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
485 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
487 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
488 if (!aResult.get()) { // In case if a feature was selected
489 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(thePrs->object());
491 aResult = aFeature->firstResult();
493 aValid = aResult.get() != NULL;
496 // We can not select a result of our feature
497 std::list<ResultPtr> aResults;
498 ModelAPI_Tools::allResults(myFeature, aResults);
499 std::list<ResultPtr>::const_iterator aIt;
500 bool isSkipSelf = false;
501 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
502 if ((*aIt) == aResult) {
515 //********************************************************************
516 bool ModuleBase_WidgetMultiSelector::processDelete()
518 appendFirstSelectionInHistory();
520 // find attribute indices to delete
521 std::set<int> anAttributeIds;
522 getSelectedAttributeIndices(anAttributeIds);
524 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
526 // refill attribute by the items which indices are not in the list of ids
528 DataPtr aData = myFeature->data();
529 AttributePtr anAttribute = aData->attribute(attributeID());
530 std::string aType = anAttribute->attributeType();
531 aDone = !anAttributeIds.empty();
532 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
533 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
534 aSelectionListAttr->remove(anAttributeIds);
537 else if (aType == ModelAPI_AttributeRefList::typeId()) {
538 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
539 aRefListAttr->remove(anAttributeIds);
541 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
542 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
543 aRefAttrListAttr->remove(anAttributeIds);
547 // update object is necessary to flush update signal. It leads to objects references map update
548 // and the operation presentation will not contain deleted items visualized as parameters of
550 updateObject(myFeature);
553 myWorkshop->setSelected(getAttributeSelection());
555 // may be the feature's result is not displayed, but attributes should be
556 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
557 true); /// hope that something is redisplayed by object updated
561 myListView->restoreSelection(anIndices);
563 appendSelectionInHistory();
564 return true/*aDone*/; // following #2438 Delete should be processed even if nothing is delete
567 //********************************************************************
568 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
570 QList<QWidget*> result;
571 result << myListView->getControl();
575 //********************************************************************
576 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
578 // Clear current selection in order to avoid updating of object browser with obsolete indexes
579 // which can appear because of results deletetion after changing a type of selection
580 QString aSelectionType = myTypeCtrl->textValue();
581 QList<ModuleBase_ViewerPrsPtr> aEmptyList;
582 myWorkshop->setSelected(aEmptyList);
584 updateSelectionModesAndFilters(true);
585 myWorkshop->selectionActivate()->updateSelectionModes();
589 /// store the selected type
590 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
591 std::string aType = anAttribute->attributeType();
592 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
593 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
594 aSelectionListAttr->setSelectionType(aSelectionType.toStdString());
597 // clear attribute values
598 DataPtr aData = myFeature->data();
599 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
600 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
601 aSelectionListAttr->clear();
603 else if (aType == ModelAPI_AttributeRefList::typeId()) {
604 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
605 aRefListAttr->clear();
607 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
608 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
609 aRefAttrListAttr->clear();
612 // update object is necessary to flush update signal. It leads to objects references map update
613 // and the operation presentation will not contain deleted items visualized as parameters of
615 updateObject(myFeature);
617 myWorkshop->setSelected(getAttributeSelection());
618 // may be the feature's result is not displayed, but attributes should be
619 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
620 true); /// hope that something is redisplayed by object updated
621 // clear history should follow after set selected to do not increase history by setSelected
622 clearSelectedHistory();
624 if (myWorkshop->propertyPanel()->activeWidget() != this)
625 myWorkshop->propertyPanel()->activateWidget(this);
628 //********************************************************************
629 bool ModuleBase_WidgetMultiSelector::processSelection()
631 if (!myIsNeutralPointClear) {
632 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
633 // do not clear selected object
634 if (aSelected.size() == 0) {
635 if (!getAttributeSelection().empty()) {
636 // Restore selection in the viewer by the attribute selection list
637 // it should be postponed to exit from the selectionChanged processing
638 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
639 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
640 Events_Loop::loop()->flush(anEvent);
645 appendFirstSelectionInHistory();
646 bool aDone = ModuleBase_WidgetSelector::processSelection();
647 appendSelectionInHistory();
651 void ModuleBase_WidgetMultiSelector::appendFirstSelectionInHistory()
653 if (mySelectedHistoryValues.empty()) {
654 myCurrentHistoryIndex++;
655 mySelectedHistoryValues.append(getAttributeSelection());
657 #ifdef DEBUG_UNDO_REDO
658 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
663 void ModuleBase_WidgetMultiSelector::appendSelectionInHistory()
665 while (myCurrentHistoryIndex != mySelectedHistoryValues.count() - 1)
666 mySelectedHistoryValues.removeLast();
668 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
669 myCurrentHistoryIndex++;
670 mySelectedHistoryValues.append(aSelected);
671 myWorkshop->updateCommandStatus();
673 #ifdef DEBUG_UNDO_REDO
674 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
678 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
680 mySelectedHistoryValues.clear();
681 myCurrentHistoryIndex = -1;
682 myWorkshop->updateCommandStatus();
684 #ifdef DEBUG_UNDO_REDO
685 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
689 void ModuleBase_WidgetMultiSelector::updateFocus()
691 // Set focus to List control in order to make possible
692 // to use Tab key for transfer the focus to next widgets
693 ModuleBase_Tools::setFocus(myListView->getControl(),
694 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
697 //********************************************************************
698 void ModuleBase_WidgetMultiSelector::updateSelectionName()
702 //********************************************************************
703 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
705 if (myIsSetSelectionBlocked)
707 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
709 // according to #2154 we need to update OB selection when selection in the viewer happens
710 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
711 // because we need that Object Browser has been already updated when synchronize happens.
713 // Restore selection in the viewer by the attribute selection list
714 // it is possible that diring selection attribute filling, selection in Object Browser
715 // is changed(some items were removed/added) and as result, selection in the viewer
716 // differs from the selection come to this method. By next rows, we restore selection
717 // in the viewer according to content of selection attribute. Case is Edge selection in Group
718 myIsSetSelectionBlocked = true;
719 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
720 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
721 Events_Loop::loop()->flush(anEvent);
722 myIsSetSelectionBlocked = false;
725 //********************************************************************
726 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
728 QIntList aShapeTypes;
730 if (myShapeTypes.length() > 1 && myIsUseChoice) {
731 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCtrl->textValue()));
734 foreach (QString aType, myShapeTypes) {
735 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
741 //********************************************************************
742 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
744 QString aShapeTypeName;
747 foreach (QString aShapeTypeName, myShapeTypes) {
748 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
749 if(aRefType == theShapeType && idx != myTypeCtrl->value()) {
750 updateSelectionModesAndFilters(false);
751 bool isBlocked = myTypeCtrl->blockSignals(true);
752 myTypeCtrl->setValue(idx);
753 myTypeCtrl->blockSignals(isBlocked);
754 updateSelectionModesAndFilters(true);
761 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
763 QList<ModuleBase_ViewerPrsPtr> aSelected;
764 convertIndicesToViewerSelection(std::set<int>(), aSelected);
768 //********************************************************************
769 void ModuleBase_WidgetMultiSelector::updateSelectionList()
771 myListView->getControl()->clear();
773 DataPtr aData = myFeature->data();
774 AttributePtr anAttribute = aData->attribute(attributeID());
775 std::string aType = anAttribute->attributeType();
776 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
777 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
778 for (int i = 0; i < aSelectionListAttr->size(); i++) {
779 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
780 myListView->addItem(aAttr->namingName().c_str(), i);
783 else if (aType == ModelAPI_AttributeRefList::typeId()) {
784 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
785 for (int i = 0; i < aRefListAttr->size(); i++) {
786 ObjectPtr anObject = aRefListAttr->object(i);
787 if (anObject.get()) {
788 myListView->addItem(anObject->data()->name().c_str(), i);
792 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
793 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
794 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
795 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
797 if (anAttribute.get()) {
798 std::string anAttrName = ModuleBase_Tools::generateName(anAttribute, myWorkshop);
799 aName = QString::fromStdString(anAttrName);
802 ObjectPtr anObject = aRefAttrListAttr->object(i);
803 if (anObject.get()) {
804 aName = anObject->data()->name().c_str();
807 myListView->addItem(aName, i);
811 // We have to call repaint because sometimes the List control is not updated
812 myListView->getControl()->update();
815 //********************************************************************
816 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
820 if (theType == "Vertices")
822 else if (theType == "Edges")
824 else if (theType == "Faces")
826 else if (theType == "Solids")
832 //********************************************************************
833 void ModuleBase_WidgetMultiSelector::clearSelection()
835 bool isClearInNeutralPoint = myIsNeutralPointClear;
836 myIsNeutralPointClear = true;
838 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
839 // This method will call Selection changed event which will call onSelectionChanged
840 // To clear mySelection, myListView and storeValue()
841 // So, we don't need to call it
842 myWorkshop->setSelected(anEmptyList);
844 myIsNeutralPointClear = isClearInNeutralPoint;
847 //********************************************************************
848 void ModuleBase_WidgetMultiSelector::onDeleteItem()
853 //********************************************************************
854 void ModuleBase_WidgetMultiSelector::onListSelection()
856 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
860 //********************************************************************
861 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
863 myListView->getSelectedIndices(theAttributeIds);
866 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
867 QList<ModuleBase_ViewerPrsPtr>& theValues) const
869 if(myFeature.get() == NULL)
872 DataPtr aData = myFeature->data();
873 AttributePtr anAttribute = aData->attribute(attributeID());
874 std::string aType = anAttribute->attributeType();
875 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
876 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
877 for (int i = 0; i < aSelectionListAttr->size(); i++) {
878 // filter by attribute indices only if the container is not empty otherwise return all items
879 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
881 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
882 ObjectPtr anObject = anAttr->contextObject();
884 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
885 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
888 else if (aType == ModelAPI_AttributeRefList::typeId()) {
889 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
890 for (int i = 0; i < aRefListAttr->size(); i++) {
891 // filter by attribute indices only if the container is not empty otherwise return all items
892 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
894 ObjectPtr anObject = aRefListAttr->object(i);
895 if (anObject.get()) {
896 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
897 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
901 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
902 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
903 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
904 // filter by attribute indices only if the container is not empty otherwise return all items
905 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
907 ObjectPtr anObject = aRefAttrListAttr->object(i);
911 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
912 if (anAttribute.get()) {
913 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
914 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
915 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
921 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
922 (QList<ModuleBase_ViewerPrsPtr>& theValues)
926 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
927 DataPtr aData = myFeature->data();
928 AttributePtr anAttribute = aData->attribute(attributeID());
929 std::string aType = anAttribute->attributeType();
930 std::set<GeomShapePtr> aShapes;
931 std::set<int> anIndicesToBeRemoved;
933 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
934 // iteration through data model to find not selected elements to remove them
935 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
936 for (int i = 0; i < aSelectionListAttr->size(); i++) {
937 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
938 //aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->contextObject());
939 //if (!aFeature.get()) { // Feature can not be found as geometry selection
940 bool aFound = findInSelection(
941 anAttr->contextObject(), anAttr->value(), aGeomSelection, myWorkshop);
943 anIndicesToBeRemoved.insert(i);
946 isDone = anIndicesToBeRemoved.size() > 0;
948 aSelectionListAttr->remove(anIndicesToBeRemoved);
950 else if (aType == ModelAPI_AttributeRefList::typeId()) {
951 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
952 for (int i = 0; i < aRefListAttr->size(); i++) {
953 ObjectPtr anObject = aRefListAttr->object(i);
954 if (anObject.get()) {
955 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
958 anIndicesToBeRemoved.insert(i);
961 isDone = anIndicesToBeRemoved.size() > 0;
962 aRefListAttr->remove(anIndicesToBeRemoved);
964 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
965 std::set<AttributePtr> anAttributes;
966 QList<ModuleBase_ViewerPrsPtr>::const_iterator
967 anIt = theValues.begin(), aLast = theValues.end();
970 for (; anIt != aLast; anIt++) {
971 ModuleBase_ViewerPrsPtr aPrs = *anIt;
972 getGeomSelection(aPrs, anObject, aShape);
973 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
974 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
975 anAttributes.insert(anAttr);
978 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
979 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
981 if (aRefAttrListAttr->isAttribute(i)) {
982 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
983 aFound = anAttributes.find(anAttribute) != anAttributes.end();
986 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
990 anIndicesToBeRemoved.insert(i);
992 isDone = anIndicesToBeRemoved.size() > 0;
993 aRefAttrListAttr->remove(anIndicesToBeRemoved);
999 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
1000 (QList<ModuleBase_ViewerPrsPtr>& theValues)
1002 // convert prs list to objects map
1003 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
1004 std::set<GeomShapePtr> aShapes;
1005 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
1007 GeomShapePtr aShape;
1008 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
1009 for (; anIt != aLast; anIt++) {
1010 ModuleBase_ViewerPrsPtr aPrs = *anIt;
1011 getGeomSelection(aPrs, anObject, aShape);
1013 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
1014 aShapes = aGeomSelection[anObject];
1015 // we need to know if there was an empty shape in selection for the object
1017 aShape = anEmptyShape;
1018 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
1019 aShapes.insert(aShape);
1020 aGeomSelection[anObject] = aShapes;
1022 return aGeomSelection;
1025 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
1026 GeomShapePtr theShape,
1027 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
1028 ModuleBase_IWorkshop* theWorkshop)
1030 if (!theObject.get())
1032 // issue #2154: we should not remove from list objects hidden in the viewer if selection
1033 // was done with SHIFT button
1034 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
1037 bool aFound = false;
1038 GeomShapePtr aShape = theShape;
1039 if (!aShape.get()) {
1040 // #2429 (the preselection of a sketch is not taken into account)
1041 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1043 aShape = aResult->shape();
1045 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
1046 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
1047 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
1048 for (; anIt != aLast && !aFound; anIt++) {
1049 GeomShapePtr aCShape = *anIt;
1052 // treat shape equal to context as null: 2219, keep order of shapes in list
1053 if (aCShape->isNull()) { // in selection, shape of result is equal to selected shape
1054 // if so, here we need to check shape of result
1055 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1057 aCShape = aResult->shape();
1059 aFound = aCShape->isSame(aShape);
1064 // issue #2903: (Possibility to hide faces) - check whether given shape is a hidden sub-shape
1065 if (!aFound && theShape.get() && theWorkshop->hasSHIFTPressed() && theObject->isDisplayed()) {
1066 AISObjectPtr anAIS = theWorkshop->findPresentation(theObject);
1067 if (anAIS.get() != NULL) {
1068 Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1070 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(anAISIO);
1071 if (!aResultPrs.IsNull() && aResultPrs->isSubShapeHidden(theShape->impl<TopoDS_Shape>()))
1080 ModuleBase_WidgetMultiSelector::actionsList(ModuleBase_ActionType theActionType) const
1082 QList<ActionInfo> aList;
1083 if (myCurrentHistoryIndex > -1) {
1085 QString aTitle("Selection %1 items");
1086 QString aTit("Selection %1 item");
1087 QIcon aIcon(":pictures/selection.png");
1089 switch (theActionType) {
1092 while (i <= myCurrentHistoryIndex) {
1093 aNb = mySelectedHistoryValues.at(i).count();
1095 ActionInfo aInfo(aIcon, aTit.arg(aNb));
1096 aList.insert(0, aInfo);
1098 ActionInfo aInfo(aIcon, aTitle.arg(aNb));
1099 aList.insert(0, aInfo);
1105 i = mySelectedHistoryValues.length() - 1;
1106 while (i > myCurrentHistoryIndex) {
1107 aNb = mySelectedHistoryValues.at(i).count();
1109 ActionInfo aInfo(aIcon, aTit.arg(mySelectedHistoryValues.at(i).count()));
1110 aList.insert(0, aInfo);
1112 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));
1113 aList.insert(0, aInfo);
1124 void ModuleBase_WidgetMultiSelector::onFeatureAccepted()
1126 defaultValues[myFeatureId + attributeID()] = myDefMode;
1129 void ModuleBase_WidgetMultiSelector::onListActivated()
1132 emitFocusInWidget();
1135 void ModuleBase_WidgetMultiSelector::onSameTopology(bool theOn)
1137 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
1138 std::string aType = anAttribute->attributeType();
1139 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
1140 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
1141 aSelectionListAttr->setGeometricalSelection(theOn);
1142 updateObject(myFeature);
1146 void ModuleBase_WidgetMultiSelector::onShowOnly(bool theChecked)
1148 std::list<ResultPtr> aResults = myFeature->results();
1149 std::list<ResultPtr>::const_iterator aIt;
1151 myVisibleObjects = myWorkshop->displayedObjects();
1152 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1153 myVisibleObjects.removeAll(*aIt);
1155 myWorkshop->module()->disableCustomMode(ModuleBase_IModule::CustomizeArguments);
1158 myWorkshop->module()->enableCustomModes();
1160 foreach(ObjectPtr aObj, myVisibleObjects) {
1161 aObj->setDisplayed(!theChecked);
1165 // Hide and show the group result in order to make it above all objects
1166 bool aOldState = myWorkshop->enableUpdateViewer(false);
1167 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1168 (*aIt)->setDisplayed(false);
1170 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1171 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1172 (*aIt)->setDisplayed(true);
1174 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1175 myWorkshop->enableUpdateViewer(aOldState);
1177 myVisibleObjects.clear();
1179 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));