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 parameter value in the xml file
97 /// \return the boolean result
98 std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
100 /// Returns this widget value state
101 /// \return the enumeration result
102 ValueState getValueState() const { return myState; }
104 /// Stores the widget value if it is modified
105 void processValueState();
107 /// Returns an attribute error according to the value state
108 /// It exists in all cases excepring the "Store" case
109 Events_InfoMessage getValueStateError() const;
111 /// Defines if it is supposed that the widget should interact with the viewer.
112 virtual bool isViewerSelector() { return false; }
114 /// Defines if it is supported to set the value in this widget
115 /// By default it returns true
116 virtual bool canAcceptFocus() const { return true; };
118 //! Returns the widget error, get it from the attribute validator and state of the widget
119 //! If the feature is correct, it returns an empty value
120 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
121 //! \return string value
122 QString getError(const bool theValueStateChecked = true) const;
124 /// Set the given wrapped value to the current widget
125 /// This value should be processed in the widget according to the needs
126 /// \param theValues the wrapped selection values
127 /// \param theToValidate the boolean value whether the value should be checked by filters
128 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
129 const bool theToValidate)
134 /// Returns values which should be highlighted when the whidget is active
135 /// \param theValues a list of presentations
136 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
138 /// Checks if the selection presentation is valid in widget
139 /// \param theValue a selected presentation in the view
140 /// \return a boolean value
141 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
144 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
145 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
147 /// Restore value from attribute data to the widget's control.
148 /// Emits signals before and after store
149 /// \return True in success
152 /// Saves the internal parameters to the given feature. Emits signals before and after store
153 /// \return True in success
156 /// Set focus to the first control of the current widget.
157 /// The focus policy of the control is checked.
158 /// If the widget has the NonFocus focus policy, it is skipped.
159 /// \return the state whether the widget can accept the focus
160 virtual bool focusTo();
162 /// Select the internal content if it can be selected. It is empty in the default realization
163 virtual void selectContent() {}
165 /// The method called when widget is activated
168 /// The method called when widget is deactivated
169 virtual void deactivate();
171 /// Returns list of widget controls
172 /// \return a control list
173 virtual QList<QWidget*> getControls() const = 0;
175 /// Returns the first or the last control that can accept the focus
176 /// \param isFirst if true, the first controls is returned or the last one
177 /// \return a control from a list of controls
178 QWidget* getControlAcceptingFocus(const bool isFirst);
180 /// FocusIn events processing
181 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
183 /// \brief Enables processing of focus event on all controls by the widget
184 /// if this widget is not obligatory and set no-focus policy otherwise
185 virtual void enableFocusProcessing();
187 //! Switch On/Off highlighting of the widget
188 virtual void setHighlighted(bool isHighlighted);
190 /// Returns the attribute name
191 /// \returns the string value
192 std::string attributeID() const
194 return myAttributeID;
197 /// \return Current feature
198 FeaturePtr feature() const
203 /// \return Context for translation
204 virtual std::string context() const {
206 std::string aContext = myFeatureId;
207 if(!aContext.empty() && !myAttributeID.empty()) {
210 aContext += myAttributeID;
215 /// Set feature which is processing by active operation
216 /// \param theFeature a feature object
217 /// \param theToStoreValue a value about necessity to store the widget value to the feature
218 /// \param isUpdateFlushed a flag if update should be flushed on store value
219 void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
220 const bool isUpdateFlushed = true);
222 /// Editing mode depends on mode of current operation. This value is defined by it.
223 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
225 /// \return Current Editing mode
226 bool isEditingMode() const { return myIsEditing; }
228 /// Returns true if the event is processed. The default implementation is empty, returns false.
229 virtual bool processEnter();
231 /// Returns true if the event is processed. The default implementation is empty, returns false.
232 virtual bool processDelete();
234 /// Sends Update and Redisplay for the given object
235 /// \param theObj is updating object
236 void updateObject(ObjectPtr theObj);
238 /// Sends Move event for the given object
239 /// \param theObj is object for moving
240 static void moveObject(ObjectPtr theObj);
242 /// Translate passed string with widget context()
243 virtual QString translate(const std::string& theStr) const;
246 /// The signal about widget values are to be changed
247 void beforeValuesChanged();
248 /// The signal about widget values changed
249 void valuesChanged();
250 /// The signal about widget values modified
251 void valuesModified();
252 /// The signal about widget values are to be changed
253 void afterValuesChanged();
255 /// The signal about widget values are to be restored
256 void beforeValuesRestored();
257 /// The signal about widget values are to be restored
258 void afterValuesRestored();
260 /// The signal about key release on the control, that corresponds to the attribute
261 /// \param theObject a sender of the event
262 /// \param theEvent key release event
263 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
265 /// The signal is emitted if the enter is clicked in the control of the widget
266 /// \param theObject a sender of the event
267 void enterClicked(QObject* theObject);
269 /// The signal about the widget is get focus
270 /// \param theWidget the model base widget
271 void focusInWidget(ModuleBase_ModelWidget* theWidget);
273 /// The signal about the widget is lost focus
274 /// \param theWidget the model base widget
275 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
277 /// The signal about value state modification
278 void valueStateChanged(int theState);
280 /// The signal is emitted after flush of updates singal for the widget
281 void objectUpdated();
284 /// Sets default value of widget. Normally, widget should fetch this value
285 /// from the xml. However, some widgets derived widgets could define it
286 void setDefaultValue(const std::string& theValue);
287 /// \brief Set the attribute name
288 /// \param theAttribute the string value with attribute name
289 void setAttributeID(const std::string& theAttribute)
291 myAttributeID = theAttribute;
294 /// Sets the current value state. If the value is changed, the signal is emitted
295 /// If the current value state is Blocked, this method do nothing
296 /// \param theState a new state
297 /// \return the previous value state
298 ValueState setValueState(const ValueState& theState);
300 /// Blocks the value state change.
301 /// \param theBlocked a block state
302 /// \return the previous value
303 bool blockValueState(const bool theBlocked);
305 /// Compute the feature default value and fill the controls with it
306 /// or store the control value to the feature
307 virtual void initializeValueByActivate();
309 /// Saves the internal parameters to the given feature
310 /// \return True in success
311 virtual bool storeValueCustom() = 0;
313 /// Restore value from attribute data to the widget's control
314 virtual bool restoreValueCustom() = 0;
316 /// Fills the widget with default values
317 /// \return true if the widget current value is reset
318 virtual bool resetCustom() { return false; };
320 /// The method called when widget is activated
321 virtual void activateCustom() {};
324 /// Processing of values changed in model widget by store the current value to the feature
325 void onWidgetValuesChanged();
327 /// Changes widget state.
328 void onWidgetValuesModified();
331 /// own validator, by default it is zero
332 ModuleBase_WidgetValidator* myWidgetValidator;
334 /// The attribute name of the model feature
335 std::string myAttributeID;
337 /// A feature which is processing by active operation
338 FeaturePtr myFeature;
341 std::string myFeatureId;
343 /// Flag which shows that current operation is in editing mode
346 /// Flag which shows whether current widget is obligatory
347 /// The non-obligatory widgets should not accept the focus in the property panel
350 /// Flag about value of the control is enabled (can be modified)
351 EnableState myIsValueEnabled;
353 /// The widget value state
357 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
358 bool myIsComputedDefault;
360 /// the default value, which is defined in the XML for this attribute
361 std::string myDefaultValue;
363 /// an XML internal state
366 // an XML state, the value is not stored into model if the widget is in edit mode
367 std::string myIsModifiedInEdit;
369 /// the reset state. If it is false, the reset method of the widget is not performed
371 /// blocked flag of modification of the value state
372 bool myIsValueStateBlocked;
373 /// do not flush updated signal
374 bool myFlushUpdateBlocked;