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