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