Salome HOME
Issue #2571: Clear error flag for integer input widget
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.h
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 #ifndef MODULEBASE_MODELWIDGET_H
22 #define MODULEBASE_MODELWIDGET_H
23
24 #include <ModuleBase.h>
25 #include <ModuleBase_ActionType.h>
26 #include <ModuleBase_Definitions.h>
27 #include <ModuleBase_OperationFeature.h>
28 #include <ModuleBase_ActionInfo.h>
29 #include <ModuleBase_ActionParameter.h>
30 #include <ModelAPI_Feature.h>
31
32 #include <SelectMgr_ListOfFilter.hxx>
33
34 #include <QWidget>
35
36 #include <memory>
37
38 class Config_WidgetAPI;
39 class Events_InfoMessage;
40 class ModuleBase_IPropertyPanel;
41 class ModuleBase_IWorkshop;
42 class ModuleBase_ViewerPrs;
43 class ModuleBase_WidgetValidator;
44 class QKeyEvent;
45
46 /**\class ModuleBase_ModelWidget
47  * \ingroup GUI
48  * \brief An abstract custom widget class. This class realization is assumed 
49  * to create some controls.
50  * The controls values modification should send signal about values change.
51  *
52  * Common interface for widgets in the property panel.
53  * Every widget are able to save/restore data from the model and/or to contain other widgets.
54  *
55  */
56 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
57 {
58 Q_OBJECT
59  public:
60    /// State of the widget
61    enum ValueState { Stored, /// modification is finished and applyed to the model
62                      ModifiedInPP, /// modification has not been finished and set to the model yet
63                      ModifiedInViewer, /// modification performed by viewer events
64                      Reset }; /// the value is reset
65
66    enum EnableState { On,  /// the widget value is always enabled
67                       Off, /// the widget value is always disabled
68                       /// the widget value enable state is defined in preferences
69                       DefinedInPreferences };
70
71   /// Constructor
72   /// \param theParent the parent object
73   /// \param theData the widget configuration. The attribute of the model widget is obtained from
74   /// a low-level API for reading xml definitions of widgets
75   ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
76   /// Destructor
77   virtual ~ModuleBase_ModelWidget();
78
79   /// Fills the widget with default values. It calls the resetCustom method and change
80   /// the widget state to Reset if the reset is performed.
81   /// \return true if the widget current value is reset
82   bool reset();
83
84   /// Returns the state whether the attribute of the feature is initialized
85   /// \param theObject a model feature to be checked
86   /// \return the boolean result
87   bool isInitialized(ObjectPtr theObject) const;
88
89   /// Fills given container with selection modes if the widget has it
90   /// \param [out] theModuleSelectionModes module additional modes, -1 means all default modes
91   /// \param [out] theModes a container of modes
92   virtual void selectionModes(int& theModuleSelectionModes, QIntList& theModes);
93
94   /// Appends into container of workshop selection filters
95   /// \param [out] theModuleSelectionFilters module additional modes, -1 means all default modes
96   /// \param [out] theSelectionFilters selection filters
97   virtual void selectionFilters(QIntList& theModuleSelectionFilters,
98                                 SelectMgr_ListOfFilter& theSelectionFilters);
99
100   /// Returns true, if default value of the widget should be computed
101   /// on operation's execute, like radius for circle's constraint (can not be zero)
102   bool isComputedDefault() const { return myIsComputedDefault; }
103
104   /// Returns true, if default value of the widget is defined in the XML and it is not the
105   /// computed value
106   /// \return the boolean result
107   std::string getDefaultValue() const { return myDefaultValue; }
108
109   /// Returns true, if widget is internal
110   /// \return the boolean result
111   bool isInternal() const { return myIsInternal; }
112
113   /// Returns true, if the obligatory value of the widget is
114   /// not defined in the XML or has true value
115   /// \return the boolean result
116   bool isObligatory() const { return myIsObligatory; }
117
118   /// Returns true, if the widget value is enabled and might be modified manualy.
119   /// It returns false if the application preferences allow having disabled value
120   /// and the internal state tells to disable
121   /// \return the boolean result
122   virtual bool isValueEnabled() const;
123
124   /// Returns this parameter value in the xml file
125   /// \return the boolean result
126   bool isUseReset() const { return myUseReset; }
127
128   /// Returns this parameter value in the xml file
129   /// \return the boolean result
130   std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
131
132   /// Returns this widget value state
133   /// \return the enumeration result
134   ValueState getValueState() const { return myState; }
135
136   /// Stores the widget value if it is modified
137   void processValueState();
138
139   /// Returns an attribute error according to the value state
140   /// It exists in all cases excepring the "Store" case
141   Events_InfoMessage getValueStateError() const;
142
143   /// Defines if it is supposed that the widget should interact with the viewer.
144   virtual bool isViewerSelector() { return false; }
145
146   /// Defines if it is supported to set the value in this widget
147   /// By default it returns true
148   virtual bool canAcceptFocus() const { return true; };
149
150   //! Returns the widget error, get it from the attribute validator and state of the widget
151   //! If the feature is correct, it returns an empty value
152   //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
153   //! \return string value
154   QString getError(const bool theValueStateChecked = true) const;
155
156   /// Set the given wrapped value to the current widget
157   /// This value should be processed in the widget according to the needs
158   /// \param theValues the wrapped selection values
159   /// \param theToValidate the boolean value whether the value should be checked by filters
160   virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
161                             const bool theToValidate)
162   {
163     return false;
164   }
165
166   /// Returns values which should be highlighted when the whidget is active
167   /// \param theValues a list of presentations
168   virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
169
170   /// Checks if the selection presentation is valid in widget
171   /// \param theValue a selected presentation in the view
172   /// \return a boolean value
173   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
174   { return true; }
175
176   /// Returns widget validator, by default it is NULL. To be created in a child if necessary
177   ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
178
179   /// Restore value from attribute data to the widget's control.
180   /// Emits signals before and after store
181   /// \return True in success
182   bool restoreValue();
183
184   /// Saves the internal parameters to the given feature. Emits signals before and after store
185   /// \return True in success
186   bool storeValue();
187
188   /// Set focus to the first control of the current widget.
189   /// The focus policy of the control is checked.
190   /// If the widget has the NonFocus focus policy, it is skipped.
191   /// \return the state whether the widget can accept the focus
192   virtual bool focusTo();
193
194   /// Select the internal content if it can be selected. It is empty in the default realization
195   virtual void selectContent() {}
196
197   /// The method called when widget is activated
198   void activate();
199
200   /// The method called when widget is deactivated
201   virtual void deactivate();
202
203   /// Opportunity to do something after the active widget of the property panel changed
204   virtual void updateAfterDeactivation() {}
205   /// Opportunity to do something after the active widget of the property panel changed
206   virtual void updateAfterActivation() {}
207
208   /// The method called if widget should be activated always
209   virtual bool needToBeActiated() { return false; }
210
211   /// Returns list of widget controls
212   /// \return a control list
213   virtual QList<QWidget*> getControls() const = 0;
214
215   /// Returns the first or the last control that can accept the focus
216   /// \param isFirst if true, the first controls is returned or the last one
217   /// \return a control from a list of controls
218   QWidget* getControlAcceptingFocus(const bool isFirst);
219
220   /// FocusIn events processing
221   virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
222
223   /// \brief Enables processing of focus event on all controls by the widget
224   /// if this widget is not obligatory and set no-focus policy otherwise
225   virtual void enableFocusProcessing();
226
227   //! Switch On/Off highlighting of the widget
228   virtual void setHighlighted(bool isHighlighted);
229
230   /// Returns the attribute name
231   /// \returns the string value
232   std::string attributeID() const
233   {
234     return myAttributeID;
235   }
236
237   /// \return Current feature
238   FeaturePtr feature() const
239   {
240     return myFeature;
241   }
242
243   /// \return Context for translation
244   virtual std::string context() const {
245
246     std::string aContext = myFeatureId;
247     if(!aContext.empty() && !myAttributeID.empty()) {
248       aContext += ":";
249     }
250     aContext += myAttributeID;
251
252     return aContext;
253   }
254
255   /// Set feature which is processing by active operation
256   /// \param theFeature a feature object
257   /// \param theToStoreValue a value about necessity to store the widget value to the feature
258   /// \param isUpdateFlushed a flag if update should be flushed on store value
259   void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
260                   const bool isUpdateFlushed = true);
261
262   /// Editing mode depends on mode of current operation. This value is defined by it.
263   virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
264
265   /// \return Current Editing mode
266   bool isEditingMode() const { return myIsEditing; }
267
268   /// Returns true if the action can be processed. By default it is empty and returns false.
269   /// \param theActionType an action type
270   /// \param isActionEnabled if true, the enable state of the action
271   virtual bool canProcessAction(ModuleBase_ActionType theActionType, bool& isActionEnabled);
272
273   /// Returns true if the event is processed. The default implementation is empty, returns false.
274   virtual bool processAction(ModuleBase_ActionType theActionType,
275                              const ActionParamPtr& theParam = ActionParamPtr());
276
277   /// Returns list of accessible actions for Undo/Redo commands. By default it returns empty list.
278   /// \param theActionType type of action. It can be ActionUndo or ActionRedo.
279   virtual QList<ActionInfo> actionsList(ModuleBase_ActionType theActionType) const
280   { return QList<ActionInfo>(); }
281
282   /// Sends Update and Redisplay for the given object
283   /// \param theObj is updating object
284   void updateObject(ObjectPtr theObj);
285
286   /// Translate passed string with widget context()
287   virtual QString translate(const std::string& theStr) const;
288
289   /// Emit focus in widget to set this control as active in propety panel
290   void emitFocusInWidget() { emit focusInWidget(this); }
291
292   /// Finds model widget parent of the given sub widget
293   /// \param theWidget a candidate to be a child of the model widget
294   /// \param theProp a property panel instance
295   /// \return a model widget or NULL
296   static ModuleBase_ModelWidget* findModelWidget(ModuleBase_IPropertyPanel* theProp,
297                                                  QWidget* theWidget);
298
299   /// Returns True if the widget uses feature attribute.
300   /// If not then it means that the widget do not need attribute at all.
301   virtual bool usesAttribute() const { return true; }
302
303   /// It is called when user press Ok or OkPlus buttons in the parent property panel
304   /// By default this slot does nothing
305   virtual void onFeatureAccepted() {}
306
307 signals:
308   /// The signal about widget values are to be changed
309   void beforeValuesChanged();
310   /// The signal about widget values changed
311   void valuesChanged();
312   /// The signal about widget values modified
313   void valuesModified();
314   /// The signal about widget values are to be changed
315   void afterValuesChanged();
316
317   /// The signal about widget values are to be restored
318   void beforeValuesRestored();
319   /// The signal about widget values are to be restored
320   void afterValuesRestored();
321
322   /// The signal about key release on the control, that corresponds to the attribute
323   /// \param theObject a sender of the event
324   /// \param theEvent key release event
325   void keyReleased(QObject* theObject, QKeyEvent* theEvent);
326
327   /// The signal is emitted if the enter is clicked in the control of the widget
328   /// \param theObject a sender of the event
329   void enterClicked(QObject* theObject);
330
331   /// The signal about the widget is get focus
332   /// \param theWidget the model base widget
333   void focusInWidget(ModuleBase_ModelWidget* theWidget);
334
335   /// The signal about the widget is lost focus
336   /// \param theWidget the model base widget
337   void focusOutWidget(ModuleBase_ModelWidget* theWidget);
338
339   /// The signal about value state modification
340   void valueStateChanged(int theState);
341
342   /// The signal is emitted after flush of updates singal for the widget
343   void objectUpdated();
344
345 protected:
346   /// Sets default value of widget. Normally, widget should fetch this value
347   /// from the xml. However, some widgets derived widgets could define it
348   void setDefaultValue(const std::string& theValue);
349   /// \brief Set the attribute name
350   /// \param theAttribute the string value with attribute name
351   void setAttributeID(const std::string& theAttribute)
352   {
353     myAttributeID = theAttribute;
354   }
355
356   /// Sets the current value state. If the value is changed, the signal is emitted
357   /// If the current value state is Blocked, this method do nothing
358   /// \param theState a new state
359   /// \return the previous value state
360   ValueState setValueState(const ValueState& theState);
361
362   /// Blocks the value state change.
363   /// \param theBlocked a block state
364   /// \return the previous value
365   bool blockValueState(const bool theBlocked);
366
367   /// Compute the feature default value and fill the controls with it
368   /// or store the control value to the feature
369   virtual void initializeValueByActivate();
370
371   /// Saves the internal parameters to the given feature
372   /// \return True in success
373   virtual bool storeValueCustom() = 0;
374
375   /// Restore value from attribute data to the widget's control
376   virtual bool restoreValueCustom() = 0;
377
378   /// Fills the widget with default values
379   /// \return true if the widget current value is reset
380   virtual bool resetCustom() { return false; };
381
382   /// The method called when widget is activated
383   virtual void activateCustom() {};
384
385     //// Returns true if the event is processed. The default implementation is empty, returns false.
386   virtual bool processEnter();
387
388   //// Returns true if the event is processed. The default implementation is empty, returns false.
389   virtual bool processEscape();
390
391   //// Returns true if the event is processed. The default implementation is empty, returns false.
392   virtual bool processDelete();
393
394   /// Returns true if envent is processed. It applyes workshop selection for the widget attribute.
395   virtual bool processSelection();
396
397   protected slots:
398   /// Processing of values changed in model widget by store the current value to the feature
399   void onWidgetValuesChanged();
400
401   /// Changes widget state.
402   void onWidgetValuesModified();
403
404  protected:
405     /// own validator, by default it is zero
406    ModuleBase_WidgetValidator* myWidgetValidator;
407
408   /// The attribute name of the model feature
409   std::string myAttributeID;
410
411   /// A feature which is processing by active operation
412   FeaturePtr myFeature;
413
414   /// A feature ID
415   std::string myFeatureId;
416
417   /// Flag which shows that current operation is in editing mode
418   bool myIsEditing;
419
420   /// Flag which shows whether current widget is obligatory
421   /// The non-obligatory widgets should not accept the focus in the property panel
422   bool myIsObligatory;
423
424   /// Flag about value of the control is enabled (can be modified)
425   EnableState myIsValueEnabled;
426
427   /// The widget value state
428   ValueState myState;
429
430 private:
431   /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
432   bool myIsComputedDefault;
433
434   /// the default value, which is defined in the XML for this attribute
435   std::string myDefaultValue;
436
437   /// an XML internal state
438   bool myIsInternal;
439
440   // an XML state, the value is not stored into model if the widget is in edit mode
441   std::string myIsModifiedInEdit;
442
443   /// the reset state. If it is false, the reset method of the widget is not performed
444   bool myUseReset;
445   /// blocked flag of modification of the value state
446   bool myIsValueStateBlocked;
447   /// do not flush updated signal
448   bool myFlushUpdateBlocked;
449 };
450
451 #endif