]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp
Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: extrusion by preselection
[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 bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
111                                                   const bool theToValidate)
112 {
113   if (theValues.empty()) {
114     // In order to make reselection possible, set empty object and shape should be done
115     setSelectionCustom(ModuleBase_ViewerPrs());
116     return false;
117   }
118   // it removes the processed value from the parameters list
119   ModuleBase_ViewerPrs aValue = theValues.takeFirst();
120   bool isDone = false;
121
122   if (!theToValidate || isValidInFilters(aValue)) {
123     isDone = setSelectionCustom(aValue);
124     // updateObject - to update/redisplay feature
125     // it is commented in order to perfom it outside the method
126     //updateObject(myFeature);
127     // to storeValue()
128     //emit valuesChanged();
129   }
130   return isDone;
131 }
132
133 //********************************************************************
134 QList<ModuleBase_ViewerPrs> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
135 {
136   QList<ModuleBase_ViewerPrs> aSelected;
137   if(myFeature) {
138     DataPtr aData = myFeature->data();
139     AttributePtr anAttribute = myFeature->attribute(attributeID());
140
141     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
142     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
143     ModuleBase_ViewerPrs aPrs(anObject, aShapePtr, NULL);
144     aSelected.append(aPrs);
145   }
146   return aSelected;
147 }
148
149 //********************************************************************
150 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
151 {
152   bool isBlocked = this->blockSignals(true);
153   updateSelectionName();
154   this->blockSignals(isBlocked);
155
156   return true;
157 }
158
159 //********************************************************************
160 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
161 {
162   QList<QWidget*> aControls;
163   aControls.append(myTextLine);
164   return aControls;
165 }
166
167 void ModuleBase_WidgetShapeSelector::updateFocus()
168 {
169   emit focusOutWidget(this);
170 }
171
172 //********************************************************************
173 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
174 {
175   QIntList aShapeTypes;
176   foreach(QString aType, myShapeTypes) {
177     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
178   }
179   return aShapeTypes;
180 }
181
182 //********************************************************************
183 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
184 {
185   GeomShapePtr aShape;
186   DataPtr aData = myFeature->data();
187   if (!aData->isValid())
188     return aShape;
189
190   std::string aType = aData->attribute(attributeID())->attributeType();
191   if (aType == ModelAPI_AttributeReference::typeId()) {
192   } else if (aType == ModelAPI_AttributeRefAttr::typeId()) {
193     AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
194     if (aRefAttr.get() && !aRefAttr->isObject()) {
195       AttributePtr anAttribute = aRefAttr->attr();
196       aShape = myWorkshop->module()->findShape(anAttribute);
197     }
198   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
199     AttributeSelectionPtr aSelectAttr = aData->selection(attributeID());
200     aShape = aSelectAttr->value();
201   }
202
203   return aShape;
204 }
205
206 //********************************************************************
207 void ModuleBase_WidgetShapeSelector::updateSelectionName()
208 {
209   DataPtr aData = myFeature->data();
210   if (!aData->isValid())
211     return;
212
213   bool isNameUpdated = false;
214   AttributeSelectionPtr aSelect = aData->selection(attributeID());
215   if (aSelect) {
216     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
217     isNameUpdated = true;
218   }
219   if (!isNameUpdated) {
220     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
221     if (anObject.get() != NULL) {
222       std::string aName = anObject->data()->name();
223       myTextLine->setText(QString::fromStdString(aName));
224     } else {
225       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
226       if (aRefAttr && aRefAttr->attr().get() != NULL) {
227         //myIsObject = aRefAttr->isObject();
228         std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
229         myTextLine->setText(QString::fromStdString(anAttrName));
230       }
231       else {
232         myTextLine->setText(getDefaultValue().c_str());
233       }
234     }
235   }
236 }
237
238 //********************************************************************
239 void ModuleBase_WidgetShapeSelector::storeAttributeValue()
240 {
241   ModuleBase_WidgetValidated::storeAttributeValue();
242
243   DataPtr aData = myFeature->data();
244   AttributePtr anAttribute = myFeature->attribute(attributeID());
245
246   myObject = ModuleBase_Tools::getObject(anAttribute);
247   myShape = getShape();
248   myRefAttribute = AttributePtr();
249   myIsObject = false;
250   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
251   if (aRefAttr) {
252     myIsObject = aRefAttr->isObject();
253     myRefAttribute = aRefAttr->attr();
254   }
255 }
256
257 //********************************************************************
258 void ModuleBase_WidgetShapeSelector::restoreAttributeValue(bool theValid)
259 {
260   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
261
262   DataPtr aData = myFeature->data();
263   AttributePtr anAttribute = myFeature->attribute(attributeID());
264
265   setObject(myObject, myShape);
266   AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
267   if (aRefAttr) {
268     if (!myIsObject)
269       aRefAttr->setAttr(myRefAttribute);
270   }
271 }