1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: ModuleBase_ModelWidget.h
4 // Created: 25 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #ifndef MODULEBASE_MODELWIDGET_H
8 #define MODULEBASE_MODELWIDGET_H
10 #include <ModuleBase.h>
11 #include <ModuleBase_OperationFeature.h>
12 #include <ModelAPI_Feature.h>
18 class Config_WidgetAPI;
19 class ModuleBase_IWorkshop;
20 class ModuleBase_ViewerPrs;
21 class ModuleBase_WidgetValidator;
24 /**\class ModuleBase_ModelWidget
26 * \brief An abstract custom widget class. This class realization is assumed to create some controls.
27 * The controls values modification should send signal about values change.
29 * Common interface for widgets in the property panel.
30 * Every widget are able to save/restore data from the model and/or to contain other widgets.
33 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
37 /// State of the widget
38 enum ValueState { Stored, /// modification is finished and applyed to the model
39 ModifiedInPP, /// modification has not been finished and set to the model yet
40 ModifiedInViewer, /// modification performed by viewer events
41 Reset }; /// the value is reset
43 enum EnableState { On, /// the widget value is always enabled
44 Off, /// the widget value is always disabled
45 DefinedInPreferences }; /// the widget value enable state is defined in preferences
48 /// \param theParent the parent object
49 /// \param theData the widget configuration. The attribute of the model widget is obtained from
50 /// a low-level API for reading xml definitions of widgets
51 ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
53 virtual ~ModuleBase_ModelWidget();
55 /// Fills the widget with default values. It calls the resetCustom method and change
56 /// the widget state to Reset if the reset is performed.
57 /// \return true if the widget current value is reset
60 /// Returns the state whether the attribute of the feature is initialized
61 /// \param theObject a model feature to be checked
62 /// \return the boolean result
63 bool isInitialized(ObjectPtr theObject) const;
65 /// Returns true, if default value of the widget should be computed
66 /// on operation's execute, like radius for circle's constraint (can not be zero)
67 bool isComputedDefault() const { return myIsComputedDefault; }
69 /// Returns true, if default value of the widget is defined in the XML and it is not the
71 /// \return the boolean result
72 std::string getDefaultValue() const { return myDefaultValue; }
74 /// Returns true, if widget is internal
75 /// \return the boolean result
76 bool isInternal() const { return myIsInternal; }
78 /// Returns true, if the obligatory value of the widget is not defined in the XML or has true value
79 /// \return the boolean result
80 bool isObligatory() const { return myIsObligatory; }
82 /// Returns true, if the widget value is enabled and might be modified manualy. It returns false if
83 /// the application preferences allow having disabled value and the internal state tells to disable
84 /// \return the boolean result
85 virtual bool isValueEnabled() const;
87 /// Returns this parameter value in the xml file
88 /// \return the boolean result
89 bool isUseReset() const { return myUseReset; }
91 /// Returns this widget value state
92 /// \return the enumeration result
93 ValueState getValueState() const { return myState; }
95 /// Stores the widget value if it is modified
96 void processValueState();
98 /// Returns an attribute error according to the value state
99 /// It exists in all cases excepring the "Store" case
100 QString getValueStateError() const;
102 /// Defines if it is supposed that the widget should interact with the viewer.
103 virtual bool isViewerSelector() { return false; }
105 /// Defines if it is supported to set the value in this widget
106 /// By default it returns true
107 virtual bool canAcceptFocus() const { return true; };
109 //! Returns the widget error, get it from the attribute validator and state of the widget
110 //! If the feature is correct, it returns an empty value
111 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
112 //! \return string value
113 QString getError(const bool theValueStateChecked = true) const;
115 /// Set the given wrapped value to the current widget
116 /// This value should be processed in the widget according to the needs
117 /// \param theValues the wrapped selection values
118 /// \param theToValidate the boolean value whether the value should be checked by filters
119 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
120 const bool theToValidate)
125 /// Returns values which should be highlighted when the whidget is active
126 /// \param theValues a list of presentations
127 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
129 /// Checks if the selection presentation is valid in widget
130 /// \param theValue a selected presentation in the view
131 /// \return a boolean value
132 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue) { return true; }
134 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
135 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
137 /// Restore value from attribute data to the widget's control. Emits signals before and after store
138 /// \return True in success
141 /// Saves the internal parameters to the given feature. Emits signals before and after store
142 /// \return True in success
145 /// Set focus to the first control of the current widget. The focus policy of the control is checked.
146 /// If the widget has the NonFocus focus policy, it is skipped.
147 /// \return the state whether the widget can accept the focus
148 virtual bool focusTo();
150 /// Select the internal content if it can be selected. It is empty in the default realization
151 virtual void selectContent() {}
153 /// The method called when widget is activated
156 /// The method called when widget is deactivated
157 virtual void deactivate();
159 /// Returns list of widget controls
160 /// \return a control list
161 virtual QList<QWidget*> getControls() const = 0;
163 /// Returns the first or the last control that can accept the focus
164 /// \param isFirst if true, the first controls is returned or the last one
165 /// \return a control from a list of controls
166 QWidget* getControlAcceptingFocus(const bool isFirst);
168 /// FocusIn events processing
169 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
171 /// \brief Enables processing of focus event on all controls by the widget
172 /// if this widget is not obligatory and set no-focus policy otherwise
173 virtual void enableFocusProcessing();
175 //! Switch On/Off highlighting of the widget
176 virtual void setHighlighted(bool isHighlighted);
178 /// Returns the attribute name
179 /// \returns the string value
180 std::string attributeID() const
182 return myAttributeID;
185 /// \return Current feature
186 FeaturePtr feature() const
191 /// Set feature which is processing by active operation
192 /// \param theFeature a feature object
193 /// \param theToStoreValue a value about necessity to store the widget value to the feature
194 /// \param isUpdateFlushed a flag if update should be flushed on store value
195 void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
196 const bool isUpdateFlushed = true);
198 /// Editing mode depends on mode of current operation. This value is defined by it.
199 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
201 /// \return Current Editing mode
202 bool isEditingMode() const { return myIsEditing; }
204 /// Returns true if the event is processed. The default implementation is empty, returns false.
205 virtual bool processEnter();
207 /// Returns true if the event is processed. The default implementation is empty, returns false.
208 virtual bool processDelete();
210 /// Sends Update and Redisplay for the given object
211 /// \param theObj is updating object
212 void updateObject(ObjectPtr theObj);
214 /// Sends Move event for the given object
215 /// \param theObj is object for moving
216 static void moveObject(ObjectPtr theObj);
219 /// The signal about widget values are to be changed
220 void beforeValuesChanged();
221 /// The signal about widget values changed
222 void valuesChanged();
223 /// The signal about widget values modified
224 void valuesModified();
225 /// The signal about widget values are to be changed
226 void afterValuesChanged();
228 /// The signal about widget values are to be restored
229 void beforeValuesRestored();
230 /// The signal about widget values are to be restored
231 void afterValuesRestored();
233 /// The signal about key release on the control, that corresponds to the attribute
234 /// \param theObject a sender of the event
235 /// \param theEvent key release event
236 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
238 /// The signal is emitted if the enter is clicked in the control of the widget
239 /// \param theObject a sender of the event
240 void enterClicked(QObject* theObject);
242 /// The signal about the widget is get focus
243 /// \param theWidget the model base widget
244 void focusInWidget(ModuleBase_ModelWidget* theWidget);
246 /// The signal about the widget is lost focus
247 /// \param theWidget the model base widget
248 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
250 /// The signal about value state modification
251 void valueStateChanged(int theState);
253 /// The signal is emitted after flush of updates singal for the widget
254 void objectUpdated();
257 /// Sets default value of widget. Normally, widget should fetch this value
258 /// from the xml. However, some widgets derived widgets could define it
259 void setDefaultValue(const std::string& theValue);
260 /// \brief Set the attribute name
261 /// \param theAttribute the string value with attribute name
262 void setAttributeID(const std::string& theAttribute)
264 myAttributeID = theAttribute;
267 /// Sets the current value state. If the value is changed, the signal is emitted
268 /// If the current value state is Blocked, this method do nothing
269 /// \param theState a new state
270 /// \return the previous value state
271 ValueState setValueState(const ValueState& theState);
273 /// Blocks the value state change.
274 /// \param theBlocked a block state
275 /// \return the previous value
276 bool blockValueState(const bool theBlocked);
278 /// Compute the feature default value and fill the controls with it
279 /// or store the control value to the feature
280 virtual void initializeValueByActivate();
282 /// Saves the internal parameters to the given feature
283 /// \return True in success
284 virtual bool storeValueCustom() = 0;
286 /// Restore value from attribute data to the widget's control
287 virtual bool restoreValueCustom() = 0;
289 /// Fills the widget with default values
290 /// \return true if the widget current value is reset
291 virtual bool resetCustom() { return false; };
293 /// The method called when widget is activated
294 virtual void activateCustom() {};
296 QString translateString(const QString& theMsg) const;
299 /// Processing of values changed in model widget by store the current value to the feature
300 void onWidgetValuesChanged();
302 /// Changes widget state.
303 void onWidgetValuesModified();
306 /// own validator, by default it is zero
307 ModuleBase_WidgetValidator* myWidgetValidator;
309 /// The attribute name of the model feature
310 std::string myAttributeID;
312 /// A feature which is processing by active operation
313 FeaturePtr myFeature;
315 /// Flag which shows that current operation is in editing mode
318 /// Flag which shows whether current widget is obligatory
319 /// The non-obligatory widgets should not accept the focus in the property panel
322 /// Flag about value of the control is enabled (can be modified)
323 EnableState myIsValueEnabled;
325 /// The widget value state
329 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
330 bool myIsComputedDefault;
332 /// the default value, which is defined in the XML for this attribute
333 std::string myDefaultValue;
335 /// an XML internal state
338 /// the reset state. If it is false, the reset method of the widget is not performed
340 /// blocked flag of modification of the value state
341 bool myIsValueStateBlocked;
342 /// do not flush updated signal
343 bool myFlushUpdateBlocked;