Salome HOME
ModuleBase_WidgetShapeSelector correction to process AttrRefAtt like MultiSelector...
[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 void ModuleBase_WidgetShapeSelector::clearAttribute()
165 {
166   // In order to make reselection possible, set empty object and shape should be done
167   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
168 }
169
170 //********************************************************************
171 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
172 {
173   bool isBlocked = this->blockSignals(true);
174   updateSelectionName();
175   this->blockSignals(isBlocked);
176
177   return true;
178 }
179
180 //********************************************************************
181 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
182 {
183   QList<QWidget*> aControls;
184   aControls.append(myTextLine);
185   return aControls;
186 }
187
188 void ModuleBase_WidgetShapeSelector::updateFocus()
189 {
190   emit focusOutWidget(this);
191 }
192
193 //********************************************************************
194 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
195 {
196   QIntList aShapeTypes;
197   foreach(QString aType, myShapeTypes) {
198     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
199   }
200   return aShapeTypes;
201 }
202
203 //********************************************************************
204 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
205 {
206   GeomShapePtr aShape;
207   DataPtr aData = myFeature->data();
208   if (!aData->isValid())
209     return aShape;
210
211   AttributeSelectionPtr aSelect = aData->selection(attributeID());
212   if (aSelect)
213     aShape = aSelect->value();
214
215   return aShape;
216 }
217
218 //********************************************************************
219 void ModuleBase_WidgetShapeSelector::updateSelectionName()
220 {
221   DataPtr aData = myFeature->data();
222   if (!aData->isValid())
223     return;
224
225   bool isNameUpdated = false;
226   AttributeSelectionPtr aSelect = aData->selection(attributeID());
227   if (aSelect) {
228     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
229     isNameUpdated = true;
230   }
231   if (!isNameUpdated) {
232     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
233     if (anObject.get() != NULL) {
234       std::string aName = anObject->data()->name();
235       myTextLine->setText(QString::fromStdString(aName));
236     } else {
237       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
238       if (aRefAttr && aRefAttr->attr().get() != NULL) {
239         //myIsObject = aRefAttr->isObject();
240         std::string anAttrName = generateName(aRefAttr->attr());
241         myTextLine->setText(QString::fromStdString(anAttrName));
242       }
243       else {
244         myTextLine->setText(getDefaultValue().c_str());
245       }
246     }
247   }
248 }
249
250 //********************************************************************
251 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
252 {
253   ModuleBase_WidgetValidated::storeAttributeValue();
254
255   DataPtr aData = myFeature->data();
256   AttributePtr anAttribute = myFeature->attribute(attributeID());
257
258   myObject = ModuleBase_Tools::getObject(anAttribute);
259   myShape = getShape();
260   myRefAttribute = AttributePtr();
261   myIsObject = false;
262   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
263   if (aRefAttr) {
264     myIsObject = aRefAttr->isObject();
265     myRefAttribute = aRefAttr->attr();
266   }
267 }
268
269 //********************************************************************
270 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
271 {
272   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
273
274   DataPtr aData = myFeature->data();
275   AttributePtr anAttribute = myFeature->attribute(attributeID());
276
277   setObject(myObject, myShape);
278   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
279   if (aRefAttr) {
280     if (!myIsObject)
281       aRefAttr->setAttr(myRefAttribute);
282   }
283 }