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 int aNb = mySelectedHistoryValues.count();
509 myWorkshop->updateCommandStatus();
511 #ifdef DEBUG_UNDO_REDO
512 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
516 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
518 mySelectedHistoryValues.clear();
519 myCurrentHistoryIndex = -1;
520 myWorkshop->updateCommandStatus();
522 #ifdef DEBUG_UNDO_REDO
523 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
527 void ModuleBase_WidgetMultiSelector::updateFocus()
529 // Set focus to List control in order to make possible
530 // to use Tab key for transfer the focus to next widgets
531 ModuleBase_Tools::setFocus(myListView->getControl(),
532 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
535 //********************************************************************
536 void ModuleBase_WidgetMultiSelector::updateSelectionName()
540 //********************************************************************
541 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
543 if (myIsSetSelectionBlocked)
545 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
547 // according to #2154 we need to update OB selection when selection in the viewer happens
548 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
549 // because we need that Object Browser has been already updated when synchronize happens.
551 // Restore selection in the viewer by the attribute selection list
552 // it is possible that diring selection attribute filling, selection in Object Browser
553 // is changed(some items were removed/added) and as result, selection in the viewer
554 // differs from the selection come to this method. By next rows, we restore selection
555 // in the viewer according to content of selection attribute. Case is Edge selection in Group
556 myIsSetSelectionBlocked = true;
557 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
558 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
559 Events_Loop::loop()->flush(anEvent);
560 myIsSetSelectionBlocked = false;
563 //********************************************************************
564 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
566 QIntList aShapeTypes;
568 if (myTypeCombo->count() > 1 && myIsUseChoice) {
569 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
572 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
573 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
578 //********************************************************************
579 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
581 QString aShapeTypeName;
583 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
584 aShapeTypeName = myTypeCombo->itemText(idx);
585 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
586 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
587 bool aWasActivated = activateSelectionAndFilters(false);
588 bool isBlocked = myTypeCombo->blockSignals(true);
589 myTypeCombo->setCurrentIndex(idx);
590 myTypeCombo->blockSignals(isBlocked);
592 activateSelectionAndFilters(true);
598 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
600 QList<ModuleBase_ViewerPrsPtr> aSelected;
601 convertIndicesToViewerSelection(std::set<int>(), aSelected);
605 //********************************************************************
606 void ModuleBase_WidgetMultiSelector::updateSelectionList()
608 myListView->getControl()->clear();
610 DataPtr aData = myFeature->data();
611 AttributePtr anAttribute = aData->attribute(attributeID());
612 std::string aType = anAttribute->attributeType();
613 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
614 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
615 for (int i = 0; i < aSelectionListAttr->size(); i++) {
616 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
617 myListView->addItem(aAttr->namingName().c_str(), i);
620 else if (aType == ModelAPI_AttributeRefList::typeId()) {
621 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
622 for (int i = 0; i < aRefListAttr->size(); i++) {
623 ObjectPtr anObject = aRefListAttr->object(i);
624 if (anObject.get()) {
625 myListView->addItem(anObject->data()->name().c_str(), i);
629 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
630 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
631 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
632 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
634 if (anAttribute.get()) {
635 std::string anAttrName = generateName(anAttribute, myWorkshop);
636 aName = QString::fromStdString(anAttrName);
639 ObjectPtr anObject = aRefAttrListAttr->object(i);
640 if (anObject.get()) {
641 aName = anObject->data()->name().c_str();
644 myListView->addItem(aName, i);
648 // We have to call repaint because sometimes the List control is not updated
649 myListView->getControl()->repaint();
652 //********************************************************************
653 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
657 if (theType == "Vertices")
659 else if (theType == "Edges")
661 else if (theType == "Faces")
663 else if (theType == "Solids")
669 //********************************************************************
670 void ModuleBase_WidgetMultiSelector::clearSelection()
672 bool isClearInNeutralPoint = myIsNeutralPointClear;
673 myIsNeutralPointClear = true;
675 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
676 // This method will call Selection changed event which will call onSelectionChanged
677 // To clear mySelection, myListView and storeValue()
678 // So, we don't need to call it
679 myWorkshop->setSelected(anEmptyList);
681 myIsNeutralPointClear = isClearInNeutralPoint;
684 //********************************************************************
685 void ModuleBase_WidgetMultiSelector::onDeleteItem()
690 //********************************************************************
691 void ModuleBase_WidgetMultiSelector::onListSelection()
693 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
697 //********************************************************************
698 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
700 myListView->getSelectedIndices(theAttributeIds);
703 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
704 QList<ModuleBase_ViewerPrsPtr>& theValues) const
706 if(myFeature.get() == NULL)
709 DataPtr aData = myFeature->data();
710 AttributePtr anAttribute = aData->attribute(attributeID());
711 std::string aType = anAttribute->attributeType();
712 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
713 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
714 for (int i = 0; i < aSelectionListAttr->size(); i++) {
715 // filter by attribute indices only if the container is not empty otherwise return all items
716 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
718 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
719 ResultPtr anObject = anAttr->context();
721 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
722 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
725 else if (aType == ModelAPI_AttributeRefList::typeId()) {
726 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
727 for (int i = 0; i < aRefListAttr->size(); i++) {
728 // filter by attribute indices only if the container is not empty otherwise return all items
729 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
731 ObjectPtr anObject = aRefListAttr->object(i);
732 if (anObject.get()) {
733 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
734 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
738 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
739 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
740 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
741 // filter by attribute indices only if the container is not empty otherwise return all items
742 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
744 ObjectPtr anObject = aRefAttrListAttr->object(i);
748 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
749 if (anAttribute.get()) {
750 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
751 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
752 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
758 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
759 (QList<ModuleBase_ViewerPrsPtr>& theValues)
763 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
764 DataPtr aData = myFeature->data();
765 AttributePtr anAttribute = aData->attribute(attributeID());
766 std::string aType = anAttribute->attributeType();
767 std::set<GeomShapePtr> aShapes;
768 std::set<int> anIndicesToBeRemoved;
769 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
770 // iteration through data model to find not selected elements to remove them
771 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
772 for (int i = 0; i < aSelectionListAttr->size(); i++) {
773 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
774 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection,
777 anIndicesToBeRemoved.insert(i);
779 isDone = anIndicesToBeRemoved.size() > 0;
780 aSelectionListAttr->remove(anIndicesToBeRemoved);
782 else if (aType == ModelAPI_AttributeRefList::typeId()) {
783 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
784 for (int i = 0; i < aRefListAttr->size(); i++) {
785 ObjectPtr anObject = aRefListAttr->object(i);
786 if (anObject.get()) {
787 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
790 anIndicesToBeRemoved.insert(i);
793 isDone = anIndicesToBeRemoved.size() > 0;
794 aRefListAttr->remove(anIndicesToBeRemoved);
796 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
797 std::set<AttributePtr> anAttributes;
798 QList<ModuleBase_ViewerPrsPtr>::const_iterator
799 anIt = theValues.begin(), aLast = theValues.end();
802 for (; anIt != aLast; anIt++) {
803 ModuleBase_ViewerPrsPtr aPrs = *anIt;
804 getGeomSelection(aPrs, anObject, aShape);
805 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
806 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
807 anAttributes.insert(anAttr);
810 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
811 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
813 if (aRefAttrListAttr->isAttribute(i)) {
814 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
815 aFound = anAttributes.find(anAttribute) != anAttributes.end();
818 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
822 anIndicesToBeRemoved.insert(i);
824 isDone = anIndicesToBeRemoved.size() > 0;
825 aRefAttrListAttr->remove(anIndicesToBeRemoved);
831 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
832 (QList<ModuleBase_ViewerPrsPtr>& theValues)
834 // convert prs list to objects map
835 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
836 std::set<GeomShapePtr> aShapes;
837 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
840 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
841 for (; anIt != aLast; anIt++) {
842 ModuleBase_ViewerPrsPtr aPrs = *anIt;
843 getGeomSelection(aPrs, anObject, aShape);
845 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
846 aShapes = aGeomSelection[anObject];
847 // we need to know if there was an empty shape in selection for the object
849 aShape = anEmptyShape;
850 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
851 aShapes.insert(aShape);
852 aGeomSelection[anObject] = aShapes;
854 return aGeomSelection;
857 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
858 GeomShapePtr theShape,
859 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
860 ModuleBase_IWorkshop* theWorkshop)
862 // issue #2154: we should not remove from list objects hidden in the viewer if selection
863 // was done with SHIFT button
864 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
868 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
869 if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
870 const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
871 if (aContext.get() && aContext->shape()->isEqual(theShape))
874 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
875 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
876 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
877 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
878 for (; anIt != aLast && !aFound; anIt++) {
879 GeomShapePtr aCShape = *anIt;
881 aFound = aCShape->isSame(aShape);
888 ModuleBase_WidgetMultiSelector::actionsList(ModuleBase_ActionType theActionType) const
890 QList<ActionInfo> aList;
891 if (myCurrentHistoryIndex > -1) {
893 QString aTitle("Selection %1 items");
894 QIcon aIcon(":pictures/selection.png");
895 switch (theActionType) {
898 while (i <= myCurrentHistoryIndex) {
899 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));
905 i = mySelectedHistoryValues.length() - 1;
906 while (i > myCurrentHistoryIndex) {
907 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));