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 aSelectionListAttr->setSelectionType(myDefMode);
336 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
337 myDefMode = aSelectionType;
342 myGeomCheck->setChecked(aSelectionListAttr->isGeometricalSelection());
343 updateSelectionList();
347 //********************************************************************
348 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
349 const bool theToValidate)
351 if (myIsSetSelectionBlocked)
354 AttributeSelectionListPtr aSelectionListAttr;
355 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
356 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
357 if (aSelectionListAttr.get())
358 aSelectionListAttr->cashValues(true);
360 /// remove unused objects from the model attribute.
361 /// It should be performed before new attributes append.
362 bool isDone = removeUnusedAttributeObjects(theValues);
364 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
365 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
366 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
367 for (; anIt != aLast; anIt++) {
368 ModuleBase_ViewerPrsPtr aValue = *anIt;
369 // do not validate and append to attribute selection presentation if it exists in the attribute
372 getGeomSelection(aValue, anObject, aShape);
373 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
374 anAttributeValues.append(aValue);
377 if (theToValidate && !isValidInFilters(aValue))
378 anInvalidValues.append(aValue);
380 bool aHasInvalidValues = anInvalidValues.size() > 0;
382 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
383 ModuleBase_ViewerPrsPtr aValue = *anIt;
384 bool aProcessed = false;
385 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
386 anAttributeValues.contains(aValue))
388 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
389 // if there is at least one set, the result is true
390 isDone = isDone || aProcessed;
392 // Check the selection with validators
393 QString aError = getError();
394 if (aError.length() > 0) {
395 aSelectionListAttr->clear();
398 // updateObject - to update/redisplay feature
399 // it is commented in order to perfom it outside the method
401 //updateObject(myFeature);
402 // this emit is necessary to call store/restore method an restore type of selection
403 //emit valuesChanged();
406 if (aSelectionListAttr.get())
407 aSelectionListAttr->cashValues(false);
410 if (!anInvalidValues.empty())
411 theValues.append(anInvalidValues);
413 if (isDone) // may be the feature's result is not displayed, but attributes should be
414 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments,
415 true);/// hope that something is redisplayed by object updated
420 //********************************************************************
421 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
423 std::set<int> anAttributeIds;
424 getSelectedAttributeIndices(anAttributeIds);
425 if (!anAttributeIds.empty())
426 convertIndicesToViewerSelection(anAttributeIds, theValues);
429 //********************************************************************
430 bool ModuleBase_WidgetMultiSelector::canProcessAction(ModuleBase_ActionType theActionType,
431 bool& isActionEnabled)
433 isActionEnabled = false;
434 bool aCanProcess = false;
435 switch (theActionType) {
439 isActionEnabled = theActionType == ActionUndo ? myCurrentHistoryIndex > 0
440 : (mySelectedHistoryValues.size() > 0 &&
441 myCurrentHistoryIndex < mySelectedHistoryValues.size() - 1);
445 aCanProcess = ModuleBase_WidgetSelector::canProcessAction(theActionType, isActionEnabled);
451 //********************************************************************
452 bool ModuleBase_WidgetMultiSelector::processAction(ModuleBase_ActionType theActionType,
453 const ActionParamPtr& theParam)
455 switch (theActionType) {
458 ActionIntParamPtr aParam =
459 std::dynamic_pointer_cast<ModuleBase_ActionIntParameter>(theParam);
460 int aNb = aParam->value();
461 if (theActionType == ActionUndo)
462 myCurrentHistoryIndex -= aNb;
464 myCurrentHistoryIndex += aNb;
465 QList<ModuleBase_ViewerPrsPtr> aSelected = mySelectedHistoryValues[myCurrentHistoryIndex];
466 // equal vertices should not be used here
467 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
468 bool isDone = setSelection(aSelected,
469 false /*need not validate because values already was in list*/);
470 updateOnSelectionChanged(isDone);
472 myWorkshop->updateCommandStatus();
473 #ifdef DEBUG_UNDO_REDO
474 printHistoryInfo(QString("processAction %1").arg(theActionType == ActionUndo ? "Undo"
475 : "Redo"), myCurrentHistoryIndex, mySelectedHistoryValues);
480 return ModuleBase_ModelWidget::processAction(theActionType, theParam);
484 //********************************************************************
485 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
487 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
489 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
490 if (!aResult.get()) { // In case if a feature was selected
491 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(thePrs->object());
493 aResult = aFeature->firstResult();
495 aValid = aResult.get() != NULL;
498 // We can not select a result of our feature
499 std::list<ResultPtr> aResults;
500 ModelAPI_Tools::allResults(myFeature, aResults);
501 std::list<ResultPtr>::const_iterator aIt;
502 bool isSkipSelf = false;
503 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
504 if ((*aIt) == aResult) {
517 //********************************************************************
518 bool ModuleBase_WidgetMultiSelector::processDelete()
520 appendFirstSelectionInHistory();
522 // find attribute indices to delete
523 std::set<int> anAttributeIds;
524 getSelectedAttributeIndices(anAttributeIds);
526 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
528 // refill attribute by the items which indices are not in the list of ids
530 DataPtr aData = myFeature->data();
531 AttributePtr anAttribute = aData->attribute(attributeID());
532 std::string aType = anAttribute->attributeType();
533 aDone = !anAttributeIds.empty();
534 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
535 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
536 aSelectionListAttr->remove(anAttributeIds);
539 else if (aType == ModelAPI_AttributeRefList::typeId()) {
540 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
541 aRefListAttr->remove(anAttributeIds);
543 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
544 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
545 aRefAttrListAttr->remove(anAttributeIds);
549 // update object is necessary to flush update signal. It leads to objects references map update
550 // and the operation presentation will not contain deleted items visualized as parameters of
552 updateObject(myFeature);
555 myWorkshop->setSelected(getAttributeSelection());
557 // may be the feature's result is not displayed, but attributes should be
558 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments,
559 true); /// hope that something is redisplayed by object updated
563 myListView->restoreSelection(anIndices);
565 appendSelectionInHistory();
566 return true/*aDone*/; // following #2438 Delete should be processed even if nothing is delete
569 //********************************************************************
570 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
572 QList<QWidget*> result;
573 result << myListView->getControl();
577 //********************************************************************
578 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
580 // Clear current selection in order to avoid updating of object browser with obsolete indexes
581 // which can appear because of results deletetion after changing a type of selection
582 QString aSelectionType = myTypeCtrl->textValue();
583 QList<ModuleBase_ViewerPrsPtr> aEmptyList;
584 myWorkshop->setSelected(aEmptyList);
586 updateSelectionModesAndFilters(true);
587 myWorkshop->selectionActivate()->updateSelectionModes();
591 /// store the selected type
592 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
593 std::string aType = anAttribute->attributeType();
594 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
595 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
596 aSelectionListAttr->setSelectionType(aSelectionType.toStdString());
599 // clear attribute values
600 DataPtr aData = myFeature->data();
601 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
602 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
603 aSelectionListAttr->clear();
605 else if (aType == ModelAPI_AttributeRefList::typeId()) {
606 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
607 aRefListAttr->clear();
609 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
610 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
611 aRefAttrListAttr->clear();
614 // update object is necessary to flush update signal. It leads to objects references map update
615 // and the operation presentation will not contain deleted items visualized as parameters of
617 updateObject(myFeature);
619 myWorkshop->setSelected(getAttributeSelection());
620 // may be the feature's result is not displayed, but attributes should be
621 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments,
622 true); /// hope that something is redisplayed by object updated
623 // clear history should follow after set selected to do not increase history by setSelected
624 clearSelectedHistory();
626 if (myWorkshop->propertyPanel()->activeWidget() != this)
627 myWorkshop->propertyPanel()->activateWidget(this);
630 //********************************************************************
631 bool ModuleBase_WidgetMultiSelector::processSelection()
633 if (!myIsNeutralPointClear) {
634 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
635 // do not clear selected object
636 if (aSelected.size() == 0) {
637 if (!getAttributeSelection().empty()) {
638 // Restore selection in the viewer by the attribute selection list
639 // it should be postponed to exit from the selectionChanged processing
640 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
641 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
642 Events_Loop::loop()->flush(anEvent);
647 appendFirstSelectionInHistory();
648 bool aDone = ModuleBase_WidgetSelector::processSelection();
649 appendSelectionInHistory();
653 void ModuleBase_WidgetMultiSelector::appendFirstSelectionInHistory()
655 if (mySelectedHistoryValues.empty()) {
656 myCurrentHistoryIndex++;
657 mySelectedHistoryValues.append(getAttributeSelection());
659 #ifdef DEBUG_UNDO_REDO
660 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
665 void ModuleBase_WidgetMultiSelector::appendSelectionInHistory()
667 while (myCurrentHistoryIndex != mySelectedHistoryValues.count() - 1)
668 mySelectedHistoryValues.removeLast();
670 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
671 myCurrentHistoryIndex++;
672 mySelectedHistoryValues.append(aSelected);
673 myWorkshop->updateCommandStatus();
675 #ifdef DEBUG_UNDO_REDO
676 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
680 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
682 mySelectedHistoryValues.clear();
683 myCurrentHistoryIndex = -1;
684 myWorkshop->updateCommandStatus();
686 #ifdef DEBUG_UNDO_REDO
687 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
691 void ModuleBase_WidgetMultiSelector::updateFocus()
693 // Set focus to List control in order to make possible
694 // to use Tab key for transfer the focus to next widgets
695 ModuleBase_Tools::setFocus(myListView->getControl(),
696 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
699 //********************************************************************
700 void ModuleBase_WidgetMultiSelector::updateSelectionName()
704 //********************************************************************
705 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
707 if (myIsSetSelectionBlocked)
709 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
711 // according to #2154 we need to update OB selection when selection in the viewer happens
712 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
713 // because we need that Object Browser has been already updated when synchronize happens.
715 // Restore selection in the viewer by the attribute selection list
716 // it is possible that diring selection attribute filling, selection in Object Browser
717 // is changed(some items were removed/added) and as result, selection in the viewer
718 // differs from the selection come to this method. By next rows, we restore selection
719 // in the viewer according to content of selection attribute. Case is Edge selection in Group
720 myIsSetSelectionBlocked = true;
721 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
722 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
723 Events_Loop::loop()->flush(anEvent);
724 myIsSetSelectionBlocked = false;
727 //********************************************************************
728 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
730 QIntList aShapeTypes;
732 if (myShapeTypes.length() > 1 && myIsUseChoice) {
733 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCtrl->textValue()));
736 foreach (QString aType, myShapeTypes) {
737 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
743 //********************************************************************
744 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
746 QString aShapeTypeName;
749 foreach (QString aShapeTypeName, myShapeTypes) {
750 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
751 if(aRefType == theShapeType && idx != myTypeCtrl->value()) {
752 updateSelectionModesAndFilters(false);
753 bool isBlocked = myTypeCtrl->blockSignals(true);
754 myTypeCtrl->setValue(idx);
755 myTypeCtrl->blockSignals(isBlocked);
756 updateSelectionModesAndFilters(true);
763 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
765 QList<ModuleBase_ViewerPrsPtr> aSelected;
766 convertIndicesToViewerSelection(std::set<int>(), aSelected);
770 //********************************************************************
771 void ModuleBase_WidgetMultiSelector::updateSelectionList()
773 myListView->getControl()->clear();
775 DataPtr aData = myFeature->data();
776 AttributePtr anAttribute = aData->attribute(attributeID());
777 std::string aType = anAttribute->attributeType();
778 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
779 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
780 for (int i = 0; i < aSelectionListAttr->size(); i++) {
781 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
782 myListView->addItem(aAttr->namingName().c_str(), i);
785 else if (aType == ModelAPI_AttributeRefList::typeId()) {
786 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
787 for (int i = 0; i < aRefListAttr->size(); i++) {
788 ObjectPtr anObject = aRefListAttr->object(i);
789 if (anObject.get()) {
790 myListView->addItem(anObject->data()->name().c_str(), i);
794 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
795 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
796 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
797 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
799 if (anAttribute.get()) {
800 std::string anAttrName = ModuleBase_Tools::generateName(anAttribute, myWorkshop);
801 aName = QString::fromStdString(anAttrName);
804 ObjectPtr anObject = aRefAttrListAttr->object(i);
805 if (anObject.get()) {
806 aName = anObject->data()->name().c_str();
809 myListView->addItem(aName, i);
813 // We have to call repaint because sometimes the List control is not updated
814 myListView->getControl()->update();
817 //********************************************************************
818 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
822 if (theType == "Vertices")
824 else if (theType == "Edges")
826 else if (theType == "Faces")
828 else if (theType == "Solids")
834 //********************************************************************
835 void ModuleBase_WidgetMultiSelector::clearSelection()
837 bool isClearInNeutralPoint = myIsNeutralPointClear;
838 myIsNeutralPointClear = true;
840 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
841 // This method will call Selection changed event which will call onSelectionChanged
842 // To clear mySelection, myListView and storeValue()
843 // So, we don't need to call it
844 myWorkshop->setSelected(anEmptyList);
846 myIsNeutralPointClear = isClearInNeutralPoint;
849 //********************************************************************
850 void ModuleBase_WidgetMultiSelector::onDeleteItem()
855 //********************************************************************
856 void ModuleBase_WidgetMultiSelector::onListSelection()
858 myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
862 //********************************************************************
863 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
865 myListView->getSelectedIndices(theAttributeIds);
868 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
869 QList<ModuleBase_ViewerPrsPtr>& theValues) const
871 if(myFeature.get() == NULL)
874 DataPtr aData = myFeature->data();
875 AttributePtr anAttribute = aData->attribute(attributeID());
876 std::string aType = anAttribute->attributeType();
877 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
878 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
879 for (int i = 0; i < aSelectionListAttr->size(); i++) {
880 // filter by attribute indices only if the container is not empty otherwise return all items
881 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
883 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
884 ObjectPtr anObject = anAttr->contextObject();
886 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
887 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
890 else if (aType == ModelAPI_AttributeRefList::typeId()) {
891 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
892 for (int i = 0; i < aRefListAttr->size(); i++) {
893 // filter by attribute indices only if the container is not empty otherwise return all items
894 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
896 ObjectPtr anObject = aRefListAttr->object(i);
897 if (anObject.get()) {
898 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
899 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
903 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
904 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
905 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
906 // filter by attribute indices only if the container is not empty otherwise return all items
907 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
909 ObjectPtr anObject = aRefAttrListAttr->object(i);
913 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
914 if (anAttribute.get()) {
915 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
916 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
917 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
923 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
924 (QList<ModuleBase_ViewerPrsPtr>& theValues)
928 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
929 DataPtr aData = myFeature->data();
930 AttributePtr anAttribute = aData->attribute(attributeID());
931 std::string aType = anAttribute->attributeType();
932 std::set<GeomShapePtr> aShapes;
933 std::set<int> anIndicesToBeRemoved;
935 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
936 // iteration through data model to find not selected elements to remove them
937 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
938 for (int i = 0; i < aSelectionListAttr->size(); i++) {
939 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
940 //aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->contextObject());
941 //if (!aFeature.get()) { // Feature can not be found as geometry selection
942 bool aFound = findInSelection(
943 anAttr->contextObject(), anAttr->value(), aGeomSelection, myWorkshop);
945 anIndicesToBeRemoved.insert(i);
948 isDone = anIndicesToBeRemoved.size() > 0;
950 aSelectionListAttr->remove(anIndicesToBeRemoved);
952 else if (aType == ModelAPI_AttributeRefList::typeId()) {
953 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
954 for (int i = 0; i < aRefListAttr->size(); i++) {
955 ObjectPtr anObject = aRefListAttr->object(i);
956 if (anObject.get()) {
957 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
960 anIndicesToBeRemoved.insert(i);
963 isDone = anIndicesToBeRemoved.size() > 0;
964 aRefListAttr->remove(anIndicesToBeRemoved);
966 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
967 std::set<AttributePtr> anAttributes;
968 QList<ModuleBase_ViewerPrsPtr>::const_iterator
969 anIt = theValues.begin(), aLast = theValues.end();
972 for (; anIt != aLast; anIt++) {
973 ModuleBase_ViewerPrsPtr aPrs = *anIt;
974 getGeomSelection(aPrs, anObject, aShape);
975 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
976 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
977 anAttributes.insert(anAttr);
980 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
981 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
983 if (aRefAttrListAttr->isAttribute(i)) {
984 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
985 aFound = anAttributes.find(anAttribute) != anAttributes.end();
988 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
992 anIndicesToBeRemoved.insert(i);
994 isDone = anIndicesToBeRemoved.size() > 0;
995 aRefAttrListAttr->remove(anIndicesToBeRemoved);
1001 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
1002 (QList<ModuleBase_ViewerPrsPtr>& theValues)
1004 // convert prs list to objects map
1005 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
1006 std::set<GeomShapePtr> aShapes;
1007 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
1009 GeomShapePtr aShape;
1010 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
1011 for (; anIt != aLast; anIt++) {
1012 ModuleBase_ViewerPrsPtr aPrs = *anIt;
1013 getGeomSelection(aPrs, anObject, aShape);
1015 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
1016 aShapes = aGeomSelection[anObject];
1017 // we need to know if there was an empty shape in selection for the object
1019 aShape = anEmptyShape;
1020 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
1021 aShapes.insert(aShape);
1022 aGeomSelection[anObject] = aShapes;
1024 return aGeomSelection;
1027 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
1028 GeomShapePtr theShape,
1029 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
1030 ModuleBase_IWorkshop* theWorkshop)
1032 if (!theObject.get())
1034 // issue #2154: we should not remove from list objects hidden in the viewer if selection
1035 // was done with SHIFT button
1036 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
1039 bool aFound = false;
1040 GeomShapePtr aShape = theShape;
1041 if (!aShape.get()) {
1042 // #2429 (the preselection of a sketch is not taken into account)
1043 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1045 aShape = aResult->shape();
1047 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
1048 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
1049 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
1050 for (; anIt != aLast && !aFound; anIt++) {
1051 GeomShapePtr aCShape = *anIt;
1054 // treat shape equal to context as null: 2219, keep order of shapes in list
1055 if (aCShape->isNull()) { // in selection, shape of result is equal to selected shape
1056 // if so, here we need to check shape of result
1057 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
1059 aCShape = aResult->shape();
1061 aFound = aCShape->isSame(aShape);
1066 // issue #2903: (Possibility to hide faces) - check whether given shape is a hidden sub-shape
1067 if (!aFound && theShape.get() && theWorkshop->hasSHIFTPressed() && theObject->isDisplayed()) {
1068 AISObjectPtr anAIS = theWorkshop->findPresentation(theObject);
1069 if (anAIS.get() != NULL) {
1070 Handle(AIS_InteractiveObject) anAISIO = anAIS->impl<Handle(AIS_InteractiveObject)>();
1072 Handle(ModuleBase_ResultPrs) aResultPrs = Handle(ModuleBase_ResultPrs)::DownCast(anAISIO);
1073 if (!aResultPrs.IsNull() && aResultPrs->isSubShapeHidden(theShape->impl<TopoDS_Shape>()))
1082 ModuleBase_WidgetMultiSelector::actionsList(ModuleBase_ActionType theActionType) const
1084 QList<ActionInfo> aList;
1085 if (myCurrentHistoryIndex > -1) {
1087 QString aTitle("Selection %1 items");
1088 QString aTit("Selection %1 item");
1089 QIcon aIcon(":pictures/selection.png");
1091 switch (theActionType) {
1094 while (i <= myCurrentHistoryIndex) {
1095 aNb = mySelectedHistoryValues.at(i).count();
1097 ActionInfo aInfo(aIcon, aTit.arg(aNb));
1098 aList.insert(0, aInfo);
1100 ActionInfo aInfo(aIcon, aTitle.arg(aNb));
1101 aList.insert(0, aInfo);
1107 i = mySelectedHistoryValues.length() - 1;
1108 while (i > myCurrentHistoryIndex) {
1109 aNb = mySelectedHistoryValues.at(i).count();
1111 ActionInfo aInfo(aIcon, aTit.arg(mySelectedHistoryValues.at(i).count()));
1112 aList.insert(0, aInfo);
1114 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));
1115 aList.insert(0, aInfo);
1126 void ModuleBase_WidgetMultiSelector::onFeatureAccepted()
1128 defaultValues[myFeatureId + attributeID()] = myDefMode;
1131 void ModuleBase_WidgetMultiSelector::onListActivated()
1134 emitFocusInWidget();
1137 void ModuleBase_WidgetMultiSelector::onSameTopology(bool theOn)
1139 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
1140 std::string aType = anAttribute->attributeType();
1141 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
1142 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
1143 aSelectionListAttr->setGeometricalSelection(theOn);
1144 updateObject(myFeature);
1148 void ModuleBase_WidgetMultiSelector::onShowOnly(bool theChecked)
1150 std::list<ResultPtr> aResults = myFeature->results();
1151 std::list<ResultPtr>::const_iterator aIt;
1153 myVisibleObjects = myWorkshop->displayedObjects();
1154 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1155 myVisibleObjects.removeAll(*aIt);
1157 myWorkshop->module()->disableCustomMode(ModuleBase_IModule::CustomizeArguments);
1160 myWorkshop->module()->enableCustomModes();
1162 foreach(ObjectPtr aObj, myVisibleObjects) {
1163 aObj->setDisplayed(!theChecked);
1167 // Hide and show the group result in order to make it above all objects
1168 bool aOldState = myWorkshop->enableUpdateViewer(false);
1169 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1170 (*aIt)->setDisplayed(false);
1172 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1173 for (aIt = aResults.cbegin(); aIt != aResults.cend(); aIt++) {
1174 (*aIt)->setDisplayed(true);
1176 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1177 myWorkshop->enableUpdateViewer(aOldState);
1179 myVisibleObjects.clear();
1181 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1184 bool ModuleBase_WidgetMultiSelector::isModified() const
1186 return myListView->getControl()->count() > 0;