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 ModuleBase_WidgetMultiSelector::ModuleBase_WidgetMultiSelector(QWidget* theParent,
40 ModuleBase_IWorkshop* theWorkshop,
41 const Config_WidgetAPI* theData,
42 const std::string& theParentId)
43 : ModuleBase_WidgetValidated(theParent, theData, theParentId),
44 myWorkshop(theWorkshop)
46 QGridLayout* aMainLay = new QGridLayout(this);
47 ModuleBase_Tools::adjustMargins(aMainLay);
49 QLabel* aTypeLabel = new QLabel(tr("Type"), this);
50 aMainLay->addWidget(aTypeLabel, 0, 0);
52 myTypeCombo = new QComboBox(this);
53 // There is no sence to paramerize list of types while we can not parametrize selection mode
55 myShapeValidator = new GeomValidators_ShapeType();
57 std::string aPropertyTypes = theData->getProperty("type_choice");
58 QString aTypesStr = aPropertyTypes.c_str();
59 QStringList aShapeTypes = aTypesStr.split(' ');
61 //myIsUseChoice = theData->getBooleanAttribute("use_choice", true);
63 myTypeCombo->addItems(aShapeTypes);
64 aMainLay->addWidget(myTypeCombo, 0, 1);
65 // if the xml definition contains one type, the controls to select a type should not be shown
66 if (aShapeTypes.size() == 1/* || !myIsUseChoice*/) {
67 aTypeLabel->setVisible(false);
68 myTypeCombo->setVisible(false);
71 QLabel* aListLabel = new QLabel(tr("Selected objects:"), this);
72 aMainLay->addWidget(aListLabel, 1, 0);
73 // if the xml definition contains one type, an information label should be shown near to the latest
74 if (aShapeTypes.size() == 1) {
75 QString aLabelText = QString::fromStdString(theData->widgetLabel());
76 QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
77 QLabel* aSelectedLabel = new QLabel(aLabelText, this);
78 if (!aLabelIcon.isEmpty())
79 aSelectedLabel->setPixmap(QPixmap(aLabelIcon));
80 aMainLay->addWidget(aSelectedLabel, 1, 1);
81 aMainLay->setColumnStretch(2, 1);
84 myListControl = new QListWidget(this);
85 aMainLay->addWidget(myListControl, 2, 0, 2, -1);
86 aMainLay->setRowStretch(2, 1);
87 aMainLay->addWidget(new QLabel(this)); //FIXME(sbh)???
88 aMainLay->setRowMinimumHeight(3, 20);
89 this->setLayout(aMainLay);
90 connect(myTypeCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(onSelectionTypeChanged()));
92 myCopyAction = new QAction(QIcon(":pictures/copy.png"), tr("Copy"), this);
93 myCopyAction->setShortcut(QKeySequence::Copy);
94 myCopyAction->setEnabled(false);
95 connect(myCopyAction, SIGNAL(triggered(bool)), SLOT(onCopyItem()));
96 myListControl->addAction(myCopyAction);
97 myListControl->setContextMenuPolicy(Qt::ActionsContextMenu);
98 connect(myListControl, SIGNAL(itemSelectionChanged()), SLOT(onListSelection()));
101 ModuleBase_WidgetMultiSelector::~ModuleBase_WidgetMultiSelector()
103 activateShapeSelection(false);
104 activateFilters(myWorkshop, false);
106 delete myShapeValidator;
109 //TODO: nds stabilization hotfix
110 void ModuleBase_WidgetMultiSelector::disconnectSignals()
112 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
115 //********************************************************************
116 void ModuleBase_WidgetMultiSelector::activateCustom()
118 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
119 connect(myWorkshop, SIGNAL(selectionChanged()),
120 this, SLOT(onSelectionChanged()),
121 Qt::UniqueConnection);
123 activateShapeSelection(true);
125 // Restore selection in the viewer by the attribute selection list
126 myWorkshop->setSelected(getAttributeSelection());
128 activateFilters(myWorkshop, true);
131 //********************************************************************
132 void ModuleBase_WidgetMultiSelector::deactivate()
134 disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
135 activateShapeSelection(false);
136 activateFilters(myWorkshop, false);
139 //********************************************************************
140 bool ModuleBase_WidgetMultiSelector::storeValueCustom() const
142 // the value is stored on the selection changed signal processing
143 // A rare case when plugin was not loaded.
146 DataPtr aData = myFeature->data();
147 AttributeSelectionListPtr aSelectionListAttr =
148 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
150 if (aSelectionListAttr) {
152 TopAbs_ShapeEnum aCurrentType =
153 ModuleBase_Tools::shapeType(myTypeCombo->currentText());
154 aSelectionListAttr->setSelectionType(myTypeCombo->currentText().toStdString());
159 //********************************************************************
160 bool ModuleBase_WidgetMultiSelector::restoreValue()
162 // A rare case when plugin was not loaded.
165 DataPtr aData = myFeature->data();
166 AttributeSelectionListPtr aSelectionListAttr =
167 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
169 if (aSelectionListAttr) {
170 // Restore shape type
171 if (!aSelectionListAttr->selectionType().empty())
172 setCurrentShapeType(ModuleBase_Tools::shapeType(aSelectionListAttr->selectionType().c_str()));
173 updateSelectionList(aSelectionListAttr);
179 //********************************************************************
180 void ModuleBase_WidgetMultiSelector::storeAttributeValue()
182 DataPtr aData = myFeature->data();
183 AttributeSelectionListPtr aSelectionListAttr =
184 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
185 if (aSelectionListAttr.get() == NULL)
188 mySelectionType = aSelectionListAttr->selectionType();
190 int aSize = aSelectionListAttr->size();
191 for (int i = 0; i < aSelectionListAttr->size(); i++) {
192 AttributeSelectionPtr aSelectAttr = aSelectionListAttr->value(i);
193 mySelection.append(GeomSelection(aSelectAttr->context(), aSelectAttr->value()));
197 //********************************************************************
198 void ModuleBase_WidgetMultiSelector::restoreAttributeValue(bool/* theValid*/)
200 DataPtr aData = myFeature->data();
201 AttributeSelectionListPtr aSelectionListAttr =
202 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
203 if (aSelectionListAttr.get() == NULL)
205 aSelectionListAttr->clear();
208 aSelectionListAttr->setSelectionType(mySelectionType);
210 // Store selection in the attribute
211 int aSize = mySelection.size();
212 foreach (GeomSelection aSelec, mySelection) {
213 aSelectionListAttr->append(aSelec.first, aSelec.second);
217 //********************************************************************
218 void ModuleBase_WidgetMultiSelector::customValidators(
219 std::list<ModelAPI_Validator*>& theValidators,
220 std::list<std::list<std::string> >& theArguments) const
223 std::list<std::string> anArguments;
225 theValidators.push_back(myShapeValidator);
226 if (true/*myIsUseChoice*/) {
227 QString aType = myTypeCombo->currentText();
228 anArguments.push_back(validatorType(aType));
231 for(int i = 0, aCount = myTypeCombo->count(); i < aCount; i++) {
232 anArguments.push_back(validatorType(myTypeCombo->itemText(i)));
235 theArguments.push_back(anArguments);
238 //********************************************************************
239 bool ModuleBase_WidgetMultiSelector::acceptSubShape(const TopoDS_Shape& theShape) const
242 if (theShape.IsNull()) {
243 aValid = true; // do not check the shape type if the shape is empty
244 // extrusion uses a sketch object selectected in Object browser
248 TopAbs_ShapeEnum aShapeType = theShape.ShapeType();
249 if (myTypeCombo->count() > 1) {
250 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->currentText());
251 aValid = aShapeType == aType;
254 for(int i = 0, aCount = myTypeCombo->count(); i < aCount && !aValid; i++) {
255 TopAbs_ShapeEnum aType = ModuleBase_Tools::shapeType(myTypeCombo->itemText(i));
256 aValid = aShapeType == aType;
263 //********************************************************************
264 bool ModuleBase_WidgetMultiSelector::setSelection(const QList<ModuleBase_ViewerPrs>& theValues,
270 QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
272 for (int i = thePosition; i < theValues.size(); i++) {
273 ModuleBase_ViewerPrs aValue = theValues[i];
274 bool aProcessed = false;
275 if (isValidSelection(aValue)) {
276 aProcessed = setSelectionCustom(aValue);
278 // if there is at least one set, the result is true
279 isDone = isDone || aProcessed;
280 // when an object, which do not satisfy the validating process, stop set selection
287 updateObject(myFeature);
288 // this emit is necessary to call store/restore method an restore type of selection
289 emit valuesChanged();
294 //********************************************************************
295 bool ModuleBase_WidgetMultiSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
297 TopoDS_Shape aShape = thePrs.shape();
298 if (!acceptSubShape(aShape))
302 if (!thePrs.owner().IsNull()) {
303 ObjectPtr anObject = myWorkshop->selection()->getSelectableObject(thePrs.owner());
304 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
307 aResult = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
312 // We can not select a result of our feature
313 const std::list<ResultPtr>& aResList = myFeature->results();
314 std::list<ResultPtr>::const_iterator aIt;
315 bool isSkipSelf = false;
316 for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
317 if ((*aIt) == aResult) {
326 // if the result has the similar shap as the parameter shape, just the context is set to the
327 // selection list attribute.
328 DataPtr aData = myFeature->data();
329 AttributeSelectionListPtr aSelectionListAttr =
330 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
332 const TopoDS_Shape& aTDSShape = thePrs.shape();
333 // if only result is selected, an empty shape is set to the model
334 if (aTDSShape.IsNull()) {
335 aSelectionListAttr->append(aResult, GeomShapePtr());
338 GeomShapePtr aShape(new GeomAPI_Shape());
339 aShape->setImpl(new TopoDS_Shape(aTDSShape));
340 // We can not select a result of our feature
341 if (aShape->isEqual(aResult->shape()))
342 aSelectionListAttr->append(aResult, GeomShapePtr());
344 aSelectionListAttr->append(aResult, aShape);
349 //********************************************************************
350 QList<QWidget*> ModuleBase_WidgetMultiSelector::getControls() const
352 QList<QWidget*> result;
353 //result << myTypeCombo;
354 result << myListControl;
358 //********************************************************************
359 bool ModuleBase_WidgetMultiSelector::eventFilter(QObject* theObj, QEvent* theEvent)
361 //TODO: Remove maybe?
362 return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
365 //********************************************************************
366 void ModuleBase_WidgetMultiSelector::onSelectionTypeChanged()
368 activateShapeSelection(true);
369 activateFilters(myWorkshop, true);
370 QList<ModuleBase_ViewerPrs> anEmptyList;
371 // This method will call Selection changed event which will call onSelectionChanged
372 // To clear mySelection, myListControl and storeValue()
373 // So, we don't need to call it
374 myWorkshop->setSelected(anEmptyList);
377 //********************************************************************
378 void ModuleBase_WidgetMultiSelector::onSelectionChanged()
380 QList<ModuleBase_ViewerPrs> aSelected = getSelectedEntitiesOrObjects(myWorkshop->selection());
382 DataPtr aData = myFeature->data();
383 AttributeSelectionListPtr aSelectionListAttr =
384 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
386 aSelectionListAttr->clear();
387 if (aSelected.size() > 0) {
388 foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
389 if (isValidSelection(aPrs)) {
390 setSelectionCustom(aPrs);
394 emit valuesChanged();
395 // the updateObject method should be called to flush the updated sigal. The workshop listens it,
396 // calls validators for the feature and, as a result, updates the Apply button state.
397 updateObject(myFeature);
399 // Set focus to List control in order to make possible
400 // to use Tab key for transfer the focus to next widgets
401 myListControl->setCurrentRow(myListControl->model()->rowCount() - 1);
402 myListControl->setFocus();
405 //********************************************************************
406 void ModuleBase_WidgetMultiSelector::setCurrentShapeType(const TopAbs_ShapeEnum theShapeType)
408 QString aShapeTypeName;
410 for (int idx = 0; idx < myTypeCombo->count(); ++idx) {
411 aShapeTypeName = myTypeCombo->itemText(idx);
412 TopAbs_ShapeEnum aRefType = ModuleBase_Tools::shapeType(aShapeTypeName);
413 if(aRefType == theShapeType && idx != myTypeCombo->currentIndex()) {
414 activateShapeSelection(false);
415 activateFilters(myWorkshop, false);
416 bool isBlocked = myTypeCombo->blockSignals(true);
417 myTypeCombo->setCurrentIndex(idx);
418 myTypeCombo->blockSignals(isBlocked);
420 activateShapeSelection(true);
421 activateFilters(myWorkshop, true);
427 void ModuleBase_WidgetMultiSelector::activateShapeSelection(const bool isActivated)
429 ModuleBase_IViewer* aViewer = myWorkshop->viewer();
432 QString aNewType = myTypeCombo->currentText();
434 if (true /*myIsUseChoice*/) {
435 aList.append(ModuleBase_Tools::shapeType(aNewType));
438 for(int i = 0, aCount = myTypeCombo->count(); i < aCount; i++)
439 aList.append(ModuleBase_Tools::shapeType(myTypeCombo->itemText(i)));
441 myWorkshop->activateSubShapesSelection(aList);
443 myWorkshop->deactivateSubShapesSelection();
447 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetMultiSelector::getAttributeSelection() const
449 QList<ModuleBase_ViewerPrs> aSelected;
450 // Restore selection in the viewer by the attribute selection list
452 DataPtr aData = myFeature->data();
453 AttributeSelectionListPtr aListAttr =
454 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(aData->attribute(attributeID()));
456 for (int i = 0; i < aListAttr->size(); i++) {
457 AttributeSelectionPtr anAttr = aListAttr->value(i);
458 ResultPtr anObject = anAttr->context();
459 if (anObject.get()) {
461 std::shared_ptr<GeomAPI_Shape> aShapePtr = anAttr->value();
462 if (aShapePtr.get()) {
463 aShape = aShapePtr->impl<TopoDS_Shape>();
465 aSelected.append(ModuleBase_ViewerPrs(anObject, aShape, NULL));
473 //********************************************************************
474 void ModuleBase_WidgetMultiSelector::updateSelectionList(AttributeSelectionListPtr theList)
476 myListControl->clear();
477 for (int i = 0; i < theList->size(); i++) {
478 AttributeSelectionPtr aAttr = theList->value(i);
479 myListControl->addItem(aAttr->namingName().c_str());
481 // We have to call repaint because sometimes the List control is not updated
482 myListControl->repaint();
485 //********************************************************************
486 std::string ModuleBase_WidgetMultiSelector::validatorType(const QString& theType) const
490 if (theType == "Vertices")
492 else if (theType == "Edges")
494 else if (theType == "Faces")
496 else if (theType == "Solids")
502 //********************************************************************
503 void ModuleBase_WidgetMultiSelector::onCopyItem()
505 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
507 foreach(QListWidgetItem* aItem, aItems) {
510 aRes += aItem->text();
512 if (!aRes.isEmpty()) {
513 QClipboard *clipboard = QApplication::clipboard();
514 clipboard->setText(aRes);
518 //********************************************************************
519 void ModuleBase_WidgetMultiSelector::onListSelection()
521 QList<QListWidgetItem*> aItems = myListControl->selectedItems();
522 myCopyAction->setEnabled(!aItems.isEmpty());