Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom.git into Dev_0.6
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // File:        ModuleBase_WidgetShapeSelector.h
2 // Created:     2 June 2014
3 // Author:      Vitaly Smetannikov
4
5 #include <ModuleBase_WidgetShapeSelector.h>
6 #include <ModuleBase_Definitions.h>
7 #include <ModuleBase_ISelection.h>
8 #include <ModuleBase_IWorkshop.h>
9 #include <ModuleBase_IViewer.h>
10 #include <ModuleBase_Tools.h>
11 #include <ModuleBase_WidgetValueFeature.h>
12
13 #include <Config_WidgetAPI.h>
14 #include <Events_Loop.h>
15 #include <Events_Message.h>
16 #include <GeomAPI_Interface.h>
17 #include <GeomAPI_Shape.h>
18
19 #include <ModelAPI_AttributeReference.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Feature.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_ResultConstruction.h>
26 #include <ModelAPI_AttributeReference.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30 #include <ModelAPI_ResultBody.h>
31 #include <ModelAPI_AttributeRefAttr.h>
32
33 #include <Config_WidgetAPI.h>
34 #include <Events_Error.h>
35
36 #include <GeomAPI_Shape.h>
37
38 #include <TopoDS_Shape.hxx>
39 #include <TopExp_Explorer.hxx>
40
41 #include <QWidget>
42 #include <QLayout>
43 #include <QLabel>
44 #include <QLineEdit>
45 #include <QToolButton>
46 #include <QString>
47 #include <QEvent>
48 #include <QDockWidget>
49 #include <QApplication>
50
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Shape.hxx>
53
54 #include <memory>
55
56 #include <list>
57 #include <string>
58
59 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
60 static ShapeTypes MyShapeTypes;
61
62 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
63 {
64   if (MyShapeTypes.count() == 0) {
65     MyShapeTypes["face"] = TopAbs_FACE;
66     MyShapeTypes["faces"] = TopAbs_FACE;
67     MyShapeTypes["vertex"] = TopAbs_VERTEX;
68     MyShapeTypes["vertices"] = TopAbs_VERTEX;
69     MyShapeTypes["wire"] = TopAbs_WIRE;
70     MyShapeTypes["edge"] = TopAbs_EDGE;
71     MyShapeTypes["edges"] = TopAbs_EDGE;
72     MyShapeTypes["shell"] = TopAbs_SHELL;
73     MyShapeTypes["solid"] = TopAbs_SOLID;
74     MyShapeTypes["solids"] = TopAbs_SOLID;
75   }
76   QString aType = theType.toLower();
77   if (MyShapeTypes.contains(aType))
78     return MyShapeTypes[aType];
79   Events_Error::send("Shape type defined in XML is not implemented!");
80   return TopAbs_SHAPE;
81 }
82
83 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
84                                                      ModuleBase_IWorkshop* theWorkshop,
85                                                      const Config_WidgetAPI* theData,
86                                                      const std::string& theParentId)
87     : ModuleBase_ModelWidget(theParent, theData, theParentId),
88       myWorkshop(theWorkshop), myIsActive(false), myUseSubShapes(false)
89 {
90   myContainer = new QWidget(theParent);
91   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
92   ModuleBase_Tools::adjustMargins(aLayout);
93
94   QString aLabelText = QString::fromStdString(theData->widgetLabel());
95   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
96   myLabel = new QLabel(aLabelText, myContainer);
97   if (!aLabelIcon.isEmpty())
98     myLabel->setPixmap(QPixmap(aLabelIcon));
99
100   aLayout->addWidget(myLabel);
101
102   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
103   myTextLine = new QLineEdit(myContainer);
104   myTextLine->setReadOnly(true);
105   myTextLine->setToolTip(aToolTip);
106   myTextLine->installEventFilter(this);
107
108   aLayout->addWidget(myTextLine, 1);
109
110   std::string aTypes = theData->getProperty("shape_types");
111   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
112
113   std::string aObjTypes = theData->getProperty("object_types");
114   myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
115
116   myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); 
117 }
118
119 //********************************************************************
120 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
121 {
122   activateSelection(false);
123 }
124
125 //********************************************************************
126 bool ModuleBase_WidgetShapeSelector::storeValue() const
127 {
128   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
129   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
130     return false;
131
132   DataPtr aData = myFeature->data();
133   if (myUseSubShapes) {
134     ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
135     if (aBody) {
136       AttributePtr aAttr = aData->attribute(attributeID());
137
138       // We have to check several attributes types
139       AttributeSelectionPtr aSelectAttr = 
140         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
141       if (aSelectAttr) {
142         aSelectAttr->setValue(aBody, myShape);
143         updateObject(myFeature);
144         return true;
145       } else {
146         AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
147         if (aRefAttr) {
148           aRefAttr->setObject(mySelectedObject);
149           updateObject(myFeature);
150           return true;
151         }
152       }
153     }
154   } else {
155     AttributeReferencePtr aRef = aData->reference(attributeID());
156     if (aRef) {
157       ObjectPtr aObject = aRef->value();
158       if (!(aObject && aObject->isSame(mySelectedObject))) {
159         aRef->setValue(mySelectedObject);
160         updateObject(myFeature);
161         return true;
162       }
163     } else {
164       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
165       if (aRefAttr) {
166         ObjectPtr aObject = aRefAttr->object();
167         if (!(aObject && aObject->isSame(mySelectedObject))) {
168           aRefAttr->setObject(mySelectedObject);
169           updateObject(myFeature);
170           return true;
171         }
172       }
173     }
174   }
175   return false;
176 }
177
178 //********************************************************************
179 bool ModuleBase_WidgetShapeSelector::restoreValue()
180 {
181   DataPtr aData = myFeature->data();
182   bool isBlocked = this->blockSignals(true);
183   if (myUseSubShapes) {
184     AttributeSelectionPtr aSelect = aData->selection(attributeID());
185     if (aSelect) {
186       mySelectedObject = aSelect->context();
187       myShape = aSelect->value();
188     } else {
189       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
190       if (aRefAttr) {
191         mySelectedObject = aRefAttr->object();
192       }
193     }
194   } else {
195     AttributeReferencePtr aRef = aData->reference(attributeID());
196     if (aRef)
197       mySelectedObject = aRef->value();
198     else {
199       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
200       if (aRefAttr)
201         mySelectedObject = aRefAttr->object();
202     }
203   }
204   updateSelectionName();
205
206   this->blockSignals(isBlocked);
207   return true;
208 }
209
210 //********************************************************************
211 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
212 {
213   QList<QWidget*> aControls;
214   aControls.append(myTextLine);
215   return aControls;
216 }
217
218 //********************************************************************
219 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
220 {
221   QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
222   if (aObjects.size() > 0) {
223     ObjectPtr aObject = aObjects.first();
224     if ((!mySelectedObject) && (!aObject))
225       return;
226
227     // Check that the selected object is result (others can not be accepted)
228     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
229     if (!aRes)
230       return;
231
232     if (myFeature) {
233       // We can not select a result of our feature
234       const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
235       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
236       for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
237         if ((*aIt) == aRes)
238           return;
239       }
240     }
241     // Check that object belongs to active document or PartSet
242     DocumentPtr aDoc = aRes->document();
243     SessionPtr aMgr = ModelAPI_Session::get();
244     if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
245       return;
246
247     // Check that the result has a shape
248     GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
249     if (!aShape)
250       return;
251
252     /// Check that object has acceptable type
253     if (!acceptObjectType(aObject)) 
254       return;
255
256     // Get sub-shapes from local selection
257     if (myUseSubShapes) {
258       NCollection_List<TopoDS_Shape> aShapeList;
259       std::list<ObjectPtr> aOwners;
260       myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
261       if (aShapeList.Extent() > 0) {
262         aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
263         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
264       }
265     }
266
267     // Check that the selection corresponds to selection type
268     if (myUseSubShapes) {
269       if (!acceptSubShape(aShape))
270         return;
271     } else {
272       if (!acceptObjectShape(aObject))
273         return;
274     }
275     setObject(aObject, aShape);
276     emit focusOutWidget(this);
277   }
278 }
279
280 //********************************************************************
281 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
282 {
283   mySelectedObject = theObj;
284   myShape = theShape;
285   if (mySelectedObject) {
286     raisePanel();
287   } 
288   updateSelectionName();
289   emit valuesChanged();
290 }
291
292 //********************************************************************
293 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
294 {
295   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
296
297   // Check that the shape of necessary type
298   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
299   if (!aShapePtr)
300     return false;
301   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
302   if (aShape.IsNull())
303     return false;
304
305   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
306   if (aShapeType == TopAbs_COMPOUND) {
307     foreach (QString aType, myShapeTypes) {
308       TopExp_Explorer aEx(aShape, shapeType(aType));
309       if (aEx.More())
310         return true;
311     }
312   } else {
313     foreach (QString aType, myShapeTypes) {
314       if (shapeType(aType) == aShapeType)
315         return true;
316     }
317   }
318   return false;
319 }
320
321 //********************************************************************
322 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
323 {
324   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
325   foreach (QString aType, myShapeTypes) {
326     if (aShape.ShapeType() == shapeType(aType))
327       return true;
328   }
329   return false;
330 }
331
332 //********************************************************************
333 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
334 {
335   // Definition of types is not obligatory. If types are not defined then
336   // it means that accepted any type
337   if (myObjectTypes.isEmpty())
338     return true;
339
340   foreach (QString aType, myObjectTypes) {
341     if (aType.toLower() == "construction") {
342       ResultConstructionPtr aConstr = 
343         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
344       return (aConstr != NULL);
345     } // ToDo: Process other types of objects
346   }
347   // Object type is defined but not found
348   return false;
349 }
350
351
352 //********************************************************************
353 void ModuleBase_WidgetShapeSelector::updateSelectionName()
354 {
355   if (mySelectedObject) {
356     std::string aName = mySelectedObject->data()->name();
357     myTextLine->setText(QString::fromStdString(aName));
358   } else {
359     if (myIsActive) {
360       myTextLine->setText("");
361     }
362   }
363 }
364
365
366 //********************************************************************
367 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
368 {
369   if (myIsActive == toActivate)
370     return;
371   myIsActive = toActivate;
372   updateSelectionName();
373
374   if (myIsActive) {
375     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
376     if (myUseSubShapes) {
377
378       QIntList aList;
379       foreach (QString aType, myShapeTypes)
380         aList.append(shapeType(aType));
381       myWorkshop->activateSubShapesSelection(aList);
382       if (!myObjectTypes.isEmpty()) {
383         myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
384         myWorkshop->viewer()->clearSelectionFilters();
385         myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
386       }
387     }
388   } else {
389     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
390     if (myUseSubShapes) {
391       if (!myObjTypeFilter.IsNull()) {
392         myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
393         myObjTypeFilter.Nullify();
394       }
395       myWorkshop->deactivateSubShapesSelection();
396     }
397   }
398 }
399
400 //********************************************************************
401 void ModuleBase_WidgetShapeSelector::raisePanel() const
402 {
403   QWidget* aParent = myContainer->parentWidget();
404   QWidget* aLastPanel = 0;
405   while (!aParent->inherits("QDockWidget")) {
406     aLastPanel = aParent;
407     aParent = aParent->parentWidget();
408     if (!aParent)
409       return;
410   }
411   if (aParent->inherits("QDockWidget")) {
412     QDockWidget* aTabWgt = (QDockWidget*) aParent;
413     aTabWgt->raise();
414   }
415 }
416
417 //********************************************************************
418 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
419 {
420   if (theValue) {
421     ModuleBase_WidgetValueFeature* aFeatureValue =
422         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
423     if (aFeatureValue && aFeatureValue->object()) {
424       ObjectPtr aObject = aFeatureValue->object();
425       if (acceptObjectShape(aObject)) {
426         setObject(aObject);
427         return true;
428       }
429     }
430   }
431   return false;
432 }
433
434 //********************************************************************
435 void ModuleBase_WidgetShapeSelector::activate()
436 {
437   activateSelection(true);
438 }
439
440 //********************************************************************
441 void ModuleBase_WidgetShapeSelector::deactivate()
442 {
443   activateSelection(false);
444 }