Salome HOME
An improvement to deselect a value in a shape selector control in the same way as...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetShapeSelector.h
4 // Created:     2 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include <ModuleBase_WidgetShapeSelector.h>
8 #include <ModuleBase_Definitions.h>
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_Tools.h>
13 #include <ModuleBase_FilterFactory.h>
14 #include <ModuleBase_Filter.h>
15
16 #include <Config_WidgetAPI.h>
17 #include <Events_Loop.h>
18 #include <Events_Message.h>
19 #include <GeomAPI_Interface.h>
20 #include <GeomAPI_Shape.h>
21 #include <GeomValidators_Tools.h>
22
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_Validator.h>
37 #include <ModelAPI_AttributeValidator.h>
38 #include <ModelAPI_ShapeValidator.h>
39
40 #include <Config_WidgetAPI.h>
41 #include <Events_Error.h>
42
43 #include <GeomAPI_Shape.h>
44
45 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopExp_Explorer.hxx>
48
49 #include <QWidget>
50 #include <QLayout>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QToolButton>
54 #include <QString>
55 #include <QEvent>
56 #include <QDockWidget>
57 #include <QApplication>
58 #include <QFormLayout>
59
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Shape.hxx>
62
63 #include <memory>
64
65 #include <list>
66 #include <string>
67
68
69 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
70                                                      ModuleBase_IWorkshop* theWorkshop,
71                                                      const Config_WidgetAPI* theData,
72                                                      const std::string& theParentId)
73     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
74       myWorkshop(theWorkshop)
75 {
76   QFormLayout* aLayout = new QFormLayout(this);
77   ModuleBase_Tools::adjustMargins(aLayout);
78
79   QString aLabelText = QString::fromStdString(theData->widgetLabel());
80   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
81   myLabel = new QLabel(aLabelText, this);
82   if (!aLabelIcon.isEmpty())
83     myLabel->setPixmap(QPixmap(aLabelIcon));
84
85
86   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
87   myTextLine = new QLineEdit(this);
88   myTextLine->setReadOnly(true);
89   myTextLine->setToolTip(aToolTip);
90   myTextLine->installEventFilter(this);
91
92   aLayout->addRow(myLabel, myTextLine);
93
94   std::string aTypes = theData->getProperty("shape_types");
95   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
96 }
97
98 //********************************************************************
99 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
100 {
101   activateSelection(false);
102   activateFilters(myWorkshop, false);
103 }
104
105 //********************************************************************
106 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
107 {
108   // the value is stored on the selection changed signal processing 
109   return true;
110 }
111
112 //********************************************************************
113 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
114                                                GeomShapePtr theShape)
115 {
116   bool isChanged = false;
117   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
118   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
119     return isChanged;
120
121   DataPtr aData = myFeature->data();
122   AttributeReferencePtr aRef = aData->reference(attributeID());
123   if (aRef) {
124     ObjectPtr aObject = aRef->value();
125     if (!(aObject && aObject->isSame(theSelectedObject))) {
126       aRef->setValue(theSelectedObject);
127       isChanged = true;
128     }
129   } else {
130     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
131     if (aRefAttr) {
132       ObjectPtr aObject = aRefAttr->object();
133       if (!(aObject && aObject->isSame(theSelectedObject))) {
134         aRefAttr->setObject(theSelectedObject);
135         isChanged = true;
136       }
137     } else {
138       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
139       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
140       if (aSelectAttr.get() != NULL) {
141         aSelectAttr->setValue(aResult, theShape);
142         isChanged = true;
143       }
144     }
145   }
146   return isChanged;
147 }
148
149 //********************************************************************
150 void ModuleBase_WidgetShapeSelector::clearAttribute()
151 {
152   DataPtr aData = myFeature->data();
153   AttributeSelectionPtr aSelect = aData->selection(attributeID());
154   if (aSelect) {
155     aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
156     return;
157   }
158   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
159   if (aRefAttr) {
160     aRefAttr->setObject(ObjectPtr());
161     return;
162   }
163   AttributeReferencePtr aRef = aData->reference(attributeID());
164   if (aRef) {
165     aRef->setObject(ObjectPtr());
166   }
167 }
168
169 //********************************************************************
170 bool ModuleBase_WidgetShapeSelector::restoreValue()
171 {
172   bool isBlocked = this->blockSignals(true);
173   updateSelectionName();
174   this->blockSignals(isBlocked);
175
176   return true;
177 }
178
179 //********************************************************************
180 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
181 {
182   QList<QWidget*> aControls;
183   aControls.append(myTextLine);
184   return aControls;
185 }
186
187 //********************************************************************
188 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
189 {
190   // In order to make reselection possible, set empty object and shape should be done
191   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
192
193   bool aHasObject = false;
194   QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(myWorkshop->selection());
195   if (!aSelectedPrs.empty()) {
196     ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
197     if (!aPrs.isEmpty() && isValidSelection(aPrs)) {
198       setSelectionCustom(aPrs);
199       aHasObject = true;
200     }
201   }
202   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
203   // calls validators for the feature and, as a result, updates the Apply button state.
204   updateObject(myFeature);
205   // the widget loses the focus only if the selected object is set
206   if (aHasObject)
207     emit focusOutWidget(this);
208 }
209
210 //********************************************************************
211 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
212 {
213   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
214   foreach (QString aType, myShapeTypes) {
215     if (aShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
216       return true;
217   }
218   return false;
219 }
220
221 //********************************************************************
222 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
223 {
224   GeomShapePtr aShape;
225   DataPtr aData = myFeature->data();
226   if (aData.get() == NULL)
227     return aShape;
228
229   AttributeSelectionPtr aSelect = aData->selection(attributeID());
230   if (aSelect)
231     aShape = aSelect->value();
232
233   return aShape;
234 }
235
236 //********************************************************************
237 void ModuleBase_WidgetShapeSelector::updateSelectionName()
238 {
239   DataPtr aData = myFeature->data();
240   if (aData.get() == NULL)
241     return;
242
243   bool isNameUpdated = false;
244   AttributeSelectionPtr aSelect = aData->selection(attributeID());
245   if (aSelect) {
246     myTextLine->setText(QString::fromStdString(aSelect->namingName()));
247     isNameUpdated = true;
248   }
249   if (!isNameUpdated) {
250     ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
251     if (anObject.get() != NULL) {
252       std::string aName = anObject->data()->name();
253       myTextLine->setText(QString::fromStdString(aName));
254     } else {
255       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
256       if (aRefAttr && aRefAttr->attr().get() != NULL) {
257         //myIsObject = aRefAttr->isObject();
258         AttributePtr anAttr = aRefAttr->attr();
259         if (anAttr.get() != NULL) {
260           std::stringstream aName;
261           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
262           myTextLine->setText(QString::fromStdString(aName.str()));
263         }
264       }
265       else {
266         myTextLine->setText("");
267       }
268     }
269   }
270 }
271
272
273 //********************************************************************
274 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
275 {
276   updateSelectionName();
277
278   if (toActivate) {
279     QIntList aList;
280     foreach (QString aType, myShapeTypes) {
281       aList.append(ModuleBase_Tools::shapeType(aType));
282     }
283     myWorkshop->activateSubShapesSelection(aList);
284   } else {
285     myWorkshop->deactivateSubShapesSelection();
286   }
287 }
288
289 //********************************************************************
290 void ModuleBase_WidgetShapeSelector::raisePanel() const
291 {
292   QWidget* aParent = this->parentWidget();
293   QWidget* aLastPanel = 0;
294   while (!aParent->inherits("QDockWidget")) {
295     aLastPanel = aParent;
296     aParent = aParent->parentWidget();
297     if (!aParent)
298       return;
299   }
300   if (aParent->inherits("QDockWidget")) {
301     QDockWidget* aTabWgt = (QDockWidget*) aParent;
302     aTabWgt->raise();
303   }
304 }
305
306 //********************************************************************
307 void ModuleBase_WidgetShapeSelector::activateCustom()
308 {
309   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
310   activateSelection(true);
311   // Restore selection in the viewer by the attribute selection list
312   QList<ModuleBase_ViewerPrs> aSelected;
313   if(myFeature) {
314     DataPtr aData = myFeature->data();
315     AttributePtr anAttribute = myFeature->attribute(attributeID());
316
317     ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
318     TopoDS_Shape aShape;
319     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
320     if (aShapePtr.get()) {
321       aShape = aShapePtr->impl<TopoDS_Shape>();
322     }
323     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
324     aSelected.append(aPrs);
325   }
326   myWorkshop->setSelected(aSelected);
327
328   activateFilters(myWorkshop, true);
329 }
330
331 //********************************************************************
332 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
333 {
334   DataPtr aData = myFeature->data();
335   AttributePtr anAttribute = myFeature->attribute(attributeID());
336
337   myObject = GeomValidators_Tools::getObject(anAttribute);
338   myShape = getShape();
339   myRefAttribute = AttributePtr();
340   myIsObject = false;
341   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
342   if (aRefAttr) {
343     myIsObject = aRefAttr->isObject();
344     myRefAttribute = aRefAttr->attr();
345   }
346 }
347
348 //********************************************************************
349 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
350 {
351   DataPtr aData = myFeature->data();
352   AttributePtr anAttribute = myFeature->attribute(attributeID());
353
354   setObject(myObject, myShape);
355   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
356   if (aRefAttr) {
357     if (!myIsObject)
358       aRefAttr->setAttr(myRefAttribute);
359   }
360 }
361
362 //********************************************************************
363 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
364 {
365   bool isDone = false;
366
367   // It should be checked by corresponded validator
368   ObjectPtr aObject = thePrs.object();
369   ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
370   /*
371   if ((!aCurrentObject) && (!aObject))
372     return false;*/
373
374   // It should be checked by corresponded validator
375   // Check that the selected object is result (others can not be accepted)
376   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
377   if (!aRes)
378     return false;
379   /*if (myFeature) {
380     // We can not select a result of our feature
381     const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
382     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
383     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
384       if ((*aIt) == aRes)
385         return false;
386     }
387   }
388   */
389   // It should be checked by corresponded validator
390   /*
391   // Check that object belongs to active document or PartSet
392   DocumentPtr aDoc = aRes->document();
393   SessionPtr aMgr = ModelAPI_Session::get();
394   if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
395     return false;*/
396
397   // It should be checked by corresponded validator
398   // Check that the result has a shape
399   GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
400   if (!aShape)
401     return false;
402
403   // Get sub-shapes from local selection
404   if (!thePrs.shape().IsNull()) {
405     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
406     aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
407   }
408   // Check that the selection corresponds to selection type
409   if (!acceptSubShape(aShape))
410     return false;
411
412   setObject(aObject, aShape);
413   return true;
414 }
415
416 //********************************************************************
417 void ModuleBase_WidgetShapeSelector::deactivate()
418 {
419   activateSelection(false);
420   activateFilters(myWorkshop, false);
421   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
422 }