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 Events_InfoMessage;
20 class ModuleBase_IWorkshop;
21 class ModuleBase_ViewerPrs;
22 class ModuleBase_WidgetValidator;
25 /**\class ModuleBase_ModelWidget
27 * \brief An abstract custom widget class. This class realization is assumed
28 * to create some controls.
29 * The controls values modification should send signal about values change.
31 * Common interface for widgets in the property panel.
32 * Every widget are able to save/restore data from the model and/or to contain other widgets.
35 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
39 /// State of the widget
40 enum ValueState { Stored, /// modification is finished and applyed to the model
41 ModifiedInPP, /// modification has not been finished and set to the model yet
42 ModifiedInViewer, /// modification performed by viewer events
43 Reset }; /// the value is reset
45 enum EnableState { On, /// the widget value is always enabled
46 Off, /// the widget value is always disabled
47 /// the widget value enable state is defined in preferences
48 DefinedInPreferences };
51 /// \param theParent the parent object
52 /// \param theData the widget configuration. The attribute of the model widget is obtained from
53 /// a low-level API for reading xml definitions of widgets
54 ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
56 virtual ~ModuleBase_ModelWidget();
58 /// Fills the widget with default values. It calls the resetCustom method and change
59 /// the widget state to Reset if the reset is performed.
60 /// \return true if the widget current value is reset
63 /// Returns the state whether the attribute of the feature is initialized
64 /// \param theObject a model feature to be checked
65 /// \return the boolean result
66 bool isInitialized(ObjectPtr theObject) const;
68 /// Returns true, if default value of the widget should be computed
69 /// on operation's execute, like radius for circle's constraint (can not be zero)
70 bool isComputedDefault() const { return myIsComputedDefault; }
72 /// Returns true, if default value of the widget is defined in the XML and it is not the
74 /// \return the boolean result
75 std::string getDefaultValue() const { return myDefaultValue; }
77 /// Returns true, if widget is internal
78 /// \return the boolean result
79 bool isInternal() const { return myIsInternal; }
81 /// Returns true, if the obligatory value of the widget is
82 /// not defined in the XML or has true value
83 /// \return the boolean result
84 bool isObligatory() const { return myIsObligatory; }
86 /// Returns true, if the widget value is enabled and might be modified manualy.
87 /// It returns false if the application preferences allow having disabled value
88 /// and the internal state tells to disable
89 /// \return the boolean result
90 virtual bool isValueEnabled() const;
92 /// Returns this parameter value in the xml file
93 /// \return the boolean result
94 bool isUseReset() const { return myUseReset; }
96 /// Returns this widget value state
97 /// \return the enumeration result
98 ValueState getValueState() const { return myState; }
100 /// Stores the widget value if it is modified
101 void processValueState();
103 /// Returns an attribute error according to the value state
104 /// It exists in all cases excepring the "Store" case
105 Events_InfoMessage getValueStateError() const;
107 /// Defines if it is supposed that the widget should interact with the viewer.
108 virtual bool isViewerSelector() { return false; }
110 /// Defines if it is supported to set the value in this widget
111 /// By default it returns true
112 virtual bool canAcceptFocus() const { return true; };
114 //! Returns the widget error, get it from the attribute validator and state of the widget
115 //! If the feature is correct, it returns an empty value
116 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
117 //! \return string value
118 QString getError(const bool theValueStateChecked = true) const;
120 /// Set the given wrapped value to the current widget
121 /// This value should be processed in the widget according to the needs
122 /// \param theValues the wrapped selection values
123 /// \param theToValidate the boolean value whether the value should be checked by filters
124 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
125 const bool theToValidate)
130 /// Returns values which should be highlighted when the whidget is active
131 /// \param theValues a list of presentations
132 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
134 /// Checks if the selection presentation is valid in widget
135 /// \param theValue a selected presentation in the view
136 /// \return a boolean value
137 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
140 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
141 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
143 /// Restore value from attribute data to the widget's control.
144 /// Emits signals before and after store
145 /// \return True in success
148 /// Saves the internal parameters to the given feature. Emits signals before and after store
149 /// \return True in success
152 /// Set focus to the first control of the current widget.
153 /// The focus policy of the control is checked.
154 /// If the widget has the NonFocus focus policy, it is skipped.
155 /// \return the state whether the widget can accept the focus
156 virtual bool focusTo();
158 /// Select the internal content if it can be selected. It is empty in the default realization
159 virtual void selectContent() {}
161 /// The method called when widget is activated
164 /// The method called when widget is deactivated
165 virtual void deactivate();
167 /// Returns list of widget controls
168 /// \return a control list
169 virtual QList<QWidget*> getControls() const = 0;
171 /// Returns the first or the last control that can accept the focus
172 /// \param isFirst if true, the first controls is returned or the last one
173 /// \return a control from a list of controls
174 QWidget* getControlAcceptingFocus(const bool isFirst);
176 /// FocusIn events processing
177 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
179 /// \brief Enables processing of focus event on all controls by the widget
180 /// if this widget is not obligatory and set no-focus policy otherwise
181 virtual void enableFocusProcessing();
183 //! Switch On/Off highlighting of the widget
184 virtual void setHighlighted(bool isHighlighted);
186 /// Returns the attribute name
187 /// \returns the string value
188 std::string attributeID() const
190 return myAttributeID;
193 /// \return Current feature
194 FeaturePtr feature() const
199 /// \return Context for translation
200 virtual std::string context() const {
202 std::string aContext = myFeatureId;
203 if(!aContext.empty() && !myAttributeID.empty()) {
206 aContext += myAttributeID;
211 /// Set feature which is processing by active operation
212 /// \param theFeature a feature object
213 /// \param theToStoreValue a value about necessity to store the widget value to the feature
214 /// \param isUpdateFlushed a flag if update should be flushed on store value
215 void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
216 const bool isUpdateFlushed = true);
218 /// Editing mode depends on mode of current operation. This value is defined by it.
219 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
221 /// \return Current Editing mode
222 bool isEditingMode() const { return myIsEditing; }
224 /// Returns true if the event is processed. The default implementation is empty, returns false.
225 virtual bool processEnter();
227 /// Returns true if the event is processed. The default implementation is empty, returns false.
228 virtual bool processDelete();
230 /// Sends Update and Redisplay for the given object
231 /// \param theObj is updating object
232 void updateObject(ObjectPtr theObj);
234 /// Sends Move event for the given object
235 /// \param theObj is object for moving
236 static void moveObject(ObjectPtr theObj);
238 /// Translate passed string with widget context()
239 virtual QString translate(const std::string& theStr) const;
242 /// The signal about widget values are to be changed
243 void beforeValuesChanged();
244 /// The signal about widget values changed
245 void valuesChanged();
246 /// The signal about widget values modified
247 void valuesModified();
248 /// The signal about widget values are to be changed
249 void afterValuesChanged();
251 /// The signal about widget values are to be restored
252 void beforeValuesRestored();
253 /// The signal about widget values are to be restored
254 void afterValuesRestored();
256 /// The signal about key release on the control, that corresponds to the attribute
257 /// \param theObject a sender of the event
258 /// \param theEvent key release event
259 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
261 /// The signal is emitted if the enter is clicked in the control of the widget
262 /// \param theObject a sender of the event
263 void enterClicked(QObject* theObject);
265 /// The signal about the widget is get focus
266 /// \param theWidget the model base widget
267 void focusInWidget(ModuleBase_ModelWidget* theWidget);
269 /// The signal about the widget is lost focus
270 /// \param theWidget the model base widget
271 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
273 /// The signal about value state modification
274 void valueStateChanged(int theState);
276 /// The signal is emitted after flush of updates singal for the widget
277 void objectUpdated();
280 /// Sets default value of widget. Normally, widget should fetch this value
281 /// from the xml. However, some widgets derived widgets could define it
282 void setDefaultValue(const std::string& theValue);
283 /// \brief Set the attribute name
284 /// \param theAttribute the string value with attribute name
285 void setAttributeID(const std::string& theAttribute)
287 myAttributeID = theAttribute;
290 /// Sets the current value state. If the value is changed, the signal is emitted
291 /// If the current value state is Blocked, this method do nothing
292 /// \param theState a new state
293 /// \return the previous value state
294 ValueState setValueState(const ValueState& theState);
296 /// Blocks the value state change.
297 /// \param theBlocked a block state
298 /// \return the previous value
299 bool blockValueState(const bool theBlocked);
301 /// Compute the feature default value and fill the controls with it
302 /// or store the control value to the feature
303 virtual void initializeValueByActivate();
305 /// Saves the internal parameters to the given feature
306 /// \return True in success
307 virtual bool storeValueCustom() = 0;
309 /// Restore value from attribute data to the widget's control
310 virtual bool restoreValueCustom() = 0;
312 /// Fills the widget with default values
313 /// \return true if the widget current value is reset
314 virtual bool resetCustom() { return false; };
316 /// The method called when widget is activated
317 virtual void activateCustom() {};
320 /// Processing of values changed in model widget by store the current value to the feature
321 void onWidgetValuesChanged();
323 /// Changes widget state.
324 void onWidgetValuesModified();
327 /// own validator, by default it is zero
328 ModuleBase_WidgetValidator* myWidgetValidator;
330 /// The attribute name of the model feature
331 std::string myAttributeID;
333 /// A feature which is processing by active operation
334 FeaturePtr myFeature;
337 std::string myFeatureId;
339 /// Flag which shows that current operation is in editing mode
342 /// Flag which shows whether current widget is obligatory
343 /// The non-obligatory widgets should not accept the focus in the property panel
346 /// Flag about value of the control is enabled (can be modified)
347 EnableState myIsValueEnabled;
349 /// The widget value state
353 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
354 bool myIsComputedDefault;
356 /// the default value, which is defined in the XML for this attribute
357 std::string myDefaultValue;
359 /// an XML internal state
362 /// the reset state. If it is false, the reset method of the widget is not performed
364 /// blocked flag of modification of the value state
365 bool myIsValueStateBlocked;
366 /// do not flush updated signal
367 bool myFlushUpdateBlocked;