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