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