Salome HOME
Issue #2353: Correct misprint and provide undo/redo by selection in Undo/Redo list
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetFeatureSelector.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_WidgetFeatureSelector.h>
22
23 #include <ModuleBase_Definitions.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_IWorkshop.h>
26 #include <ModuleBase_IViewer.h>
27 #include <ModuleBase_Tools.h>
28 #include <ModuleBase_Filter.h>
29 #include <ModuleBase_IModule.h>
30 #include <ModuleBase_ViewerPrs.h>
31 #include <ModuleBase_IconFactory.h>
32 #include <ModuleBase_ResultPrs.h>
33
34 #include <Config_WidgetAPI.h>
35 #include <Events_Loop.h>
36 #include <Events_Message.h>
37 #include <GeomAPI_Interface.h>
38 #include <GeomAPI_Shape.h>
39
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_Session.h>
46
47 #include <Config_WidgetAPI.h>
48
49 #include <GeomAPI_Shape.h>
50
51 #include <QWidget>
52 #include <QLayout>
53 #include <QLabel>
54 #include <QLineEdit>
55 #include <QToolButton>
56 #include <QString>
57 #include <QEvent>
58 #include <QApplication>
59 #include <QFormLayout>
60
61 #include <memory>
62
63 #include <list>
64 #include <string>
65
66
67 ModuleBase_WidgetFeatureSelector::ModuleBase_WidgetFeatureSelector(QWidget* theParent,
68                                                      ModuleBase_IWorkshop* theWorkshop,
69                                                      const Config_WidgetAPI* theData)
70 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
71 {
72   QFormLayout* aLayout = new QFormLayout(this);
73   ModuleBase_Tools::adjustMargins(aLayout);
74
75   QString aLabelText = translate(theData->widgetLabel());
76   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
77   myLabel = new QLabel(aLabelText, this);
78   if (!aLabelIcon.isEmpty())
79     myLabel->setPixmap(ModuleBase_IconFactory::loadPixmap(aLabelIcon));
80
81
82   QString aToolTip = translate(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
94 //********************************************************************
95 ModuleBase_WidgetFeatureSelector::~ModuleBase_WidgetFeatureSelector()
96 {
97 }
98
99 //********************************************************************
100 bool ModuleBase_WidgetFeatureSelector::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
101 {
102   ModuleBase_ISelection* aSelection = myWorkshop->selection();
103   ObjectPtr anObject = ModelAPI_Feature::feature(thePrs->object());
104   GeomShapePtr aShape;
105
106   FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
107   // the last flag is to be depending on hasObject is called before. To be corrected later
108   return ModuleBase_Tools::setObject(attribute(), aFeature, aShape,
109                                      myWorkshop, myIsInValidate, true);
110 }
111
112 //********************************************************************
113 void ModuleBase_WidgetFeatureSelector::deactivate()
114 {
115   ModuleBase_ModelWidget::deactivate();
116   activateFilters(false);
117   myWorkshop->deactivateSubShapesSelection();
118 }
119
120 //********************************************************************
121 bool ModuleBase_WidgetFeatureSelector::processAction(ModuleBase_ActionType theActionType,
122                                                      const ActionParamPtr& theParam)
123 {
124   if (theActionType == ActionSelection)
125     onSelectionChanged();
126   else
127     return ModuleBase_WidgetValidated::processAction(theActionType, theParam);
128
129   return true;
130 }
131
132 //********************************************************************
133 void ModuleBase_WidgetFeatureSelector::activateCustom()
134 {
135   activateFilters(true);
136
137   QIntList aShapeTypes;
138   aShapeTypes.push_back(ModuleBase_ResultPrs::Sel_Result);
139   myWorkshop->activateSubShapesSelection(aShapeTypes);
140
141   // Restore selection in the viewer by the attribute selection list
142   // it should be postponed to have current widget as active to validate restored selection
143   //static Events_ID anEvent = Events_Loop::eventByName(EVENT_UPDATE_BY_WIDGET_SELECTION);
144   //ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
145 }
146
147 //********************************************************************
148 bool ModuleBase_WidgetFeatureSelector::storeValueCustom()
149 {
150   // the value is stored on the selection changed signal processing
151   return true;
152 }
153
154 //********************************************************************
155 bool ModuleBase_WidgetFeatureSelector::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
156                                                   const bool theToValidate)
157 {
158   if (theValues.empty()) {
159     // In order to make reselection possible, set empty object and shape should be done
160     setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(new ModuleBase_ViewerPrs(
161                                                   ObjectPtr(), GeomShapePtr(), NULL)));
162     return false;
163   }
164   // it removes the processed value from the parameters list
165   ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
166   bool isDone = false;
167
168   if (!theToValidate || isValidSelection(aValue))
169     isDone = setSelectionCustom(aValue);
170
171   return isDone;
172 }
173
174 //********************************************************************
175 bool ModuleBase_WidgetFeatureSelector::restoreValueCustom()
176 {
177   bool isBlocked = this->blockSignals(true);
178   updateSelectionName();
179   this->blockSignals(isBlocked);
180
181   return true;
182 }
183
184 //********************************************************************
185 QList<QWidget*> ModuleBase_WidgetFeatureSelector::getControls() const
186 {
187   QList<QWidget*> aControls;
188   aControls.append(myTextLine);
189   return aControls;
190 }
191
192 void ModuleBase_WidgetFeatureSelector::updateFocus()
193 {
194   emit focusOutWidget(this);
195 }
196
197 //********************************************************************
198 void ModuleBase_WidgetFeatureSelector::updateSelectionName()
199 {
200   DataPtr aData = myFeature->data();
201   if (!aData->isValid())
202     return;
203
204   ObjectPtr anObject = ModuleBase_Tools::getObject(myFeature->attribute(attributeID()));
205   if (anObject.get() != NULL) {
206     std::string aName = anObject->data()->name();
207     myTextLine->setText(QString::fromStdString(aName));
208   } else {
209     myTextLine->clear();
210   }
211 }
212
213 //********************************************************************
214 bool ModuleBase_WidgetFeatureSelector::isValidInFilters(const ModuleBase_ViewerPrsPtr& thePrs)
215 {
216   bool aValid = false;
217
218   ObjectPtr anObject = thePrs->object();
219   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
220   aValid = aFeature.get();
221   if (!aValid) {
222     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
223     aValid = aResult.get() && aResult->shape() == thePrs->shape();
224   }
225
226   return aValid;
227 }
228
229 //********************************************************************
230 void ModuleBase_WidgetFeatureSelector::onSelectionChanged()
231 {
232   QList<ModuleBase_ViewerPrsPtr> aSelected = myWorkshop->selection()->getSelected(
233                                                               ModuleBase_ISelection::AllControls);
234
235   bool isDone = setSelection(aSelected, true/*false*/);
236   updateOnSelectionChanged(isDone);
237 }
238
239 //********************************************************************
240 void ModuleBase_WidgetFeatureSelector::updateOnSelectionChanged(const bool theDone)
241 {
242   // "false" flag should be used here, it connects to the #26658 OCC bug, when the user click in
243   // the same place repeatedly without mouse moved. In the case validation by filters is not
244   // perfromed, so an invalid object is selected. E.g. distance constraint, selection of a point.
245   // the 3rd click in the same point allow using this point.
246   emit valuesChanged();
247   // the updateObject method should be called to flush the updated sigal. The workshop listens it,
248   // calls validators for the feature and, as a result, updates the Apply button state.
249   updateObject(myFeature);
250
251   // we need to forget about previous validation result as the current selection can influence on it
252   clearValidatedCash();
253
254   if (theDone)
255     updateFocus();
256 }
257
258