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