Salome HOME
A modification to commit the Lenght and the Radius constraints by the preselection.
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.h
1 // File:        ModuleBase_ModelWidget.h
2 // Created:     25 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #ifndef MODULEBASE_MODELWIDGET_H
6 #define MODULEBASE_MODELWIDGET_H
7
8 #include <ModuleBase.h>
9
10 #include <ModelAPI_Feature.h>
11
12 #include <QObject>
13
14 #include <memory>
15
16 class Config_WidgetAPI;
17 class ModuleBase_WidgetValue;
18 class QKeyEvent;
19
20 /**\class ModuleBase_ModelWidget
21  * \brief An abstract custom widget class. This class realization is assumed to create some controls.
22  * The controls values modification should send signal about values change.
23  *
24  * Common interface for widgets in the property panel.
25  * Every widget are able to save/restore data from the model and/or to contain other widgets.
26  *
27  */
28 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QObject
29 {
30 Q_OBJECT
31  public:
32   /// Constructor
33   /// \theParent the parent object
34   /// \theData the widget configuation. The attribute of the model widget is obtained from
35   ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData,
36                          const std::string& theParentId);
37   /// Destructor
38   virtual ~ModuleBase_ModelWidget()
39   {
40   }
41
42   /// Returns the state whether the attribute of the feature is initialized
43   /// \param theObject a model feature to be checked
44   /// \return the boolean result
45   bool isInitialized(ObjectPtr theObject) const;
46
47   /// Returns true, if default value of the widget should be computed
48   /// on operation's execute, like radius for circle's constraint (can not be zero)
49   bool isComputedDefault() { return myIsComputedDefault; }
50
51   /// Defines if it is supposed that the widget should interact with the viewer.
52   virtual bool isViewerSelector() { return false; }
53
54   /// Defines if it is supported to set the value in this widget
55   /// By default it returns true
56   virtual bool canSetValue() const { return true; };
57
58   /// Set the given wrapped value to the current widget
59   /// This value should be processed in the widget according to the needs
60   /// \param theValue the wrapped widget value
61   virtual bool setValue(ModuleBase_WidgetValue* theValue)
62   {
63     return false;
64   }
65
66   /// Saves the internal parameters to the given feature
67   /// \param theObject a model feature to be changed
68   virtual bool storeValue() const = 0;
69
70   virtual bool restoreValue() = 0;
71
72   /// Set focus to the first control of the current widget. The focus policy of the control is checked.
73   /// If the widget has the NonFocus focus policy, it is skipped.
74   /// \return the state whether the widget can accept the focus
75   virtual bool focusTo();
76
77   /// The methiod called when widget is activated
78   virtual void activate() {}
79
80   /// The methiod called when widget is deactivated
81   virtual void deactivate() {}
82
83   /// Returns the internal parent wiget control, that can be shown anywhere
84   /// \returns the widget
85   virtual QWidget* getControl() const = 0;
86
87   /// Returns list of widget controls
88   /// \return a control list
89   virtual QList<QWidget*> getControls() const = 0;
90
91
92   /// FocusIn events processing
93   virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
94
95
96   void enableFocusProcessing();
97
98   void setHighlighted(bool isHighlighted);
99
100   /// Returns the attribute name
101   /// \returns the string value
102   std::string attributeID() const
103   {
104     return myAttributeID;
105   }
106
107   /// Returns the parent of the attribute
108   /// \returns the string value
109   std::string parentID() const
110   {
111     return myParentId;
112   }
113
114   FeaturePtr feature() const
115   {
116     return myFeature;
117   }
118
119   void setFeature(const FeaturePtr& theFeature)
120   {
121     myFeature = theFeature;
122   }
123
124 signals:
125   /// The signal about widget values changed
126   void valuesChanged();
127   /// The signal about key release on the control, that corresponds to the attribute
128   /// \param theAttributeName a name of the attribute
129   /// \param theEvent key release event
130   void keyReleased(QKeyEvent* theEvent);
131   /// The signal about the widget is get focus
132   /// \param theWidget the model base widget
133   void focusInWidget(ModuleBase_ModelWidget* theWidget);
134   /// The signal about the widget is lost focus
135   /// \param theWidget the model base widget
136   void focusOutWidget(ModuleBase_ModelWidget* theWidget);
137
138  protected:
139   /// Returns the attribute name
140   /// \returns the string value
141   void setAttributeID(const std::string& theAttribute)
142   {
143     myAttributeID = theAttribute;
144   }
145
146   void updateObject(ObjectPtr theObj) const;
147
148  protected:
149   std::string myAttributeID; /// the attribute name of the model feature
150   std::string myParentId;    /// name of parent
151   FeaturePtr myFeature;
152
153   bool myIsComputedDefault; /// Value should be computed on execute,
154                             /// like radius for circle's constraint (can not be zero)
155 };
156
157 #endif