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