]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
Salome HOME
Make Data not null, but invalid after the feature remove
[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   activateSelection(false);
106   activateFilters(myWorkshop, false);
107
108   delete myShapeValidator;
109 }
110
111 //********************************************************************
112 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
113 {
114   // the value is stored on the selection changed signal processing 
115   return true;
116 }
117
118 //********************************************************************
119 bool ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
120                                                GeomShapePtr theShape)
121 {
122   bool isChanged = false;
123   FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(theSelectedObject);
124   if (aSelectedFeature == myFeature)  // In order to avoid selection of the same object
125     return isChanged;
126
127   DataPtr aData = myFeature->data();
128   AttributeReferencePtr aRef = aData->reference(attributeID());
129   if (aRef) {
130     ObjectPtr aObject = aRef->value();
131     if (!(aObject && aObject->isSame(theSelectedObject))) {
132       aRef->setValue(theSelectedObject);
133       isChanged = true;
134     }
135   } else {
136     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
137     if (aRefAttr) {
138       ObjectPtr aObject = aRefAttr->object();
139       if (!(aObject && aObject->isSame(theSelectedObject))) {
140         aRefAttr->setObject(theSelectedObject);
141         isChanged = true;
142       }
143     } else {
144       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
145       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
146       if (aSelectAttr.get() != NULL) {
147         aSelectAttr->setValue(aResult, theShape);
148         isChanged = true;
149       }
150     }
151   }
152   return isChanged;
153 }
154
155 //********************************************************************
156 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
157 {
158   QList<ModuleBase_ViewerPrs> aSelected;
159   if(myFeature) {
160     DataPtr aData = myFeature->data();
161     AttributePtr anAttribute = myFeature->attribute(attributeID());
162
163     ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
164     TopoDS_Shape aShape;
165     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
166     if (aShapePtr.get()) {
167       aShape = aShapePtr->impl<TopoDS_Shape>();
168     }
169     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
170     aSelected.append(aPrs);
171   }
172   return aSelected;
173 }
174
175 //********************************************************************
176 void ModuleBase_WidgetShapeSelector::clearAttribute()
177 {
178   DataPtr aData = myFeature->data();
179   AttributeSelectionPtr aSelect = aData->selection(attributeID());
180   if (aSelect) {
181     aSelect->setValue(ResultPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
182     return;
183   }
184   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
185   if (aRefAttr) {
186     aRefAttr->setObject(ObjectPtr());
187     return;
188   }
189   AttributeReferencePtr aRef = aData->reference(attributeID());
190   if (aRef) {
191     aRef->setObject(ObjectPtr());
192   }
193 }
194
195 //********************************************************************
196 bool ModuleBase_WidgetShapeSelector::restoreValue()
197 {
198   bool isBlocked = this->blockSignals(true);
199   updateSelectionName();
200   this->blockSignals(isBlocked);
201
202   return true;
203 }
204
205 //********************************************************************
206 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
207 {
208   QList<QWidget*> aControls;
209   aControls.append(myTextLine);
210   return aControls;
211 }
212
213 //********************************************************************
214 void ModuleBase_WidgetShapeSelector::onSelectionChanged()
215 {
216   // In order to make reselection possible, set empty object and shape should be done
217   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
218
219   bool aHasObject = false;
220   QList<ModuleBase_ViewerPrs> aSelectedPrs = getSelectedEntitiesOrObjects(myWorkshop->selection());
221   if (!aSelectedPrs.empty()) {
222     ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
223     if (!aPrs.isEmpty() && isValidSelection(aPrs)) {
224       setSelectionCustom(aPrs);
225       aHasObject = true;
226     }
227   }
228   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
229   // calls validators for the feature and, as a result, updates the Apply button state.
230   updateObject(myFeature);
231   // the widget loses the focus only if the selected object is set
232   if (aHasObject)
233     emit focusOutWidget(this);
234 }
235
236 //********************************************************************
237 /*bool ModuleBase_WidgetShapeSelector::acceptSubShape(std::shared_ptr<GeomAPI_Shape> theShape) const
238 {
239   return true;
240
241   TopoDS_Shape aShape = theShape->impl<TopoDS_Shape>();
242   foreach (QString aType, myShapeTypes) {
243     if (aShape.ShapeType() == ModuleBase_Tools::shapeType(aType))
244       return true;
245   }
246   return false;
247 }*/
248
249 //********************************************************************
250 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
251 {
252   GeomShapePtr aShape;
253   DataPtr aData = myFeature->data();
254   if (!aData->isValid())
255     return aShape;
256
257   AttributeSelectionPtr aSelect = aData->selection(attributeID());
258   if (aSelect)
259     aShape = aSelect->value();
260
261   return aShape;
262 }
263
264 //********************************************************************
265 void ModuleBase_WidgetShapeSelector::updateSelectionName()
266 {
267   DataPtr aData = myFeature->data();
268   if (!aData->isValid())
269     return;
270
271   bool isNameUpdated = false;
272   AttributeSelectionPtr aSelect = aData->selection(attributeID());
273   if (aSelect) {
274     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
275     isNameUpdated = true;
276   }
277   if (!isNameUpdated) {
278     ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
279     if (anObject.get() != NULL) {
280       std::string aName = anObject->data()->name();
281       myTextLine->setText(QString::fromStdString(aName));
282     } else {
283       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
284       if (aRefAttr && aRefAttr->attr().get() != NULL) {
285         //myIsObject = aRefAttr->isObject();
286         AttributePtr anAttr = aRefAttr->attr();
287         if (anAttr.get() != NULL) {
288           std::stringstream aName;
289           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
290           myTextLine->setText(QString::fromStdString(aName.str()));
291         }
292       }
293       else {
294         myTextLine->setText(getDefaultValue().c_str());
295       }
296     }
297   }
298 }
299
300
301 //********************************************************************
302 void ModuleBase_WidgetShapeSelector::activateSelection(bool toActivate)
303 {
304   updateSelectionName();
305
306   if (toActivate) {
307     QIntList aList;
308     foreach (QString aType, myShapeTypes) {
309       aList.append(ModuleBase_Tools::shapeType(aType));
310     }
311     myWorkshop->activateSubShapesSelection(aList);
312   } else {
313     myWorkshop->deactivateSubShapesSelection();
314   }
315 }
316
317 //********************************************************************
318 void ModuleBase_WidgetShapeSelector::raisePanel() const
319 {
320   QWidget* aParent = this->parentWidget();
321   QWidget* aLastPanel = 0;
322   while (!aParent->inherits("QDockWidget")) {
323     aLastPanel = aParent;
324     aParent = aParent->parentWidget();
325     if (!aParent)
326       return;
327   }
328   if (aParent->inherits("QDockWidget")) {
329     QDockWidget* aTabWgt = (QDockWidget*) aParent;
330     aTabWgt->raise();
331   }
332 }
333
334 //********************************************************************
335 void ModuleBase_WidgetShapeSelector::activateCustom()
336 {
337   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
338   activateSelection(true);
339
340   // Restore selection in the viewer by the attribute selection list
341   myWorkshop->setSelected(getAttributeSelection());
342
343   activateFilters(myWorkshop, true);
344 }
345
346 //********************************************************************
347 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
348 {
349   DataPtr aData = myFeature->data();
350   AttributePtr anAttribute = myFeature->attribute(attributeID());
351
352   myObject = GeomValidators_Tools::getObject(anAttribute);
353   myShape = getShape();
354   myRefAttribute = AttributePtr();
355   myIsObject = false;
356   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
357   if (aRefAttr) {
358     myIsObject = aRefAttr->isObject();
359     myRefAttribute = aRefAttr->attr();
360   }
361 }
362
363 //********************************************************************
364 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
365 {
366   DataPtr aData = myFeature->data();
367   AttributePtr anAttribute = myFeature->attribute(attributeID());
368
369   setObject(myObject, myShape);
370   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
371   if (aRefAttr) {
372     if (!myIsObject)
373       aRefAttr->setAttr(myRefAttribute);
374   }
375 }
376
377 //********************************************************************
378 void ModuleBase_WidgetShapeSelector::customValidators(
379                                     std::list<ModelAPI_Validator*>& theValidators,
380                                     std::list<std::list<std::string> >& theArguments) const
381 {
382   theValidators.push_back(myShapeValidator);
383
384   std::list<std::string> anArguments;
385   foreach(QString aType, myShapeTypes) {
386     anArguments.push_back(aType.toStdString().c_str());
387   }
388   theArguments.push_back(anArguments);
389 }
390
391 //********************************************************************
392 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
393 {
394   bool isDone = false;
395
396   // It should be checked by corresponded validator
397   ObjectPtr aObject = thePrs.object();
398   ObjectPtr aCurrentObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
399   /*
400   if ((!aCurrentObject) && (!aObject))
401     return false;*/
402
403   // It should be checked by corresponded validator
404   // Check that the selected object is result (others can not be accepted)
405   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aObject);
406   if (!aRes)
407     return false;
408   /*if (myFeature) {
409     // We can not select a result of our feature
410     const std::list<std::shared_ptr<ModelAPI_Result>>& aResList = myFeature->results();
411     std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
412     for (aIt = aResList.cbegin(); aIt != aResList.cend(); ++aIt) {
413       if ((*aIt) == aRes)
414         return false;
415     }
416   }
417   */
418   // It should be checked by corresponded validator
419   /*
420   // Check that object belongs to active document or PartSet
421   DocumentPtr aDoc = aRes->document();
422   SessionPtr aMgr = ModelAPI_Session::get();
423   if (!(aDoc == aMgr->activeDocument()) && !(aDoc == aMgr->moduleDocument()))
424     return false;*/
425
426   // It should be checked by corresponded validator
427   // Check that the result has a shape
428   GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
429   if (!aShape)
430     return false;
431
432   // Get sub-shapes from local selection
433   if (!thePrs.shape().IsNull()) {
434     aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
435     aShape->setImpl(new TopoDS_Shape(thePrs.shape()));
436   }
437   // Check that the selection corresponds to selection type
438   //if (!acceptSubShape(aShape))
439   //  return false;
440
441   setObject(aObject, aShape);
442   return true;
443 }
444
445 //********************************************************************
446 void ModuleBase_WidgetShapeSelector::deactivate()
447 {
448   activateSelection(false);
449   activateFilters(myWorkshop, false);
450   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
451 }