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