Salome HOME
9708a6ccdade9d7fd5a44d6020a909fedad023cb
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetShapeSelector.h
4 // Created:     2 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include <ModuleBase_WidgetShapeSelector.h>
8 #include <ModuleBase_Definitions.h>
9 #include <ModuleBase_ISelection.h>
10 #include <ModuleBase_IWorkshop.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_Tools.h>
13 #include <ModuleBase_FilterFactory.h>
14 #include <ModuleBase_Filter.h>
15
16 #include <Config_WidgetAPI.h>
17 #include <Events_Loop.h>
18 #include <Events_Message.h>
19 #include <GeomAPI_Interface.h>
20 #include <GeomAPI_Shape.h>
21
22 #include <ModelAPI_AttributeReference.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Result.h>
28 #include <ModelAPI_ResultConstruction.h>
29 #include <ModelAPI_AttributeReference.h>
30 #include <ModelAPI_AttributeSelection.h>
31 #include <ModelAPI_Session.h>
32 #include <ModelAPI_Tools.h>
33 #include <ModelAPI_ResultBody.h>
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_Validator.h>
36 #include <ModelAPI_ResultValidator.h>
37 #include <ModelAPI_AttributeValidator.h>
38 #include <ModelAPI_ShapeValidator.h>
39
40 #include <Config_WidgetAPI.h>
41 #include <Events_Error.h>
42
43 #include <GeomAPI_Shape.h>
44
45 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopExp_Explorer.hxx>
48
49 #include <QWidget>
50 #include <QLayout>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QToolButton>
54 #include <QString>
55 #include <QEvent>
56 #include <QDockWidget>
57 #include <QApplication>
58 #include <QFormLayout>
59
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Shape.hxx>
62
63 #include <memory>
64
65 #include <list>
66 #include <string>
67
68 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
69 static ShapeTypes MyShapeTypes;
70
71 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
72 {
73   if (MyShapeTypes.count() == 0) {
74     MyShapeTypes["face"] = TopAbs_FACE;
75     MyShapeTypes["faces"] = TopAbs_FACE;
76     MyShapeTypes["vertex"] = TopAbs_VERTEX;
77     MyShapeTypes["vertices"] = TopAbs_VERTEX;
78     MyShapeTypes["wire"] = TopAbs_WIRE;
79     MyShapeTypes["edge"] = TopAbs_EDGE;
80     MyShapeTypes["edges"] = TopAbs_EDGE;
81     MyShapeTypes["shell"] = TopAbs_SHELL;
82     MyShapeTypes["solid"] = TopAbs_SOLID;
83     MyShapeTypes["solids"] = TopAbs_SOLID;
84   }
85   QString aType = theType.toLower();
86   if (MyShapeTypes.contains(aType))
87     return MyShapeTypes[aType];
88   Events_Error::send("Shape type defined in XML is not implemented!");
89   return TopAbs_SHAPE;
90 }
91
92 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
93                                                      ModuleBase_IWorkshop* theWorkshop,
94                                                      const Config_WidgetAPI* theData,
95                                                      const std::string& theParentId)
96     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
97       myWorkshop(theWorkshop), myIsActive(false)
98 {
99   QFormLayout* aLayout = new QFormLayout(this);
100   ModuleBase_Tools::adjustMargins(aLayout);
101
102   QString aLabelText = QString::fromStdString(theData->widgetLabel());
103   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
104   myLabel = new QLabel(aLabelText, this);
105   if (!aLabelIcon.isEmpty())
106     myLabel->setPixmap(QPixmap(aLabelIcon));
107
108
109   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
110   myTextLine = new QLineEdit(this);
111   myTextLine->setReadOnly(true);
112   myTextLine->setToolTip(aToolTip);
113   myTextLine->installEventFilter(this);
114
115   aLayout->addRow(myLabel, myTextLine);
116
117   std::string aTypes = theData->getProperty("shape_types");
118   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
119 }
120
121 //********************************************************************
122 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
123 {
124   activateSelection(false);
125 }
126
127 //********************************************************************
128 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
129 {
130   // the value is stored on the selection changed signal processing 
131   return true;
132 }
133
134 //********************************************************************
135 bool ModuleBase_WidgetShapeSelector::storeAttributeValues(ObjectPtr theSelectedObject,
136                                                           GeomShapePtr theShape) const
137 {
138   bool isChanged = false;
139   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
140   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
141     return isChanged;
142
143   DataPtr aData = myFeature->data();
144   AttributeReferencePtr aRef = aData->reference(attributeID());
145   if (aRef) {
146     ObjectPtr aObject = aRef->value();
147     if (!(aObject && aObject->isSame(theSelectedObject))) {
148       aRef->setValue(theSelectedObject);
149       isChanged = true;
150     }
151   } else {
152     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
153     if (aRefAttr) {
154       ObjectPtr aObject = aRefAttr->object();
155       if (!(aObject && aObject->isSame(theSelectedObject))) {
156         aRefAttr->setObject(theSelectedObject);
157         isChanged = true;
158       }
159     } else {
160       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
161       ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
162       if (aSelectAttr && aBody && (theShape.get() != NULL)) {
163         aSelectAttr->setValue(aBody, theShape);
164         isChanged = true;
165       }
166     }
167   }
168   return isChanged;
169 }
170
171 //********************************************************************
172 void ModuleBase_WidgetShapeSelector::clearAttribute()
173 {
174   DataPtr aData = myFeature->data();
175   AttributeSelectionPtr aSelect = aData->selection(attributeID());
176   if (aSelect) {
177     aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
178     return;
179   }
180   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
181   if (aRefAttr) {
182     aRefAttr->setObject(ObjectPtr());
183     return;
184   }
185   AttributeReferencePtr aRef = aData->reference(attributeID());
186   if (aRef) {
187     aRef->setObject(ObjectPtr());
188   }
189 }
190
191 //********************************************************************
192 bool ModuleBase_WidgetShapeSelector::restoreValue()
193 {
194   bool isBlocked = this->blockSignals(true);
195   updateSelectionName();
196   this->blockSignals(isBlocked);
197
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   // In order to make reselection possible
213   // TODO: check with MPV clearAttribute();
214
215   //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
216   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
217   if (aSelected.size() > 0) {
218     Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
219     if (isValid(anOwner)) {
220       setSelection(anOwner);
221       // the updateObject method should be called to flush the updated sigal. The workshop listens it,
222       // calls validators for the feature and, as a result, updates the Apply button state.
223       updateObject(myFeature);
224       //if (theObj) {
225         //  raisePanel();
226       //} 
227       //updateSelectionName();
228       //emit valuesChanged();
229       emit focusOutWidget(this);
230     }
231   }
232 }
233
234 //********************************************************************
235 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
236 //{
237 //  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
238 //
239 //  // Check that the shape of necessary type
240 //  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
241 //  if (!aShapePtr)
242 //    return false;
243 //  TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
244 //  if (aShape.IsNull())
245 //    return false;
246 //
247 //  TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
248 //  if (aShapeType == TopAbs_COMPOUND) {
249 //    foreach (QString aType,
250 //      myShapeTypes) {
251 //      TopExp_Explorer aEx(aShape, shapeType(aType));
252 //      if (aEx.More())
253 //        return true;
254 //    }
255 //  } else {
256 //    foreach (QString aType, myShapeTypes) {
257 //      if (shapeType(aType) == aShapeType)
258 //        return true;
259 //    }
260 //  }
261 //  return false;
262 //}
263
264 //********************************************************************
265 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
266 {
267   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
268   foreach (QString aType, myShapeTypes) {
269     if (aShape.ShapeType() == shapeType(aType))
270       return true;
271   }
272   return false;
273 }
274
275 ObjectPtr ModuleBase_WidgetShapeSelector::getObject(const AttributePtr& theAttribute)
276 {
277   ObjectPtr anObject;
278   std::string anAttrType = theAttribute->attributeType();
279   if (anAttrType == ModelAPI_AttributeRefAttr::type()) {
280     AttributeRefAttrPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
281     if (anAttr != NULL && anAttr->isObject())
282       anObject = anAttr->object();
283   }
284   if (anAttrType == ModelAPI_AttributeSelection::type()) {
285     AttributeSelectionPtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
286     if (anAttr != NULL && anAttr->isInitialized())
287       anObject = anAttr->context();
288   }
289   if (anAttrType == ModelAPI_AttributeReference::type()) {
290     AttributeReferencePtr anAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(theAttribute);
291     if (anAttr.get() != NULL && anAttr->isInitialized())
292       anObject = anAttr->value();
293   }
294   return anObject;
295 }
296
297
298 //********************************************************************
299 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
300 {
301   GeomShapePtr aShape;
302   DataPtr aData = myFeature->data();
303   if (aData.get() == NULL)
304     return aShape;
305
306   AttributeSelectionPtr aSelect = aData->selection(attributeID());
307   if (aSelect)
308     aShape = aSelect->value();
309
310   return aShape;
311 }
312
313 //********************************************************************
314 void ModuleBase_WidgetShapeSelector::updateSelectionName()
315 {
316   DataPtr aData = myFeature->data();
317   if (aData.get() == NULL)
318     return;
319
320   bool isNameUpdated = false;
321   AttributeSelectionPtr aSelect = aData->selection(attributeID());
322   if (aSelect) {
323     myTextLine->setText(QString::fromStdString(aSelect->namingName()));
324     isNameUpdated = true;
325   }
326   if (!isNameUpdated) {
327     ObjectPtr anObject = getObject(myFeature->attribute(attributeID()));
328     if (anObject.get() != NULL) {
329       std::string aName = anObject->data()->name();
330       myTextLine->setText(QString::fromStdString(aName));
331     } else {
332       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
333       if (aRefAttr && aRefAttr->attr().get() != NULL) {
334         //myIsObject = aRefAttr->isObject();
335         AttributePtr anAttr = aRefAttr->attr();
336         if (anAttr.get() != NULL) {
337           std::stringstream aName;
338           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
339           myTextLine->setText(QString::fromStdString(aName.str()));
340         }
341       }
342       else if (myIsActive) {
343         myTextLine->setText("");
344       }
345     }
346   }
347 }
348
349
350 //********************************************************************
351 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
352 {
353   if (myIsActive == toActivate)
354     return;
355   myIsActive = toActivate;
356   updateSelectionName();
357
358   if (myIsActive) {
359     QIntList aList;
360     foreach (QString aType, myShapeTypes) {
361       aList.append(shapeType(aType));
362     }
363     myWorkshop->activateSubShapesSelection(aList);
364   } else {
365     myWorkshop->deactivateSubShapesSelection();
366   }
367
368   activateFilters(myWorkshop, myIsActive);
369 }
370
371 //********************************************************************
372 void ModuleBase_WidgetShapeSelector::raisePanel() const
373 {
374   QWidget* aParent = this->parentWidget();
375   QWidget* aLastPanel = 0;
376   while (!aParent->inherits("QDockWidget")) {
377     aLastPanel = aParent;
378     aParent = aParent->parentWidget();
379     if (!aParent)
380       return;
381   }
382   if (aParent->inherits("QDockWidget")) {
383     QDockWidget* aTabWgt = (QDockWidget*) aParent;
384     aTabWgt->raise();
385   }
386 }
387
388 //********************************************************************
389 void ModuleBase_WidgetShapeSelector::activateCustom()
390 {
391   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
392   activateSelection(true);
393 }
394
395 //********************************************************************
396 void ModuleBase_WidgetShapeSelector::backupAttributeValue(const bool isBackup)
397 {
398   DataPtr aData = myFeature->data();
399   AttributePtr anAttribute = myFeature->attribute(attributeID());
400
401   if (isBackup) {
402     myObject = getObject(anAttribute);
403     myShape = getShape();
404     myRefAttribute = NULL;
405     myIsObject = false;
406     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
407     if (aRefAttr) {
408       myIsObject = aRefAttr->isObject();
409       myRefAttribute = aRefAttr->attr();
410     }
411   }
412   else {
413     storeAttributeValues(myObject, myShape);
414     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
415     if (aRefAttr) {
416       if (!myIsObject)
417         aRefAttr->setAttr(myRefAttribute);
418     }
419   }
420 }
421
422 //********************************************************************
423 bool ModuleBase_WidgetShapeSelector::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
424 {
425   bool isDone = false;
426
427   ModuleBase_ViewerPrs aPrs;
428   myWorkshop->selection()->fillPresentation(aPrs, theOwner);
429   ObjectPtr aObject = aPrs.object();
430   ObjectPtr aCurrentObject = getObject(myFeature->attribute(attributeID()));
431   if ((!aCurrentObject) && (!aObject))
432     return false;
433
434   // Check that the selected object is result (others can not be accepted)
435   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
436   if (!aRes)
437     return false;
438
439   if (myFeature) {
440     // We can not select a result of our feature
441     const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
442     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
443     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
444       if ((*aIt) == aRes)
445         return false;
446     }
447   }
448   // Check that object belongs to active document or PartSet
449   DocumentPtr aDoc = aRes->document();
450   SessionPtr aMgr = ModelAPI_Session::get();
451   if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
452     return false;
453
454   // Check that the result has a shape
455   GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
456   if (!aShape)
457     return false;
458
459   // Get sub-shapes from local selection
460   if (!aPrs.shape().IsNull()) {
461     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
462     aShape->setImpl(new TopoDS_Shape(aPrs.shape()));
463   }
464   // Check that the selection corresponds to selection type
465   if (!acceptSubShape(aShape))
466     return false;
467
468   storeAttributeValues(aObject, aShape);
469   return true;
470 }
471
472 //********************************************************************
473 void ModuleBase_WidgetShapeSelector::deactivate()
474 {
475   activateSelection(false);
476   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
477 }