Salome HOME
correction of Model_Data for AttributeRefAttrList
[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 <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_AttributeValidator.h>
37
38 #include <Config_WidgetAPI.h>
39 #include <Events_Error.h>
40
41 #include <GeomAPI_Shape.h>
42
43 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopExp_Explorer.hxx>
46
47 #include <QWidget>
48 #include <QLayout>
49 #include <QLabel>
50 #include <QLineEdit>
51 #include <QToolButton>
52 #include <QString>
53 #include <QEvent>
54 #include <QDockWidget>
55 #include <QApplication>
56 #include <QFormLayout>
57
58 #include <TopExp_Explorer.hxx>
59 #include <TopoDS_Shape.hxx>
60
61 #include <memory>
62
63 #include <list>
64 #include <string>
65
66 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
67                                                      ModuleBase_IWorkshop* theWorkshop,
68                                                      const Config_WidgetAPI* theData,
69                                                      const std::string& theParentId)
70  : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
71 {
72   QFormLayout* aLayout = new QFormLayout(this);
73   ModuleBase_Tools::adjustMargins(aLayout);
74
75   QString aLabelText = QString::fromStdString(theData->widgetLabel());
76   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
77   myLabel = new QLabel(aLabelText, this);
78   if (!aLabelIcon.isEmpty())
79     myLabel->setPixmap(QPixmap(aLabelIcon));
80
81
82   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
83   myTextLine = new QLineEdit(this);
84   QString anObjName = QString::fromStdString(attributeID());
85   myTextLine->setObjectName(anObjName);
86   myTextLine->setReadOnly(true);
87   myTextLine->setToolTip(aToolTip);
88   myTextLine->installEventFilter(this);
89
90   aLayout->addRow(myLabel, myTextLine);
91   myLabel->setToolTip(aToolTip);
92
93   std::string aTypes = theData->getProperty("shape_types");
94   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
95 }
96
97 //********************************************************************
98 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
99 {
100 }
101
102 //********************************************************************
103 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
104 {
105   // the value is stored on the selection changed signal processing 
106   return true;
107 }
108
109 //********************************************************************
110 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
111                                                GeomShapePtr theShape)
112 {
113   DataPtr aData = myFeature->data();
114   std::string aType = aData->attribute(attributeID())->attributeType();
115   if (aType == ModelAPI_AttributeReference::typeId()) {
116     AttributeReferencePtr aRef = aData->reference(attributeID());
117     ObjectPtr aObject = aRef->value();
118     if (!(aObject && aObject->isSame(theSelectedObject))) {
119       aRef->setValue(theSelectedObject);
120     }
121   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
122     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
123     ObjectPtr aObject = aRefAttr->object();
124     if (!(aObject && aObject->isSame(theSelectedObject))) {
125       aRefAttr->setObject(theSelectedObject);
126     }
127   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
128     AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
129     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
130     if (aSelectAttr.get() != NULL) {
131       aSelectAttr->setValue(aResult, theShape);
132     }
133   }
134 }
135
136 //********************************************************************
137 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
138 {
139   QList<ModuleBase_ViewerPrs> aSelected;
140   if(myFeature) {
141     DataPtr aData = myFeature->data();
142     AttributePtr anAttribute = myFeature->attribute(attributeID());
143
144     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
145     TopoDS_Shape aShape;
146     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
147     if (aShapePtr.get()) {
148       aShape = aShapePtr->impl<TopoDS_Shape>();
149     }
150     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
151     aSelected.append(aPrs);
152   }
153   return aSelected;
154 }
155
156 //********************************************************************
157 void ModuleBase_WidgetShapeSelector::clearAttribute()
158 {
159   // In order to make reselection possible, set empty object and shape should be done
160   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
161 }
162
163 //********************************************************************
164 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
165 {
166   bool isBlocked = this->blockSignals(true);
167   updateSelectionName();
168   this->blockSignals(isBlocked);
169
170   return true;
171 }
172
173 //********************************************************************
174 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
175 {
176   QList<QWidget*> aControls;
177   aControls.append(myTextLine);
178   return aControls;
179 }
180
181 void ModuleBase_WidgetShapeSelector::updateFocus()
182 {
183   emit focusOutWidget(this);
184 }
185
186 //********************************************************************
187 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
188 {
189   QIntList aShapeTypes;
190   foreach(QString aType, myShapeTypes) {
191     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
192   }
193   return aShapeTypes;
194 }
195
196 //********************************************************************
197 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
198 {
199   GeomShapePtr aShape;
200   DataPtr aData = myFeature->data();
201   if (!aData->isValid())
202     return aShape;
203
204   AttributeSelectionPtr aSelect = aData->selection(attributeID());
205   if (aSelect)
206     aShape = aSelect->value();
207
208   return aShape;
209 }
210
211 //********************************************************************
212 void ModuleBase_WidgetShapeSelector::updateSelectionName()
213 {
214   DataPtr aData = myFeature->data();
215   if (!aData->isValid())
216     return;
217
218   bool isNameUpdated = false;
219   AttributeSelectionPtr aSelect = aData->selection(attributeID());
220   if (aSelect) {
221     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
222     isNameUpdated = true;
223   }
224   if (!isNameUpdated) {
225     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
226     if (anObject.get() != NULL) {
227       std::string aName = anObject->data()->name();
228       myTextLine->setText(QString::fromStdString(aName));
229     } else {
230       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
231       if (aRefAttr && aRefAttr->attr().get() != NULL) {
232         //myIsObject = aRefAttr->isObject();
233         std::string anAttrName = generateName(aRefAttr->attr());
234         myTextLine->setText(QString::fromStdString(anAttrName));
235       }
236       else {
237         myTextLine->setText(getDefaultValue().c_str());
238       }
239     }
240   }
241 }
242
243 //********************************************************************
244 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
245 {
246   ModuleBase_WidgetValidated::storeAttributeValue();
247
248   DataPtr aData = myFeature->data();
249   AttributePtr anAttribute = myFeature->attribute(attributeID());
250
251   myObject = ModuleBase_Tools::getObject(anAttribute);
252   myShape = getShape();
253   myRefAttribute = AttributePtr();
254   myIsObject = false;
255   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
256   if (aRefAttr) {
257     myIsObject = aRefAttr->isObject();
258     myRefAttribute = aRefAttr->attr();
259   }
260 }
261
262 //********************************************************************
263 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
264 {
265   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
266
267   DataPtr aData = myFeature->data();
268   AttributePtr anAttribute = myFeature->attribute(attributeID());
269
270   setObject(myObject, myShape);
271   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
272   if (aRefAttr) {
273     if (!myIsObject)
274       aRefAttr->setAttr(myRefAttribute);
275   }
276 }
277
278 //********************************************************************
279 bool ModuleBase_WidgetShapeSelector::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
280 {
281   ObjectPtr anObject;
282   GeomShapePtr aShape;
283   getGeomSelection(thePrs, anObject, aShape);
284
285   setObject(anObject, aShape);
286   return true;
287 }