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