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