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