Salome HOME
Issue #1330 correction for attribute ref attr list. Crash of rectangle creation
[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 #include <ModuleBase_IModule.h>
16
17 #include <Config_WidgetAPI.h>
18 #include <Events_Loop.h>
19 #include <Events_Message.h>
20 #include <GeomAPI_Interface.h>
21 #include <GeomAPI_Shape.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
39 #include <Config_WidgetAPI.h>
40 #include <Events_Error.h>
41
42 #include <GeomAPI_Shape.h>
43
44 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopExp_Explorer.hxx>
47
48 #include <QWidget>
49 #include <QLayout>
50 #include <QLabel>
51 #include <QLineEdit>
52 #include <QToolButton>
53 #include <QString>
54 #include <QEvent>
55 #include <QDockWidget>
56 #include <QApplication>
57 #include <QFormLayout>
58
59 #include <TopExp_Explorer.hxx>
60 #include <TopoDS_Shape.hxx>
61
62 #include <memory>
63
64 #include <list>
65 #include <string>
66
67 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
68                                                      ModuleBase_IWorkshop* theWorkshop,
69                                                      const Config_WidgetAPI* theData,
70                                                      const std::string& theParentId)
71  : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
72 {
73   QFormLayout* aLayout = new QFormLayout(this);
74   ModuleBase_Tools::adjustMargins(aLayout);
75
76   QString aLabelText = QString::fromStdString(theData->widgetLabel());
77   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
78   myLabel = new QLabel(aLabelText, this);
79   if (!aLabelIcon.isEmpty())
80     myLabel->setPixmap(QPixmap(aLabelIcon));
81
82
83   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
84   myTextLine = new QLineEdit(this);
85   QString anObjName = QString::fromStdString(attributeID());
86   myTextLine->setObjectName(anObjName);
87   myTextLine->setReadOnly(true);
88   myTextLine->setToolTip(aToolTip);
89   myTextLine->installEventFilter(this);
90
91   aLayout->addRow(myLabel, myTextLine);
92   myLabel->setToolTip(aToolTip);
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   std::string aType = aData->attribute(attributeID())->attributeType();
116   if (aType == ModelAPI_AttributeReference::typeId()) {
117     AttributeReferencePtr aRef = aData->reference(attributeID());
118     ObjectPtr aObject = aRef->value();
119     if (!(aObject && aObject->isSame(theSelectedObject))) {
120       aRef->setValue(theSelectedObject);
121     }
122   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
123     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
124
125     AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
126     if (anAttribute.get())
127       aRefAttr->setAttr(anAttribute);
128     else {
129       ObjectPtr aObject = aRefAttr->object();
130       if (!(aObject && aObject->isSame(theSelectedObject))) {
131         aRefAttr->setObject(theSelectedObject);
132       }
133     }
134   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
135     AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
136     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
137     if (aSelectAttr.get() != NULL) {
138       aSelectAttr->setValue(aResult, theShape);
139     }
140   }
141 }
142
143 //********************************************************************
144 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
145 {
146   QList<ModuleBase_ViewerPrs> aSelected;
147   if(myFeature) {
148     DataPtr aData = myFeature->data();
149     AttributePtr anAttribute = myFeature->attribute(attributeID());
150
151     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
152     TopoDS_Shape aShape;
153     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
154     if (aShapePtr.get()) {
155       aShape = aShapePtr->impl<TopoDS_Shape>();
156     }
157     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
158     aSelected.append(aPrs);
159   }
160   return aSelected;
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   std::string aType = aData->attribute(attributeID())->attributeType();
205   if (aType == ModelAPI_AttributeReference::typeId()) {
206   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
207     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
208     if (aRefAttr.get() && !aRefAttr->isObject()) {
209       AttributePtr anAttribute = aRefAttr->attr();
210       aShape = myWorkshop->module()->findShape(anAttribute);
211     }
212   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
213     AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
214     aShape = aSelectAttr->value();
215   }
216
217   return aShape;
218 }
219
220 //********************************************************************
221 void ModuleBase_WidgetShapeSelector::updateSelectionName()
222 {
223   DataPtr aData = myFeature->data();
224   if (!aData->isValid())
225     return;
226
227   bool isNameUpdated = false;
228   AttributeSelectionPtr aSelect = aData->selection(attributeID());
229   if (aSelect) {
230     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
231     isNameUpdated = true;
232   }
233   if (!isNameUpdated) {
234     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
235     if (anObject.get() != NULL) {
236       std::string aName = anObject->data()->name();
237       myTextLine->setText(QString::fromStdString(aName));
238     } else {
239       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
240       if (aRefAttr && aRefAttr->attr().get() != NULL) {
241         //myIsObject = aRefAttr->isObject();
242         std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
243         myTextLine->setText(QString::fromStdString(anAttrName));
244       }
245       else {
246         myTextLine->setText(getDefaultValue().c_str());
247       }
248     }
249   }
250 }
251
252 //********************************************************************
253 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
254 {
255   ModuleBase_WidgetValidated::storeAttributeValue();
256
257   DataPtr aData = myFeature->data();
258   AttributePtr anAttribute = myFeature->attribute(attributeID());
259
260   myObject = ModuleBase_Tools::getObject(anAttribute);
261   myShape = getShape();
262   myRefAttribute = AttributePtr();
263   myIsObject = false;
264   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
265   if (aRefAttr) {
266     myIsObject = aRefAttr->isObject();
267     myRefAttribute = aRefAttr->attr();
268   }
269 }
270
271 //********************************************************************
272 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
273 {
274   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
275
276   DataPtr aData = myFeature->data();
277   AttributePtr anAttribute = myFeature->attribute(attributeID());
278
279   setObject(myObject, myShape);
280   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
281   if (aRefAttr) {
282     if (!myIsObject)
283       aRefAttr->setAttr(myRefAttribute);
284   }
285 }