Salome HOME
12cb9898040bc2dcb62d8399faca6c57c945a1fb
[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       AttributeSelectionPtr aSelectAttr = 
139         std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aAttr);
140       if (aSelectAttr)
141         aSelectAttr->setValue(aBody, myShape);
142       else {
143         AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
144         if (aRefAttr) 
145           aRefAttr->setObject(mySelectedObject);
146       }
147       updateObject(myFeature);
148       return true;
149     }
150   } else {
151     AttributeReferencePtr aRef = aData->reference(attributeID());
152     if (aRef) {
153       ObjectPtr aObject = aRef->value();
154       if (!(aObject && aObject->isSame(mySelectedObject))) {
155         aRef->setValue(mySelectedObject);
156         updateObject(myFeature);
157         return true;
158       }
159     } else {
160       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
161       if (aRefAttr) {
162         ObjectPtr aObject = aRefAttr->object();
163         if (!(aObject && aObject->isSame(mySelectedObject))) {
164           aRefAttr->setObject(mySelectedObject);
165           updateObject(myFeature);
166           return true;
167         }
168       }
169     }
170   }
171   return false;
172 }
173
174 //********************************************************************
175 bool ModuleBase_WidgetShapeSelector::restoreValue()
176 {
177   DataPtr aData = myFeature->data();
178   bool isBlocked = this->blockSignals(true);
179   if (myUseSubShapes) {
180     AttributeSelectionPtr aSelect = aData->selection(attributeID());
181     if (aSelect) {
182       mySelectedObject = aSelect->context();
183       myShape = aSelect->value();
184     }
185   } else {
186     AttributeReferencePtr aRef = aData->reference(attributeID());
187     if (aRef)
188       mySelectedObject = aRef->value();
189     else {
190       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
191       if (aRefAttr)
192         mySelectedObject = aRefAttr->object();
193     }
194   }
195   updateSelectionName();
196
197   this->blockSignals(isBlocked);
198   return true;
199 }
200
201 //********************************************************************
202 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
203 {
204   QList<QWidget*> aControls;
205   aControls.append(myTextLine);
206   return aControls;
207 }
208
209 //********************************************************************
210 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
211 {
212   QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
213   if (aObjects.size() > 0) {
214     ObjectPtr aObject = aObjects.first();
215     if ((!mySelectedObject) && (!aObject))
216       return;
217
218     // Check that the selected object is result (others can not be accepted)
219     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
220     if (!aRes)
221       return;
222
223     if (myFeature) {
224       // We can not select a result of our feature
225       const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
226       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
227       for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
228         if ((*aIt) == aRes)
229           return;
230       }
231     }
232     // Check that object belongs to active document or PartSet
233     DocumentPtr aDoc = aRes->document();
234     SessionPtr aMgr = ModelAPI_Session::get();
235     if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
236       return;
237
238     // Check that the result has a shape
239     GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
240     if (!aShape)
241       return;
242
243     /// Check that object has acceptable type
244     if (!acceptObjectType(aObject)) 
245       return;
246
247     // Get sub-shapes from local selection
248     if (myUseSubShapes) {
249       NCollection_List<TopoDS_Shape> aShapeList;
250       std::list<ObjectPtr> aOwners;
251       myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
252       if (aShapeList.Extent() > 0) {
253         aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
254         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
255       }
256     }
257
258     // Check that the selection corresponds to selection type
259     if (myUseSubShapes) {
260       if (!acceptSubShape(aShape))
261         return;
262     } else {
263       if (!acceptObjectShape(aObject))
264         return;
265     }
266     setObject(aObject, aShape);
267     emit focusOutWidget(this);
268   }
269 }
270
271 //********************************************************************
272 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
273 {
274   mySelectedObject = theObj;
275   myShape = theShape;
276   if (mySelectedObject) {
277     raisePanel();
278   } 
279   updateSelectionName();
280   emit valuesChanged();
281 }
282
283 //********************************************************************
284 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
285 {
286   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
287
288   // Check that the shape of necessary type
289   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
290   if (!aShapePtr)
291     return false;
292   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
293   if (aShape.IsNull())
294     return false;
295
296   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
297   if (aShapeType == TopAbs_COMPOUND) {
298     foreach (QString aType, myShapeTypes) {
299       TopExp_Explorer aEx(aShape, shapeType(aType));
300       if (aEx.More())
301         return true;
302     }
303   } else {
304     foreach (QString aType, myShapeTypes) {
305       if (shapeType(aType) == aShapeType)
306         return true;
307     }
308   }
309   return false;
310 }
311
312 //********************************************************************
313 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
314 {
315   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
316   foreach (QString aType, myShapeTypes) {
317     if (aShape.ShapeType() == shapeType(aType))
318       return true;
319   }
320   return false;
321 }
322
323 //********************************************************************
324 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
325 {
326   // Definition of types is not obligatory. If types are not defined then
327   // it means that accepted any type
328   if (myObjectTypes.isEmpty())
329     return true;
330
331   foreach (QString aType, myObjectTypes) {
332     if (aType.toLower() == "construction") {
333       ResultConstructionPtr aConstr = 
334         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
335       return (aConstr != NULL);
336     } // ToDo: Process other types of objects
337   }
338   // Object type is defined but not found
339   return false;
340 }
341
342
343 //********************************************************************
344 void ModuleBase_WidgetShapeSelector::updateSelectionName()
345 {
346   if (mySelectedObject) {
347     std::string aName = mySelectedObject->data()->name();
348     myTextLine->setText(QString::fromStdString(aName));
349   } else {
350     if (myIsActive) {
351       myTextLine->setText("");
352     }
353   }
354 }
355
356
357 //********************************************************************
358 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
359 {
360   if (myIsActive == toActivate)
361     return;
362   myIsActive = toActivate;
363   updateSelectionName();
364
365   if (myIsActive) {
366     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
367     if (myUseSubShapes) {
368
369       QIntList aList;
370       foreach (QString aType, myShapeTypes)
371         aList.append(shapeType(aType));
372       myWorkshop->activateSubShapesSelection(aList);
373       if (!myObjectTypes.isEmpty()) {
374         myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
375         myWorkshop->viewer()->clearSelectionFilters();
376         myWorkshop->viewer()->addSelectionFilter(myObjTypeFilter);
377       }
378     }
379   } else {
380     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
381     if (myUseSubShapes) {
382       if (!myObjTypeFilter.IsNull()) {
383         myWorkshop->viewer()->removeSelectionFilter(myObjTypeFilter);
384         myObjTypeFilter.Nullify();
385       }
386       myWorkshop->deactivateSubShapesSelection();
387     }
388   }
389 }
390
391 //********************************************************************
392 void ModuleBase_WidgetShapeSelector::raisePanel() const
393 {
394   QWidget* aParent = myContainer->parentWidget();
395   QWidget* aLastPanel = 0;
396   while (!aParent->inherits("QDockWidget")) {
397     aLastPanel = aParent;
398     aParent = aParent->parentWidget();
399     if (!aParent)
400       return;
401   }
402   if (aParent->inherits("QDockWidget")) {
403     QDockWidget* aTabWgt = (QDockWidget*) aParent;
404     aTabWgt->raise();
405   }
406 }
407
408 //********************************************************************
409 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
410 {
411   if (theValue) {
412     ModuleBase_WidgetValueFeature* aFeatureValue =
413         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
414     if (aFeatureValue && aFeatureValue->object()) {
415       ObjectPtr aObject = aFeatureValue->object();
416       if (acceptObjectShape(aObject)) {
417         setObject(aObject);
418         return true;
419       }
420     }
421   }
422   return false;
423 }
424
425 //********************************************************************
426 void ModuleBase_WidgetShapeSelector::activate()
427 {
428   activateSelection(true);
429 }
430
431 //********************************************************************
432 void ModuleBase_WidgetShapeSelector::deactivate()
433 {
434   activateSelection(false);
435 }