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