Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_WidgetShapeSelector.h>
22 #include <ModuleBase_Definitions.h>
23 #include <ModuleBase_ISelection.h>
24 #include <ModuleBase_IWorkshop.h>
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_Tools.h>
27 #include <ModuleBase_Filter.h>
28 #include <ModuleBase_IModule.h>
29 #include <ModuleBase_ViewerPrs.h>
30 #include <ModuleBase_IconFactory.h>
31
32 #include <Config_Translator.h>
33 #include <Config_WidgetAPI.h>
34 #include <Events_Loop.h>
35 #include <Events_Message.h>
36 #include <GeomAPI_Interface.h>
37 #include <GeomAPI_Shape.h>
38
39 #include <ModelAPI_AttributeReference.h>
40 #include <ModelAPI_Data.h>
41 #include <ModelAPI_Document.h>
42 #include <ModelAPI_Events.h>
43 #include <ModelAPI_Feature.h>
44 #include <ModelAPI_Result.h>
45 #include <ModelAPI_ResultConstruction.h>
46 #include <ModelAPI_AttributeReference.h>
47 #include <ModelAPI_AttributeSelection.h>
48 #include <ModelAPI_Session.h>
49 #include <ModelAPI_Tools.h>
50 #include <ModelAPI_ResultBody.h>
51 #include <ModelAPI_AttributeRefAttr.h>
52 #include <ModelAPI_Validator.h>
53 #include <ModelAPI_AttributeValidator.h>
54
55 #include <Config_WidgetAPI.h>
56
57 #include <GeomAPI_Shape.h>
58
59 #include <SelectMgr_ListIteratorOfListOfFilter.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <TopExp_Explorer.hxx>
62
63 #include <QWidget>
64 #include <QLayout>
65 #include <QLabel>
66 #include <QLineEdit>
67 #include <QToolButton>
68 #include <QString>
69 #include <QEvent>
70 #include <QDockWidget>
71 #include <QApplication>
72 #include <QFormLayout>
73
74 #include <TopExp_Explorer.hxx>
75 #include <TopoDS_Shape.hxx>
76
77 #include <memory>
78
79 #include <list>
80 #include <string>
81
82 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
83                                                      ModuleBase_IWorkshop* theWorkshop,
84                                                      const Config_WidgetAPI* theData)
85 : ModuleBase_WidgetSelector(theParent, theWorkshop, theData)
86 {
87   QFormLayout* aLayout = new QFormLayout(this);
88   ModuleBase_Tools::adjustMargins(aLayout);
89
90   QString aLabelText = translate(theData->widgetLabel());
91   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
92   myLabel = new QLabel(aLabelText, this);
93   if (!aLabelIcon.isEmpty())
94     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
95
96
97   QString aToolTip = translate(theData->widgetTooltip());
98   myTextLine = new QLineEdit(this);
99   QString anObjName = QString::fromStdString(attributeID());
100   myTextLine->setObjectName(anObjName);
101   myTextLine->setReadOnly(true);
102   myTextLine->setToolTip(aToolTip);
103   myTextLine->installEventFilter(this);
104
105   aLayout->addRow(myLabel, myTextLine);
106   myLabel->setToolTip(aToolTip);
107
108   std::string aTypes = theData->getProperty("shape_types");
109   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
110 }
111
112 //********************************************************************
113 ModuleBase_WidgetShapeSelector::~ModuleBase_WidgetShapeSelector()
114 {
115 }
116
117 //********************************************************************
118 bool ModuleBase_WidgetShapeSelector::storeValueCustom()
119 {
120   // the value is stored on the selection changed signal processing
121   return true;
122 }
123
124 //********************************************************************
125 bool ModuleBase_WidgetShapeSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
126                                                   const bool theToValidate)
127 {
128   if (theValues.empty()) {
129     // In order to make reselection possible, set empty object and shape should be done
130     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
131                                                   ObjectPtr(), GeomShapePtr(), NULL)));
132     return false;
133   }
134   // it removes the processed value from the parameters list
135   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
136   bool isDone = false;
137
138   if (!theToValidate || isValidInFilters(aValue)) {
139     isDone = setSelectionCustom(aValue);
140     // updateObject - to update/redisplay feature
141     // it is commented in order to perfom it outside the method
142     //updateObject(myFeature);
143     // to storeValue()
144     //emit valuesChanged();
145   }
146   return isDone;
147 }
148
149 //********************************************************************
150 QList<ModuleBase_ViewerPrsPtr> ModuleBase_WidgetShapeSelector::getAttributeSelection() const
151 {
152   QList<ModuleBase_ViewerPrsPtr> aSelected;
153   if(myFeature) {
154     DataPtr aData = myFeature->data();
155     AttributePtr anAttribute = myFeature->attribute(attributeID());
156
157     ObjectPtr anObject = ModuleBase_Tools::getObject(anAttribute);
158     std::shared_ptr<GeomAPI_Shape> aShapePtr = getShape();
159     ModuleBase_ViewerPrsPtr aPrs(new ModuleBase_ViewerPrs(anObject, aShapePtr, NULL));
160     aSelected.append(aPrs);
161   }
162   return aSelected;
163 }
164
165 //********************************************************************
166 bool ModuleBase_WidgetShapeSelector::restoreValueCustom()
167 {
168   bool isBlocked = this->blockSignals(true);
169   updateSelectionName();
170   this->blockSignals(isBlocked);
171
172   return true;
173 }
174
175 //********************************************************************
176 QList<QWidget*> ModuleBase_WidgetShapeSelector::getControls() const
177 {
178   QList<QWidget*> aControls;
179   aControls.append(myTextLine);
180   return aControls;
181 }
182
183 void ModuleBase_WidgetShapeSelector::updateFocus()
184 {
185   emit focusOutWidget(this);
186 }
187
188 //********************************************************************
189 QIntList ModuleBase_WidgetShapeSelector::shapeTypes() const
190 {
191   QIntList aShapeTypes;
192   foreach(QString aType, myShapeTypes) {
193     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
194   }
195   return aShapeTypes;
196 }
197
198 //********************************************************************
199 GeomShapePtr ModuleBase_WidgetShapeSelector::getShape() const
200 {
201   GeomShapePtr aShape;
202   DataPtr aData = myFeature->data();
203   if (aData->isValid()) {
204     AttributePtr anAttribute = myFeature->data()->attribute(attributeID());
205     aShape = ModuleBase_Tools::getShape(anAttribute, myWorkshop);
206   }
207   return aShape;
208 }
209
210 //********************************************************************
211 void ModuleBase_WidgetShapeSelector::updateSelectionName()
212 {
213   DataPtr aData = myFeature->data();
214   if (!aData->isValid())
215     return;
216
217   bool isNameUpdated = false;
218   AttributeSelectionPtr aSelect = aData->selection(attributeID());
219   if (aSelect) {
220     myTextLine->setText(QString::fromStdString(aSelect->namingName(getDefaultValue())));
221     isNameUpdated = true;
222   }
223   if (!isNameUpdated) {
224     ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
225     if (anObject.get() != NULL) {
226       std::string aName = anObject->data()->name();
227       myTextLine->setText(QString::fromStdString(aName));
228     } else {
229       AttributeRefAttrPtr aRefAttr = aData->refattr(attributeID());
230       if (aRefAttr && aRefAttr->attr().get() != NULL) {
231         //myIsObject = aRefAttr->isObject();
232         std::string anAttrName = generateName(aRefAttr->attr(), myWorkshop);
233         myTextLine->setText(QString::fromStdString(anAttrName));
234       }
235       else {
236         myTextLine->setText(getDefaultValue().c_str());
237       }
238     }
239   }
240 }