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