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>
18 #include <GeomValidators_ShapeType.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Object.h>
23 #include <Config_WidgetAPI.h>
25 #include <QGridLayout>
27 #include <QListWidget>
33 #include <QApplication>
39 //#define DEBUG_SHAPE_VALIDATION_PREVIOUS
41 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
42 ModuleBase_IWorkshop* theWorkshop,
43 const Config_WidgetAPI* theData,
44 const std::string& theParentId)
45 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
46 myWorkshop(theWorkshop)
48 QGridLayout* aMainLay = new QGridLayout(this);
49 ModuleBase_Tools::adjustMargins(aMainLay);
51 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
52 aMainLay->addWidget(aTypeLabel, 0, 0);
54 myTypeCombo = new QComboBox(this);
55 // There is no sence to paramerize list of types while we can not parametrize selection mode
57 myShapeValidator = new GeomValidators_ShapeType();
59 std::string aPropertyTypes = theData->getProperty("type_choice");
60 QString aTypesStr = aPropertyTypes.c_str();
61 QStringList aShapeTypes = aTypesStr.split(' ');
63 //myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
65 myTypeCombo->addItems(aShapeTypes);
66 aMainLay->addWidget(myTypeCombo, 0, 1);
67 // if the xml definition contains one type, the controls to select a type should not be shown
68 if (aShapeTypes.size() == 1/* || !myIsUseChoice*/) {
69 aTypeLabel->setVisible(false);
70 myTypeCombo->setVisible(false);
73 std::string aLabelText = theData->getProperty("label");
74 QLabel* aListLabel = new QLabel(!aLabelText.empty() ? aLabelText.c_str()
75 : tr("Selected objects:"), this);
76 aMainLay->addWidget(aListLabel, 1, 0);
77 // if the xml definition contains one type, an information label should be shown near to the latest
78 if (aShapeTypes.size() == 1) {
79 QString aLabelText = QString::fromStdString(theData->widgetLabel());
80 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
81 QLabel* aSelectedLabel = new QLabel(aLabelText, this);
82 if (!aLabelIcon.isEmpty())
83 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
84 aMainLay->addWidget(aSelectedLabel, 1, 1);
85 aMainLay->setColumnStretch(2, 1);
88 myListControl = new QListWidget(this);
89 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
90 aMainLay->setRowStretch(2, 1);
91 aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
92 aMainLay->setRowMinimumHeight(3, 20);
93 this->setLayout(aMainLay);
94 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
96 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
97 myCopyAction->setShortcut(QKeySequence::Copy);
98 myCopyAction->setEnabled(false);
99 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
100 myListControl->addAction(myCopyAction);
101 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
102 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
105 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
107 delete myShapeValidator;
110 //TODO: nds stabilization hotfix
111 void ModuleBase_WidgetMultiSelector::disconnectSignals()
113 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
116 //********************************************************************
117 void ModuleBase_WidgetMultiSelector::activateCustom()
119 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
120 connect(myWorkshop, SIGNAL(selectionChanged()),
121 this, SLOT(onSelectionChanged()),
122 Qt::UniqueConnection);
124 activateShapeSelection(true);
126 // Restore selection in the viewer by the attribute selection list
127 myWorkshop->setSelected(getAttributeSelection());
129 activateFilters(myWorkshop, true);
132 //********************************************************************
133 void ModuleBase_WidgetMultiSelector::deactivate()
135 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
136 activateShapeSelection(false);
137 activateFilters(myWorkshop, false);
140 //********************************************************************
141 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
143 // the value is stored on the selection changed signal processing
144 // A rare case when plugin was not loaded.
147 DataPtr aData = myFeature->data();
148 AttributeSelectionListPtr aSelectionListAttr =
149 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
151 if (aSelectionListAttr) {
153 TopAbs_ShapeEnum aCurrentType =
154 ModuleBase_Tools::shapeType(myTypeCombo->currentText());
155 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
160 //********************************************************************
161 bool ModuleBase_WidgetMultiSelector::restoreValue()
163 // A rare case when plugin was not loaded.
166 DataPtr aData = myFeature->data();
167 AttributeSelectionListPtr aSelectionListAttr =
168 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
170 if (aSelectionListAttr) {
171 // Restore shape type
172 if (!aSelectionListAttr->selectionType().empty())
173 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
174 updateSelectionList(aSelectionListAttr);
180 //********************************************************************
181 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
183 DataPtr aData = myFeature->data();
184 AttributeSelectionListPtr aSelectionListAttr =
185 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
186 if (aSelectionListAttr.get() == NULL)
189 mySelectionType = aSelectionListAttr->selectionType();
191 int aSize = aSelectionListAttr->size();
192 for (int i = 0; i < aSelectionListAttr->size(); i++) {
193 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
194 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
198 //********************************************************************
199 void ModuleBase_WidgetMultiSelector::setObject(ObjectPtr theSelectedObject,
200 GeomShapePtr theShape)
202 DataPtr aData = myFeature->data();
203 AttributeSelectionListPtr aSelectionListAttr =
204 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
206 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
207 aSelectionListAttr->append(aResult, theShape);
210 //********************************************************************
211 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
213 DataPtr aData = myFeature->data();
214 AttributeSelectionListPtr aSelectionListAttr =
215 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
216 if (aSelectionListAttr.get() == NULL)
218 aSelectionListAttr->clear();
221 aSelectionListAttr->setSelectionType(mySelectionType);
223 // Store selection in the attribute
224 int aSize = mySelection.size();
225 foreach (GeomSelection aSelec, mySelection) {
226 setObject(aSelec.first, aSelec.second);
230 //********************************************************************
231 bool ModuleBase_WidgetMultiSelector::acceptSubShape(const TopoDS_Shape& theShape) const
234 if (theShape.IsNull()) {
235 aValid = true; // do not check the shape type if the shape is empty
236 // extrusion uses a sketch object selectected in Object browser
240 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
241 if (myTypeCombo->count() > 1) {
242 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
243 aValid = aShapeType == aType;
246 for(int i = 0, aCount = myTypeCombo->count(); i < aCount && !aValid; i++) {
247 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
248 aValid = aShapeType == aType;
255 //********************************************************************
256 bool ModuleBase_WidgetMultiSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
258 QList<ModuleBase_ViewerPrs> aSkippedValues;
260 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
262 for (; anIt != aLast; anIt++) {
263 ModuleBase_ViewerPrs aValue = *anIt;
264 bool aProcessed = false;
265 if (isValidSelection(aValue)) {
266 aProcessed = setSelectionCustom(aValue);
269 aSkippedValues.append(aValue);
270 // if there is at least one set, the result is true
271 isDone = isDone || aProcessed;
273 // updateObject - to update/redisplay feature
274 // it is commented in order to perfom it outside the method
276 //updateObject(myFeature);
277 // this emit is necessary to call store/restore method an restore type of selection
278 //emit valuesChanged();
281 if (!aSkippedValues.empty())
282 theValues.append(aSkippedValues);
287 //********************************************************************
288 bool ModuleBase_WidgetMultiSelector::isValidSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
290 #ifdef DEBUG_SHAPE_VALIDATION_PREVIOUS
293 GeomShapePtr aShape = myWorkshop->selection()->getShape(thePrs);
294 // if there is no result(the feature is presentable only), result is false
295 ResultPtr aResult = myWorkshop->selection()->getResult(thePrs);
296 bool aValid = aResult.get() != NULL;
298 // if there is no selected shape, the method returns true
302 // Check that the selection corresponds to selection type
303 TopoDS_Shape aTopoShape = aShape->impl<TopoDS_Shape>();
304 aValid = acceptSubShape(aTopoShape);
310 // We can not select a result of our feature
311 const std::list<ResultPtr>& aResList = myFeature->results();
312 std::list<ResultPtr>::const_iterator aIt;
313 bool isSkipSelf = false;
314 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
315 if ((*aIt) == aResult) {
328 //********************************************************************
329 bool ModuleBase_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
331 // DEBUG_THE_SAME_AS_SHAPE
334 getGeomSelection(thePrs, anObject, aShape);
336 setObject(anObject, aShape);
340 //********************************************************************
341 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
343 QList<QWidget*> result;
344 //result << myTypeCombo;
345 result << myListControl;
349 //********************************************************************
350 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
352 activateShapeSelection(true);
353 activateFilters(myWorkshop, true);
354 QList<ModuleBase_ViewerPrs> anEmptyList;
355 // This method will call Selection changed event which will call onSelectionChanged
356 // To clear mySelection, myListControl and storeValue()
357 // So, we don't need to call it
358 myWorkshop->setSelected(anEmptyList);
361 //********************************************************************
362 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
364 QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected(ModuleBase_ISelection::AllControls);
366 DataPtr aData = myFeature->data();
367 AttributeSelectionListPtr aSelectionListAttr =
368 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
369 aSelectionListAttr->clear();
371 setSelection(aSelected);
373 emit valuesChanged();
374 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
375 // calls validators for the feature and, as a result, updates the Apply button state.
376 updateObject(myFeature);
378 // Set focus to List control in order to make possible
379 // to use Tab key for transfer the focus to next widgets
380 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
381 myListControl->setFocus();
384 //********************************************************************
385 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
387 QString aShapeTypeName;
389 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
390 aShapeTypeName = myTypeCombo->itemText(idx);
391 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
392 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
393 activateShapeSelection(false);
394 activateFilters(myWorkshop, false);
395 bool isBlocked = myTypeCombo->blockSignals(true);
396 myTypeCombo->setCurrentIndex(idx);
397 myTypeCombo->blockSignals(isBlocked);
399 activateShapeSelection(true);
400 activateFilters(myWorkshop, true);
406 void ModuleBase_WidgetMultiSelector::activateShapeSelection(const bool isActivated)
408 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
411 QString aNewType = myTypeCombo->currentText();
413 if (true /*myIsUseChoice*/) {
414 aList.append(ModuleBase_Tools::shapeType(aNewType));
417 for(int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
418 aList.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
420 myWorkshop->activateSubShapesSelection(aList);
422 myWorkshop->deactivateSubShapesSelection();
426 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
428 QList<ModuleBase_ViewerPrs> aSelected;
429 // Restore selection in the viewer by the attribute selection list
431 DataPtr aData = myFeature->data();
432 AttributeSelectionListPtr aListAttr =
433 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
435 for (int i = 0; i < aListAttr->size(); i++) {
436 AttributeSelectionPtr anAttr = aListAttr->value(i);
437 ResultPtr anObject = anAttr->context();
438 if (anObject.get()) {
440 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
441 if (aShapePtr.get()) {
442 aShape = aShapePtr->impl<TopoDS_Shape>();
444 aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
452 //********************************************************************
453 void ModuleBase_WidgetMultiSelector::getGeomSelection(const ModuleBase_ViewerPrs& thePrs,
454 ObjectPtr& theObject,
455 GeomShapePtr& theShape)
457 // DEBUG_THE_SAME_AS_SHAPE
458 theObject = myWorkshop->selection()->getResult(thePrs);
459 theShape = myWorkshop->selection()->getShape(thePrs);
462 //********************************************************************
463 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
465 myListControl->clear();
466 for (int i = 0; i < theList->size(); i++) {
467 AttributeSelectionPtr aAttr = theList->value(i);
468 myListControl->addItem(aAttr->namingName().c_str());
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::onListSelection()
510 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
511 myCopyAction->setEnabled(!aItems.isEmpty());