Salome HOME
ff003f0e8dcf78f50a60524b514f18570d85c844
[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_ViewerPrsPtr>& 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(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
116                                                   ObjectPtr(), GeomShapePtr(), NULL)));
117     return false;
118   }
119   // it removes the processed value from the parameters list
120   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
121   bool isDone = false;
122
123   if (!theToValidate || isValidInFilters(aValue)) {
124     isDone = setSelectionCustom(aValue);
125     // updateObject - to update/redisplay feature
126     // it is commented in order to perfom it outside the method
127     //updateObject(myFeature);
128     // to storeValue()
129     //emit valuesChanged();
130   }
131   return isDone;
132 }
133
134 //********************************************************************
135 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
136 {
137   QList<ModuleBase_ViewerPrsPtr> aSelected;
138   if(myFeature) {
139     DataPtr aData = myFeature->data();
140     AttributePtr anAttribute = myFeature->attribute(attributeID());
141
142     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
143     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
144     ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
145     aSelected.append(aPrs);
146   }
147   return aSelected;
148 }
149
150 //********************************************************************
151 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
152 {
153   bool isBlocked = this->blockSignals(true);
154   updateSelectionName();
155   this->blockSignals(isBlocked);
156
157   return true;
158 }
159
160 //********************************************************************
161 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
162 {
163   QList<QWidget*> aControls;
164   aControls.append(myTextLine);
165   return aControls;
166 }
167
168 void ModuleBase_WidgetShapeSelector::updateFocus()
169 {
170   emit focusOutWidget(this);
171 }
172
173 //********************************************************************
174 QIntList ModuleBase_WidgetShapeSelector::getShapeTypes() const
175 {
176   QIntList aShapeTypes;
177   foreach(QString aType, myShapeTypes) {
178     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
179   }
180   return aShapeTypes;
181 }
182
183 //********************************************************************
184 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
185 {
186   GeomShapePtr aShape;
187   DataPtr aData = myFeature->data();
188   if (aData->isValid()) {
189     AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
190     aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
191   }
192   return aShape;
193 }
194
195 //********************************************************************
196 void ModuleBase_WidgetShapeSelector::updateSelectionName()
197 {
198   DataPtr aData = myFeature->data();
199   if (!aData->isValid())
200     return;
201
202   bool isNameUpdated = false;
203   AttributeSelectionPtr aSelect = aData->selection(attributeID());
204   if (aSelect) {
205     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
206     isNameUpdated = true;
207   }
208   if (!isNameUpdated) {
209     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
210     if (anObject.get() != NULL) {
211       std::string aName = anObject->data()->name();
212       myTextLine->setText(QString::fromStdString(aName));
213     } else {
214       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
215       if (aRefAttr && aRefAttr->attr().get() != NULL) {
216         //myIsObject = aRefAttr->isObject();
217         std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
218         myTextLine->setText(QString::fromStdString(anAttrName));
219       }
220       else {
221         myTextLine->setText(getDefaultValue().c_str());
222       }
223     }
224   }
225 }