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_Definitions.h>
24 #include <ModuleBase_Events.h>
25 #include <ModuleBase_IconFactory.h>
26 #include <ModuleBase_IModule.h>
27 #include <ModuleBase_ISelection.h>
28 #include <ModuleBase_IViewer.h>
29 #include <ModuleBase_IWorkshop.h>
30 #include <ModuleBase_ListView.h>
31 #include <ModuleBase_Tools.h>
32 #include <ModuleBase_ViewerPrs.h>
33 #include <ModuleBase_WidgetShapeSelector.h>
35 #include <ModelAPI_Data.h>
36 #include <ModelAPI_Object.h>
37 #include <ModelAPI_AttributeSelectionList.h>
38 #include <ModelAPI_AttributeRefList.h>
39 #include <ModelAPI_AttributeRefAttrList.h>
40 #include <ModelAPI_Tools.h>
41 #include <ModelAPI_Events.h>
43 #include <Config_WidgetAPI.h>
45 #include <QGridLayout>
47 #include <QListWidget>
52 #include <QApplication>
55 #include <QMainWindow>
60 //#define DEBUG_UNDO_REDO
62 #ifdef DEBUG_UNDO_REDO
63 void printHistoryInfo(const QString& theMethodName, int theCurrentHistoryIndex,
64 QList<QList<std::shared_ptr<ModuleBase_ViewerPrs> > > theSelectedHistoryValues)
67 for (int i = 0; i < theSelectedHistoryValues.size(); i++)
68 aSizes.append(QString::number(theSelectedHistoryValues[i].size()));
70 std::cout << theMethodName.toStdString()
71 << " current = " << theCurrentHistoryIndex
72 << " size(history) = " << theSelectedHistoryValues.size()
73 << " (" << aSizes.join(", ").toStdString() << ")"
78 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
79 ModuleBase_IWorkshop* theWorkshop,
80 const Config_WidgetAPI* theData)
81 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
82 myIsSetSelectionBlocked(false), myCurrentHistoryIndex(-1)
84 QGridLayout* aMainLay = new QGridLayout(this);
85 ModuleBase_Tools::adjustMargins(aMainLay);
87 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
88 aMainLay->addWidget(aTypeLabel, 0, 0);
90 myTypeCombo = new QComboBox(this);
91 // There is no sense to parameterize list of types while we can not parameterize selection mode
93 std::string aPropertyTypes = theData->getProperty("type_choice");
94 QString aTypesStr = aPropertyTypes.c_str();
95 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
97 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
99 if (!aShapeTypes.empty())
100 myTypeCombo->addItems(aShapeTypes);
101 aMainLay->addWidget(myTypeCombo, 0, 1);
102 // if the xml definition contains one type, the controls to select a type should not be shown
103 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
104 aTypeLabel->setVisible(false);
105 myTypeCombo->setVisible(false);
108 QString aLabelText = translate(theData->getProperty("label"));
109 QLabel* aListLabel = new QLabel(aLabelText, this);
110 aMainLay->addWidget(aListLabel, 1, 0);
111 // if the xml definition contains one type, an information label
112 // should be shown near to the latest
113 if (aShapeTypes.size() <= 1) {
114 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
115 if (!aLabelIcon.isEmpty()) {
116 QLabel* aSelectedLabel = new QLabel("", this);
117 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
118 aMainLay->addWidget(aSelectedLabel, 1, 1);
120 aMainLay->setColumnStretch(2, 1);
123 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
124 QString anObjName = QString::fromStdString(attributeID());
125 myListView = new ModuleBase_ListView(this, anObjName, aToolTip);
126 connect(myListView->getControl(), SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
127 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
129 aMainLay->addWidget(myListView->getControl(), 2, 0, 1, -1);
130 aMainLay->setRowStretch(2, 1);
131 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
132 //aMainLay->setRowMinimumHeight(3, 20);
133 //this->setLayout(aMainLay);
134 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
136 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
139 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
143 //********************************************************************
144 void ModuleBase_WidgetMultiSelector::activateCustom()
146 ModuleBase_WidgetSelector::activateCustom();
148 myWorkshop->module()->activateCustomPrs(myFeature,
149 ModuleBase_IModule::CustomizeHighlightedObjects, true);
150 clearSelectedHistory();
151 myWorkshop->updateCommandStatus();
154 //********************************************************************
155 void ModuleBase_WidgetMultiSelector::deactivate()
157 ModuleBase_WidgetSelector::deactivate();
159 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
160 clearSelectedHistory();
161 myWorkshop->updateCommandStatus();
164 //********************************************************************
165 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
167 // the value is stored on the selection changed signal processing
168 // A rare case when plugin was not loaded.
172 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
173 std::string aType = anAttribute->attributeType();
174 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
175 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
176 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
181 //********************************************************************
182 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
184 // A rare case when plugin was not loaded.
188 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
189 std::string aType = anAttribute->attributeType();
190 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
191 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
192 // Restore shape type
193 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
194 if (!aSelectionType.empty())
195 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
197 updateSelectionList();
201 //********************************************************************
202 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
203 const bool theToValidate)
205 if (myIsSetSelectionBlocked)
208 AttributeSelectionListPtr aSelectionListAttr;
209 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
210 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
211 if (aSelectionListAttr.get())
212 aSelectionListAttr->cashValues(true);
214 /// remove unused objects from the model attribute.
215 /// It should be performed before new attributes append.
216 bool isDone = removeUnusedAttributeObjects(theValues);
218 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
219 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
220 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
221 for (; anIt != aLast; anIt++) {
222 ModuleBase_ViewerPrsPtr aValue = *anIt;
223 // do not validate and append to attribute selection presentation if it exists in the attribute
226 getGeomSelection(aValue, anObject, aShape);
227 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
228 anAttributeValues.append(aValue);
231 if (theToValidate && !isValidInFilters(aValue))
232 anInvalidValues.append(aValue);
234 bool aHasInvalidValues = anInvalidValues.size() > 0;
236 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
237 ModuleBase_ViewerPrsPtr aValue = *anIt;
238 bool aProcessed = false;
239 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
240 anAttributeValues.contains(aValue))
242 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
243 // if there is at least one set, the result is true
244 isDone = isDone || aProcessed;
246 // updateObject - to update/redisplay feature
247 // it is commented in order to perfom it outside the method
249 //updateObject(myFeature);
250 // this emit is necessary to call store/restore method an restore type of selection
251 //emit valuesChanged();
254 if (aSelectionListAttr.get())
255 aSelectionListAttr->cashValues(false);
258 if (!anInvalidValues.empty())
259 theValues.append(anInvalidValues);
261 if (isDone) // may be the feature's result is not displayed, but attributes should be
262 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
263 true);/// hope that something is redisplayed by object updated
268 //********************************************************************
269 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
271 std::set<int> anAttributeIds;
272 getSelectedAttributeIndices(anAttributeIds);
273 if (!anAttributeIds.empty())
274 convertIndicesToViewerSelection(anAttributeIds, theValues);
277 //********************************************************************
278 bool ModuleBase_WidgetMultiSelector::canProcessAction(ModuleBase_ActionType theActionType,
279 bool& isActionEnabled)
281 isActionEnabled = false;
282 bool aCanProcess = false;
283 switch (theActionType) {
287 isActionEnabled = theActionType == ActionUndo ? myCurrentHistoryIndex > 0
288 : (mySelectedHistoryValues.size() > 0 &&
289 myCurrentHistoryIndex < mySelectedHistoryValues.size() - 1);
298 //********************************************************************
299 bool ModuleBase_WidgetMultiSelector::processAction(ModuleBase_ActionType theActionType)
301 switch (theActionType) {
304 if (theActionType == ActionUndo)
305 myCurrentHistoryIndex--;
307 myCurrentHistoryIndex++;
308 QList<ModuleBase_ViewerPrsPtr> aSelected = mySelectedHistoryValues[myCurrentHistoryIndex];
309 // equal vertices should not be used here
310 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
311 bool isDone = setSelection(aSelected,
312 false /*need not validate because values already was in list*/);
313 updateOnSelectionChanged(isDone);
315 myWorkshop->updateCommandStatus();
316 #ifdef DEBUG_UNDO_REDO
317 printHistoryInfo(QString("processAction %1").arg(theActionType == ActionUndo ? "Undo"
318 : "Redo"), myCurrentHistoryIndex, mySelectedHistoryValues);
323 return ModuleBase_ModelWidget::processAction(theActionType);
327 //********************************************************************
328 bool ModuleBase_WidgetMultiSelector::activateSelectionAndFilters(bool toActivate)
330 myWorkshop->updateCommandStatus(); // update enable state of Undo/Redo application actions
331 return ModuleBase_WidgetSelector::activateSelectionAndFilters(toActivate);
334 //********************************************************************
335 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
337 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
339 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
340 aValid = aResult.get() != NULL;
343 // We can not select a result of our feature
344 std::list<ResultPtr> aResults;
345 ModelAPI_Tools::allResults(myFeature, aResults);
346 std::list<ResultPtr>::const_iterator aIt;
347 bool isSkipSelf = false;
348 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
349 if ((*aIt) == aResult) {
362 //********************************************************************
363 bool ModuleBase_WidgetMultiSelector::processDelete()
365 appendFirstSelectionInHistory();
367 // find attribute indices to delete
368 std::set<int> anAttributeIds;
369 getSelectedAttributeIndices(anAttributeIds);
371 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
373 // refill attribute by the items which indices are not in the list of ids
375 DataPtr aData = myFeature->data();
376 AttributePtr anAttribute = aData->attribute(attributeID());
377 std::string aType = anAttribute->attributeType();
378 aDone = !anAttributeIds.empty();
379 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
380 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
381 aSelectionListAttr->remove(anAttributeIds);
384 else if (aType == ModelAPI_AttributeRefList::typeId()) {
385 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
386 aRefListAttr->remove(anAttributeIds);
388 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
389 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
390 aRefAttrListAttr->remove(anAttributeIds);
394 // update object is necessary to flush update signal. It leads to objects references map update
395 // and the operation presentation will not contain deleted items visualized as parameters of
397 updateObject(myFeature);
400 myWorkshop->setSelected(getAttributeSelection());
402 // may be the feature's result is not displayed, but attributes should be
403 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
404 true); /// hope that something is redisplayed by object updated
408 myListView->restoreSelection(anIndices);
410 appendSelectionInHistory();
414 //********************************************************************
415 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
417 QList<QWidget*> result;
418 //result << myTypeCombo;
419 result << myListView->getControl();
423 //********************************************************************
424 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
426 activateSelectionAndFilters(true);
430 /// store the selected type
431 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
432 std::string aType = anAttribute->attributeType();
433 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
434 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
435 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
438 // clear attribute values
439 DataPtr aData = myFeature->data();
440 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
441 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
442 aSelectionListAttr->clear();
444 else if (aType == ModelAPI_AttributeRefList::typeId()) {
445 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
446 aRefListAttr->clear();
448 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
449 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
450 aRefAttrListAttr->clear();
453 // update object is necessary to flush update signal. It leads to objects references map update
454 // and the operation presentation will not contain deleted items visualized as parameters of
456 updateObject(myFeature);
458 myWorkshop->setSelected(getAttributeSelection());
459 // may be the feature's result is not displayed, but attributes should be
460 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
461 true); /// hope that something is redisplayed by object updated
462 // clear history should follow after set selected to do not increase history by setSelected
463 clearSelectedHistory();
466 //********************************************************************
467 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
469 if (!myIsNeutralPointClear) {
470 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
471 // do not clear selected object
472 if (aSelected.size() == 0) {
473 if (!getAttributeSelection().empty()) {
474 // Restore selection in the viewer by the attribute selection list
475 // it should be postponed to exit from the selectionChanged processing
476 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
477 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
478 Events_Loop::loop()->flush(anEvent);
483 appendFirstSelectionInHistory();
484 ModuleBase_WidgetSelector::onSelectionChanged();
485 appendSelectionInHistory();
488 void ModuleBase_WidgetMultiSelector::appendFirstSelectionInHistory()
490 if (mySelectedHistoryValues.empty()) {
491 myCurrentHistoryIndex++;
492 mySelectedHistoryValues.append(getAttributeSelection());
494 #ifdef DEBUG_UNDO_REDO
495 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
500 void ModuleBase_WidgetMultiSelector::appendSelectionInHistory()
502 while (myCurrentHistoryIndex != mySelectedHistoryValues.count() - 1)
503 mySelectedHistoryValues.removeLast();
505 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
506 myCurrentHistoryIndex++;
507 mySelectedHistoryValues.append(aSelected);
508 myWorkshop->updateCommandStatus();
510 #ifdef DEBUG_UNDO_REDO
511 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
515 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
517 mySelectedHistoryValues.clear();
518 myCurrentHistoryIndex = -1;
519 myWorkshop->updateCommandStatus();
521 #ifdef DEBUG_UNDO_REDO
522 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
526 void ModuleBase_WidgetMultiSelector::updateFocus()
528 // Set focus to List control in order to make possible
529 // to use Tab key for transfer the focus to next widgets
530 ModuleBase_Tools::setFocus(myListView->getControl(),
531 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
534 //********************************************************************
535 void ModuleBase_WidgetMultiSelector::updateSelectionName()
539 //********************************************************************
540 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
542 if (myIsSetSelectionBlocked)
544 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
546 // according to #2154 we need to update OB selection when selection in the viewer happens
547 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
548 // because we need that Object Browser has been already updated when synchronize happens.
550 // Restore selection in the viewer by the attribute selection list
551 // it is possible that diring selection attribute filling, selection in Object Browser
552 // is changed(some items were removed/added) and as result, selection in the viewer
553 // differs from the selection come to this method. By next rows, we restore selection
554 // in the viewer according to content of selection attribute. Case is Edge selection in Group
555 myIsSetSelectionBlocked = true;
556 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
557 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
558 Events_Loop::loop()->flush(anEvent);
559 myIsSetSelectionBlocked = false;
562 //********************************************************************
563 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
565 QIntList aShapeTypes;
567 if (myTypeCombo->count() > 1 && myIsUseChoice) {
568 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
571 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
572 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
577 //********************************************************************
578 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
580 QString aShapeTypeName;
582 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
583 aShapeTypeName = myTypeCombo->itemText(idx);
584 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
585 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
586 bool aWasActivated = activateSelectionAndFilters(false);
587 bool isBlocked = myTypeCombo->blockSignals(true);
588 myTypeCombo->setCurrentIndex(idx);
589 myTypeCombo->blockSignals(isBlocked);
591 activateSelectionAndFilters(true);
597 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
599 QList<ModuleBase_ViewerPrsPtr> aSelected;
600 convertIndicesToViewerSelection(std::set<int>(), aSelected);
604 //********************************************************************
605 void ModuleBase_WidgetMultiSelector::updateSelectionList()
607 myListView->getControl()->clear();
609 DataPtr aData = myFeature->data();
610 AttributePtr anAttribute = aData->attribute(attributeID());
611 std::string aType = anAttribute->attributeType();
612 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
613 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
614 for (int i = 0; i < aSelectionListAttr->size(); i++) {
615 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
616 myListView->addItem(aAttr->namingName().c_str(), i);
619 else if (aType == ModelAPI_AttributeRefList::typeId()) {
620 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
621 for (int i = 0; i < aRefListAttr->size(); i++) {
622 ObjectPtr anObject = aRefListAttr->object(i);
623 if (anObject.get()) {
624 myListView->addItem(anObject->data()->name().c_str(), i);
628 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
629 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
630 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
631 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
633 if (anAttribute.get()) {
634 std::string anAttrName = generateName(anAttribute, myWorkshop);
635 aName = QString::fromStdString(anAttrName);
638 ObjectPtr anObject = aRefAttrListAttr->object(i);
639 if (anObject.get()) {
640 aName = anObject->data()->name().c_str();
643 myListView->addItem(aName, i);
647 // We have to call repaint because sometimes the List control is not updated
648 myListView->getControl()->repaint();
651 //********************************************************************
652 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
656 if (theType == "Vertices")
658 else if (theType == "Edges")
660 else if (theType == "Faces")
662 else if (theType == "Solids")
668 //********************************************************************
669 void ModuleBase_WidgetMultiSelector::clearSelection()
671 bool isClearInNeutralPoint = myIsNeutralPointClear;
672 myIsNeutralPointClear = true;
674 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
675 // This method will call Selection changed event which will call onSelectionChanged
676 // To clear mySelection, myListView and storeValue()
677 // So, we don't need to call it
678 myWorkshop->setSelected(anEmptyList);
680 myIsNeutralPointClear = isClearInNeutralPoint;
683 //********************************************************************
684 void ModuleBase_WidgetMultiSelector::onDeleteItem()
689 //********************************************************************
690 void ModuleBase_WidgetMultiSelector::onListSelection()
692 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
696 //********************************************************************
697 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
699 myListView->getSelectedIndices(theAttributeIds);
702 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
703 QList<ModuleBase_ViewerPrsPtr>& theValues) const
705 if(myFeature.get() == NULL)
708 DataPtr aData = myFeature->data();
709 AttributePtr anAttribute = aData->attribute(attributeID());
710 std::string aType = anAttribute->attributeType();
711 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
712 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
713 for (int i = 0; i < aSelectionListAttr->size(); i++) {
714 // filter by attribute indices only if the container is not empty otherwise return all items
715 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
717 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
718 ResultPtr anObject = anAttr->context();
720 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
721 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
724 else if (aType == ModelAPI_AttributeRefList::typeId()) {
725 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
726 for (int i = 0; i < aRefListAttr->size(); i++) {
727 // filter by attribute indices only if the container is not empty otherwise return all items
728 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
730 ObjectPtr anObject = aRefListAttr->object(i);
731 if (anObject.get()) {
732 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
733 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
737 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
738 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
739 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
740 // filter by attribute indices only if the container is not empty otherwise return all items
741 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
743 ObjectPtr anObject = aRefAttrListAttr->object(i);
747 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
748 if (anAttribute.get()) {
749 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
750 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
751 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
757 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
758 (QList<ModuleBase_ViewerPrsPtr>& theValues)
762 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
763 DataPtr aData = myFeature->data();
764 AttributePtr anAttribute = aData->attribute(attributeID());
765 std::string aType = anAttribute->attributeType();
766 std::set<GeomShapePtr> aShapes;
767 std::set<int> anIndicesToBeRemoved;
768 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
769 // iteration through data model to find not selected elements to remove them
770 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
771 for (int i = 0; i < aSelectionListAttr->size(); i++) {
772 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
773 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection,
776 anIndicesToBeRemoved.insert(i);
778 isDone = anIndicesToBeRemoved.size() > 0;
779 aSelectionListAttr->remove(anIndicesToBeRemoved);
781 else if (aType == ModelAPI_AttributeRefList::typeId()) {
782 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
783 for (int i = 0; i < aRefListAttr->size(); i++) {
784 ObjectPtr anObject = aRefListAttr->object(i);
785 if (anObject.get()) {
786 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
789 anIndicesToBeRemoved.insert(i);
792 isDone = anIndicesToBeRemoved.size() > 0;
793 aRefListAttr->remove(anIndicesToBeRemoved);
795 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
796 std::set<AttributePtr> anAttributes;
797 QList<ModuleBase_ViewerPrsPtr>::const_iterator
798 anIt = theValues.begin(), aLast = theValues.end();
801 for (; anIt != aLast; anIt++) {
802 ModuleBase_ViewerPrsPtr aPrs = *anIt;
803 getGeomSelection(aPrs, anObject, aShape);
804 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
805 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
806 anAttributes.insert(anAttr);
809 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
810 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
812 if (aRefAttrListAttr->isAttribute(i)) {
813 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
814 aFound = anAttributes.find(anAttribute) != anAttributes.end();
817 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
821 anIndicesToBeRemoved.insert(i);
823 isDone = anIndicesToBeRemoved.size() > 0;
824 aRefAttrListAttr->remove(anIndicesToBeRemoved);
830 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
831 (QList<ModuleBase_ViewerPrsPtr>& theValues)
833 // convert prs list to objects map
834 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
835 std::set<GeomShapePtr> aShapes;
836 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
839 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
840 for (; anIt != aLast; anIt++) {
841 ModuleBase_ViewerPrsPtr aPrs = *anIt;
842 getGeomSelection(aPrs, anObject, aShape);
844 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
845 aShapes = aGeomSelection[anObject];
846 // we need to know if there was an empty shape in selection for the object
848 aShape = anEmptyShape;
849 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
850 aShapes.insert(aShape);
851 aGeomSelection[anObject] = aShapes;
853 return aGeomSelection;
856 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
857 GeomShapePtr theShape,
858 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
859 ModuleBase_IWorkshop* theWorkshop)
861 // issue #2154: we should not remove from list objects hidden in the viewer if selection
862 // was done with SHIFT button
863 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
867 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
868 if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
869 const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
870 if (aContext.get() && aContext->shape()->isEqual(theShape))
873 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
874 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
875 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
876 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
877 for (; anIt != aLast && !aFound; anIt++) {
878 GeomShapePtr aCShape = *anIt;
880 aFound = aCShape->isSame(aShape);