]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
Salome HOME
A separation of WidgetSelector class in order to unite the logic of shape/multi shape...
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetShapeSelector.h
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 <GeomValidators_ShapeType.h>
17
18 #include <Config_WidgetAPI.h>
19 #include <Events_Loop.h>
20 #include <Events_Message.h>
21 #include <GeomAPI_Interface.h>
22 #include <GeomAPI_Shape.h>
23 #include <GeomValidators_Tools.h>
24
25 #include <ModelAPI_AttributeReference.h>
26 #include <ModelAPI_Data.h>
27 #include <ModelAPI_Document.h>
28 #include <ModelAPI_Events.h>
29 #include <ModelAPI_Feature.h>
30 #include <ModelAPI_Result.h>
31 #include <ModelAPI_ResultConstruction.h>
32 #include <ModelAPI_AttributeReference.h>
33 #include <ModelAPI_AttributeSelection.h>
34 #include <ModelAPI_Session.h>
35 #include <ModelAPI_Tools.h>
36 #include <ModelAPI_ResultBody.h>
37 #include <ModelAPI_AttributeRefAttr.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_AttributeValidator.h>
40 #include <ModelAPI_ShapeValidator.h>
41
42 #include <Config_WidgetAPI.h>
43 #include <Events_Error.h>
44
45 #include <GeomAPI_Shape.h>
46
47 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopExp_Explorer.hxx>
50
51 #include <QWidget>
52 #include <QLayout>
53 #include <QLabel>
54 #include <QLineEdit>
55 #include <QToolButton>
56 #include <QString>
57 #include <QEvent>
58 #include <QDockWidget>
59 #include <QApplication>
60 #include <QFormLayout>
61
62 #include <TopExp_Explorer.hxx>
63 #include <TopoDS_Shape.hxx>
64
65 #include <memory>
66
67 #include <list>
68 #include <string>
69
70 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
71                                                      ModuleBase_IWorkshop* theWorkshop,
72                                                      const Config_WidgetAPI* theData,
73                                                      const std::string& theParentId)
74  : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
75 {
76   QFormLayout* aLayout = new QFormLayout(this);
77   ModuleBase_Tools::adjustMargins(aLayout);
78
79   QString aLabelText = QString::fromStdString(theData->widgetLabel());
80   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
81   myLabel = new QLabel(aLabelText, this);
82   if (!aLabelIcon.isEmpty())
83     myLabel->setPixmap(QPixmap(aLabelIcon));
84
85
86   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
87   myTextLine = new QLineEdit(this);
88   myTextLine->setReadOnly(true);
89   myTextLine->setToolTip(aToolTip);
90   myTextLine->installEventFilter(this);
91
92   aLayout->addRow(myLabel, myTextLine);
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   AttributeReferencePtr aRef = aData->reference(attributeID());
116   if (aRef) {
117     ObjectPtr aObject = aRef->value();
118     if (!(aObject && aObject->isSame(theSelectedObject))) {
119       aRef->setValue(theSelectedObject);
120     }
121   } else {
122     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
123     if (aRefAttr) {
124       ObjectPtr aObject = aRefAttr->object();
125       if (!(aObject && aObject->isSame(theSelectedObject))) {
126         aRefAttr->setObject(theSelectedObject);
127       }
128     } else {
129       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
130       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
131       if (aSelectAttr.get() != NULL) {
132         aSelectAttr->setValue(aResult, theShape);
133       }
134     }
135   }
136 }
137
138 //********************************************************************
139 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
140 {
141   QList<ModuleBase_ViewerPrs> aSelected;
142   if(myFeature) {
143     DataPtr aData = myFeature->data();
144     AttributePtr anAttribute = myFeature->attribute(attributeID());
145
146     ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
147     TopoDS_Shape aShape;
148     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
149     if (aShapePtr.get()) {
150       aShape = aShapePtr->impl<TopoDS_Shape>();
151     }
152     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
153     aSelected.append(aPrs);
154   }
155   return aSelected;
156 }
157
158 //********************************************************************
159 void ModuleBase_WidgetShapeSelector::clearAttribute()
160 {
161   // In order to make reselection possible, set empty object and shape should be done
162   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
163 }
164
165 //********************************************************************
166 bool ModuleBase_WidgetShapeSelector::restoreValue()
167 {
168   bool isBlocked = this->blockSignals(true);
169   updateSelectionName();
170   this->blockSignals(isBlocked);
171
172   return true;
173 }
174
175 //********************************************************************
176 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
177 {
178   QList<QWidget*> aControls;
179   aControls.append(myTextLine);
180   return aControls;
181 }
182
183 void ModuleBase_WidgetShapeSelector::updateFocus()
184 {
185   ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
186   if (anObject.get())
187     emit focusOutWidget(this);
188 }
189
190 //********************************************************************
191 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
192 {
193   QIntList aShapeTypes;
194   foreach(QString aType, myShapeTypes) {
195     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
196   }
197   return aShapeTypes;
198 }
199
200 //********************************************************************
201 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
202 {
203   GeomShapePtr aShape;
204   DataPtr aData = myFeature->data();
205   if (!aData->isValid())
206     return aShape;
207
208   AttributeSelectionPtr aSelect = aData->selection(attributeID());
209   if (aSelect)
210     aShape = aSelect->value();
211
212   return aShape;
213 }
214
215 //********************************************************************
216 void ModuleBase_WidgetShapeSelector::updateSelectionName()
217 {
218   DataPtr aData = myFeature->data();
219   if (!aData->isValid())
220     return;
221
222   bool isNameUpdated = false;
223   AttributeSelectionPtr aSelect = aData->selection(attributeID());
224   if (aSelect) {
225     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
226     isNameUpdated = true;
227   }
228   if (!isNameUpdated) {
229     ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
230     if (anObject.get() != NULL) {
231       std::string aName = anObject->data()->name();
232       myTextLine->setText(QString::fromStdString(aName));
233     } else {
234       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
235       if (aRefAttr && aRefAttr->attr().get() != NULL) {
236         //myIsObject = aRefAttr->isObject();
237         AttributePtr anAttr = aRefAttr->attr();
238         if (anAttr.get() != NULL) {
239           std::stringstream aName;
240           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
241           myTextLine->setText(QString::fromStdString(aName.str()));
242         }
243       }
244       else {
245         myTextLine->setText(getDefaultValue().c_str());
246       }
247     }
248   }
249 }
250
251 //********************************************************************
252 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
253 {
254   DataPtr aData = myFeature->data();
255   AttributePtr anAttribute = myFeature->attribute(attributeID());
256
257   myObject = GeomValidators_Tools::getObject(anAttribute);
258   myShape = getShape();
259   myRefAttribute = AttributePtr();
260   myIsObject = false;
261   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
262   if (aRefAttr) {
263     myIsObject = aRefAttr->isObject();
264     myRefAttribute = aRefAttr->attr();
265   }
266 }
267
268 //********************************************************************
269 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
270 {
271   DataPtr aData = myFeature->data();
272   AttributePtr anAttribute = myFeature->attribute(attributeID());
273
274   setObject(myObject, myShape);
275   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
276   if (aRefAttr) {
277     if (!myIsObject)
278       aRefAttr->setAttr(myRefAttribute);
279   }
280 }