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), myShowOnlyBtn(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 myMainLayout = new QVBoxLayout(this);
129 ModuleBase_Tools::adjustMargins(myMainLayout);
131 QStringList aIconsList = getIconsList(myShapeTypes);
132 myTypeCtrl = new ModuleBase_ChoiceCtrl(this, myShapeTypes, aIconsList);
133 myTypeCtrl->setLabel(tr("Type"));
134 if (!myShapeTypes.empty()) {
135 std::string aDefType = theData->getProperty("default_type");
136 if (aDefType.size() > 0) {
138 int aId = QString(aDefType.c_str()).toInt(&aOk);
140 myTypeCtrl->setValue(aId);
141 myDefMode = myShapeTypes.at(aId).toStdString();
144 if (myDefMode.size() == 0) {
145 myTypeCtrl->setValue(0);
146 myDefMode = myShapeTypes.first().toStdString();
149 myMainLayout->addWidget(myTypeCtrl);
151 // There is no sense to parameterize list of types while we can not parameterize selection mode
152 // if the xml definition contains one type, the controls to select a type should not be shown
153 if (myShapeTypes.size() <= 1 || !myIsUseChoice) {
154 myTypeCtrl->setVisible(false);
157 QString aLabelText = translate(theData->getProperty("label"));
158 if (aLabelText.size() > 0) {
159 QWidget* aLabelWgt = new QWidget(this);
160 QHBoxLayout* aLabelLayout = new QHBoxLayout(aLabelWgt);
161 aLabelLayout->setContentsMargins(0, 0, 0, 0);
162 myMainLayout->addWidget(aLabelWgt);
164 QLabel* aListLabel = new QLabel(aLabelText, this);
165 aLabelLayout->addWidget(aListLabel);
166 // if the xml definition contains one type, an information label
167 // should be shown near to the latest
168 if (myShapeTypes.size() <= 1) {
169 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
170 if (!aLabelIcon.isEmpty()) {
171 QLabel* aSelectedLabel = new QLabel("", this);
172 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
173 aLabelLayout->addWidget(aSelectedLabel);
174 aLabelLayout->addStretch(1);
179 QString aToolTip = translate(theData->widgetTooltip());
180 QString anObjName = QString::fromStdString(attributeID());
181 myListView = new ModuleBase_ListView(this, anObjName, aToolTip);
182 connect(myListView->getControl(), SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
183 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
184 connect(myListView, SIGNAL(listActivated()), SLOT(onListActivated()));
186 myMainLayout->addWidget(myListView->getControl());
187 connect(myTypeCtrl, SIGNAL(valueChanged(int)), this, SLOT(onSelectionTypeChanged()));
189 myUseFilters = theData->getProperty("use_filters");
190 if (myUseFilters.length() > 0) {
191 QWidget* aFltrWgt = new QWidget(this);
192 QHBoxLayout* aFltrLayout = new QHBoxLayout(aFltrWgt);
194 myFiltersWgt = new ModuleBase_FilterStarter(myUseFilters, aFltrWgt, theWorkshop);
195 aFltrLayout->addWidget(myFiltersWgt);
197 aFltrLayout->addStretch();
199 myShowOnlyBtn = new QPushButton(tr("Show only"), aFltrWgt);
200 myShowOnlyBtn->setCheckable(true);
201 myShowOnlyBtn->setChecked(false);
202 connect(myShowOnlyBtn, SIGNAL(toggled(bool)), SLOT(onShowOnly(bool)));
203 aFltrLayout->addWidget(myShowOnlyBtn);
205 myMainLayout->addWidget(aFltrWgt);
208 bool aSameTop = theData->getBooleanAttribute("same_topology", false);
210 myGeomCheck = new QCheckBox(tr("Add elements that share the same topology"), this);
211 myMainLayout->addWidget(myGeomCheck);
212 connect(myGeomCheck, SIGNAL(toggled(bool)), SLOT(onSameTopology(bool)));
217 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
218 if (myShapeTypes.size() > 1 || myIsUseChoice) {
219 if (defaultValues.contains(myFeatureId + attributeID())) {
220 myDefMode = defaultValues[myFeatureId + attributeID()];
221 myTypeCtrl->setValue(myDefMode.c_str());
226 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
230 //********************************************************************
231 void ModuleBase_WidgetMultiSelector::activateCustom()
233 ModuleBase_WidgetSelector::activateCustom();
235 ModuleBase_IModule* aModule = myWorkshop->module();
236 aModule->activateCustomPrs(myFeature,
237 ModuleBase_IModule::CustomizeHighlightedObjects, true);
238 clearSelectedHistory();
239 if (myAllowedObjects.length() > 0) {
240 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_GlobalFilter);
241 if (!aFilter.IsNull()) {
242 Handle(ModuleBase_ShapeDocumentFilter) aDocFilter =
243 Handle(ModuleBase_ShapeDocumentFilter)::DownCast(aFilter);
244 if (!aDocFilter.IsNull()) {
245 QStringList aSelFilters = aDocFilter->nonSelectableTypes();
246 foreach(QString aType, aSelFilters) {
247 if (aSelFilters.contains(aType)) {
248 aDocFilter->removeNonSelectableType(aType);
249 myTmpAllowed.append(aType);
257 //********************************************************************
258 void ModuleBase_WidgetMultiSelector::deactivate()
260 myWorkshop->module()->enableCustomModes();
262 ModuleBase_WidgetSelector::deactivate();
263 if (myVisibleObjects.size())
264 myShowOnlyBtn->setChecked(false);
266 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
267 clearSelectedHistory();
268 if (myTmpAllowed.length() > 0) {
269 ModuleBase_IModule* aModule = myWorkshop->module();
270 Handle(SelectMgr_Filter) aFilter = aModule->selectionFilter(SF_GlobalFilter);
271 if (!aFilter.IsNull()) {
272 Handle(ModuleBase_ShapeDocumentFilter) aDocFilter =
273 Handle(ModuleBase_ShapeDocumentFilter)::DownCast(aFilter);
274 if (!aDocFilter.IsNull()) {
275 foreach(QString aType, myTmpAllowed) {
276 aDocFilter->addNonSelectableType(aType);
280 myTmpAllowed.clear();
284 //********************************************************************
285 void ModuleBase_WidgetMultiSelector::updateAfterDeactivation()
287 // restore previous Undo/Redo workshop state
288 myWorkshop->updateCommandStatus();
291 //********************************************************************
292 void ModuleBase_WidgetMultiSelector::updateAfterActivation()
294 // fill Undo/Redo actions with current information
295 myWorkshop->updateCommandStatus();
298 //********************************************************************
299 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
301 // the value is stored on the selection changed signal processing
302 // A rare case when plugin was not loaded.
306 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
307 std::string aType = anAttribute->attributeType();
308 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
309 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
310 if (myTypeCtrl->isVisible()) {
311 std::string aMode = myTypeCtrl->textValue().toStdString();
312 if (myIsFirst && (!myDefMode.empty()))
315 aSelectionListAttr->setSelectionType(aMode);
317 } else { // no type, set the type as a first element of the list shape type when it is appeared
318 if (aSelectionListAttr->size()) {
319 AttributeSelectionPtr aSel = aSelectionListAttr->value(0);
320 GeomShapePtr aFirstVal = aSel->value();
321 if (!aFirstVal.get() && aSel->context().get())
322 aFirstVal = aSel->context()->shape();
323 if (aFirstVal.get() && !aFirstVal->isNull())
324 aSelectionListAttr->setSelectionType(aFirstVal->shapeTypeStr());
331 //********************************************************************
332 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
334 // A rare case when plugin was not loaded.
338 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
339 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
340 std::string aType = anAttribute->attributeType();
341 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
342 // Restore shape type
343 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
344 if (aSelectionType.empty())
345 aSelectionListAttr->setSelectionType(myDefMode);
347 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
348 myDefMode = aSelectionType;
353 myGeomCheck->setChecked(aSelectionListAttr->isGeometricalSelection());
354 updateSelectionList();
358 //********************************************************************
359 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
360 const bool theToValidate)
362 if (myIsSetSelectionBlocked)
365 AttributeSelectionListPtr aSelectionListAttr;
366 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
367 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
368 if (aSelectionListAttr.get())
369 aSelectionListAttr->cashValues(true);
371 /// remove unused objects from the model attribute.
372 /// It should be performed before new attributes append.
373 bool isDone = removeUnusedAttributeObjects(theValues);
375 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
376 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
377 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
378 for (; anIt != aLast; anIt++) {
379 ModuleBase_ViewerPrsPtr aValue = *anIt;
380 // do not validate and append to attribute selection presentation if it exists in the attribute
383 getGeomSelection(aValue, anObject, aShape);
384 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
385 anAttributeValues.append(aValue);
388 if (theToValidate && !isValidInFilters(aValue))
389 anInvalidValues.append(aValue);
391 bool aHasInvalidValues = anInvalidValues.size() > 0;
393 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
394 ModuleBase_ViewerPrsPtr aValue = *anIt;
395 bool aProcessed = false;
396 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
397 anAttributeValues.contains(aValue))
399 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
400 // if there is at least one set, the result is true
401 isDone = isDone || aProcessed;
403 // Check the selection with validators
404 QString aError = getError();
405 if (aError.length() > 0) {
406 aSelectionListAttr->clear();
409 // updateObject - to update/redisplay feature
410 // it is commented in order to perfom it outside the method
412 //updateObject(myFeature);
413 // this emit is necessary to call store/restore method an restore type of selection
414 //emit valuesChanged();
417 if (aSelectionListAttr.get())
418 aSelectionListAttr->cashValues(false);
421 if (!anInvalidValues.empty())
422 theValues.append(anInvalidValues);
424 if (isDone) // may be the feature's result is not displayed, but attributes should be
425 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments,
426 true);/// hope that something is redisplayed by object updated
431 //********************************************************************
432 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
434 std::set<int> anAttributeIds;
435 getSelectedAttributeIndices(anAttributeIds);
436 if (!anAttributeIds.empty())
437 convertIndicesToViewerSelection(anAttributeIds, theValues);
440 //********************************************************************
441 bool ModuleBase_WidgetMultiSelector::canProcessAction(ModuleBase_ActionType theActionType,
442 bool& isActionEnabled)
444 isActionEnabled = false;
445 bool aCanProcess = false;
446 switch (theActionType) {
450 isActionEnabled = theActionType == ActionUndo ? myCurrentHistoryIndex > 0
451 : (mySelectedHistoryValues.size() > 0 &&
452 myCurrentHistoryIndex < mySelectedHistoryValues.size() - 1);
456 aCanProcess = ModuleBase_WidgetSelector::canProcessAction(theActionType, isActionEnabled);
462 //********************************************************************
463 bool ModuleBase_WidgetMultiSelector::processAction(ModuleBase_ActionType theActionType,
464 const ActionParamPtr& theParam)
466 switch (theActionType) {
469 ActionIntParamPtr aParam =
470 std::dynamic_pointer_cast<ModuleBase_ActionIntParameter>(theParam);
471 int aNb = aParam->value();
472 if (theActionType == ActionUndo)
473 myCurrentHistoryIndex -= aNb;
475 myCurrentHistoryIndex += aNb;
476 QList<ModuleBase_ViewerPrsPtr> aSelected = mySelectedHistoryValues[myCurrentHistoryIndex];
477 // equal vertices should not be used here
478 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
479 bool isDone = setSelection(aSelected,
480 false /*need not validate because values already was in list*/);
481 updateOnSelectionChanged(isDone);
483 myWorkshop->updateCommandStatus();
484 #ifdef DEBUG_UNDO_REDO
485 printHistoryInfo(QString("processAction %1").arg(theActionType == ActionUndo ? "Undo"
486 : "Redo"), myCurrentHistoryIndex, mySelectedHistoryValues);
491 return ModuleBase_ModelWidget::processAction(theActionType, theParam);
495 //********************************************************************
496 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
498 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
500 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
501 if (!aResult.get()) { // In case if a feature was selected
502 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(thePrs->object());
504 aResult = aFeature->firstResult();
506 aValid = aResult.get() != NULL;
509 // We can not select a result of our feature
510 std::list<ResultPtr> aResults;
511 ModelAPI_Tools::allResults(myFeature, aResults);
512 std::list<ResultPtr>::const_iterator aIt;
513 bool isSkipSelf = false;
514 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
515 if ((*aIt) == aResult) {
528 //********************************************************************
529 bool ModuleBase_WidgetMultiSelector::processDelete()
531 appendFirstSelectionInHistory();
533 // find attribute indices to delete
534 std::set<int> anAttributeIds;
535 getSelectedAttributeIndices(anAttributeIds);
537 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
539 // refill attribute by the items which indices are not in the list of ids
541 DataPtr aData = myFeature->data();
542 AttributePtr anAttribute = aData->attribute(attributeID());
543 std::string aType = anAttribute->attributeType();
544 aDone = !anAttributeIds.empty();
545 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
546 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
547 aSelectionListAttr->remove(anAttributeIds);
550 else if (aType == ModelAPI_AttributeRefList::typeId()) {
551 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
552 aRefListAttr->remove(anAttributeIds);
554 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
555 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
556 aRefAttrListAttr->remove(anAttributeIds);
560 // update object is necessary to flush update signal. It leads to objects references map update
561 // and the operation presentation will not contain deleted items visualized as parameters of
563 updateObject(myFeature);
566 myWorkshop->setSelected(getAttributeSelection());
568 // may be the feature's result is not displayed, but attributes should be
569 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments,
570 true); /// hope that something is redisplayed by object updated
574 myListView->restoreSelection(anIndices);
576 appendSelectionInHistory();
577 return true/*aDone*/; // following #2438 Delete should be processed even if nothing is delete
580 //********************************************************************
581 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
583 QList<QWidget*> result;
584 if (myTypeCtrl->isVisible())
585 result << myTypeCtrl;
586 result << myListView->getControl();
590 //********************************************************************
591 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
593 // Clear current selection in order to avoid updating of object browser with obsolete indexes
594 // which can appear because of results deletetion after changing a type of selection
595 QString aSelectionType = myTypeCtrl->textValue();
596 QList<ModuleBase_ViewerPrsPtr> aEmptyList;
597 myWorkshop->setSelected(aEmptyList);
599 updateSelectionModesAndFilters(true);
600 myWorkshop->selectionActivate()->updateSelectionModes();
604 /// store the selected type
605 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
606 std::string aType = anAttribute->attributeType();
607 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
608 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
609 aSelectionListAttr->setSelectionType(aSelectionType.toStdString());
612 // clear attribute values
613 DataPtr aData = myFeature->data();
614 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
615 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
616 aSelectionListAttr->clear();
618 else if (aType == ModelAPI_AttributeRefList::typeId()) {
619 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
620 aRefListAttr->clear();
622 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
623 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
624 aRefAttrListAttr->clear();
627 // update object is necessary to flush update signal. It leads to objects references map update
628 // and the operation presentation will not contain deleted items visualized as parameters of
630 updateObject(myFeature);
632 myWorkshop->setSelected(getAttributeSelection());
633 // may be the feature's result is not displayed, but attributes should be
634 // hope that something is redisplayed by object updated
635 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments, false);
636 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeResults, true);
637 // clear history should follow after set selected to do not increase history by setSelected
638 clearSelectedHistory();
640 if (myWorkshop->propertyPanel()->activeWidget() != this)
641 myWorkshop->propertyPanel()->activateWidget(this);
644 //********************************************************************
645 bool ModuleBase_WidgetMultiSelector::processSelection()
647 if (!myIsNeutralPointClear) {
648 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
649 // do not clear selected object
650 if (aSelected.size() == 0) {
651 if (!getAttributeSelection().empty()) {
652 // Restore selection in the viewer by the attribute selection list
653 // it should be postponed to exit from the selectionChanged processing
654 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
655 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
656 Events_Loop::loop()->flush(anEvent);
661 appendFirstSelectionInHistory();
662 bool aDone = ModuleBase_WidgetSelector::processSelection();
663 appendSelectionInHistory();
667 void ModuleBase_WidgetMultiSelector::appendFirstSelectionInHistory()
669 if (mySelectedHistoryValues.empty()) {
670 myCurrentHistoryIndex++;
671 mySelectedHistoryValues.append(getAttributeSelection());
673 #ifdef DEBUG_UNDO_REDO
674 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
679 void ModuleBase_WidgetMultiSelector::appendSelectionInHistory()
681 while (myCurrentHistoryIndex != mySelectedHistoryValues.count() - 1)
682 mySelectedHistoryValues.removeLast();
684 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
685 myCurrentHistoryIndex++;
686 mySelectedHistoryValues.append(aSelected);
687 myWorkshop->updateCommandStatus();
689 #ifdef DEBUG_UNDO_REDO
690 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
694 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
696 mySelectedHistoryValues.clear();
697 myCurrentHistoryIndex = -1;
698 myWorkshop->updateCommandStatus();
700 #ifdef DEBUG_UNDO_REDO
701 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
705 void ModuleBase_WidgetMultiSelector::updateFocus()
707 // Set focus to List control in order to make possible
708 // to use Tab key for transfer the focus to next widgets
709 ModuleBase_Tools::setFocus(myListView->getControl(),
710 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
713 //********************************************************************
714 void ModuleBase_WidgetMultiSelector::updateSelectionName()
718 //********************************************************************
719 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
721 if (myIsSetSelectionBlocked)
723 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
725 // according to #2154 we need to update OB selection when selection in the viewer happens
726 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
727 // because we need that Object Browser has been already updated when synchronize happens.
729 // Restore selection in the viewer by the attribute selection list
730 // it is possible that diring selection attribute filling, selection in Object Browser
731 // is changed(some items were removed/added) and as result, selection in the viewer
732 // differs from the selection come to this method. By next rows, we restore selection
733 // in the viewer according to content of selection attribute. Case is Edge selection in Group
734 myIsSetSelectionBlocked = true;
735 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
736 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
737 Events_Loop::loop()->flush(anEvent);
738 myIsSetSelectionBlocked = false;
741 //********************************************************************
742 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
744 QIntList aShapeTypes;
746 if (myShapeTypes.length() > 1 && myIsUseChoice) {
747 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCtrl->textValue()));
750 foreach (QString aType, myShapeTypes) {
751 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
757 //********************************************************************
758 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
760 QString aShapeTypeName;
763 foreach (QString aShapeTypeName, myShapeTypes) {
764 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
765 if(aRefType == theShapeType && idx != myTypeCtrl->value()) {
766 updateSelectionModesAndFilters(false);
767 bool isBlocked = myTypeCtrl->blockSignals(true);
768 myTypeCtrl->setValue(idx);
769 myTypeCtrl->blockSignals(isBlocked);
770 updateSelectionModesAndFilters(true);
777 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
779 QList<ModuleBase_ViewerPrsPtr> aSelected;
780 convertIndicesToViewerSelection(std::set<int>(), aSelected);
784 //********************************************************************
785 void ModuleBase_WidgetMultiSelector::updateSelectionList()
787 myListView->getControl()->clear();
789 DataPtr aData = myFeature->data();
790 AttributePtr anAttribute = aData->attribute(attributeID());
791 std::string aType = anAttribute->attributeType();
792 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
793 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
794 for (int i = 0; i < aSelectionListAttr->size(); i++) {
795 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
796 myListView->addItem(aAttr->namingName().c_str(), i);
799 else if (aType == ModelAPI_AttributeRefList::typeId()) {
800 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
801 for (int i = 0; i < aRefListAttr->size(); i++) {
802 ObjectPtr anObject = aRefListAttr->object(i);
803 if (anObject.get()) {
804 myListView->addItem(anObject->data()->name().c_str(), i);
808 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
809 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
810 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
811 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
813 if (anAttribute.get()) {
814 std::string anAttrName = ModuleBase_Tools::generateName(anAttribute, myWorkshop);
815 aName = QString::fromStdString(anAttrName);
818 ObjectPtr anObject = aRefAttrListAttr->object(i);
819 if (anObject.get()) {
820 aName = anObject->data()->name().c_str();
823 myListView->addItem(aName, i);
827 // We have to call repaint because sometimes the List control is not updated
828 myListView->getControl()->update();
831 //********************************************************************
832 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
836 if (theType == "Vertices")
838 else if (theType == "Edges")
840 else if (theType == "Faces")
842 else if (theType == "Solids")
848 //********************************************************************
849 void ModuleBase_WidgetMultiSelector::clearSelection()
851 bool isClearInNeutralPoint = myIsNeutralPointClear;
852 myIsNeutralPointClear = true;
854 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
855 // This method will call Selection changed event which will call onSelectionChanged
856 // To clear mySelection, myListView and storeValue()
857 // So, we don't need to call it
858 myWorkshop->setSelected(anEmptyList);
860 myIsNeutralPointClear = isClearInNeutralPoint;
863 //********************************************************************
864 void ModuleBase_WidgetMultiSelector::onDeleteItem()
869 //********************************************************************
870 void ModuleBase_WidgetMultiSelector::onListSelection()
872 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
876 //********************************************************************
877 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
879 myListView->getSelectedIndices(theAttributeIds);
882 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
883 QList<ModuleBase_ViewerPrsPtr>& theValues) const
885 if(myFeature.get() == NULL)
888 DataPtr aData = myFeature->data();
889 AttributePtr anAttribute = aData->attribute(attributeID());
890 std::string aType = anAttribute->attributeType();
891 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
892 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
893 for (int i = 0; i < aSelectionListAttr->size(); i++) {
894 // filter by attribute indices only if the container is not empty otherwise return all items
895 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
897 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
898 ObjectPtr anObject = anAttr->contextObject();
900 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
901 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
904 else if (aType == ModelAPI_AttributeRefList::typeId()) {
905 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
906 for (int i = 0; i < aRefListAttr->size(); i++) {
907 // filter by attribute indices only if the container is not empty otherwise return all items
908 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
910 ObjectPtr anObject = aRefListAttr->object(i);
911 if (anObject.get()) {
912 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
913 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
917 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
918 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
919 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
920 // filter by attribute indices only if the container is not empty otherwise return all items
921 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
923 ObjectPtr anObject = aRefAttrListAttr->object(i);
927 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
928 if (anAttribute.get()) {
929 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
930 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
931 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
937 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
938 (QList<ModuleBase_ViewerPrsPtr>& theValues)
942 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
943 DataPtr aData = myFeature->data();
944 AttributePtr anAttribute = aData->attribute(attributeID());
945 std::string aType = anAttribute->attributeType();
946 std::set<GeomShapePtr> aShapes;
947 std::set<int> anIndicesToBeRemoved;
949 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
950 // iteration through data model to find not selected elements to remove them
951 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
952 for (int i = 0; i < aSelectionListAttr->size(); i++) {
953 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
954 ObjectPtr aContextObject = anAttr->contextObject();
956 aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aContextObject);
958 aShape = anAttr->value();
960 bool aFound = findInSelection(aContextObject, aShape, aGeomSelection, myWorkshop);
962 anIndicesToBeRemoved.insert(i);
964 isDone = anIndicesToBeRemoved.size() > 0;
966 aSelectionListAttr->remove(anIndicesToBeRemoved);
968 else if (aType == ModelAPI_AttributeRefList::typeId()) {
969 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
970 for (int i = 0; i < aRefListAttr->size(); i++) {
971 ObjectPtr anObject = aRefListAttr->object(i);
972 if (anObject.get()) {
973 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
976 anIndicesToBeRemoved.insert(i);
979 isDone = anIndicesToBeRemoved.size() > 0;
980 aRefListAttr->remove(anIndicesToBeRemoved);
982 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
983 std::set<AttributePtr> anAttributes;
984 QList<ModuleBase_ViewerPrsPtr>::const_iterator
985 anIt = theValues.begin(), aLast = theValues.end();
988 for (; anIt != aLast; anIt++) {
989 ModuleBase_ViewerPrsPtr aPrs = *anIt;
990 getGeomSelection(aPrs, anObject, aShape);
991 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
992 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
993 anAttributes.insert(anAttr);
996 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
997 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
999 if (aRefAttrListAttr->isAttribute(i)) {
1000 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
1001 aFound = anAttributes.find(anAttribute) != anAttributes.end();
1004 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
1008 anIndicesToBeRemoved.insert(i);
1010 isDone = anIndicesToBeRemoved.size() > 0;
1011 aRefAttrListAttr->remove(anIndicesToBeRemoved);
1017 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
1018 (QList<ModuleBase_ViewerPrsPtr>& theValues)
1020 // convert prs list to objects map
1021 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
1022 std::set<GeomShapePtr> aShapes;
1023 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
1025 GeomShapePtr aShape;
1026 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
1027 for (; anIt != aLast; anIt++) {
1028 ModuleBase_ViewerPrsPtr aPrs = *anIt;
1029 getGeomSelection(aPrs, anObject, aShape);
1031 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
1032 aShapes = aGeomSelection[anObject];
1033 // we need to know if there was an empty shape in selection for the object
1035 aShape = anEmptyShape;
1036 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
1037 aShapes.insert(aShape);
1038 aGeomSelection[anObject] = aShapes;
1040 return aGeomSelection;
1043 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
1044 GeomShapePtr theShape,
1045 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
1046 ModuleBase_IWorkshop* theWorkshop)
1048 if (!theObject.get())
1050 // issue #2154: we should not remove from list objects hidden in the viewer if selection
1051 // was done with SHIFT button
1052 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
1055 bool aFound = false;
1056 GeomShapePtr aShape = theShape;
1057 if (!aShape.get()) {
1058 // #2429 (the preselection of a sketch is not taken into account)
1059 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1061 aShape = aResult->shape();
1063 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
1064 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
1065 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
1066 for (; anIt != aLast && !aFound; anIt++) {
1067 GeomShapePtr aCShape = *anIt;
1070 // treat shape equal to context as null: 2219, keep order of shapes in list
1071 if (aCShape->isNull()) { // in selection, shape of result is equal to selected shape
1072 // if so, here we need to check shape of result
1073 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1075 aCShape = aResult->shape();
1077 aFound = aCShape->isSame(aShape);
1082 // issue #2903: (Possibility to hide faces) - check whether given shape is a hidden sub-shape
1083 if (!aFound && theShape.get() && theWorkshop->hasSHIFTPressed() && theObject->isDisplayed()) {
1084 AISObjectPtr anAIS = theWorkshop->findPresentation(theObject);
1085 if (anAIS.get() != NULL) {
1086 Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1088 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(anAISIO);
1089 if (!aResultPrs.IsNull() && aResultPrs->isSubShapeHidden(theShape->impl<TopoDS_Shape>()))
1098 ModuleBase_WidgetMultiSelector::actionsList(ModuleBase_ActionType theActionType) const
1100 QList<ActionInfo> aList;
1101 if (myCurrentHistoryIndex > -1) {
1103 QString aTitle("Selection %1 items");
1104 QString aTit("Selection %1 item");
1105 QIcon aIcon(":pictures/selection.png");
1107 switch (theActionType) {
1110 while (i <= myCurrentHistoryIndex) {
1111 aNb = mySelectedHistoryValues.at(i).count();
1113 ActionInfo aInfo(aIcon, aTit.arg(aNb));
1114 aList.insert(0, aInfo);
1116 ActionInfo aInfo(aIcon, aTitle.arg(aNb));
1117 aList.insert(0, aInfo);
1123 i = mySelectedHistoryValues.length() - 1;
1124 while (i > myCurrentHistoryIndex) {
1125 aNb = mySelectedHistoryValues.at(i).count();
1127 ActionInfo aInfo(aIcon, aTit.arg(mySelectedHistoryValues.at(i).count()));
1128 aList.insert(0, aInfo);
1130 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));
1131 aList.insert(0, aInfo);
1142 void ModuleBase_WidgetMultiSelector::onFeatureAccepted()
1144 defaultValues[myFeatureId + attributeID()] = myDefMode;
1147 void ModuleBase_WidgetMultiSelector::onListActivated()
1150 emitFocusInWidget();
1153 void ModuleBase_WidgetMultiSelector::onSameTopology(bool theOn)
1155 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
1156 std::string aType = anAttribute->attributeType();
1157 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
1158 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
1159 aSelectionListAttr->setGeometricalSelection(theOn);
1160 updateObject(myFeature);
1164 void ModuleBase_WidgetMultiSelector::onShowOnly(bool theChecked)
1166 std::list<ResultPtr> aResults = myFeature->results();
1167 std::list<ResultPtr>::const_iterator aIt;
1169 myVisibleObjects = myWorkshop->displayedObjects();
1170 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1171 myVisibleObjects.removeAll(*aIt);
1173 myWorkshop->module()->disableCustomMode(ModuleBase_IModule::CustomizeArguments);
1176 myWorkshop->module()->enableCustomModes();
1178 foreach(ObjectPtr aObj, myVisibleObjects) {
1179 aObj->setDisplayed(!theChecked);
1183 // Hide and show the group result in order to make it above all objects
1184 bool aOldState = myWorkshop->enableUpdateViewer(false);
1185 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1186 (*aIt)->setDisplayed(false);
1188 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1189 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1190 (*aIt)->setDisplayed(true);
1192 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1193 myWorkshop->enableUpdateViewer(aOldState);
1195 myVisibleObjects.clear();
1197 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1198 myWorkshop->viewer()->update();
1201 bool ModuleBase_WidgetMultiSelector::isModified() const
1203 return myListView->getControl()->count() > 0;
1207 void ModuleBase_WidgetMultiSelector::setReadOnly(bool isReadOnly)
1209 ModuleBase_WidgetSelector::setReadOnly(isReadOnly);
1211 myShowOnlyBtn->hide();
1213 myFiltersWgt->hide();
1215 AttributeSelectionListPtr aAttrList = feature()->selectionList(attributeID());
1216 if (aAttrList.get()) {
1217 FiltersFeaturePtr aFilters = aAttrList->filters();
1218 if (aFilters.get()) {
1219 ModuleBase_WidgetSelectionFilter::SelectorFeature = feature();
1220 ModuleBase_WidgetSelectionFilter::AttributeId = attributeID();
1222 std::string aXmlCfg, aDescription;
1223 myWorkshop->module()->getXMLRepresentation(myUseFilters, aXmlCfg, aDescription);
1225 ModuleBase_WidgetSelectionFilter* aWgt =
1226 new ModuleBase_WidgetSelectionFilter(this, myWorkshop,
1227 new Config_WidgetAPI(aDescription), true);
1228 aWgt->setFeature(aFilters);
1229 aWgt->restoreValue();
1230 myMainLayout->addWidget(aWgt);