Salome HOME
Make ModuleBase_ModelWidget::restoreValue() non-virtual and create virtual ModuleBase...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetShapeSelector.cpp
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 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
71                                                      ModuleBase_IWorkshop* theWorkshop,
72                                                      const Config_WidgetAPI* theData,
73                                                      const std::string& theParentId)
74  : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
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 }
102
103 //********************************************************************
104 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
105 {
106   // the value is stored on the selection changed signal processing 
107   return true;
108 }
109
110 //********************************************************************
111 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
112                                                GeomShapePtr theShape)
113 {
114   DataPtr aData = myFeature->data();
115   AttributeReferencePtr aRef = aData->reference(attributeID());
116   if (aRef) {
117     ObjectPtr aObject = aRef->value();
118     if (!(aObject && aObject->isSame(theSelectedObject))) {
119       aRef->setValue(theSelectedObject);
120     }
121   } else {
122     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
123     if (aRefAttr) {
124       ObjectPtr aObject = aRefAttr->object();
125       if (!(aObject && aObject->isSame(theSelectedObject))) {
126         aRefAttr->setObject(theSelectedObject);
127       }
128     } else {
129       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
130       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
131       if (aSelectAttr.get() != NULL) {
132         aSelectAttr->setValue(aResult, theShape);
133       }
134     }
135   }
136 }
137
138 //********************************************************************
139 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
140 {
141   QList<ModuleBase_ViewerPrs> aSelected;
142   if(myFeature) {
143     DataPtr aData = myFeature->data();
144     AttributePtr anAttribute = myFeature->attribute(attributeID());
145
146     ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
147     TopoDS_Shape aShape;
148     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
149     if (aShapePtr.get()) {
150       aShape = aShapePtr->impl<TopoDS_Shape>();
151     }
152     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
153     aSelected.append(aPrs);
154   }
155   return aSelected;
156 }
157
158 //********************************************************************
159 void ModuleBase_WidgetShapeSelector::clearAttribute()
160 {
161   // In order to make reselection possible, set empty object and shape should be done
162   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
163 }
164
165 //********************************************************************
166 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
167 {
168   bool isBlocked = this->blockSignals(true);
169   updateSelectionName();
170   this->blockSignals(isBlocked);
171
172   return true;
173 }
174
175 //********************************************************************
176 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
177 {
178   QList<QWidget*> aControls;
179   aControls.append(myTextLine);
180   return aControls;
181 }
182
183 void ModuleBase_WidgetShapeSelector::updateFocus()
184 {
185   emit focusOutWidget(this);
186 }
187
188 //********************************************************************
189 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
190 {
191   QIntList aShapeTypes;
192   foreach(QString aType, myShapeTypes) {
193     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
194   }
195   return aShapeTypes;
196 }
197
198 //********************************************************************
199 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
200 {
201   GeomShapePtr aShape;
202   DataPtr aData = myFeature->data();
203   if (!aData->isValid())
204     return aShape;
205
206   AttributeSelectionPtr aSelect = aData->selection(attributeID());
207   if (aSelect)
208     aShape = aSelect->value();
209
210   return aShape;
211 }
212
213 //********************************************************************
214 void ModuleBase_WidgetShapeSelector::updateSelectionName()
215 {
216   DataPtr aData = myFeature->data();
217   if (!aData->isValid())
218     return;
219
220   bool isNameUpdated = false;
221   AttributeSelectionPtr aSelect = aData->selection(attributeID());
222   if (aSelect) {
223     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
224     isNameUpdated = true;
225   }
226   if (!isNameUpdated) {
227     ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
228     if (anObject.get() != NULL) {
229       std::string aName = anObject->data()->name();
230       myTextLine->setText(QString::fromStdString(aName));
231     } else {
232       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
233       if (aRefAttr && aRefAttr->attr().get() != NULL) {
234         //myIsObject = aRefAttr->isObject();
235         AttributePtr anAttr = aRefAttr->attr();
236         if (anAttr.get() != NULL) {
237           std::stringstream aName;
238           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
239           myTextLine->setText(QString::fromStdString(aName.str()));
240         }
241       }
242       else {
243         myTextLine->setText(getDefaultValue().c_str());
244       }
245     }
246   }
247 }
248
249 //********************************************************************
250 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
251 {
252   DataPtr aData = myFeature->data();
253   AttributePtr anAttribute = myFeature->attribute(attributeID());
254
255   myObject = GeomValidators_Tools::getObject(anAttribute);
256   myShape = getShape();
257   myRefAttribute = AttributePtr();
258   myIsObject = false;
259   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
260   if (aRefAttr) {
261     myIsObject = aRefAttr->isObject();
262     myRefAttribute = aRefAttr->attr();
263   }
264 }
265
266 //********************************************************************
267 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
268 {
269   DataPtr aData = myFeature->data();
270   AttributePtr anAttribute = myFeature->attribute(attributeID());
271
272   setObject(myObject, myShape);
273   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
274   if (aRefAttr) {
275     if (!myIsObject)
276       aRefAttr->setAttr(myRefAttribute);
277   }
278 }