1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <ModuleBase_WidgetMultiSelector.h>
23 #include <ModuleBase_ActionIntParameter.h>
24 #include <ModuleBase_Definitions.h>
25 #include <ModuleBase_Events.h>
26 #include <ModuleBase_IconFactory.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_ISelection.h>
29 #include <ModuleBase_ISelectionActivate.h>
30 #include <ModuleBase_IPropertyPanel.h>
31 #include <ModuleBase_IViewer.h>
32 #include <ModuleBase_IWorkshop.h>
33 #include <ModuleBase_ListView.h>
34 #include <ModuleBase_Tools.h>
35 #include <ModuleBase_ViewerPrs.h>
36 #include <ModuleBase_WidgetShapeSelector.h>
37 #include <ModuleBase_ChoiceCtrl.h>
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_Object.h>
41 #include <ModelAPI_AttributeSelectionList.h>
42 #include <ModelAPI_AttributeRefList.h>
43 #include <ModelAPI_AttributeRefAttrList.h>
44 #include <ModelAPI_Tools.h>
45 #include <ModelAPI_Events.h>
47 #include <Config_WidgetAPI.h>
49 #include <QGridLayout>
51 #include <QListWidget>
56 #include <QApplication>
59 #include <QMainWindow>
64 //#define DEBUG_UNDO_REDO
66 #ifdef DEBUG_UNDO_REDO
67 void printHistoryInfo(const QString& theMethodName, int theCurrentHistoryIndex,
68 QList<QList<std::shared_ptr<ModuleBase_ViewerPrs> > > theSelectedHistoryValues)
71 for (int i = 0; i < theSelectedHistoryValues.size(); i++)
72 aSizes.append(QString::number(theSelectedHistoryValues[i].size()));
74 std::cout << theMethodName.toStdString()
75 << " current = " << theCurrentHistoryIndex
76 << " size(history) = " << theSelectedHistoryValues.size()
77 << " (" << aSizes.join(", ").toStdString() << ")"
83 QStringList getIconsList(const QStringList& theNames)
86 foreach (QString aName, theNames) {
87 QString aUName = aName.toUpper();
88 if ((aUName == "VERTICES") || (aUName == "VERTEX"))
89 aIcons << ":pictures/vertex32.png";
90 else if ((aUName == "EDGES") || (aUName == "EDGE"))
91 aIcons << ":pictures/edge32.png";
92 else if ((aUName == "FACES") || (aUName == "FACE"))
93 aIcons << ":pictures/face32.png";
94 else if ((aUName == "SOLIDS") || (aUName == "SOLID"))
95 aIcons << ":pictures/solid32.png";
102 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
103 ModuleBase_IWorkshop* theWorkshop,
104 const Config_WidgetAPI* theData)
105 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
106 myIsSetSelectionBlocked(false), myCurrentHistoryIndex(-1)
108 std::string aPropertyTypes = theData->getProperty("type_choice");
109 QString aTypesStr = aPropertyTypes.c_str();
110 myShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
111 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
113 QGridLayout* aMainLay = new QGridLayout(this);
114 ModuleBase_Tools::adjustMargins(aMainLay);
116 QStringList aIconsList = getIconsList(myShapeTypes);
117 myTypeCtrl = new ModuleBase_ChoiceCtrl(this, myShapeTypes, aIconsList);
118 myTypeCtrl->setLabel(tr("Type"));
119 myTypeCtrl->setValue(0);
120 aMainLay->addWidget(myTypeCtrl, 0, 0, 1, 2);
122 // There is no sense to parameterize list of types while we can not parameterize selection mode
123 // if the xml definition contains one type, the controls to select a type should not be shown
124 if (myShapeTypes.size() <= 1 || !myIsUseChoice) {
125 myTypeCtrl->setVisible(false);
128 QString aLabelText = translate(theData->getProperty("label"));
129 QLabel* aListLabel = new QLabel(aLabelText, this);
130 aMainLay->addWidget(aListLabel, 1, 0);
131 // if the xml definition contains one type, an information label
132 // should be shown near to the latest
133 if (myShapeTypes.size() <= 1) {
134 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
135 if (!aLabelIcon.isEmpty()) {
136 QLabel* aSelectedLabel = new QLabel("", this);
137 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
138 aMainLay->addWidget(aSelectedLabel, 1, 1);
140 aMainLay->setColumnStretch(2, 1);
143 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
144 QString anObjName = QString::fromStdString(attributeID());
145 myListView = new ModuleBase_ListView(this, anObjName, aToolTip);
146 connect(myListView->getControl(), SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
147 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
149 aMainLay->addWidget(myListView->getControl(), 2, 0, 1, -1);
150 aMainLay->setRowStretch(2, 1);
151 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
152 //aMainLay->setRowMinimumHeight(3, 20);
153 //this->setLayout(aMainLay);
154 connect(myTypeCtrl, SIGNAL(valueChanged(int)), this, SLOT(onSelectionTypeChanged()));
156 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
159 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
163 //********************************************************************
164 void ModuleBase_WidgetMultiSelector::activateCustom()
166 ModuleBase_WidgetSelector::activateCustom();
168 myWorkshop->module()->activateCustomPrs(myFeature,
169 ModuleBase_IModule::CustomizeHighlightedObjects, true);
170 clearSelectedHistory();
173 //********************************************************************
174 void ModuleBase_WidgetMultiSelector::deactivate()
176 ModuleBase_WidgetSelector::deactivate();
178 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
179 clearSelectedHistory();
182 //********************************************************************
183 void ModuleBase_WidgetMultiSelector::updateAfterDeactivation()
185 // restore previous Undo/Redo workshop state
186 myWorkshop->updateCommandStatus();
189 //********************************************************************
190 void ModuleBase_WidgetMultiSelector::updateAfterActivation()
192 // fill Undo/Redo actions with current information
193 myWorkshop->updateCommandStatus();
196 //********************************************************************
197 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
199 // the value is stored on the selection changed signal processing
200 // A rare case when plugin was not loaded.
204 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
205 std::string aType = anAttribute->attributeType();
206 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
207 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
208 aSelectionListAttr->setSelectionType(myTypeCtrl->textValue().toStdString());
213 //********************************************************************
214 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
216 // A rare case when plugin was not loaded.
220 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
221 std::string aType = anAttribute->attributeType();
222 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
223 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
224 // Restore shape type
225 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
226 if (!aSelectionType.empty())
227 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
229 updateSelectionList();
233 //********************************************************************
234 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
235 const bool theToValidate)
237 if (myIsSetSelectionBlocked)
240 AttributeSelectionListPtr aSelectionListAttr;
241 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
242 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
243 if (aSelectionListAttr.get())
244 aSelectionListAttr->cashValues(true);
246 /// remove unused objects from the model attribute.
247 /// It should be performed before new attributes append.
248 bool isDone = removeUnusedAttributeObjects(theValues);
250 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
251 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
252 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
253 for (; anIt != aLast; anIt++) {
254 ModuleBase_ViewerPrsPtr aValue = *anIt;
255 // do not validate and append to attribute selection presentation if it exists in the attribute
258 getGeomSelection(aValue, anObject, aShape);
259 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
260 anAttributeValues.append(aValue);
263 if (theToValidate && !isValidInFilters(aValue))
264 anInvalidValues.append(aValue);
266 bool aHasInvalidValues = anInvalidValues.size() > 0;
268 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
269 ModuleBase_ViewerPrsPtr aValue = *anIt;
270 bool aProcessed = false;
271 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
272 anAttributeValues.contains(aValue))
274 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
275 // if there is at least one set, the result is true
276 isDone = isDone || aProcessed;
278 // updateObject - to update/redisplay feature
279 // it is commented in order to perfom it outside the method
281 //updateObject(myFeature);
282 // this emit is necessary to call store/restore method an restore type of selection
283 //emit valuesChanged();
286 if (aSelectionListAttr.get())
287 aSelectionListAttr->cashValues(false);
290 if (!anInvalidValues.empty())
291 theValues.append(anInvalidValues);
293 if (isDone) // may be the feature's result is not displayed, but attributes should be
294 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
295 true);/// hope that something is redisplayed by object updated
300 //********************************************************************
301 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
303 std::set<int> anAttributeIds;
304 getSelectedAttributeIndices(anAttributeIds);
305 if (!anAttributeIds.empty())
306 convertIndicesToViewerSelection(anAttributeIds, theValues);
309 //********************************************************************
310 bool ModuleBase_WidgetMultiSelector::canProcessAction(ModuleBase_ActionType theActionType,
311 bool& isActionEnabled)
313 isActionEnabled = false;
314 bool aCanProcess = false;
315 switch (theActionType) {
319 isActionEnabled = theActionType == ActionUndo ? myCurrentHistoryIndex > 0
320 : (mySelectedHistoryValues.size() > 0 &&
321 myCurrentHistoryIndex < mySelectedHistoryValues.size() - 1);
325 aCanProcess = ModuleBase_WidgetSelector::canProcessAction(theActionType, isActionEnabled);
331 //********************************************************************
332 bool ModuleBase_WidgetMultiSelector::processAction(ModuleBase_ActionType theActionType,
333 const ActionParamPtr& theParam)
335 switch (theActionType) {
338 ActionIntParamPtr aParam =
339 std::dynamic_pointer_cast<ModuleBase_ActionIntParameter>(theParam);
340 int aNb = aParam->value();
341 if (theActionType == ActionUndo)
342 myCurrentHistoryIndex -= aNb;
344 myCurrentHistoryIndex += aNb;
345 QList<ModuleBase_ViewerPrsPtr> aSelected = mySelectedHistoryValues[myCurrentHistoryIndex];
346 // equal vertices should not be used here
347 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
348 bool isDone = setSelection(aSelected,
349 false /*need not validate because values already was in list*/);
350 updateOnSelectionChanged(isDone);
352 myWorkshop->updateCommandStatus();
353 #ifdef DEBUG_UNDO_REDO
354 printHistoryInfo(QString("processAction %1").arg(theActionType == ActionUndo ? "Undo"
355 : "Redo"), myCurrentHistoryIndex, mySelectedHistoryValues);
360 return ModuleBase_ModelWidget::processAction(theActionType, theParam);
364 //********************************************************************
365 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
367 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
369 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
370 aValid = aResult.get() != NULL;
373 // We can not select a result of our feature
374 std::list<ResultPtr> aResults;
375 ModelAPI_Tools::allResults(myFeature, aResults);
376 std::list<ResultPtr>::const_iterator aIt;
377 bool isSkipSelf = false;
378 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
379 if ((*aIt) == aResult) {
392 //********************************************************************
393 bool ModuleBase_WidgetMultiSelector::processDelete()
395 appendFirstSelectionInHistory();
397 // find attribute indices to delete
398 std::set<int> anAttributeIds;
399 getSelectedAttributeIndices(anAttributeIds);
401 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
403 // refill attribute by the items which indices are not in the list of ids
405 DataPtr aData = myFeature->data();
406 AttributePtr anAttribute = aData->attribute(attributeID());
407 std::string aType = anAttribute->attributeType();
408 aDone = !anAttributeIds.empty();
409 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
410 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
411 aSelectionListAttr->remove(anAttributeIds);
414 else if (aType == ModelAPI_AttributeRefList::typeId()) {
415 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
416 aRefListAttr->remove(anAttributeIds);
418 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
419 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
420 aRefAttrListAttr->remove(anAttributeIds);
424 // update object is necessary to flush update signal. It leads to objects references map update
425 // and the operation presentation will not contain deleted items visualized as parameters of
427 updateObject(myFeature);
430 myWorkshop->setSelected(getAttributeSelection());
432 // may be the feature's result is not displayed, but attributes should be
433 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
434 true); /// hope that something is redisplayed by object updated
438 myListView->restoreSelection(anIndices);
440 appendSelectionInHistory();
444 //********************************************************************
445 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
447 QList<QWidget*> result;
448 result << myListView->getControl();
452 //********************************************************************
453 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
455 // Clear current selection in order to avoid updating of object browser with obsolete indexes
456 // which can appear because of results deletetion after changing a type of selection
457 QList<ModuleBase_ViewerPrsPtr> aEmptyList;
458 myWorkshop->setSelected(aEmptyList);
460 updateSelectionModesAndFilters(true);
461 myWorkshop->selectionActivate()->updateSelectionModes();
465 /// store the selected type
466 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
467 std::string aType = anAttribute->attributeType();
468 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
469 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
470 aSelectionListAttr->setSelectionType(myTypeCtrl->textValue().toStdString());
473 // clear attribute values
474 DataPtr aData = myFeature->data();
475 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
476 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
477 aSelectionListAttr->clear();
479 else if (aType == ModelAPI_AttributeRefList::typeId()) {
480 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
481 aRefListAttr->clear();
483 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
484 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
485 aRefAttrListAttr->clear();
488 // update object is necessary to flush update signal. It leads to objects references map update
489 // and the operation presentation will not contain deleted items visualized as parameters of
491 updateObject(myFeature);
493 myWorkshop->setSelected(getAttributeSelection());
494 // may be the feature's result is not displayed, but attributes should be
495 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
496 true); /// hope that something is redisplayed by object updated
497 // clear history should follow after set selected to do not increase history by setSelected
498 clearSelectedHistory();
500 if (myWorkshop->propertyPanel()->activeWidget() != this)
501 myWorkshop->propertyPanel()->activateWidget(this);
504 //********************************************************************
505 bool ModuleBase_WidgetMultiSelector::processSelection()
507 if (!myIsNeutralPointClear) {
508 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
509 // do not clear selected object
510 if (aSelected.size() == 0) {
511 if (!getAttributeSelection().empty()) {
512 // Restore selection in the viewer by the attribute selection list
513 // it should be postponed to exit from the selectionChanged processing
514 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
515 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
516 Events_Loop::loop()->flush(anEvent);
521 appendFirstSelectionInHistory();
522 bool aDone = ModuleBase_WidgetSelector::processSelection();
523 appendSelectionInHistory();
527 void ModuleBase_WidgetMultiSelector::appendFirstSelectionInHistory()
529 if (mySelectedHistoryValues.empty()) {
530 myCurrentHistoryIndex++;
531 mySelectedHistoryValues.append(getAttributeSelection());
533 #ifdef DEBUG_UNDO_REDO
534 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
539 void ModuleBase_WidgetMultiSelector::appendSelectionInHistory()
541 while (myCurrentHistoryIndex != mySelectedHistoryValues.count() - 1)
542 mySelectedHistoryValues.removeLast();
544 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
545 myCurrentHistoryIndex++;
546 mySelectedHistoryValues.append(aSelected);
547 myWorkshop->updateCommandStatus();
549 #ifdef DEBUG_UNDO_REDO
550 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
554 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
556 mySelectedHistoryValues.clear();
557 myCurrentHistoryIndex = -1;
558 myWorkshop->updateCommandStatus();
560 #ifdef DEBUG_UNDO_REDO
561 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
565 void ModuleBase_WidgetMultiSelector::updateFocus()
567 // Set focus to List control in order to make possible
568 // to use Tab key for transfer the focus to next widgets
569 ModuleBase_Tools::setFocus(myListView->getControl(),
570 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
573 //********************************************************************
574 void ModuleBase_WidgetMultiSelector::updateSelectionName()
578 //********************************************************************
579 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
581 if (myIsSetSelectionBlocked)
583 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
585 // according to #2154 we need to update OB selection when selection in the viewer happens
586 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
587 // because we need that Object Browser has been already updated when synchronize happens.
589 // Restore selection in the viewer by the attribute selection list
590 // it is possible that diring selection attribute filling, selection in Object Browser
591 // is changed(some items were removed/added) and as result, selection in the viewer
592 // differs from the selection come to this method. By next rows, we restore selection
593 // in the viewer according to content of selection attribute. Case is Edge selection in Group
594 myIsSetSelectionBlocked = true;
595 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
596 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
597 Events_Loop::loop()->flush(anEvent);
598 myIsSetSelectionBlocked = false;
601 //********************************************************************
602 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
604 QIntList aShapeTypes;
606 if (myShapeTypes.length() > 1 && myIsUseChoice) {
607 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCtrl->textValue()));
610 foreach (QString aType, myShapeTypes) {
611 aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
617 //********************************************************************
618 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
620 QString aShapeTypeName;
623 foreach (QString aShapeTypeName, myShapeTypes) {
624 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
625 if(aRefType == theShapeType && idx != myTypeCtrl->value()) {
626 updateSelectionModesAndFilters(false);
627 bool isBlocked = myTypeCtrl->blockSignals(true);
628 myTypeCtrl->setValue(idx);
629 myTypeCtrl->blockSignals(isBlocked);
630 updateSelectionModesAndFilters(true);
637 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
639 QList<ModuleBase_ViewerPrsPtr> aSelected;
640 convertIndicesToViewerSelection(std::set<int>(), aSelected);
644 //********************************************************************
645 void ModuleBase_WidgetMultiSelector::updateSelectionList()
647 myListView->getControl()->clear();
649 DataPtr aData = myFeature->data();
650 AttributePtr anAttribute = aData->attribute(attributeID());
651 std::string aType = anAttribute->attributeType();
652 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
653 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
654 for (int i = 0; i < aSelectionListAttr->size(); i++) {
655 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
656 myListView->addItem(aAttr->namingName().c_str(), i);
659 else if (aType == ModelAPI_AttributeRefList::typeId()) {
660 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
661 for (int i = 0; i < aRefListAttr->size(); i++) {
662 ObjectPtr anObject = aRefListAttr->object(i);
663 if (anObject.get()) {
664 myListView->addItem(anObject->data()->name().c_str(), i);
668 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
669 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
670 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
671 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
673 if (anAttribute.get()) {
674 std::string anAttrName = generateName(anAttribute, myWorkshop);
675 aName = QString::fromStdString(anAttrName);
678 ObjectPtr anObject = aRefAttrListAttr->object(i);
679 if (anObject.get()) {
680 aName = anObject->data()->name().c_str();
683 myListView->addItem(aName, i);
687 // We have to call repaint because sometimes the List control is not updated
688 myListView->getControl()->repaint();
691 //********************************************************************
692 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
696 if (theType == "Vertices")
698 else if (theType == "Edges")
700 else if (theType == "Faces")
702 else if (theType == "Solids")
708 //********************************************************************
709 void ModuleBase_WidgetMultiSelector::clearSelection()
711 bool isClearInNeutralPoint = myIsNeutralPointClear;
712 myIsNeutralPointClear = true;
714 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
715 // This method will call Selection changed event which will call onSelectionChanged
716 // To clear mySelection, myListView and storeValue()
717 // So, we don't need to call it
718 myWorkshop->setSelected(anEmptyList);
720 myIsNeutralPointClear = isClearInNeutralPoint;
723 //********************************************************************
724 void ModuleBase_WidgetMultiSelector::onDeleteItem()
729 //********************************************************************
730 void ModuleBase_WidgetMultiSelector::onListSelection()
732 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
736 //********************************************************************
737 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
739 myListView->getSelectedIndices(theAttributeIds);
742 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
743 QList<ModuleBase_ViewerPrsPtr>& theValues) const
745 if(myFeature.get() == NULL)
748 DataPtr aData = myFeature->data();
749 AttributePtr anAttribute = aData->attribute(attributeID());
750 std::string aType = anAttribute->attributeType();
751 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
752 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
753 for (int i = 0; i < aSelectionListAttr->size(); i++) {
754 // filter by attribute indices only if the container is not empty otherwise return all items
755 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
757 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
758 ResultPtr anObject = anAttr->context();
760 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
761 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
764 else if (aType == ModelAPI_AttributeRefList::typeId()) {
765 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
766 for (int i = 0; i < aRefListAttr->size(); i++) {
767 // filter by attribute indices only if the container is not empty otherwise return all items
768 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
770 ObjectPtr anObject = aRefListAttr->object(i);
771 if (anObject.get()) {
772 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
773 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
777 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
778 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
779 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
780 // filter by attribute indices only if the container is not empty otherwise return all items
781 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
783 ObjectPtr anObject = aRefAttrListAttr->object(i);
787 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
788 if (anAttribute.get()) {
789 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
790 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
791 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
797 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
798 (QList<ModuleBase_ViewerPrsPtr>& theValues)
802 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
803 DataPtr aData = myFeature->data();
804 AttributePtr anAttribute = aData->attribute(attributeID());
805 std::string aType = anAttribute->attributeType();
806 std::set<GeomShapePtr> aShapes;
807 std::set<int> anIndicesToBeRemoved;
808 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
809 // iteration through data model to find not selected elements to remove them
810 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
811 for (int i = 0; i < aSelectionListAttr->size(); i++) {
812 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
813 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection,
816 anIndicesToBeRemoved.insert(i);
818 isDone = anIndicesToBeRemoved.size() > 0;
819 aSelectionListAttr->remove(anIndicesToBeRemoved);
821 else if (aType == ModelAPI_AttributeRefList::typeId()) {
822 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
823 for (int i = 0; i < aRefListAttr->size(); i++) {
824 ObjectPtr anObject = aRefListAttr->object(i);
825 if (anObject.get()) {
826 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
829 anIndicesToBeRemoved.insert(i);
832 isDone = anIndicesToBeRemoved.size() > 0;
833 aRefListAttr->remove(anIndicesToBeRemoved);
835 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
836 std::set<AttributePtr> anAttributes;
837 QList<ModuleBase_ViewerPrsPtr>::const_iterator
838 anIt = theValues.begin(), aLast = theValues.end();
841 for (; anIt != aLast; anIt++) {
842 ModuleBase_ViewerPrsPtr aPrs = *anIt;
843 getGeomSelection(aPrs, anObject, aShape);
844 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
845 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
846 anAttributes.insert(anAttr);
849 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
850 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
852 if (aRefAttrListAttr->isAttribute(i)) {
853 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
854 aFound = anAttributes.find(anAttribute) != anAttributes.end();
857 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
861 anIndicesToBeRemoved.insert(i);
863 isDone = anIndicesToBeRemoved.size() > 0;
864 aRefAttrListAttr->remove(anIndicesToBeRemoved);
870 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
871 (QList<ModuleBase_ViewerPrsPtr>& theValues)
873 // convert prs list to objects map
874 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
875 std::set<GeomShapePtr> aShapes;
876 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
879 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
880 for (; anIt != aLast; anIt++) {
881 ModuleBase_ViewerPrsPtr aPrs = *anIt;
882 getGeomSelection(aPrs, anObject, aShape);
884 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
885 aShapes = aGeomSelection[anObject];
886 // we need to know if there was an empty shape in selection for the object
888 aShape = anEmptyShape;
889 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
890 aShapes.insert(aShape);
891 aGeomSelection[anObject] = aShapes;
893 return aGeomSelection;
896 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
897 GeomShapePtr theShape,
898 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
899 ModuleBase_IWorkshop* theWorkshop)
901 // issue #2154: we should not remove from list objects hidden in the viewer if selection
902 // was done with SHIFT button
903 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
907 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
908 if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
909 const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
910 if (aContext.get() && aContext->shape()->isEqual(theShape))
913 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
914 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
915 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
916 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
917 for (; anIt != aLast && !aFound; anIt++) {
918 GeomShapePtr aCShape = *anIt;
920 aFound = aCShape->isSame(aShape);
927 ModuleBase_WidgetMultiSelector::actionsList(ModuleBase_ActionType theActionType) const
929 QList<ActionInfo> aList;
930 if (myCurrentHistoryIndex > -1) {
932 QString aTitle("Selection %1 items");
933 QString aTit("Selection %1 item");
934 QIcon aIcon(":pictures/selection.png");
936 switch (theActionType) {
939 while (i <= myCurrentHistoryIndex) {
940 aNb = mySelectedHistoryValues.at(i).count();
942 ActionInfo aInfo(aIcon, aTit.arg(aNb));
943 aList.insert(0, aInfo);
945 ActionInfo aInfo(aIcon, aTitle.arg(aNb));
946 aList.insert(0, aInfo);
952 i = mySelectedHistoryValues.length() - 1;
953 while (i > myCurrentHistoryIndex) {
954 aNb = mySelectedHistoryValues.at(i).count();
956 ActionInfo aInfo(aIcon, aTit.arg(mySelectedHistoryValues.at(i).count()));
957 aList.insert(0, aInfo);
959 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));
960 aList.insert(0, aInfo);