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_IViewer.h>
31 #include <ModuleBase_IWorkshop.h>
32 #include <ModuleBase_ListView.h>
33 #include <ModuleBase_Tools.h>
34 #include <ModuleBase_ViewerPrs.h>
35 #include <ModuleBase_WidgetShapeSelector.h>
37 #include <ModelAPI_Data.h>
38 #include <ModelAPI_Object.h>
39 #include <ModelAPI_AttributeSelectionList.h>
40 #include <ModelAPI_AttributeRefList.h>
41 #include <ModelAPI_AttributeRefAttrList.h>
42 #include <ModelAPI_Tools.h>
43 #include <ModelAPI_Events.h>
45 #include <Config_WidgetAPI.h>
47 #include <QGridLayout>
49 #include <QListWidget>
54 #include <QApplication>
57 #include <QMainWindow>
62 //#define DEBUG_UNDO_REDO
64 #ifdef DEBUG_UNDO_REDO
65 void printHistoryInfo(const QString& theMethodName, int theCurrentHistoryIndex,
66 QList<QList<std::shared_ptr<ModuleBase_ViewerPrs> > > theSelectedHistoryValues)
69 for (int i = 0; i < theSelectedHistoryValues.size(); i++)
70 aSizes.append(QString::number(theSelectedHistoryValues[i].size()));
72 std::cout << theMethodName.toStdString()
73 << " current = " << theCurrentHistoryIndex
74 << " size(history) = " << theSelectedHistoryValues.size()
75 << " (" << aSizes.join(", ").toStdString() << ")"
80 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
81 ModuleBase_IWorkshop* theWorkshop,
82 const Config_WidgetAPI* theData)
83 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData),
84 myIsSetSelectionBlocked(false), myCurrentHistoryIndex(-1)
86 QGridLayout* aMainLay = new QGridLayout(this);
87 ModuleBase_Tools::adjustMargins(aMainLay);
89 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
90 aMainLay->addWidget(aTypeLabel, 0, 0);
92 myTypeCombo = new QComboBox(this);
93 // There is no sense to parameterize list of types while we can not parameterize selection mode
95 std::string aPropertyTypes = theData->getProperty("type_choice");
96 QString aTypesStr = aPropertyTypes.c_str();
97 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
99 myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
101 if (!aShapeTypes.empty())
102 myTypeCombo->addItems(aShapeTypes);
103 aMainLay->addWidget(myTypeCombo, 0, 1);
104 // if the xml definition contains one type, the controls to select a type should not be shown
105 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
106 aTypeLabel->setVisible(false);
107 myTypeCombo->setVisible(false);
110 QString aLabelText = translate(theData->getProperty("label"));
111 QLabel* aListLabel = new QLabel(aLabelText, this);
112 aMainLay->addWidget(aListLabel, 1, 0);
113 // if the xml definition contains one type, an information label
114 // should be shown near to the latest
115 if (aShapeTypes.size() <= 1) {
116 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
117 if (!aLabelIcon.isEmpty()) {
118 QLabel* aSelectedLabel = new QLabel("", this);
119 aSelectedLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
120 aMainLay->addWidget(aSelectedLabel, 1, 1);
122 aMainLay->setColumnStretch(2, 1);
125 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
126 QString anObjName = QString::fromStdString(attributeID());
127 myListView = new ModuleBase_ListView(this, anObjName, aToolTip);
128 connect(myListView->getControl(), SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
129 connect(myListView, SIGNAL(deleteActionClicked()), SLOT(onDeleteItem()));
131 aMainLay->addWidget(myListView->getControl(), 2, 0, 1, -1);
132 aMainLay->setRowStretch(2, 1);
133 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
134 //aMainLay->setRowMinimumHeight(3, 20);
135 //this->setLayout(aMainLay);
136 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
138 myIsNeutralPointClear = theData->getBooleanAttribute("clear_in_neutral_point", true);
141 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
145 //********************************************************************
146 void ModuleBase_WidgetMultiSelector::activateCustom()
148 ModuleBase_WidgetSelector::activateCustom();
150 myWorkshop->module()->activateCustomPrs(myFeature,
151 ModuleBase_IModule::CustomizeHighlightedObjects, true);
152 clearSelectedHistory();
153 myWorkshop->updateCommandStatus();
156 //********************************************************************
157 void ModuleBase_WidgetMultiSelector::deactivate()
159 ModuleBase_WidgetSelector::deactivate();
161 myWorkshop->module()->deactivateCustomPrs(ModuleBase_IModule::CustomizeHighlightedObjects, true);
162 clearSelectedHistory();
163 myWorkshop->updateCommandStatus();
166 //********************************************************************
167 bool ModuleBase_WidgetMultiSelector::storeValueCustom()
169 // the value is stored on the selection changed signal processing
170 // A rare case when plugin was not loaded.
174 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
175 std::string aType = anAttribute->attributeType();
176 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
177 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
178 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
183 //********************************************************************
184 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
186 // A rare case when plugin was not loaded.
190 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
191 std::string aType = anAttribute->attributeType();
192 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
193 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
194 // Restore shape type
195 std::string aSelectionType = aSelectionListAttr->selectionType().c_str();
196 if (!aSelectionType.empty())
197 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionType.c_str()));
199 updateSelectionList();
203 //********************************************************************
204 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
205 const bool theToValidate)
207 if (myIsSetSelectionBlocked)
210 AttributeSelectionListPtr aSelectionListAttr;
211 if (attribute()->attributeType() == ModelAPI_AttributeSelectionList::typeId())
212 aSelectionListAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(attribute());
213 if (aSelectionListAttr.get())
214 aSelectionListAttr->cashValues(true);
216 /// remove unused objects from the model attribute.
217 /// It should be performed before new attributes append.
218 bool isDone = removeUnusedAttributeObjects(theValues);
220 QList<ModuleBase_ViewerPrsPtr> anInvalidValues;
221 QList<ModuleBase_ViewerPrsPtr> anAttributeValues;
222 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
223 for (; anIt != aLast; anIt++) {
224 ModuleBase_ViewerPrsPtr aValue = *anIt;
225 // do not validate and append to attribute selection presentation if it exists in the attribute
228 getGeomSelection(aValue, anObject, aShape);
229 if (ModuleBase_Tools::hasObject(attribute(), anObject, aShape, myWorkshop, myIsInValidate)) {
230 anAttributeValues.append(aValue);
233 if (theToValidate && !isValidInFilters(aValue))
234 anInvalidValues.append(aValue);
236 bool aHasInvalidValues = anInvalidValues.size() > 0;
238 for (anIt = theValues.begin(); anIt != aLast; anIt++) {
239 ModuleBase_ViewerPrsPtr aValue = *anIt;
240 bool aProcessed = false;
241 if ((aHasInvalidValues && anInvalidValues.contains(aValue)) ||
242 anAttributeValues.contains(aValue))
244 aProcessed = setSelectionCustom(aValue); /// it is not optimal as hasObject() is already checked
245 // if there is at least one set, the result is true
246 isDone = isDone || aProcessed;
248 // updateObject - to update/redisplay feature
249 // it is commented in order to perfom it outside the method
251 //updateObject(myFeature);
252 // this emit is necessary to call store/restore method an restore type of selection
253 //emit valuesChanged();
256 if (aSelectionListAttr.get())
257 aSelectionListAttr->cashValues(false);
260 if (!anInvalidValues.empty())
261 theValues.append(anInvalidValues);
263 if (isDone) // may be the feature's result is not displayed, but attributes should be
264 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
265 true);/// hope that something is redisplayed by object updated
270 //********************************************************************
271 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrsPtr>& theValues)
273 std::set<int> anAttributeIds;
274 getSelectedAttributeIndices(anAttributeIds);
275 if (!anAttributeIds.empty())
276 convertIndicesToViewerSelection(anAttributeIds, theValues);
279 //********************************************************************
280 bool ModuleBase_WidgetMultiSelector::canProcessAction(ModuleBase_ActionType theActionType,
281 bool& isActionEnabled)
283 isActionEnabled = false;
284 bool aCanProcess = false;
285 switch (theActionType) {
289 isActionEnabled = theActionType == ActionUndo ? myCurrentHistoryIndex > 0
290 : (mySelectedHistoryValues.size() > 0 &&
291 myCurrentHistoryIndex < mySelectedHistoryValues.size() - 1);
295 aCanProcess = ModuleBase_WidgetSelector::canProcessAction(theActionType, isActionEnabled);
301 //********************************************************************
302 bool ModuleBase_WidgetMultiSelector::processAction(ModuleBase_ActionType theActionType,
303 const ActionParamPtr& theParam)
305 switch (theActionType) {
308 ActionIntParamPtr aParam =
309 std::dynamic_pointer_cast<ModuleBase_ActionIntParameter>(theParam);
310 int aNb = aParam->value();
311 if (theActionType == ActionUndo)
312 myCurrentHistoryIndex -= aNb;
314 myCurrentHistoryIndex += aNb;
315 QList<ModuleBase_ViewerPrsPtr> aSelected = mySelectedHistoryValues[myCurrentHistoryIndex];
316 // equal vertices should not be used here
317 ModuleBase_ISelection::filterSelectionOnEqualPoints(aSelected);
318 bool isDone = setSelection(aSelected,
319 false /*need not validate because values already was in list*/);
320 updateOnSelectionChanged(isDone);
322 myWorkshop->updateCommandStatus();
323 #ifdef DEBUG_UNDO_REDO
324 printHistoryInfo(QString("processAction %1").arg(theActionType == ActionUndo ? "Undo"
325 : "Redo"), myCurrentHistoryIndex, mySelectedHistoryValues);
330 return ModuleBase_ModelWidget::processAction(theActionType, theParam);
334 //********************************************************************
335 void ModuleBase_WidgetMultiSelector::activateSelectionAndFilters(bool toActivate)
337 myWorkshop->updateCommandStatus(); // update enable state of Undo/Redo application actions
338 ModuleBase_WidgetSelector::activateSelectionAndFilters(toActivate);
341 //********************************************************************
342 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
344 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
346 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
347 aValid = aResult.get() != NULL;
350 // We can not select a result of our feature
351 std::list<ResultPtr> aResults;
352 ModelAPI_Tools::allResults(myFeature, aResults);
353 std::list<ResultPtr>::const_iterator aIt;
354 bool isSkipSelf = false;
355 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
356 if ((*aIt) == aResult) {
369 //********************************************************************
370 bool ModuleBase_WidgetMultiSelector::processDelete()
372 appendFirstSelectionInHistory();
374 // find attribute indices to delete
375 std::set<int> anAttributeIds;
376 getSelectedAttributeIndices(anAttributeIds);
378 QModelIndexList anIndices = myListView->getControl()->selectionModel()->selectedIndexes();
380 // refill attribute by the items which indices are not in the list of ids
382 DataPtr aData = myFeature->data();
383 AttributePtr anAttribute = aData->attribute(attributeID());
384 std::string aType = anAttribute->attributeType();
385 aDone = !anAttributeIds.empty();
386 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
387 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
388 aSelectionListAttr->remove(anAttributeIds);
391 else if (aType == ModelAPI_AttributeRefList::typeId()) {
392 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
393 aRefListAttr->remove(anAttributeIds);
395 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
396 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
397 aRefAttrListAttr->remove(anAttributeIds);
401 // update object is necessary to flush update signal. It leads to objects references map update
402 // and the operation presentation will not contain deleted items visualized as parameters of
404 updateObject(myFeature);
407 myWorkshop->setSelected(getAttributeSelection());
409 // may be the feature's result is not displayed, but attributes should be
410 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
411 true); /// hope that something is redisplayed by object updated
415 myListView->restoreSelection(anIndices);
417 appendSelectionInHistory();
421 //********************************************************************
422 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
424 QList<QWidget*> result;
425 //result << myTypeCombo;
426 result << myListView->getControl();
430 //********************************************************************
431 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
433 activateSelectionAndFilters(true);
434 myWorkshop->selectionActivate()->updateSelectionModes();
438 /// store the selected type
439 AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
440 std::string aType = anAttribute->attributeType();
441 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
442 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
443 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
446 // clear attribute values
447 DataPtr aData = myFeature->data();
448 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
449 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
450 aSelectionListAttr->clear();
452 else if (aType == ModelAPI_AttributeRefList::typeId()) {
453 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
454 aRefListAttr->clear();
456 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
457 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
458 aRefAttrListAttr->clear();
461 // update object is necessary to flush update signal. It leads to objects references map update
462 // and the operation presentation will not contain deleted items visualized as parameters of
464 updateObject(myFeature);
466 myWorkshop->setSelected(getAttributeSelection());
467 // may be the feature's result is not displayed, but attributes should be
468 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeArguments,
469 true); /// hope that something is redisplayed by object updated
470 // clear history should follow after set selected to do not increase history by setSelected
471 clearSelectedHistory();
474 //********************************************************************
475 bool ModuleBase_WidgetMultiSelector::processSelection()
477 if (!myIsNeutralPointClear) {
478 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
479 // do not clear selected object
480 if (aSelected.size() == 0) {
481 if (!getAttributeSelection().empty()) {
482 // Restore selection in the viewer by the attribute selection list
483 // it should be postponed to exit from the selectionChanged processing
484 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
485 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
486 Events_Loop::loop()->flush(anEvent);
491 appendFirstSelectionInHistory();
492 bool aDone = ModuleBase_WidgetSelector::processSelection();
493 appendSelectionInHistory();
497 void ModuleBase_WidgetMultiSelector::appendFirstSelectionInHistory()
499 if (mySelectedHistoryValues.empty()) {
500 myCurrentHistoryIndex++;
501 mySelectedHistoryValues.append(getAttributeSelection());
503 #ifdef DEBUG_UNDO_REDO
504 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
509 void ModuleBase_WidgetMultiSelector::appendSelectionInHistory()
511 while (myCurrentHistoryIndex != mySelectedHistoryValues.count() - 1)
512 mySelectedHistoryValues.removeLast();
514 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
515 myCurrentHistoryIndex++;
516 mySelectedHistoryValues.append(aSelected);
517 myWorkshop->updateCommandStatus();
519 #ifdef DEBUG_UNDO_REDO
520 printHistoryInfo("appendSelectionInHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
524 void ModuleBase_WidgetMultiSelector::clearSelectedHistory()
526 mySelectedHistoryValues.clear();
527 myCurrentHistoryIndex = -1;
528 myWorkshop->updateCommandStatus();
530 #ifdef DEBUG_UNDO_REDO
531 printHistoryInfo("clearSelectedHistory", myCurrentHistoryIndex, mySelectedHistoryValues);
535 void ModuleBase_WidgetMultiSelector::updateFocus()
537 // Set focus to List control in order to make possible
538 // to use Tab key for transfer the focus to next widgets
539 ModuleBase_Tools::setFocus(myListView->getControl(),
540 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
543 //********************************************************************
544 void ModuleBase_WidgetMultiSelector::updateSelectionName()
548 //********************************************************************
549 void ModuleBase_WidgetMultiSelector::updateOnSelectionChanged(const bool theDone)
551 if (myIsSetSelectionBlocked)
553 ModuleBase_WidgetSelector::updateOnSelectionChanged(theDone);
555 // according to #2154 we need to update OB selection when selection in the viewer happens
556 // it is important to flush sinchronize selection signal after flush of Update/Create/Delete.
557 // because we need that Object Browser has been already updated when synchronize happens.
559 // Restore selection in the viewer by the attribute selection list
560 // it is possible that diring selection attribute filling, selection in Object Browser
561 // is changed(some items were removed/added) and as result, selection in the viewer
562 // differs from the selection come to this method. By next rows, we restore selection
563 // in the viewer according to content of selection attribute. Case is Edge selection in Group
564 myIsSetSelectionBlocked = true;
565 static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
566 ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
567 Events_Loop::loop()->flush(anEvent);
568 myIsSetSelectionBlocked = false;
571 //********************************************************************
572 QIntList ModuleBase_WidgetMultiSelector::shapeTypes() const
574 QIntList aShapeTypes;
576 if (myTypeCombo->count() > 1 && myIsUseChoice) {
577 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
580 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
581 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
586 //********************************************************************
587 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const int theShapeType)
589 QString aShapeTypeName;
591 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
592 aShapeTypeName = myTypeCombo->itemText(idx);
593 int aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
594 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
595 activateSelectionAndFilters(false);
596 bool isBlocked = myTypeCombo->blockSignals(true);
597 myTypeCombo->setCurrentIndex(idx);
598 myTypeCombo->blockSignals(isBlocked);
599 activateSelectionAndFilters(true);
605 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
607 QList<ModuleBase_ViewerPrsPtr> aSelected;
608 convertIndicesToViewerSelection(std::set<int>(), aSelected);
612 //********************************************************************
613 void ModuleBase_WidgetMultiSelector::updateSelectionList()
615 myListView->getControl()->clear();
617 DataPtr aData = myFeature->data();
618 AttributePtr anAttribute = aData->attribute(attributeID());
619 std::string aType = anAttribute->attributeType();
620 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
621 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
622 for (int i = 0; i < aSelectionListAttr->size(); i++) {
623 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
624 myListView->addItem(aAttr->namingName().c_str(), i);
627 else if (aType == ModelAPI_AttributeRefList::typeId()) {
628 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
629 for (int i = 0; i < aRefListAttr->size(); i++) {
630 ObjectPtr anObject = aRefListAttr->object(i);
631 if (anObject.get()) {
632 myListView->addItem(anObject->data()->name().c_str(), i);
636 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
637 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
638 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
639 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
641 if (anAttribute.get()) {
642 std::string anAttrName = generateName(anAttribute, myWorkshop);
643 aName = QString::fromStdString(anAttrName);
646 ObjectPtr anObject = aRefAttrListAttr->object(i);
647 if (anObject.get()) {
648 aName = anObject->data()->name().c_str();
651 myListView->addItem(aName, i);
655 // We have to call repaint because sometimes the List control is not updated
656 myListView->getControl()->repaint();
659 //********************************************************************
660 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
664 if (theType == "Vertices")
666 else if (theType == "Edges")
668 else if (theType == "Faces")
670 else if (theType == "Solids")
676 //********************************************************************
677 void ModuleBase_WidgetMultiSelector::clearSelection()
679 bool isClearInNeutralPoint = myIsNeutralPointClear;
680 myIsNeutralPointClear = true;
682 QList<ModuleBase_ViewerPrsPtr> anEmptyList;
683 // This method will call Selection changed event which will call onSelectionChanged
684 // To clear mySelection, myListView and storeValue()
685 // So, we don't need to call it
686 myWorkshop->setSelected(anEmptyList);
688 myIsNeutralPointClear = isClearInNeutralPoint;
691 //********************************************************************
692 void ModuleBase_WidgetMultiSelector::onDeleteItem()
697 //********************************************************************
698 void ModuleBase_WidgetMultiSelector::onListSelection()
700 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
704 //********************************************************************
705 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
707 myListView->getSelectedIndices(theAttributeIds);
710 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
711 QList<ModuleBase_ViewerPrsPtr>& theValues) const
713 if(myFeature.get() == NULL)
716 DataPtr aData = myFeature->data();
717 AttributePtr anAttribute = aData->attribute(attributeID());
718 std::string aType = anAttribute->attributeType();
719 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
720 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
721 for (int i = 0; i < aSelectionListAttr->size(); i++) {
722 // filter by attribute indices only if the container is not empty otherwise return all items
723 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
725 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
726 ResultPtr anObject = anAttr->context();
728 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
729 new ModuleBase_ViewerPrs(anObject, anAttr->value(), NULL)));
732 else if (aType == ModelAPI_AttributeRefList::typeId()) {
733 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
734 for (int i = 0; i < aRefListAttr->size(); i++) {
735 // filter by attribute indices only if the container is not empty otherwise return all items
736 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
738 ObjectPtr anObject = aRefListAttr->object(i);
739 if (anObject.get()) {
740 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
741 new ModuleBase_ViewerPrs(anObject, GeomShapePtr(), NULL)));
745 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
746 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
747 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
748 // filter by attribute indices only if the container is not empty otherwise return all items
749 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
751 ObjectPtr anObject = aRefAttrListAttr->object(i);
755 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
756 if (anAttribute.get()) {
757 GeomShapePtr aGeomShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
758 theValues.append(std::shared_ptr<ModuleBase_ViewerPrs>(
759 new ModuleBase_ViewerPrs(anObject, aGeomShape, NULL)));
765 bool ModuleBase_WidgetMultiSelector::removeUnusedAttributeObjects
766 (QList<ModuleBase_ViewerPrsPtr>& theValues)
770 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection = convertSelection(theValues);
771 DataPtr aData = myFeature->data();
772 AttributePtr anAttribute = aData->attribute(attributeID());
773 std::string aType = anAttribute->attributeType();
774 std::set<GeomShapePtr> aShapes;
775 std::set<int> anIndicesToBeRemoved;
776 if (aType == ModelAPI_AttributeSelectionList::typeId()) {
777 // iteration through data model to find not selected elements to remove them
778 AttributeSelectionListPtr aSelectionListAttr = aData->selectionList(attributeID());
779 for (int i = 0; i < aSelectionListAttr->size(); i++) {
780 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
781 bool aFound = findInSelection(anAttr->context(), anAttr->value(), aGeomSelection,
784 anIndicesToBeRemoved.insert(i);
786 isDone = anIndicesToBeRemoved.size() > 0;
787 aSelectionListAttr->remove(anIndicesToBeRemoved);
789 else if (aType == ModelAPI_AttributeRefList::typeId()) {
790 AttributeRefListPtr aRefListAttr = aData->reflist(attributeID());
791 for (int i = 0; i < aRefListAttr->size(); i++) {
792 ObjectPtr anObject = aRefListAttr->object(i);
793 if (anObject.get()) {
794 bool aFound = findInSelection(anObject, GeomShapePtr(), aGeomSelection,
797 anIndicesToBeRemoved.insert(i);
800 isDone = anIndicesToBeRemoved.size() > 0;
801 aRefListAttr->remove(anIndicesToBeRemoved);
803 else if (aType == ModelAPI_AttributeRefAttrList::typeId()) {
804 std::set<AttributePtr> anAttributes;
805 QList<ModuleBase_ViewerPrsPtr>::const_iterator
806 anIt = theValues.begin(), aLast = theValues.end();
809 for (; anIt != aLast; anIt++) {
810 ModuleBase_ViewerPrsPtr aPrs = *anIt;
811 getGeomSelection(aPrs, anObject, aShape);
812 AttributePtr anAttr = myWorkshop->module()->findAttribute(anObject, aShape);
813 if (anAttr.get() && anAttributes.find(anAttr) == anAttributes.end())
814 anAttributes.insert(anAttr);
817 AttributeRefAttrListPtr aRefAttrListAttr = aData->refattrlist(attributeID());
818 for (int i = 0; i < aRefAttrListAttr->size(); i++) {
820 if (aRefAttrListAttr->isAttribute(i)) {
821 AttributePtr anAttribute = aRefAttrListAttr->attribute(i);
822 aFound = anAttributes.find(anAttribute) != anAttributes.end();
825 aFound = findInSelection(aRefAttrListAttr->object(i), GeomShapePtr(), aGeomSelection,
829 anIndicesToBeRemoved.insert(i);
831 isDone = anIndicesToBeRemoved.size() > 0;
832 aRefAttrListAttr->remove(anIndicesToBeRemoved);
838 std::map<ObjectPtr, std::set<GeomShapePtr> > ModuleBase_WidgetMultiSelector::convertSelection
839 (QList<ModuleBase_ViewerPrsPtr>& theValues)
841 // convert prs list to objects map
842 std::map<ObjectPtr, std::set<GeomShapePtr> > aGeomSelection;
843 std::set<GeomShapePtr> aShapes;
844 QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
847 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
848 for (; anIt != aLast; anIt++) {
849 ModuleBase_ViewerPrsPtr aPrs = *anIt;
850 getGeomSelection(aPrs, anObject, aShape);
852 if (aGeomSelection.find(anObject) != aGeomSelection.end()) // found
853 aShapes = aGeomSelection[anObject];
854 // we need to know if there was an empty shape in selection for the object
856 aShape = anEmptyShape;
857 if (aShape.get() && aShapes.find(aShape) == aShapes.end()) // not found
858 aShapes.insert(aShape);
859 aGeomSelection[anObject] = aShapes;
861 return aGeomSelection;
864 bool ModuleBase_WidgetMultiSelector::findInSelection(const ObjectPtr& theObject,
865 GeomShapePtr theShape,
866 const std::map<ObjectPtr, std::set<GeomShapePtr> >& theGeomSelection,
867 ModuleBase_IWorkshop* theWorkshop)
869 // issue #2154: we should not remove from list objects hidden in the viewer if selection
870 // was done with SHIFT button
871 if (theWorkshop->hasSHIFTPressed() && !theObject->isDisplayed())
875 GeomShapePtr anEmptyShape(new GeomAPI_Shape());
876 if (theShape.get()) { // treat shape equal to context as null: 2219, keep order of shapes in list
877 const ResultPtr aContext = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
878 if (aContext.get() && aContext->shape()->isEqual(theShape))
881 GeomShapePtr aShape = theShape.get() ? theShape : anEmptyShape;
882 if (theGeomSelection.find(theObject) != theGeomSelection.end()) {// found
883 const std::set<GeomShapePtr>& aShapes = theGeomSelection.at(theObject);
884 std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
885 for (; anIt != aLast && !aFound; anIt++) {
886 GeomShapePtr aCShape = *anIt;
888 aFound = aCShape->isSame(aShape);
895 ModuleBase_WidgetMultiSelector::actionsList(ModuleBase_ActionType theActionType) const
897 QList<ActionInfo> aList;
898 if (myCurrentHistoryIndex > -1) {
900 QString aTitle("Selection %1 items");
901 QString aTit("Selection %1 item");
902 QIcon aIcon(":pictures/selection.png");
904 switch (theActionType) {
907 while (i <= myCurrentHistoryIndex) {
908 aNb = mySelectedHistoryValues.at(i).count();
910 ActionInfo aInfo(aIcon, aTit.arg(aNb));
911 aList.insert(0, aInfo);
913 ActionInfo aInfo(aIcon, aTitle.arg(aNb));
914 aList.insert(0, aInfo);
920 i = mySelectedHistoryValues.length() - 1;
921 while (i > myCurrentHistoryIndex) {
922 aNb = mySelectedHistoryValues.at(i).count();
924 ActionInfo aInfo(aIcon, aTit.arg(mySelectedHistoryValues.at(i).count()));
925 aList.insert(0, aInfo);
927 ActionInfo aInfo(aIcon, aTitle.arg(mySelectedHistoryValues.at(i).count()));
928 aList.insert(0, aInfo);