Salome HOME
Remove linking to GeomValidators.
[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   myTextLine->setReadOnly(true);
85   myTextLine->setToolTip(aToolTip);
86   myTextLine->installEventFilter(this);
87
88   aLayout->addRow(myLabel, myTextLine);
89   myLabel->setToolTip(aToolTip);
90
91   std::string aTypes = theData->getProperty("shape_types");
92   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
93 }
94
95 //********************************************************************
96 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
97 {
98 }
99
100 //********************************************************************
101 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
102 {
103   // the value is stored on the selection changed signal processing 
104   return true;
105 }
106
107 //********************************************************************
108 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
109                                                GeomShapePtr theShape)
110 {
111   DataPtr aData = myFeature->data();
112   AttributeReferencePtr aRef = aData->reference(attributeID());
113   if (aRef) {
114     ObjectPtr aObject = aRef->value();
115     if (!(aObject && aObject->isSame(theSelectedObject))) {
116       aRef->setValue(theSelectedObject);
117     }
118   } else {
119     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
120     if (aRefAttr) {
121       ObjectPtr aObject = aRefAttr->object();
122       if (!(aObject && aObject->isSame(theSelectedObject))) {
123         aRefAttr->setObject(theSelectedObject);
124       }
125     } else {
126       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
127       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
128       if (aSelectAttr.get() != NULL) {
129         aSelectAttr->setValue(aResult, theShape);
130       }
131     }
132   }
133 }
134
135 //********************************************************************
136 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
137 {
138   QList<ModuleBase_ViewerPrs> aSelected;
139   if(myFeature) {
140     DataPtr aData = myFeature->data();
141     AttributePtr anAttribute = myFeature->attribute(attributeID());
142
143     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
144     TopoDS_Shape aShape;
145     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
146     if (aShapePtr.get()) {
147       aShape = aShapePtr->impl<TopoDS_Shape>();
148     }
149     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
150     aSelected.append(aPrs);
151   }
152   return aSelected;
153 }
154
155 //********************************************************************
156 void ModuleBase_WidgetShapeSelector::clearAttribute()
157 {
158   // In order to make reselection possible, set empty object and shape should be done
159   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
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   AttributeSelectionPtr aSelect = aData->selection(attributeID());
204   if (aSelect)
205     aShape = aSelect->value();
206
207   return aShape;
208 }
209
210 //********************************************************************
211 void ModuleBase_WidgetShapeSelector::updateSelectionName()
212 {
213   DataPtr aData = myFeature->data();
214   if (!aData->isValid())
215     return;
216
217   bool isNameUpdated = false;
218   AttributeSelectionPtr aSelect = aData->selection(attributeID());
219   if (aSelect) {
220     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
221     isNameUpdated = true;
222   }
223   if (!isNameUpdated) {
224     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
225     if (anObject.get() != NULL) {
226       std::string aName = anObject->data()->name();
227       myTextLine->setText(QString::fromStdString(aName));
228     } else {
229       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
230       if (aRefAttr && aRefAttr->attr().get() != NULL) {
231         //myIsObject = aRefAttr->isObject();
232         AttributePtr anAttr = aRefAttr->attr();
233         if (anAttr.get() != NULL) {
234           std::stringstream aName;
235           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
236           myTextLine->setText(QString::fromStdString(aName.str()));
237         }
238       }
239       else {
240         myTextLine->setText(getDefaultValue().c_str());
241       }
242     }
243   }
244 }
245
246 //********************************************************************
247 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
248 {
249   ModuleBase_WidgetValidated::storeAttributeValue();
250
251   DataPtr aData = myFeature->data();
252   AttributePtr anAttribute = myFeature->attribute(attributeID());
253
254   myObject = ModuleBase_Tools::getObject(anAttribute);
255   myShape = getShape();
256   myRefAttribute = AttributePtr();
257   myIsObject = false;
258   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
259   if (aRefAttr) {
260     myIsObject = aRefAttr->isObject();
261     myRefAttribute = aRefAttr->attr();
262   }
263 }
264
265 //********************************************************************
266 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
267 {
268   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
269
270   DataPtr aData = myFeature->data();
271   AttributePtr anAttribute = myFeature->attribute(attributeID());
272
273   setObject(myObject, myShape);
274   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
275   if (aRefAttr) {
276     if (!myIsObject)
277       aRefAttr->setAttr(myRefAttribute);
278   }
279 }