Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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 #include <GeomValidators_Tools.h>
22
23 #include <ModelAPI_AttributeReference.h>
24 #include <ModelAPI_Data.h>
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Events.h>
27 #include <ModelAPI_Feature.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_ResultConstruction.h>
30 #include <ModelAPI_AttributeReference.h>
31 #include <ModelAPI_AttributeSelection.h>
32 #include <ModelAPI_Session.h>
33 #include <ModelAPI_Tools.h>
34 #include <ModelAPI_ResultBody.h>
35 #include <ModelAPI_AttributeRefAttr.h>
36 #include <ModelAPI_Validator.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
69 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
70                                                      ModuleBase_IWorkshop* theWorkshop,
71                                                      const Config_WidgetAPI* theData,
72                                                      const std::string& theParentId)
73     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
74       myWorkshop(theWorkshop), myIsActive(false)
75 {
76   QFormLayout* aLayout = new QFormLayout(this);
77   ModuleBase_Tools::adjustMargins(aLayout);
78
79   QString aLabelText = QString::fromStdString(theData->widgetLabel());
80   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
81   myLabel = new QLabel(aLabelText, this);
82   if (!aLabelIcon.isEmpty())
83     myLabel->setPixmap(QPixmap(aLabelIcon));
84
85
86   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
87   myTextLine = new QLineEdit(this);
88   myTextLine->setReadOnly(true);
89   myTextLine->setToolTip(aToolTip);
90   myTextLine->installEventFilter(this);
91
92   aLayout->addRow(myLabel, myTextLine);
93
94   std::string aTypes = theData->getProperty("shape_types");
95   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
96 }
97
98 //********************************************************************
99 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
100 {
101   activateSelection(false);
102 }
103
104 //********************************************************************
105 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
106 {
107   // the value is stored on the selection changed signal processing 
108   return true;
109 }
110
111 //********************************************************************
112 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
113                                                GeomShapePtr theShape)
114 {
115   bool isChanged = false;
116   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
117   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
118     return isChanged;
119
120   DataPtr aData = myFeature->data();
121   AttributeReferencePtr aRef = aData->reference(attributeID());
122   if (aRef) {
123     ObjectPtr aObject = aRef->value();
124     if (!(aObject && aObject->isSame(theSelectedObject))) {
125       aRef->setValue(theSelectedObject);
126       isChanged = true;
127     }
128   } else {
129     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
130     if (aRefAttr) {
131       ObjectPtr aObject = aRefAttr->object();
132       if (!(aObject && aObject->isSame(theSelectedObject))) {
133         aRefAttr->setObject(theSelectedObject);
134         isChanged = true;
135       }
136     } else {
137       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
138       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
139       if (aSelectAttr.get() != NULL) {
140         aSelectAttr->setValue(aResult, theShape);
141         isChanged = true;
142       }
143     }
144   }
145   return isChanged;
146 }
147
148 //********************************************************************
149 void ModuleBase_WidgetShapeSelector::clearAttribute()
150 {
151   DataPtr aData = myFeature->data();
152   AttributeSelectionPtr aSelect = aData->selection(attributeID());
153   if (aSelect) {
154     aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
155     return;
156   }
157   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
158   if (aRefAttr) {
159     aRefAttr->setObject(ObjectPtr());
160     return;
161   }
162   AttributeReferencePtr aRef = aData->reference(attributeID());
163   if (aRef) {
164     aRef->setObject(ObjectPtr());
165   }
166 }
167
168 //********************************************************************
169 bool ModuleBase_WidgetShapeSelector::restoreValue()
170 {
171   bool isBlocked = this->blockSignals(true);
172   updateSelectionName();
173   this->blockSignals(isBlocked);
174
175   return true;
176 }
177
178 //********************************************************************
179 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
180 {
181   QList<QWidget*> aControls;
182   aControls.append(myTextLine);
183   return aControls;
184 }
185
186 //********************************************************************
187 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
188 {
189   // In order to make reselection possible
190   // TODO: check with MPV clearAttribute();
191
192   QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(myWorkshop->selection());
193   if (aSelectedPrs.empty())
194     return;
195   ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
196   if (aPrs.isEmpty() || !isValidSelection(aPrs))
197     return;
198
199   if (!aPrs.isEmpty() && isValidSelection(aPrs)) {
200     setSelectionCustom(aPrs);
201     // the updateObject method should be called to flush the updated sigal. The workshop listens it,
202     // calls validators for the feature and, as a result, updates the Apply button state.
203     updateObject(myFeature);
204     //if (theObj) {
205       //  raisePanel();
206     //} 
207     //updateSelectionName();
208     //emit valuesChanged();
209     emit focusOutWidget(this);
210   }
211
212 }
213
214 //********************************************************************
215 //bool ModuleBase_WidgetShapeSelector::acceptObjectShape(const ObjectPtr theResult) const
216 //{
217 //  ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theResult);
218 //
219 //  // Check that the shape of necessary type
220 //  std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aResult);
221 //  if (!aShapePtr)
222 //    return false;
223 //  TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
224 //  if (aShape.IsNull())
225 //    return false;
226 //
227 //  TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
228 //  if (aShapeType == TopAbs_COMPOUND) {
229 //    foreach (QString aType,
230 //      myShapeTypes) {
231 //      TopExp_Explorer aEx(aShape, shapeType(aType));
232 //      if (aEx.More())
233 //        return true;
234 //    }
235 //  } else {
236 //    foreach (QString aType, myShapeTypes) {
237 //      if (shapeType(aType) == aShapeType)
238 //        return true;
239 //    }
240 //  }
241 //  return false;
242 //}
243
244 //********************************************************************
245 bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
246 {
247   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
248   foreach (QString aType, myShapeTypes) {
249     if (aShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
250       return true;
251   }
252   return false;
253 }
254
255 //********************************************************************
256 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
257 {
258   GeomShapePtr aShape;
259   DataPtr aData = myFeature->data();
260   if (aData.get() == NULL)
261     return aShape;
262
263   AttributeSelectionPtr aSelect = aData->selection(attributeID());
264   if (aSelect)
265     aShape = aSelect->value();
266
267   return aShape;
268 }
269
270 //********************************************************************
271 void ModuleBase_WidgetShapeSelector::updateSelectionName()
272 {
273   DataPtr aData = myFeature->data();
274   if (aData.get() == NULL)
275     return;
276
277   bool isNameUpdated = false;
278   AttributeSelectionPtr aSelect = aData->selection(attributeID());
279   if (aSelect) {
280     myTextLine->setText(QString::fromStdString(aSelect->namingName()));
281     isNameUpdated = true;
282   }
283   if (!isNameUpdated) {
284     ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
285     if (anObject.get() != NULL) {
286       std::string aName = anObject->data()->name();
287       myTextLine->setText(QString::fromStdString(aName));
288     } else {
289       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
290       if (aRefAttr && aRefAttr->attr().get() != NULL) {
291         //myIsObject = aRefAttr->isObject();
292         AttributePtr anAttr = aRefAttr->attr();
293         if (anAttr.get() != NULL) {
294           std::stringstream aName;
295           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
296           myTextLine->setText(QString::fromStdString(aName.str()));
297         }
298       }
299       else if (myIsActive) {
300         myTextLine->setText("");
301       }
302     }
303   }
304 }
305
306
307 //********************************************************************
308 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
309 {
310   if (myIsActive == toActivate)
311     return;
312   myIsActive = toActivate;
313   updateSelectionName();
314
315   if (myIsActive) {
316     QIntList aList;
317     foreach (QString aType, myShapeTypes) {
318       aList.append(ModuleBase_Tools::shapeType(aType));
319     }
320     myWorkshop->activateSubShapesSelection(aList);
321   } else {
322     myWorkshop->deactivateSubShapesSelection();
323   }
324
325   activateFilters(myWorkshop, myIsActive);
326 }
327
328 //********************************************************************
329 void ModuleBase_WidgetShapeSelector::raisePanel() const
330 {
331   QWidget* aParent = this->parentWidget();
332   QWidget* aLastPanel = 0;
333   while (!aParent->inherits("QDockWidget")) {
334     aLastPanel = aParent;
335     aParent = aParent->parentWidget();
336     if (!aParent)
337       return;
338   }
339   if (aParent->inherits("QDockWidget")) {
340     QDockWidget* aTabWgt = (QDockWidget*) aParent;
341     aTabWgt->raise();
342   }
343 }
344
345 //********************************************************************
346 void ModuleBase_WidgetShapeSelector::activateCustom()
347 {
348   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
349   activateSelection(true);
350 }
351
352 //********************************************************************
353 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
354 {
355   DataPtr aData = myFeature->data();
356   AttributePtr anAttribute = myFeature->attribute(attributeID());
357
358   myObject = GeomValidators_Tools::getObject(anAttribute);
359   myShape = getShape();
360   myRefAttribute = AttributePtr();
361   myIsObject = false;
362   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
363   if (aRefAttr) {
364     myIsObject = aRefAttr->isObject();
365     myRefAttribute = aRefAttr->attr();
366   }
367 }
368
369 //********************************************************************
370 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
371 {
372   DataPtr aData = myFeature->data();
373   AttributePtr anAttribute = myFeature->attribute(attributeID());
374
375   setObject(myObject, myShape);
376   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
377   if (aRefAttr) {
378     if (!myIsObject)
379       aRefAttr->setAttr(myRefAttribute);
380   }
381 }
382
383 //********************************************************************
384 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
385 {
386   bool isDone = false;
387
388   // It should be checked by corresponded validator
389   ObjectPtr aObject = thePrs.object();
390   ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
391   /*
392   if ((!aCurrentObject) && (!aObject))
393     return false;*/
394
395   // It should be checked by corresponded validator
396   // Check that the selected object is result (others can not be accepted)
397   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
398   if (!aRes)
399     return false;
400   /*if (myFeature) {
401     // We can not select a result of our feature
402     const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
403     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
404     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
405       if ((*aIt) == aRes)
406         return false;
407     }
408   }
409   */
410   // It should be checked by corresponded validator
411   /*
412   // Check that object belongs to active document or PartSet
413   DocumentPtr aDoc = aRes->document();
414   SessionPtr aMgr = ModelAPI_Session::get();
415   if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
416     return false;*/
417
418   // It should be checked by corresponded validator
419   // Check that the result has a shape
420   GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
421   if (!aShape)
422     return false;
423
424   // Get sub-shapes from local selection
425   if (!thePrs.shape().IsNull()) {
426     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
427     aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
428   }
429   // Check that the selection corresponds to selection type
430   if (!acceptSubShape(aShape))
431     return false;
432
433   setObject(aObject, aShape);
434   return true;
435 }
436
437 //********************************************************************
438 void ModuleBase_WidgetShapeSelector::deactivate()
439 {
440   activateSelection(false);
441   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
442 }