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