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