Salome HOME
#839 Selection of Part sub-results
[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 #include <GeomValidators_Tools.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 #include <ModelAPI_ShapeValidator.h>
39
40 #include <Config_WidgetAPI.h>
41 #include <Events_Error.h>
42
43 #include <GeomAPI_Shape.h>
44
45 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
46 #include <TopoDS_Shape.hxx>
47 #include <TopExp_Explorer.hxx>
48
49 #include <QWidget>
50 #include <QLayout>
51 #include <QLabel>
52 #include <QLineEdit>
53 #include <QToolButton>
54 #include <QString>
55 #include <QEvent>
56 #include <QDockWidget>
57 #include <QApplication>
58 #include <QFormLayout>
59
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Shape.hxx>
62
63 #include <memory>
64
65 #include <list>
66 #include <string>
67
68 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
69                                                      ModuleBase_IWorkshop* theWorkshop,
70                                                      const Config_WidgetAPI* theData,
71                                                      const std::string& theParentId)
72  : ModuleBase_WidgetSelector(theParent, theWorkshop, theData, theParentId)
73 {
74   QFormLayout* aLayout = new QFormLayout(this);
75   ModuleBase_Tools::adjustMargins(aLayout);
76
77   QString aLabelText = QString::fromStdString(theData->widgetLabel());
78   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
79   myLabel = new QLabel(aLabelText, this);
80   if (!aLabelIcon.isEmpty())
81     myLabel->setPixmap(QPixmap(aLabelIcon));
82
83
84   QString aToolTip = QString::fromStdString(theData->widgetTooltip());
85   myTextLine = new QLineEdit(this);
86   myTextLine->setReadOnly(true);
87   myTextLine->setToolTip(aToolTip);
88   myTextLine->installEventFilter(this);
89
90   aLayout->addRow(myLabel, myTextLine);
91
92   std::string aTypes = theData->getProperty("shape_types");
93   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
94 }
95
96 //********************************************************************
97 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
98 {
99 }
100
101 //********************************************************************
102 bool ModuleBase_WidgetShapeSelector::storeValueCustom() const
103 {
104   // the value is stored on the selection changed signal processing 
105   return true;
106 }
107
108 //********************************************************************
109 void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theSelectedObject,
110                                                GeomShapePtr theShape)
111 {
112   DataPtr aData = myFeature->data();
113   AttributeReferencePtr aRef = aData->reference(attributeID());
114   if (aRef) {
115     ObjectPtr aObject = aRef->value();
116     if (!(aObject && aObject->isSame(theSelectedObject))) {
117       aRef->setValue(theSelectedObject);
118     }
119   } else {
120     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
121     if (aRefAttr) {
122       ObjectPtr aObject = aRefAttr->object();
123       if (!(aObject && aObject->isSame(theSelectedObject))) {
124         aRefAttr->setObject(theSelectedObject);
125       }
126     } else {
127       AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
128       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theSelectedObject);
129       if (aSelectAttr.get() != NULL) {
130         aSelectAttr->setValue(aResult, theShape);
131       }
132     }
133   }
134 }
135
136 //********************************************************************
137 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
138 {
139   QList<ModuleBase_ViewerPrs> aSelected;
140   if(myFeature) {
141     DataPtr aData = myFeature->data();
142     AttributePtr anAttribute = myFeature->attribute(attributeID());
143
144     ObjectPtr anObject = GeomValidators_Tools::getObject(anAttribute);
145     TopoDS_Shape aShape;
146     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
147     if (aShapePtr.get()) {
148       aShape = aShapePtr->impl<TopoDS_Shape>();
149     }
150     ModuleBase_ViewerPrs aPrs(anObject, aShape, NULL);
151     aSelected.append(aPrs);
152   }
153   return aSelected;
154 }
155
156 //********************************************************************
157 void ModuleBase_WidgetShapeSelector::clearAttribute()
158 {
159   // In order to make reselection possible, set empty object and shape should be done
160   setObject(ObjectPtr(), std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape()));
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   AttributeSelectionPtr aSelect = aData->selection(attributeID());
205   if (aSelect)
206     aShape = aSelect->value();
207
208   return aShape;
209 }
210
211 //********************************************************************
212 void ModuleBase_WidgetShapeSelector::updateSelectionName()
213 {
214   DataPtr aData = myFeature->data();
215   if (!aData->isValid())
216     return;
217
218   bool isNameUpdated = false;
219   AttributeSelectionPtr aSelect = aData->selection(attributeID());
220   if (aSelect) {
221     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
222     isNameUpdated = true;
223   }
224   if (!isNameUpdated) {
225     ObjectPtr anObject = GeomValidators_Tools::getObject(myFeature->attribute(attributeID()));
226     if (anObject.get() != NULL) {
227       std::string aName = anObject->data()->name();
228       myTextLine->setText(QString::fromStdString(aName));
229     } else {
230       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
231       if (aRefAttr && aRefAttr->attr().get() != NULL) {
232         //myIsObject = aRefAttr->isObject();
233         AttributePtr anAttr = aRefAttr->attr();
234         if (anAttr.get() != NULL) {
235           std::stringstream aName;
236           aName <<anAttr->owner()->data()->name()<<"/"<<anAttr->id();
237           myTextLine->setText(QString::fromStdString(aName.str()));
238         }
239       }
240       else {
241         myTextLine->setText(getDefaultValue().c_str());
242       }
243     }
244   }
245 }
246
247 //********************************************************************
248 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
249 {
250   ModuleBase_WidgetValidated::storeAttributeValue();
251
252   DataPtr aData = myFeature->data();
253   AttributePtr anAttribute = myFeature->attribute(attributeID());
254
255   myObject = GeomValidators_Tools::getObject(anAttribute);
256   myShape = getShape();
257   myRefAttribute = AttributePtr();
258   myIsObject = false;
259   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
260   if (aRefAttr) {
261     myIsObject = aRefAttr->isObject();
262     myRefAttribute = aRefAttr->attr();
263   }
264 }
265
266 //********************************************************************
267 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
268 {
269   ModuleBase_WidgetValidated::restoreAttributeValue(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 }