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