Salome HOME
Merge branch 'master' of newgeom:newgeom
[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   myBasePalet = myTextLine->palette();
105   myInactivePalet = myBasePalet;
106   myInactivePalet.setBrush(QPalette::Base, QBrush(Qt::gray, Qt::Dense6Pattern));
107   myTextLine->setPalette(myInactivePalet);
108
109   aLayout->addWidget(myTextLine, 1);
110
111   std::string aTypes = theData->getProperty("shape_types");
112   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
113
114   std::string aObjTypes = theData->getProperty("object_types");
115   myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
116
117   myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); 
118 }
119
120 //********************************************************************
121 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
122 {
123   activateSelection(false);
124 }
125
126 //********************************************************************
127 bool ModuleBase_WidgetShapeSelector::storeValue() const
128 {
129   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
130   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
131     return false;
132
133   DataPtr aData = myFeature->data();
134   if (myUseSubShapes) {
135     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = 
136       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
137
138     ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
139     if (aBody) {
140       aSelect->setValue(aBody, myShape);
141       updateObject(myFeature);
142       return true;
143     }
144   } else {
145     boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
146       boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
147
148     ObjectPtr aObject = aRef->value();
149     if (!(aObject && aObject->isSame(mySelectedObject))) {
150       aRef->setValue(mySelectedObject);
151       updateObject(myFeature);
152       return true;
153     }
154   }
155   return false;
156 }
157
158 //********************************************************************
159 bool ModuleBase_WidgetShapeSelector::restoreValue()
160 {
161   DataPtr aData = myFeature->data();
162   bool isBlocked = this->blockSignals(true);
163   if (myUseSubShapes) {
164     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = aData->selection(attributeID());
165     if (aSelect) {
166       mySelectedObject = aSelect->context();
167       myShape = aSelect->value();
168     }
169   } else {
170     boost::shared_ptr<ModelAPI_AttributeReference> aRef = aData->reference(attributeID());
171     mySelectedObject = aRef->value();
172   }
173   updateSelectionName();
174
175   this->blockSignals(isBlocked);
176   return true;
177 }
178
179 //********************************************************************
180 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
181 {
182   QList<QWidget*> aControls;
183   aControls.append(myLabel);
184   aControls.append(myTextLine);
185   return aControls;
186 }
187
188 //********************************************************************
189 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
190 {
191   QList<ObjectPtr> aObjects = myWorkshop->selection()->selectedObjects();
192   if (aObjects.size() > 0) {
193     ObjectPtr aObject = aObjects.first();
194     if ((!mySelectedObject) && (!aObject))
195       return;
196
197     // Check that the selected object is result (others can not be accepted)
198     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
199     if (!aRes)
200       return;
201
202     if (myFeature) {
203       // We can not select a result of our feature
204       const std::list<boost::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
205       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
206       for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
207         if ((*aIt) == aRes)
208           return;
209       }
210     }
211     // Check that object belongs to active document or PartSet
212     DocumentPtr aDoc = aRes->document();
213     SessionPtr aMgr = ModelAPI_Session::get();
214     if (!(aDoc == aMgr->activeDocument()) || (aDoc == aMgr->moduleDocument()))
215       return;
216
217     // Check that the result has a shape
218     GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
219     if (!aShape)
220       return;
221
222     /// Check that object has acceptable type
223     if (!acceptObjectType(aObject)) 
224       return;
225
226     // Get sub-shapes from local selection
227     if (myUseSubShapes) {
228       NCollection_List<TopoDS_Shape> aShapeList;
229       std::list<ObjectPtr> aOwners;
230       myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
231       if (aShapeList.Extent() > 0) {
232         aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
233         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
234       }
235     }
236
237     // Check that the selection corresponds to selection type
238     if (myUseSubShapes) {
239       if (!acceptSubShape(aShape))
240         return;
241     } else {
242       if (!acceptObjectShape(aObject))
243         return;
244     }
245     setObject(aObject, aShape);
246     //activateSelection(false);
247     emit focusOutWidget(this);
248   }
249 }
250
251 //********************************************************************
252 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_ptr<GeomAPI_Shape> theShape)
253 {
254   mySelectedObject = theObj;
255   myShape = theShape;
256   if (mySelectedObject) {
257     raisePanel();
258     if (!myUseSubShapes) {
259       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
260       ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
261     }
262   } 
263   updateSelectionName();
264   activateSelection(false);
265   emit valuesChanged();
266 }
267
268 //********************************************************************
269 bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
270 {
271   ResultPtr aResult = boost::dynamic_pointer_cast<ModelAPI_Result>(theResult);
272
273   // Check that the shape of necessary type
274   boost::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
275   if (!aShapePtr)
276     return false;
277   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
278   if (aShape.IsNull())
279     return false;
280
281   TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
282   if (aShapeType == TopAbs_COMPOUND) {
283     foreach (QString aType, myShapeTypes) {
284       TopExp_Explorer aEx(aShape, shapeType(aType));
285       if (aEx.More())
286         return true;
287     }
288   } else {
289     foreach (QString aType, myShapeTypes) {
290       if (shapeType(aType) == aShapeType)
291         return true;
292     }
293   }
294   return false;
295 }
296
297 //********************************************************************
298 bool ModuleBase_WidgetShapeSelector::acceptSubShape(boost::shared_ptr<GeomAPI_Shape> theShape) const
299 {
300   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
301   foreach (QString aType, myShapeTypes) {
302     if (aShape.ShapeType() == shapeType(aType))
303       return true;
304   }
305   return false;
306 }
307
308 //********************************************************************
309 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
310 {
311   // Definition of types is not obligatory. If types are not defined then
312   // it means that accepted any type
313   if (myObjectTypes.isEmpty())
314     return true;
315
316   foreach (QString aType, myObjectTypes) {
317     if (aType.toLower() == "construction") {
318       ResultConstructionPtr aConstr = 
319         boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
320       return (aConstr != NULL);
321     } // ToDo: Process other types of objects
322   }
323   // Object type is defined but not found
324   return false;
325 }
326
327
328 //********************************************************************
329 void ModuleBase_WidgetShapeSelector::updateSelectionName()
330 {
331   if (mySelectedObject) {
332     std::string aName = mySelectedObject->data()->name();
333     myTextLine->setText(QString::fromStdString(aName));
334   } else {
335     if (myIsActive) {
336       QString aMsg = tr("Select a ");
337       int i = 0;
338       foreach (QString aType, myShapeTypes) {
339         if (i > 0)
340           aMsg += " or ";
341         aMsg += aType;
342         i++;
343       }
344       myTextLine->setText(aMsg);
345     } else
346       myTextLine->setText(tr("No object selected"));
347   }
348 }
349
350
351 //********************************************************************
352 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
353 {
354   myIsActive = toActivate;
355   if (myIsActive)
356     myTextLine->setPalette(myBasePalet);
357   else
358     myTextLine->setPalette(myInactivePalet);
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::focusTo()
406 {
407   activateSelection(true);
408   return true;
409 }
410
411 //********************************************************************
412 bool ModuleBase_WidgetShapeSelector::eventFilter(QObject* theObj, QEvent* theEvent)
413 {
414   if (theObj == myTextLine) {
415     if (theEvent->type() == QEvent::FocusIn)
416       activateSelection(true);
417   }
418   return ModuleBase_ModelWidget::eventFilter(theObj, theEvent);
419 }
420
421 //********************************************************************
422 bool ModuleBase_WidgetShapeSelector::setValue(ModuleBase_WidgetValue* theValue)
423 {
424   if (theValue) {
425     ModuleBase_WidgetValueFeature* aFeatureValue =
426         dynamic_cast<ModuleBase_WidgetValueFeature*>(theValue);
427     if (aFeatureValue && aFeatureValue->object()) {
428       ObjectPtr aObject = aFeatureValue->object();
429       if (acceptObjectShape(aObject)) {
430         setObject(aObject);
431         return true;
432       }
433     }
434   }
435   return false;
436 }
437