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