Salome HOME
25db7bcfce3ca4be4388b04609374bb89eeebc8d
[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 widget value state
97   /// \return the enumeration result
98   ValueState getValueState() const { return myState; }
99
100   /// Stores the widget value if it is modified
101   void processValueState();
102
103   /// Returns an attribute error according to the value state
104   /// It exists in all cases excepring the "Store" case
105   Events_InfoMessage getValueStateError() const;
106
107   /// Defines if it is supposed that the widget should interact with the viewer.
108   virtual bool isViewerSelector() { return false; }
109
110   /// Defines if it is supported to set the value in this widget
111   /// By default it returns true
112   virtual bool canAcceptFocus() const { return true; };
113
114   //! Returns the widget error, get it from the attribute validator and state of the widget
115   //! If the feature is correct, it returns an empty value
116   //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
117   //! \return string value
118   QString getError(const bool theValueStateChecked = true) const;
119
120   /// Set the given wrapped value to the current widget
121   /// This value should be processed in the widget according to the needs
122   /// \param theValues the wrapped selection values
123   /// \param theToValidate the boolean value whether the value should be checked by filters
124   virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
125                             const bool theToValidate)
126   {
127     return false;
128   }
129
130   /// Returns values which should be highlighted when the whidget is active
131   /// \param theValues a list of presentations
132   virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
133
134   /// Checks if the selection presentation is valid in widget
135   /// \param theValue a selected presentation in the view
136   /// \return a boolean value
137   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
138   { return true; }
139
140   /// Returns widget validator, by default it is NULL. To be created in a child if necessary
141   ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
142
143   /// Restore value from attribute data to the widget's control.
144   /// Emits signals before and after store
145   /// \return True in success
146   bool restoreValue();
147
148   /// Saves the internal parameters to the given feature. Emits signals before and after store
149   /// \return True in success
150   bool storeValue();
151
152   /// Set focus to the first control of the current widget.
153   /// The focus policy of the control is checked.
154   /// If the widget has the NonFocus focus policy, it is skipped.
155   /// \return the state whether the widget can accept the focus
156   virtual bool focusTo();
157
158   /// Select the internal content if it can be selected. It is empty in the default realization
159   virtual void selectContent() {}
160
161   /// The method called when widget is activated
162   void activate();
163
164   /// The method called when widget is deactivated
165   virtual void deactivate();
166
167   /// Returns list of widget controls
168   /// \return a control list
169   virtual QList<QWidget*> getControls() const = 0;
170
171   /// Returns the first or the last control that can accept the focus
172   /// \param isFirst if true, the first controls is returned or the last one
173   /// \return a control from a list of controls
174   QWidget* getControlAcceptingFocus(const bool isFirst);
175
176   /// FocusIn events processing
177   virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
178
179   /// \brief Enables processing of focus event on all controls by the widget
180   /// if this widget is not obligatory and set no-focus policy otherwise
181   virtual void enableFocusProcessing();
182
183   //! Switch On/Off highlighting of the widget
184   virtual void setHighlighted(bool isHighlighted);
185
186   /// Returns the attribute name
187   /// \returns the string value
188   std::string attributeID() const
189   {
190     return myAttributeID;
191   }
192
193   /// \return Current feature
194   FeaturePtr feature() const
195   {
196     return myFeature;
197   }
198
199   /// \return Context for translation
200   virtual std::string context() const {
201
202     std::string aContext = myFeatureId;
203     if(!aContext.empty() && !myAttributeID.empty()) {
204       aContext += ":";
205     }
206     aContext += myAttributeID;
207
208     return aContext;
209   }
210
211   /// Set feature which is processing by active operation
212   /// \param theFeature a feature object
213   /// \param theToStoreValue a value about necessity to store the widget value to the feature
214   /// \param isUpdateFlushed a flag if update should be flushed on store value
215   void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
216                   const bool isUpdateFlushed = true);
217
218   /// Editing mode depends on mode of current operation. This value is defined by it.
219   virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
220
221   /// \return Current Editing mode
222   bool isEditingMode() const { return myIsEditing; }
223
224   /// Returns true if the event is processed. The default implementation is empty, returns false.
225   virtual bool processEnter();
226
227   /// Returns true if the event is processed. The default implementation is empty, returns false.
228   virtual bool processDelete();
229
230   /// Sends Update and Redisplay for the given object
231   /// \param theObj is updating object
232   void updateObject(ObjectPtr theObj);
233
234   /// Sends Move event for the given object
235   /// \param theObj is object for moving
236   static void moveObject(ObjectPtr theObj);
237
238   /// Translate passed string with widget context()
239   virtual QString translate(const std::string& theStr) const;
240
241 signals:
242   /// The signal about widget values are to be changed
243   void beforeValuesChanged();
244   /// The signal about widget values changed
245   void valuesChanged();
246   /// The signal about widget values modified
247   void valuesModified();
248   /// The signal about widget values are to be changed
249   void afterValuesChanged();
250
251   /// The signal about widget values are to be restored
252   void beforeValuesRestored();
253   /// The signal about widget values are to be restored
254   void afterValuesRestored();
255
256   /// The signal about key release on the control, that corresponds to the attribute
257   /// \param theObject a sender of the event
258   /// \param theEvent key release event
259   void keyReleased(QObject* theObject, QKeyEvent* theEvent);
260
261   /// The signal is emitted if the enter is clicked in the control of the widget
262   /// \param theObject a sender of the event
263   void enterClicked(QObject* theObject);
264
265   /// The signal about the widget is get focus
266   /// \param theWidget the model base widget
267   void focusInWidget(ModuleBase_ModelWidget* theWidget);
268
269   /// The signal about the widget is lost focus
270   /// \param theWidget the model base widget
271   void focusOutWidget(ModuleBase_ModelWidget* theWidget);
272
273   /// The signal about value state modification
274   void valueStateChanged(int theState);
275
276   /// The signal is emitted after flush of updates singal for the widget
277   void objectUpdated();
278
279 protected:
280   /// Sets default value of widget. Normally, widget should fetch this value
281   /// from the xml. However, some widgets derived widgets could define it
282   void setDefaultValue(const std::string& theValue);
283   /// \brief Set the attribute name
284   /// \param theAttribute the string value with attribute name
285   void setAttributeID(const std::string& theAttribute)
286   {
287     myAttributeID = theAttribute;
288   }
289
290   /// Sets the current value state. If the value is changed, the signal is emitted
291   /// If the current value state is Blocked, this method do nothing
292   /// \param theState a new state
293   /// \return the previous value state
294   ValueState setValueState(const ValueState& theState);
295
296   /// Blocks the value state change.
297   /// \param theBlocked a block state
298   /// \return the previous value
299   bool blockValueState(const bool theBlocked);
300
301   /// Compute the feature default value and fill the controls with it
302   /// or store the control value to the feature
303   virtual void initializeValueByActivate();
304
305   /// Saves the internal parameters to the given feature
306   /// \return True in success
307   virtual bool storeValueCustom() = 0;
308
309   /// Restore value from attribute data to the widget's control
310   virtual bool restoreValueCustom() = 0;
311
312   /// Fills the widget with default values
313   /// \return true if the widget current value is reset
314   virtual bool resetCustom() { return false; };
315
316   /// The method called when widget is activated
317   virtual void activateCustom() {};
318
319 protected slots:
320   /// Processing of values changed in model widget by store the current value to the feature
321   void onWidgetValuesChanged();
322
323   /// Changes widget state.
324   void onWidgetValuesModified();
325
326  protected:
327     /// own validator, by default it is zero
328    ModuleBase_WidgetValidator* myWidgetValidator;
329
330   /// The attribute name of the model feature
331   std::string myAttributeID;
332
333   /// A feature which is processing by active operation
334   FeaturePtr myFeature;
335
336   /// A feature ID
337   std::string myFeatureId;
338
339   /// Flag which shows that current operation is in editing mode
340   bool myIsEditing;
341
342   /// Flag which shows whether current widget is obligatory
343   /// The non-obligatory widgets should not accept the focus in the property panel
344   bool myIsObligatory;
345
346   /// Flag about value of the control is enabled (can be modified)
347   EnableState myIsValueEnabled;
348
349   /// The widget value state
350   ValueState myState;
351
352 private:
353   /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
354   bool myIsComputedDefault;
355
356   /// the default value, which is defined in the XML for this attribute
357   std::string myDefaultValue;
358
359   /// an XML internal state
360   bool myIsInternal;
361
362   /// the reset state. If it is false, the reset method of the widget is not performed
363   bool myUseReset;
364   /// blocked flag of modification of the value state
365   bool myIsValueStateBlocked;
366   /// do not flush updated signal
367   bool myFlushUpdateBlocked;
368 };
369
370 #endif