Salome HOME
Issue #805 In the sketch presentation, show both the parameter and its value: minor...
[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 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
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
124     AttributePtr anAttribute = myWorkshop->module()->findAttribute(theSelectedObject, theShape);
125     if (anAttribute.get())
126       aRefAttr->setAttr(anAttribute);
127     else {
128       ObjectPtr aObject = aRefAttr->object();
129       if (!(aObject && aObject->isSame(theSelectedObject))) {
130         aRefAttr->setObject(theSelectedObject);
131       }
132     }
133   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
134     AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
135     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
136     if (aSelectAttr.get() != NULL) {
137       aSelectAttr->setValue(aResult, theShape);
138     }
139   }
140 }
141
142 //********************************************************************
143 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
144 {
145   QList<ModuleBase_ViewerPrs> aSelected;
146   if(myFeature) {
147     DataPtr aData = myFeature->data();
148     AttributePtr anAttribute = myFeature->attribute(attributeID());
149
150     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
151     TopoDS_Shape aShape;
152     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
153     if (aShapePtr.get()) {
154       aShape = aShapePtr->impl<TopoDS_Shape>();
155     }
156     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
157     aSelected.append(aPrs);
158   }
159   return aSelected;
160 }
161
162 //********************************************************************
163 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
164 {
165   bool isBlocked = this->blockSignals(true);
166   updateSelectionName();
167   this->blockSignals(isBlocked);
168
169   return true;
170 }
171
172 //********************************************************************
173 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
174 {
175   QList<QWidget*> aControls;
176   aControls.append(myTextLine);
177   return aControls;
178 }
179
180 void ModuleBase_WidgetShapeSelector::updateFocus()
181 {
182   emit focusOutWidget(this);
183 }
184
185 //********************************************************************
186 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
187 {
188   QIntList aShapeTypes;
189   foreach(QString aType, myShapeTypes) {
190     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
191   }
192   return aShapeTypes;
193 }
194
195 //********************************************************************
196 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
197 {
198   GeomShapePtr aShape;
199   DataPtr aData = myFeature->data();
200   if (!aData->isValid())
201     return aShape;
202
203   std::string aType = aData->attribute(attributeID())->attributeType();
204   if (aType == ModelAPI_AttributeReference::typeId()) {
205   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
206     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
207     if (aRefAttr.get() && !aRefAttr->isObject()) {
208       AttributePtr anAttribute = aRefAttr->attr();
209       aShape = myWorkshop->module()->findShape(anAttribute);
210     }
211   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
212     AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
213     aShape = aSelectAttr->value();
214   }
215
216   return aShape;
217 }
218
219 //********************************************************************
220 void ModuleBase_WidgetShapeSelector::updateSelectionName()
221 {
222   DataPtr aData = myFeature->data();
223   if (!aData->isValid())
224     return;
225
226   bool isNameUpdated = false;
227   AttributeSelectionPtr aSelect = aData->selection(attributeID());
228   if (aSelect) {
229     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
230     isNameUpdated = true;
231   }
232   if (!isNameUpdated) {
233     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
234     if (anObject.get() != NULL) {
235       std::string aName = anObject->data()->name();
236       myTextLine->setText(QString::fromStdString(aName));
237     } else {
238       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
239       if (aRefAttr && aRefAttr->attr().get() != NULL) {
240         //myIsObject = aRefAttr->isObject();
241         std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
242         myTextLine->setText(QString::fromStdString(anAttrName));
243       }
244       else {
245         myTextLine->setText(getDefaultValue().c_str());
246       }
247     }
248   }
249 }
250
251 //********************************************************************
252 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
253 {
254   ModuleBase_WidgetValidated::storeAttributeValue();
255
256   DataPtr aData = myFeature->data();
257   AttributePtr anAttribute = myFeature->attribute(attributeID());
258
259   myObject = ModuleBase_Tools::getObject(anAttribute);
260   myShape = getShape();
261   myRefAttribute = AttributePtr();
262   myIsObject = false;
263   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
264   if (aRefAttr) {
265     myIsObject = aRefAttr->isObject();
266     myRefAttribute = aRefAttr->attr();
267   }
268 }
269
270 //********************************************************************
271 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
272 {
273   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
274
275   DataPtr aData = myFeature->data();
276   AttributePtr anAttribute = myFeature->attribute(attributeID());
277
278   setObject(myObject, myShape);
279   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
280   if (aRefAttr) {
281     if (!myIsObject)
282       aRefAttr->setAttr(myRefAttribute);
283   }
284 }