]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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_RefAttrValidator.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
58 #include <TopExp_Explorer.hxx>
59 #include <TopoDS_Shape.hxx>
60
61 #include <memory>
62
63 #include <list>
64 #include <string>
65
66 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
67 static ShapeTypes MyShapeTypes;
68
69 TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theType)
70 {
71   if (MyShapeTypes.count() == 0) {
72     MyShapeTypes["face"] = TopAbs_FACE;
73     MyShapeTypes["faces"] = TopAbs_FACE;
74     MyShapeTypes["vertex"] = TopAbs_VERTEX;
75     MyShapeTypes["vertices"] = TopAbs_VERTEX;
76     MyShapeTypes["wire"] = TopAbs_WIRE;
77     MyShapeTypes["edge"] = TopAbs_EDGE;
78     MyShapeTypes["edges"] = TopAbs_EDGE;
79     MyShapeTypes["shell"] = TopAbs_SHELL;
80     MyShapeTypes["solid"] = TopAbs_SOLID;
81     MyShapeTypes["solids"] = TopAbs_SOLID;
82   }
83   QString aType = theType.toLower();
84   if (MyShapeTypes.contains(aType))
85     return MyShapeTypes[aType];
86   Events_Error::send("Shape type defined in XML is not implemented!");
87   return TopAbs_SHAPE;
88 }
89
90 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
91                                                      ModuleBase_IWorkshop* theWorkshop,
92                                                      const Config_WidgetAPI* theData,
93                                                      const std::string& theParentId)
94     : ModuleBase_ModelWidget(theParent, theData, theParentId),
95       myWorkshop(theWorkshop), myIsActive(false)
96 {
97   myContainer = new QWidget(theParent);
98   QHBoxLayout* aLayout = new QHBoxLayout(myContainer);
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, myContainer);
104   if (!aLabelIcon.isEmpty())
105     myLabel->setPixmap(QPixmap(aLabelIcon));
106
107   aLayout->addWidget(myLabel);
108
109   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
110   myTextLine = new QLineEdit(myContainer);
111   myTextLine->setReadOnly(true);
112   myTextLine->setToolTip(aToolTip);
113   myTextLine->installEventFilter(this);
114
115   aLayout->addWidget(myTextLine, 1);
116
117   std::string aTypes = theData->getProperty("shape_types");
118   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
119
120   std::string aObjTypes = theData->getProperty("object_types");
121   myObjectTypes = QString(aObjTypes.c_str()).split(' ', QString::SkipEmptyParts);
122
123   //myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); 
124 }
125
126 //********************************************************************
127 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
128 {
129   activateSelection(false);
130 }
131
132 //********************************************************************
133 bool ModuleBase_WidgetShapeSelector::storeValue() const
134 {
135   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject);
136   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
137     return false;
138
139   DataPtr aData = myFeature->data();
140   AttributeReferencePtr aRef = aData->reference(attributeID());
141   if (aRef) {
142     ObjectPtr aObject = aRef->value();
143     if (!(aObject && aObject->isSame(mySelectedObject))) {
144       aRef->setValue(mySelectedObject);
145       updateObject(myFeature);
146       return true;
147     }
148   } else {
149     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
150     if (aRefAttr) {
151       ObjectPtr aObject = aRefAttr->object();
152       if (!(aObject && aObject->isSame(mySelectedObject))) {
153         aRefAttr->setObject(mySelectedObject);
154         updateObject(myFeature);
155         return true;
156       }
157     } else {
158       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
159       ResultPtr aBody = std::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
160       if (aSelectAttr && aBody && (myShape.get() != NULL)) {
161         aSelectAttr->setValue(aBody, myShape);
162         updateObject(myFeature);
163         return true;
164       }
165     }
166   }
167   return false;
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   DataPtr aData = myFeature->data();
194   bool isBlocked = this->blockSignals(true);
195
196   AttributeSelectionPtr aSelect = aData->selection(attributeID());
197   if (aSelect) {
198     mySelectedObject = aSelect->context();
199     myShape = aSelect->value();
200   } else {
201     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
202     if (aRefAttr) {
203       mySelectedObject = aRefAttr->object();
204     } else {
205       AttributeReferencePtr aRef = aData->reference(attributeID());
206       if (aRef) {
207         mySelectedObject = aRef->value();
208       }
209     }
210   }
211   updateSelectionName();
212
213   this->blockSignals(isBlocked);
214   return true;
215 }
216
217 //********************************************************************
218 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
219 {
220   QList<QWidget*> aControls;
221   aControls.append(myTextLine);
222   return aControls;
223 }
224
225 //********************************************************************
226 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
227 {
228   // In order to make reselection possible
229   // TODO: check with MPV clearAttribute();
230
231   //QObjectPtrList aObjects = myWorkshop->selection()->selectedPresentations();
232   QList<ModuleBase_ViewerPrs> aSelected = myWorkshop->selection()->getSelected();
233   if (aSelected.size() > 0) {
234     if (setSelection(aSelected.first()))
235       emit focusOutWidget(this);
236   }
237 }
238
239 //********************************************************************
240 bool ModuleBase_WidgetShapeSelector::setSelection(ModuleBase_ViewerPrs theValue)
241 {
242   ObjectPtr aObject = theValue.object();
243   if ((!mySelectedObject) && (!aObject))
244     return false;
245
246   // Check that the selected object is result (others can not be accepted)
247   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
248   if (!aRes)
249     return false;
250
251   if (myFeature) {
252     // We can not select a result of our feature
253     const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
254     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
255     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
256       if ((*aIt) == aRes)
257         return false;
258     }
259   }
260   // Check that object belongs to active document or PartSet
261   DocumentPtr aDoc = aRes->document();
262   SessionPtr aMgr = ModelAPI_Session::get();
263   if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
264     return false;
265
266   // Check that the result has a shape
267   GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
268   if (!aShape)
269     return false;
270
271   /// Check that object has acceptable type
272   if (!acceptObjectType(aObject)) 
273     return false;
274
275   // Get sub-shapes from local selection
276   if (!theValue.shape().IsNull()) {
277     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
278     aShape->setImpl(new TopoDS_Shape(theValue.shape()));
279   }
280
281   // Check that the selection corresponds to selection type
282   if (!acceptSubShape(aShape))
283     return false;
284 //  if (!acceptObjectShape(aObject))
285 //      return false;
286
287   if (isValid(aObject, aShape)) {
288     setObject(aObject, aShape);
289     return true;
290   }
291   return false;
292 }
293
294 //********************************************************************
295 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
296 {
297   mySelectedObject = theObj;
298   myShape = theShape;
299   if (mySelectedObject) {
300     raisePanel();
301   } 
302   updateSelectionName();
303   emit valuesChanged();
304 }
305
306 //********************************************************************
307 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
308 //{
309 //  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
310 //
311 //  // Check that the shape of necessary type
312 //  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
313 //  if (!aShapePtr)
314 //    return false;
315 //  TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
316 //  if (aShape.IsNull())
317 //    return false;
318 //
319 //  TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
320 //  if (aShapeType == TopAbs_COMPOUND) {
321 //    foreach (QString aType,
322 //      myShapeTypes) {
323 //      TopExp_Explorer aEx(aShape, shapeType(aType));
324 //      if (aEx.More())
325 //        return true;
326 //    }
327 //  } else {
328 //    foreach (QString aType, myShapeTypes) {
329 //      if (shapeType(aType) == aShapeType)
330 //        return true;
331 //    }
332 //  }
333 //  return false;
334 //}
335
336 //********************************************************************
337 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
338 {
339   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
340   foreach (QString aType, myShapeTypes) {
341     if (aShape.ShapeType() == shapeType(aType))
342       return true;
343   }
344   return false;
345 }
346
347 //********************************************************************
348 bool ModuleBase_WidgetShapeSelector::acceptObjectType(const ObjectPtr theObject) const
349 {
350   // Definition of types is not obligatory. If types are not defined then
351   // it means that accepted any type
352   if (myObjectTypes.isEmpty())
353     return true;
354
355   foreach (QString aType, myObjectTypes) {
356     if (aType.toLower() == "construction") {
357       ResultConstructionPtr aConstr = 
358         std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theObject);
359       return (aConstr != NULL);
360     } // ToDo: Process other types of objects
361   }
362   // Object type is defined but not found
363   return false;
364 }
365
366
367 //********************************************************************
368 void ModuleBase_WidgetShapeSelector::updateSelectionName()
369 {
370   if (mySelectedObject) {
371     std::string aName = mySelectedObject->data()->name();
372     myTextLine->setText(QString::fromStdString(aName));
373   } else {
374     if (myIsActive) {
375       myTextLine->setText("");
376     }
377   }
378 }
379
380
381 //********************************************************************
382 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
383 {
384   if (myIsActive == toActivate)
385     return;
386   myIsActive = toActivate;
387   updateSelectionName();
388   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
389
390   if (myIsActive) {
391     connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
392     QIntList aList;
393     foreach (QString aType, myShapeTypes) {
394       aList.append(shapeType(aType));
395     }
396     myWorkshop->activateSubShapesSelection(aList);
397     if (!myObjectTypes.isEmpty()) {
398       myObjTypeFilter = new ModuleBase_ObjectTypesFilter(myWorkshop, myObjectTypes);
399       aViewer->clearSelectionFilters();
400       aViewer->addSelectionFilter(myObjTypeFilter);
401     }
402   } else {
403     disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
404     if (!myObjTypeFilter.IsNull()) {
405       aViewer->removeSelectionFilter(myObjTypeFilter);
406       myObjTypeFilter.Nullify();
407     }
408     myWorkshop->deactivateSubShapesSelection();
409   }
410   // apply filters loaded from the XML definition of the widget
411   ModuleBase_FilterFactory* aFactory = myWorkshop->selectionFilters();
412   SelectMgr_ListOfFilter aFilters;
413   aFactory->filters(parentID(), attributeID(), aFilters);
414   SelectMgr_ListIteratorOfListOfFilter aIt(aFilters);
415   for (; aIt.More(); aIt.Next()) {
416     Handle(SelectMgr_Filter) aSelFilter = aIt.Value();
417     if (aSelFilter.IsNull())
418       continue;
419
420     //Handle(ModuleBase_Filter) aFilter = Handle(ModuleBase_Filter)::DownCast(aIt.Value());
421     //if (aFilter.IsNull())
422     //  continue;
423     if (myIsActive)
424       aViewer->addSelectionFilter(aSelFilter);
425     else
426       aViewer->removeSelectionFilter(aSelFilter);
427   }
428 }
429
430 //********************************************************************
431 void ModuleBase_WidgetShapeSelector::raisePanel() const
432 {
433   QWidget* aParent = myContainer->parentWidget();
434   QWidget* aLastPanel = 0;
435   while (!aParent->inherits("QDockWidget")) {
436     aLastPanel = aParent;
437     aParent = aParent->parentWidget();
438     if (!aParent)
439       return;
440   }
441   if (aParent->inherits("QDockWidget")) {
442     QDockWidget* aTabWgt = (QDockWidget*) aParent;
443     aTabWgt->raise();
444   }
445 }
446
447 //********************************************************************
448 void ModuleBase_WidgetShapeSelector::activate()
449 {
450   activateSelection(true);
451 }
452
453 //********************************************************************
454 void ModuleBase_WidgetShapeSelector::deactivate()
455 {
456   activateSelection(false);
457 }
458
459 //********************************************************************
460 bool ModuleBase_WidgetShapeSelector::isValid(ObjectPtr theObj, std::shared_ptr<GeomAPI_Shape> theShape)
461 {
462   SessionPtr aMgr = ModelAPI_Session::get();
463   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
464   std::list<ModelAPI_Validator*> aValidators;
465   std::list<std::list<std::string> > anArguments;
466   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
467
468   // Check the type of selected object
469   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
470   bool isValid = true;
471   for (; aValidator != aValidators.end(); aValidator++) {
472     const ModelAPI_ResultValidator* aResValidator =
473         dynamic_cast<const ModelAPI_ResultValidator*>(*aValidator);
474     if (aResValidator) {
475       isValid = false;
476       if (aResValidator->isValid(theObj)) {
477         isValid = true;
478         break;
479       }
480     }
481   }
482   if (!isValid)
483     return false;
484
485   // Check the acceptability of the object as attribute
486   aValidator = aValidators.begin();
487   std::list<std::list<std::string> >::iterator aArgs = anArguments.begin();
488   for (; aValidator != aValidators.end(); aValidator++, aArgs++) {
489     const ModelAPI_RefAttrValidator* aAttrValidator =
490         dynamic_cast<const ModelAPI_RefAttrValidator*>(*aValidator);
491     if (aAttrValidator) {
492       if (!aAttrValidator->isValid(myFeature, *aArgs, theObj)) {
493         return false;
494       }
495     }
496   }
497   return true;
498 }