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