Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetShapeSelector.h>
21 #include <ModuleBase_Definitions.h>
22 #include <ModuleBase_ISelection.h>
23 #include <ModuleBase_IWorkshop.h>
24 #include <ModuleBase_IViewer.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_Filter.h>
27 #include <ModuleBase_IModule.h>
28 #include <ModuleBase_ViewerPrs.h>
29 #include <ModuleBase_IconFactory.h>
30
31 #include <Config_Translator.h>
32 #include <Config_WidgetAPI.h>
33 #include <Events_Loop.h>
34 #include <Events_Message.h>
35 #include <GeomAPI_Interface.h>
36 #include <GeomAPI_Shape.h>
37
38 #include <ModelAPI_AttributeReference.h>
39 #include <ModelAPI_Data.h>
40 #include <ModelAPI_Document.h>
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_Feature.h>
43 #include <ModelAPI_Result.h>
44 #include <ModelAPI_ResultConstruction.h>
45 #include <ModelAPI_AttributeReference.h>
46 #include <ModelAPI_AttributeSelection.h>
47 #include <ModelAPI_Session.h>
48 #include <ModelAPI_Tools.h>
49 #include <ModelAPI_ResultBody.h>
50 #include <ModelAPI_AttributeRefAttr.h>
51 #include <ModelAPI_Validator.h>
52 #include <ModelAPI_AttributeValidator.h>
53
54 #include <Config_WidgetAPI.h>
55
56 #include <GeomAPI_Shape.h>
57
58 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <TopExp_Explorer.hxx>
61
62 #include <QWidget>
63 #include <QLayout>
64 #include <QLabel>
65 #include <QLineEdit>
66 #include <QToolButton>
67 #include <QString>
68 #include <QEvent>
69 #include <QDockWidget>
70 #include <QApplication>
71 #include <QFormLayout>
72
73 #include <TopExp_Explorer.hxx>
74 #include <TopoDS_Shape.hxx>
75
76 #include <memory>
77
78 #include <list>
79 #include <string>
80
81 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
82                                                      ModuleBase_IWorkshop* theWorkshop,
83                                                      const Config_WidgetAPI* theData)
84 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
85 {
86   QFormLayout* aLayout = new QFormLayout(this);
87   ModuleBase_Tools::adjustMargins(aLayout);
88
89   QString aLabelText = translate(theData->widgetLabel());
90   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
91   myLabel = new QLabel(aLabelText, this);
92   if (!aLabelIcon.isEmpty())
93     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
94
95
96   QString aToolTip = translate(theData->widgetTooltip());
97   myTextLine = new QLineEdit(this);
98   QString anObjName = QString::fromStdString(attributeID());
99   myTextLine->setObjectName(anObjName);
100   myTextLine->setReadOnly(true);
101   myTextLine->setToolTip(aToolTip);
102   myTextLine->installEventFilter(this);
103
104   aLayout->addRow(myLabel, myTextLine);
105   myLabel->setToolTip(aToolTip);
106
107   std::string aTypes = theData->getProperty("shape_types");
108   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
109 }
110
111 //********************************************************************
112 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
113 {
114 }
115
116 //********************************************************************
117 bool ModuleBase_WidgetShapeSelector::storeValueCustom()
118 {
119   // the value is stored on the selection changed signal processing
120   return true;
121 }
122
123 //********************************************************************
124 bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
125                                                   const bool theToValidate)
126 {
127   if (theValues.empty()) {
128     // In order to make reselection possible, set empty object and shape should be done
129     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
130                                                   ObjectPtr(), GeomShapePtr(), NULL)));
131     return false;
132   }
133   // it removes the processed value from the parameters list
134   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
135   bool isDone = false;
136
137   if (!theToValidate || isValidInFilters(aValue)) {
138     isDone = setSelectionCustom(aValue);
139     // updateObject - to update/redisplay feature
140     // it is commented in order to perfom it outside the method
141     //updateObject(myFeature);
142     // to storeValue()
143     //emit valuesChanged();
144   }
145   return isDone;
146 }
147
148 //********************************************************************
149 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
150 {
151   QList<ModuleBase_ViewerPrsPtr> aSelected;
152   if(myFeature) {
153     DataPtr aData = myFeature->data();
154     AttributePtr anAttribute = myFeature->attribute(attributeID());
155
156     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
157     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
158     ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
159     aSelected.append(aPrs);
160   }
161   return aSelected;
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::shapeTypes() 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     AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
204     aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
205   }
206   return aShape;
207 }
208
209 //********************************************************************
210 void ModuleBase_WidgetShapeSelector::updateSelectionName()
211 {
212   DataPtr aData = myFeature->data();
213   if (!aData->isValid())
214     return;
215
216   bool isNameUpdated = false;
217   AttributeSelectionPtr aSelect = aData->selection(attributeID());
218   if (aSelect) {
219     std::wstring aDefault = translate(getDefaultValue()).toStdWString();
220     myTextLine->setText(QString::fromStdWString(aSelect->namingName(aDefault)));
221     isNameUpdated = true;
222   }
223   if (!isNameUpdated) {
224     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
225     if (anObject.get() != NULL) {
226       std::wstring aName = anObject->data()->name();
227       myTextLine->setText(QString::fromStdWString(aName));
228     } else {
229       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
230       if (aRefAttr && aRefAttr->attr().get() != NULL) {
231         //myIsObject = aRefAttr->isObject();
232         std::wstring anAttrName = ModuleBase_Tools::generateName(aRefAttr->attr(), myWorkshop);
233         myTextLine->setText(QString::fromStdWString(anAttrName));
234       }
235       else {
236         myTextLine->setText(translate(getDefaultValue()));
237       }
238     }
239   }
240 }
241
242 //********************************************************************
243 bool ModuleBase_WidgetShapeSelector::isModified() const
244 {
245   return !myTextLine->text().isEmpty();
246 }