1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 * ModuleBase_WidgetMultiSelector.cpp
6 * Created on: Aug 28, 2014
10 #include <ModuleBase_WidgetMultiSelector.h>
11 #include <ModuleBase_WidgetShapeSelector.h>
12 #include <ModuleBase_ISelection.h>
13 #include <ModuleBase_IWorkshop.h>
14 #include <ModuleBase_IViewer.h>
15 #include <ModuleBase_Tools.h>
16 #include <ModuleBase_Definitions.h>
17 #include <ModuleBase_IModule.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Object.h>
21 #include <ModelAPI_AttributeSelectionList.h>
22 #include <ModelAPI_AttributeRefList.h>
24 #include <Config_WidgetAPI.h>
26 #include <QGridLayout>
28 #include <QListWidget>
34 #include <QApplication>
41 const int ATTRIBUTE_SELECTION_INDEX_ROLE = Qt::UserRole + 1;
44 * Customization of a List Widget to make it to be placed on full width of container
46 class CustomListWidget : public QListWidget
50 /// \param theParent a parent widget
51 CustomListWidget( QWidget* theParent )
52 : QListWidget( theParent )
56 /// Redefinition of virtual method
57 virtual QSize sizeHint() const
59 int aHeight = 2*QFontMetrics( font() ).height();
60 QSize aSize = QListWidget::sizeHint();
61 return QSize( aSize.width(), aHeight );
64 /// Redefinition of virtual method
65 virtual QSize minimumSizeHint() const
67 int aHeight = 4/*2*/*QFontMetrics( font() ).height();
68 QSize aSize = QListWidget::minimumSizeHint();
69 return QSize( aSize.width(), aHeight );
73 // The code is necessary only for Linux because
74 //it can not update viewport on widget resize
76 void resizeEvent(QResizeEvent* theEvent)
78 QListWidget::resizeEvent(theEvent);
79 QTimer::singleShot(5, viewport(), SLOT(repaint()));
84 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
85 ModuleBase_IWorkshop* theWorkshop,
86 const Config_WidgetAPI* theData,
87 const std::string& theParentId)
88 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId),
91 QGridLayout* aMainLay = new QGridLayout(this);
92 ModuleBase_Tools::adjustMargins(aMainLay);
94 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
95 aMainLay->addWidget(aTypeLabel, 0, 0);
97 myTypeCombo = new QComboBox(this);
98 // There is no sense to parameterize list of types while we can not parameterize selection mode
100 std::string aPropertyTypes = theData->getProperty("type_choice");
101 QString aTypesStr = aPropertyTypes.c_str();
102 QStringList aShapeTypes = aTypesStr.split(' ', QString::SkipEmptyParts);
104 myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
106 if (!aShapeTypes.empty())
107 myTypeCombo->addItems(aShapeTypes);
108 aMainLay->addWidget(myTypeCombo, 0, 1);
109 // if the xml definition contains one type, the controls to select a type should not be shown
110 if (aShapeTypes.size() <= 1 || !myIsUseChoice) {
111 aTypeLabel->setVisible(false);
112 myTypeCombo->setVisible(false);
115 std::string aLabelText = theData->getProperty("label");
116 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
117 : tr("Selected objects:"), this);
118 aMainLay->addWidget(aListLabel, 1, 0);
119 // if the xml definition contains one type, an information label should be shown near to the latest
120 if (aShapeTypes.size() <= 1) {
121 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
122 if (!aLabelIcon.isEmpty()) {
123 QLabel* aSelectedLabel = new QLabel("", this);
124 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
125 aMainLay->addWidget(aSelectedLabel, 1, 1);
127 aMainLay->setColumnStretch(2, 1);
130 QString aToolTip = QString::fromStdString(theData->widgetTooltip());
131 myListControl = new CustomListWidget(this);
132 QString anObjName = QString::fromStdString(attributeID());
133 myListControl->setObjectName(anObjName);
134 myListControl->setToolTip(aToolTip);
135 myListControl->setSelectionMode(QAbstractItemView::ExtendedSelection);
137 aMainLay->addWidget(myListControl, 2, 0, 1, -1);
138 aMainLay->setRowStretch(2, 1);
139 //aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
140 //aMainLay->setRowMinimumHeight(3, 20);
141 //this->setLayout(aMainLay);
142 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
144 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
145 myCopyAction->setShortcut(QKeySequence::Copy);
146 myCopyAction->setEnabled(false);
147 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
148 myListControl->addAction(myCopyAction);
150 myDeleteAction = new QAction(QIcon(":pictures/delete.png"), tr("Delete"), this);
151 myDeleteAction->setEnabled(false);
152 connect(myDeleteAction, SIGNAL(triggered(bool)), SLOT(onDeleteItem()));
153 myListControl->addAction(myDeleteAction);
155 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
156 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
159 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
163 //********************************************************************
164 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
166 // the value is stored on the selection changed signal processing
167 // A rare case when plugin was not loaded.
171 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
172 if (aSelectionListAttr.get()) {
173 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
178 //********************************************************************
179 bool ModuleBase_WidgetMultiSelector::restoreValueCustom()
181 // A rare case when plugin was not loaded.
185 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
186 if (aSelectionListAttr.get()) {
187 // Restore shape type
188 if (!aSelectionListAttr->selectionType().empty())
189 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
191 updateSelectionList();
195 //********************************************************************
196 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
198 ModuleBase_WidgetValidated::storeAttributeValue();
200 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
201 if (aSelectionListAttr.get()) {
202 mySelectionType = aSelectionListAttr->selectionType();
203 mySelectionCount = aSelectionListAttr->size();
206 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
207 mySelectionCount = aRefListAttr->size();
211 //********************************************************************
212 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool theValid)
214 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
217 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
218 if (aSelectionListAttr.get()) {
219 aSelectionListAttr->setSelectionType(mySelectionType);
221 // restore selection in the attribute. Indeed there is only one stored object
222 int aCountAppened = aSelectionListAttr->size() - mySelectionCount;
223 for (int i = 0; i < aCountAppened; i++)
224 aSelectionListAttr->removeLast();
227 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
228 // restore objects in the attribute. Indeed there is only one stored object
229 int aCountAppened = aRefListAttr->size() - mySelectionCount;
230 for (int i = 0; i < aCountAppened; i++)
231 aRefListAttr->removeLast();
235 //********************************************************************
236 void ModuleBase_WidgetMultiSelector::clearAttribute()
238 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
239 if (aSelectionListAttr.get())
240 aSelectionListAttr->clear();
242 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
243 aRefListAttr->clear();
247 //********************************************************************
248 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
249 GeomShapePtr theShape)
251 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
252 if (aSelectionListAttr.get()) {
253 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
254 aSelectionListAttr->append(aResult, theShape, myIsInValidate);
257 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
258 aRefListAttr->append(theSelectedObject);
262 //********************************************************************
263 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
264 const bool theToValidate)
266 QList<ModuleBase_ViewerPrs> aSkippedValues;
268 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
270 for (; anIt != aLast; anIt++) {
271 ModuleBase_ViewerPrs aValue = *anIt;
272 bool aProcessed = false;
273 if (!theToValidate || isValidInFilters(aValue)) {
274 aProcessed = setSelectionCustom(aValue);
277 aSkippedValues.append(aValue);
278 // if there is at least one set, the result is true
279 isDone = isDone || aProcessed;
281 // updateObject - to update/redisplay feature
282 // it is commented in order to perfom it outside the method
284 //updateObject(myFeature);
285 // this emit is necessary to call store/restore method an restore type of selection
286 //emit valuesChanged();
289 if (!aSkippedValues.empty())
290 theValues.append(aSkippedValues);
295 //********************************************************************
296 void ModuleBase_WidgetMultiSelector::getHighlighted(QList<ModuleBase_ViewerPrs>& theValues)
298 std::set<int> anAttributeIds;
299 getSelectedAttributeIndices(anAttributeIds);
300 if (!anAttributeIds.empty())
301 convertIndicesToViewerSelection(anAttributeIds, theValues);
304 //********************************************************************
305 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
307 bool aValid = ModuleBase_WidgetSelector::isValidSelectionCustom(thePrs);
309 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
310 aValid = aResult.get() != NULL;
313 // We can not select a result of our feature
314 const std::list<ResultPtr>& aResList = myFeature->results();
315 std::list<ResultPtr>::const_iterator aIt;
316 bool isSkipSelf = false;
317 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
318 if ((*aIt) == aResult) {
331 //********************************************************************
332 bool ModuleBase_WidgetMultiSelector::processDelete()
334 // find attribute indices to delete
335 std::set<int> anAttributeIds;
336 getSelectedAttributeIndices(anAttributeIds);
338 // refill attribute by the items which indices are not in the list of ids
340 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
341 if (aSelectionListAttr.get()) {
342 aDone = !anAttributeIds.empty();
343 aSelectionListAttr->remove(anAttributeIds);
346 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
347 if (aRefListAttr.get()) {
348 aDone = !anAttributeIds.empty();
349 aRefListAttr->remove(anAttributeIds);
353 // update object is necessary to flush update signal. It leads to objects references map update
354 // and the operation presentation will not contain deleted items visualized as parameters of
356 updateObject(myFeature);
359 myWorkshop->setSelected(getAttributeSelection());
364 //********************************************************************
365 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
367 QList<QWidget*> result;
368 //result << myTypeCombo;
369 result << myListControl;
373 //********************************************************************
374 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
376 activateSelectionAndFilters(true);
377 QList<ModuleBase_ViewerPrs> anEmptyList;
378 // This method will call Selection changed event which will call onSelectionChanged
379 // To clear mySelection, myListControl and storeValue()
380 // So, we don't need to call it
381 myWorkshop->setSelected(anEmptyList);
384 void ModuleBase_WidgetMultiSelector::updateFocus()
386 // Set focus to List control in order to make possible
387 // to use Tab key for transfer the focus to next widgets
388 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
389 ModuleBase_Tools::setFocus(myListControl,
390 "ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()");
393 //********************************************************************
394 void ModuleBase_WidgetMultiSelector::updateSelectionName()
398 //********************************************************************
399 QIntList ModuleBase_WidgetMultiSelector::getShapeTypes() const
401 QIntList aShapeTypes;
403 if (myTypeCombo->count() > 1 && myIsUseChoice) {
404 aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCombo->currentText()));
407 for (int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
408 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
409 aShapeTypes.append(aType);
410 if (aType == TopAbs_SOLID)
411 aShapeTypes.append(TopAbs_COMPSOLID);
417 //********************************************************************
418 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
420 QString aShapeTypeName;
422 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
423 aShapeTypeName = myTypeCombo->itemText(idx);
424 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
425 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
426 activateSelectionAndFilters(false);
427 bool isBlocked = myTypeCombo->blockSignals(true);
428 myTypeCombo->setCurrentIndex(idx);
429 myTypeCombo->blockSignals(isBlocked);
431 activateSelectionAndFilters(true);
437 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
439 QList<ModuleBase_ViewerPrs> aSelected;
440 convertIndicesToViewerSelection(std::set<int>(), aSelected);
444 //********************************************************************
445 void ModuleBase_WidgetMultiSelector::updateSelectionList()
447 myListControl->clear();
449 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
450 if (aSelectionListAttr.get()) {
451 for (int i = 0; i < aSelectionListAttr->size(); i++) {
452 AttributeSelectionPtr aAttr = aSelectionListAttr->value(i);
453 QListWidgetItem* anItem = new QListWidgetItem(aAttr->namingName().c_str(), myListControl);
454 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
455 myListControl->addItem(anItem);
459 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
460 for (int i = 0; i < aRefListAttr->size(); i++) {
461 ObjectPtr anObject = aRefListAttr->object(i);
462 if (anObject.get()) {
463 QListWidgetItem* anItem = new QListWidgetItem(anObject->data()->name().c_str(),
465 anItem->setData(ATTRIBUTE_SELECTION_INDEX_ROLE, i);
466 myListControl->addItem(anItem);
470 // We have to call repaint because sometimes the List control is not updated
471 myListControl->repaint();
474 //********************************************************************
475 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
479 if (theType == "Vertices")
481 else if (theType == "Edges")
483 else if (theType == "Faces")
485 else if (theType == "Solids")
491 //********************************************************************
492 void ModuleBase_WidgetMultiSelector::onCopyItem()
494 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
496 foreach(QListWidgetItem* aItem, aItems) {
499 aRes += aItem->text();
501 if (!aRes.isEmpty()) {
502 QClipboard *clipboard = QApplication::clipboard();
503 clipboard->setText(aRes);
507 //********************************************************************
508 void ModuleBase_WidgetMultiSelector::onDeleteItem()
513 //********************************************************************
514 void ModuleBase_WidgetMultiSelector::onListSelection()
516 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
517 myCopyAction->setEnabled(!aItems.isEmpty());
518 myDeleteAction->setEnabled(!aItems.isEmpty());
520 myWorkshop->module()->customizeObject(myFeature, ModuleBase_IModule::CustomizeHighlightedObjects,
524 //********************************************************************
525 void ModuleBase_WidgetMultiSelector::getSelectedAttributeIndices(std::set<int>& theAttributeIds)
527 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
528 foreach(QListWidgetItem* anItem, aItems) {
529 int anIndex = anItem->data(ATTRIBUTE_SELECTION_INDEX_ROLE).toInt();
530 if (theAttributeIds.find(anIndex) == theAttributeIds.end())
531 theAttributeIds.insert(anIndex);
535 void ModuleBase_WidgetMultiSelector::convertIndicesToViewerSelection(std::set<int> theAttributeIds,
536 QList<ModuleBase_ViewerPrs>& theValues) const
538 if(myFeature.get() == NULL)
540 AttributeSelectionListPtr aSelectionListAttr = myFeature->data()->selectionList(attributeID());
541 if (aSelectionListAttr.get()) {
542 for (int i = 0; i < aSelectionListAttr->size(); i++) {
543 // filter by attribute indices only if the container is not empty otherwise return all items
544 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
546 AttributeSelectionPtr anAttr = aSelectionListAttr->value(i);
547 ResultPtr anObject = anAttr->context();
548 if (anObject.get()) {
550 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
551 if (aShapePtr.get()) {
552 aShape = aShapePtr->impl<TopoDS_Shape>();
554 theValues.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
559 AttributeRefListPtr aRefListAttr = myFeature->data()->reflist(attributeID());
560 if (aRefListAttr.get()) {
561 for (int i = 0; i < aRefListAttr->size(); i++) {
562 // filter by attribute indices only if the container is not empty otherwise return all items
563 if (!theAttributeIds.empty() && theAttributeIds.find(i) == theAttributeIds.end())
565 ObjectPtr anObject = aRefListAttr->object(i);
566 if (anObject.get()) {
567 theValues.append(ModuleBase_ViewerPrs(anObject, TopoDS_Shape(), NULL));