Salome HOME
Feature selector widget for recover feature.
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeatureSelector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D\r
2 \r
3 // File:        ModuleBase_WidgetFeatureSelector.cpp\r
4 // Created:     5 Sep 2016\r
5 // Author:      Natalia Ermolaeva\r
6 \r
7 #include <ModuleBase_WidgetFeatureSelector.h>\r
8 \r
9 #include <ModuleBase_Definitions.h>\r
10 #include <ModuleBase_ISelection.h>\r
11 #include <ModuleBase_IWorkshop.h>\r
12 #include <ModuleBase_IViewer.h>\r
13 #include <ModuleBase_Tools.h>\r
14 #include <ModuleBase_FilterFactory.h>\r
15 #include <ModuleBase_Filter.h>\r
16 #include <ModuleBase_IModule.h>\r
17 #include <ModuleBase_ViewerPrs.h>\r
18 #include <ModuleBase_IconFactory.h>\r
19 #include <ModuleBase_ResultPrs.h>\r
20 \r
21 #include <Config_WidgetAPI.h>\r
22 #include <Events_Loop.h>\r
23 #include <Events_Message.h>\r
24 #include <GeomAPI_Interface.h>\r
25 #include <GeomAPI_Shape.h>\r
26 \r
27 #include <ModelAPI_Data.h>\r
28 #include <ModelAPI_Document.h>\r
29 #include <ModelAPI_Events.h>\r
30 #include <ModelAPI_Feature.h>\r
31 #include <ModelAPI_Result.h>\r
32 #include <ModelAPI_Session.h>\r
33 \r
34 #include <Config_WidgetAPI.h>\r
35 \r
36 #include <GeomAPI_Shape.h>\r
37 \r
38 #include <QWidget>\r
39 #include <QLayout>\r
40 #include <QLabel>\r
41 #include <QLineEdit>\r
42 #include <QToolButton>\r
43 #include <QString>\r
44 #include <QEvent>\r
45 #include <QApplication>\r
46 #include <QFormLayout>\r
47 \r
48 #include <memory>\r
49 \r
50 #include <list>\r
51 #include <string>\r
52 \r
53 \r
54 ModuleBase_WidgetFeatureSelector::ModuleBase_WidgetFeatureSelector(QWidget* theParent,\r
55                                                      ModuleBase_IWorkshop* theWorkshop,\r
56                                                      const Config_WidgetAPI* theData)\r
57 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)\r
58 {\r
59   QFormLayout* aLayout = new QFormLayout(this);\r
60   ModuleBase_Tools::adjustMargins(aLayout);\r
61 \r
62   QString aLabelText = QString::fromStdString(theData->widgetLabel());\r
63   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());\r
64   myLabel = new QLabel(aLabelText, this);\r
65   if (!aLabelIcon.isEmpty())\r
66     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));\r
67 \r
68 \r
69   QString aToolTip = QString::fromStdString(theData->widgetTooltip());\r
70   myTextLine = new QLineEdit(this);\r
71   QString anObjName = QString::fromStdString(attributeID());\r
72   myTextLine->setObjectName(anObjName);\r
73   myTextLine->setReadOnly(true);\r
74   myTextLine->setToolTip(aToolTip);\r
75   myTextLine->installEventFilter(this);\r
76 \r
77   aLayout->addRow(myLabel, myTextLine);\r
78   myLabel->setToolTip(aToolTip);\r
79 }\r
80 \r
81 //********************************************************************\r
82 ModuleBase_WidgetFeatureSelector::~ModuleBase_WidgetFeatureSelector()\r
83 {\r
84 }\r
85 \r
86 //********************************************************************
87 bool ModuleBase_WidgetFeatureSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
88 {
89   ModuleBase_ISelection* aSelection = myWorkshop->selection();
90   ObjectPtr anObject = ModelAPI_Feature::feature(thePrs->object());
91   GeomShapePtr aShape;
92
93   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
94   // the last flag is to be depending on hasObject is called before. To be corrected later
95   return ModuleBase_Tools::setObject(attribute(), aFeature, aShape,
96                                      myWorkshop, myIsInValidate, true);
97 }
98
99 //********************************************************************
100 void ModuleBase_WidgetFeatureSelector::deactivate()
101 {
102   ModuleBase_ModelWidget::deactivate();
103   disconnect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
104   activateFilters(false);\r
105   myWorkshop->deactivateSubShapesSelection();
106 }\r
107 \r
108 //********************************************************************
109 void ModuleBase_WidgetFeatureSelector::activateCustom()
110 {
111   connect(myWorkshop, SIGNAL(selectionChanged()), this,
112           SLOT(onSelectionChanged()), Qt::UniqueConnection);
113   
114   activateFilters(true);\r
115
116   QIntList aShapeTypes;
117   aShapeTypes.push_back(ModuleBase_ResultPrs::Sel_Result);
118   myWorkshop->activateSubShapesSelection(aShapeTypes);
119
120   // Restore selection in the viewer by the attribute selection list
121   // it should be postponed to have current widget as active to validate restored selection
122   //static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
123   //ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
124 }
125 \r
126 //********************************************************************\r
127 bool ModuleBase_WidgetFeatureSelector::storeValueCustom()\r
128 {\r
129   // the value is stored on the selection changed signal processing \r
130   return true;\r
131 }\r
132 \r
133 //********************************************************************\r
134 bool ModuleBase_WidgetFeatureSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,\r
135                                                   const bool theToValidate)\r
136 {\r
137   if (theValues.empty()) {\r
138     // In order to make reselection possible, set empty object and shape should be done\r
139     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(\r
140                                                   ObjectPtr(), GeomShapePtr(), NULL)));\r
141     return false;\r
142   }\r
143   // it removes the processed value from the parameters list\r
144   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();\r
145   bool isDone = false;\r
146 \r
147   if (!theToValidate || isValidSelection(aValue))\r
148     isDone = setSelectionCustom(aValue);\r
149 \r
150   return isDone;\r
151 }\r
152 \r
153 //********************************************************************\r
154 bool ModuleBase_WidgetFeatureSelector::restoreValueCustom()\r
155 {\r
156   bool isBlocked = this->blockSignals(true);\r
157   updateSelectionName();\r
158   this->blockSignals(isBlocked);\r
159 \r
160   return true;\r
161 }\r
162 \r
163 //********************************************************************\r
164 QList<QWidget*> ModuleBase_WidgetFeatureSelector::getControls() const\r
165 {\r
166   QList<QWidget*> aControls;\r
167   aControls.append(myTextLine);\r
168   return aControls;\r
169 }\r
170 \r
171 void ModuleBase_WidgetFeatureSelector::updateFocus()\r
172 {\r
173   emit focusOutWidget(this);\r
174 }\r
175 \r
176 //********************************************************************\r
177 void ModuleBase_WidgetFeatureSelector::updateSelectionName()\r
178 {\r
179   DataPtr aData = myFeature->data();\r
180   if (!aData->isValid())\r
181     return;\r
182 \r
183   ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));\r
184   if (anObject.get() != NULL) {\r
185     std::string aName = anObject->data()->name();\r
186     myTextLine->setText(QString::fromStdString(aName));\r
187   }\r
188 }\r
189 \r
190 //********************************************************************\r
191 bool ModuleBase_WidgetFeatureSelector::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)\r
192 {\r
193   bool aValid = false;\r
194 \r
195   ObjectPtr anObject = thePrs->object();\r
196   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);\r
197   aValid = aFeature.get();\r
198   if (!aValid) {\r
199     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);\r
200     aValid = aResult.get() && aResult->shape() == thePrs->shape();\r
201   }\r
202 \r
203   return aValid;\r
204 }\r
205 \r
206 //********************************************************************\r
207 void ModuleBase_WidgetFeatureSelector::onSelectionChanged()
208 {
209   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(\r
210                                                               ModuleBase_ISelection::AllControls);\r
211
212   bool isDone = setSelection(aSelected, true/*false*/);
213   updateOnSelectionChanged(isDone);
214 }
215
216 //********************************************************************
217 void ModuleBase_WidgetFeatureSelector::updateOnSelectionChanged(const bool theDone)
218 {
219   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in 
220   // the same place repeatedly without mouse moved. In the case validation by filters is not
221   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
222   // the 3rd click in the same point allow using this point.
223   emit valuesChanged();
224   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
225   // calls validators for the feature and, as a result, updates the Apply button state.
226   updateObject(myFeature);
227
228   // we need to forget about previous validation result as the current selection can influence on it
229   clearValidatedCash();
230
231   if (theDone)
232     updateFocus();
233 }
234
235