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