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_IPropertyPanel;
21 class ModuleBase_IWorkshop;
22 class ModuleBase_ViewerPrs;
23 class ModuleBase_WidgetValidator;
26 /**\class ModuleBase_ModelWidget
28 * \brief An abstract custom widget class. This class realization is assumed
29 * to create some controls.
30 * The controls values modification should send signal about values change.
32 * Common interface for widgets in the property panel.
33 * Every widget are able to save/restore data from the model and/or to contain other widgets.
36 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
40 /// State of the widget
41 enum ValueState { Stored, /// modification is finished and applyed to the model
42 ModifiedInPP, /// modification has not been finished and set to the model yet
43 ModifiedInViewer, /// modification performed by viewer events
44 Reset }; /// the value is reset
46 enum EnableState { On, /// the widget value is always enabled
47 Off, /// the widget value is always disabled
48 /// the widget value enable state is defined in preferences
49 DefinedInPreferences };
52 /// \param theParent the parent object
53 /// \param theData the widget configuration. The attribute of the model widget is obtained from
54 /// a low-level API for reading xml definitions of widgets
55 ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
57 virtual ~ModuleBase_ModelWidget();
59 /// Fills the widget with default values. It calls the resetCustom method and change
60 /// the widget state to Reset if the reset is performed.
61 /// \return true if the widget current value is reset
64 /// Returns the state whether the attribute of the feature is initialized
65 /// \param theObject a model feature to be checked
66 /// \return the boolean result
67 bool isInitialized(ObjectPtr theObject) const;
69 /// Returns true, if default value of the widget should be computed
70 /// on operation's execute, like radius for circle's constraint (can not be zero)
71 bool isComputedDefault() const { return myIsComputedDefault; }
73 /// Returns true, if default value of the widget is defined in the XML and it is not the
75 /// \return the boolean result
76 std::string getDefaultValue() const { return myDefaultValue; }
78 /// Returns true, if widget is internal
79 /// \return the boolean result
80 bool isInternal() const { return myIsInternal; }
82 /// Returns true, if the obligatory value of the widget is
83 /// not defined in the XML or has true value
84 /// \return the boolean result
85 bool isObligatory() const { return myIsObligatory; }
87 /// Returns true, if the widget value is enabled and might be modified manualy.
88 /// It returns false if the application preferences allow having disabled value
89 /// and the internal state tells to disable
90 /// \return the boolean result
91 virtual bool isValueEnabled() const;
93 /// Returns this parameter value in the xml file
94 /// \return the boolean result
95 bool isUseReset() const { return myUseReset; }
97 /// Returns this parameter value in the xml file
98 /// \return the boolean result
99 std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
101 /// Returns this widget value state
102 /// \return the enumeration result
103 ValueState getValueState() const { return myState; }
105 /// Stores the widget value if it is modified
106 void processValueState();
108 /// Returns an attribute error according to the value state
109 /// It exists in all cases excepring the "Store" case
110 Events_InfoMessage getValueStateError() const;
112 /// Defines if it is supposed that the widget should interact with the viewer.
113 virtual bool isViewerSelector() { return false; }
115 /// Defines if it is supported to set the value in this widget
116 /// By default it returns true
117 virtual bool canAcceptFocus() const { return true; };
119 //! Returns the widget error, get it from the attribute validator and state of the widget
120 //! If the feature is correct, it returns an empty value
121 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
122 //! \return string value
123 QString getError(const bool theValueStateChecked = true) const;
125 /// Set the given wrapped value to the current widget
126 /// This value should be processed in the widget according to the needs
127 /// \param theValues the wrapped selection values
128 /// \param theToValidate the boolean value whether the value should be checked by filters
129 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
130 const bool theToValidate)
135 /// Returns values which should be highlighted when the whidget is active
136 /// \param theValues a list of presentations
137 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
139 /// Checks if the selection presentation is valid in widget
140 /// \param theValue a selected presentation in the view
141 /// \return a boolean value
142 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
145 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
146 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
148 /// Restore value from attribute data to the widget's control.
149 /// Emits signals before and after store
150 /// \return True in success
153 /// Saves the internal parameters to the given feature. Emits signals before and after store
154 /// \return True in success
157 /// Set focus to the first control of the current widget.
158 /// The focus policy of the control is checked.
159 /// If the widget has the NonFocus focus policy, it is skipped.
160 /// \return the state whether the widget can accept the focus
161 virtual bool focusTo();
163 /// Select the internal content if it can be selected. It is empty in the default realization
164 virtual void selectContent() {}
166 /// The method called when widget is activated
169 /// The method called when widget is deactivated
170 virtual void deactivate();
172 /// Returns list of widget controls
173 /// \return a control list
174 virtual QList<QWidget*> getControls() const = 0;
176 /// Returns the first or the last control that can accept the focus
177 /// \param isFirst if true, the first controls is returned or the last one
178 /// \return a control from a list of controls
179 QWidget* getControlAcceptingFocus(const bool isFirst);
181 /// FocusIn events processing
182 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
184 /// \brief Enables processing of focus event on all controls by the widget
185 /// if this widget is not obligatory and set no-focus policy otherwise
186 virtual void enableFocusProcessing();
188 //! Switch On/Off highlighting of the widget
189 virtual void setHighlighted(bool isHighlighted);
191 /// Returns the attribute name
192 /// \returns the string value
193 std::string attributeID() const
195 return myAttributeID;
198 /// \return Current feature
199 FeaturePtr feature() const
204 /// \return Context for translation
205 virtual std::string context() const {
207 std::string aContext = myFeatureId;
208 if(!aContext.empty() && !myAttributeID.empty()) {
211 aContext += myAttributeID;
216 /// Set feature which is processing by active operation
217 /// \param theFeature a feature object
218 /// \param theToStoreValue a value about necessity to store the widget value to the feature
219 /// \param isUpdateFlushed a flag if update should be flushed on store value
220 void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
221 const bool isUpdateFlushed = true);
223 /// Editing mode depends on mode of current operation. This value is defined by it.
224 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
226 /// \return Current Editing mode
227 bool isEditingMode() const { return myIsEditing; }
229 /// Returns true if the event is processed. The default implementation is empty, returns false.
230 virtual bool processEnter();
232 /// Returns true if the event is processed. The default implementation is empty, returns false.
233 virtual bool processDelete();
235 /// Sends Update and Redisplay for the given object
236 /// \param theObj is updating object
237 void updateObject(ObjectPtr theObj);
239 /// Sends Move event for the given object
240 /// \param theObj is object for moving
241 static void moveObject(ObjectPtr theObj);
243 /// Translate passed string with widget context()
244 virtual QString translate(const std::string& theStr) const;
246 /// Emit focus in widget to set this control as active in propety panel
247 void emitFocusInWidget() { emit focusInWidget(this); }
249 /// Finds model widget parent of the given sub widget
250 /// \param theWidget a candidate to be a child of the model widget
251 /// \param theProp a property panel instance
252 /// \return a model widget or NULL
253 static ModuleBase_ModelWidget* findModelWidget(ModuleBase_IPropertyPanel* theProp,
256 /// The signal about widget values are to be changed
257 void beforeValuesChanged();
258 /// The signal about widget values changed
259 void valuesChanged();
260 /// The signal about widget values modified
261 void valuesModified();
262 /// The signal about widget values are to be changed
263 void afterValuesChanged();
265 /// The signal about widget values are to be restored
266 void beforeValuesRestored();
267 /// The signal about widget values are to be restored
268 void afterValuesRestored();
270 /// The signal about key release on the control, that corresponds to the attribute
271 /// \param theObject a sender of the event
272 /// \param theEvent key release event
273 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
275 /// The signal is emitted if the enter is clicked in the control of the widget
276 /// \param theObject a sender of the event
277 void enterClicked(QObject* theObject);
279 /// The signal about the widget is get focus
280 /// \param theWidget the model base widget
281 void focusInWidget(ModuleBase_ModelWidget* theWidget);
283 /// The signal about the widget is lost focus
284 /// \param theWidget the model base widget
285 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
287 /// The signal about value state modification
288 void valueStateChanged(int theState);
290 /// The signal is emitted after flush of updates singal for the widget
291 void objectUpdated();
294 /// Sets default value of widget. Normally, widget should fetch this value
295 /// from the xml. However, some widgets derived widgets could define it
296 void setDefaultValue(const std::string& theValue);
297 /// \brief Set the attribute name
298 /// \param theAttribute the string value with attribute name
299 void setAttributeID(const std::string& theAttribute)
301 myAttributeID = theAttribute;
304 /// Sets the current value state. If the value is changed, the signal is emitted
305 /// If the current value state is Blocked, this method do nothing
306 /// \param theState a new state
307 /// \return the previous value state
308 ValueState setValueState(const ValueState& theState);
310 /// Blocks the value state change.
311 /// \param theBlocked a block state
312 /// \return the previous value
313 bool blockValueState(const bool theBlocked);
315 /// Compute the feature default value and fill the controls with it
316 /// or store the control value to the feature
317 virtual void initializeValueByActivate();
319 /// Saves the internal parameters to the given feature
320 /// \return True in success
321 virtual bool storeValueCustom() = 0;
323 /// Restore value from attribute data to the widget's control
324 virtual bool restoreValueCustom() = 0;
326 /// Fills the widget with default values
327 /// \return true if the widget current value is reset
328 virtual bool resetCustom() { return false; };
330 /// The method called when widget is activated
331 virtual void activateCustom() {};
334 /// Processing of values changed in model widget by store the current value to the feature
335 void onWidgetValuesChanged();
337 /// Changes widget state.
338 void onWidgetValuesModified();
341 /// own validator, by default it is zero
342 ModuleBase_WidgetValidator* myWidgetValidator;
344 /// The attribute name of the model feature
345 std::string myAttributeID;
347 /// A feature which is processing by active operation
348 FeaturePtr myFeature;
351 std::string myFeatureId;
353 /// Flag which shows that current operation is in editing mode
356 /// Flag which shows whether current widget is obligatory
357 /// The non-obligatory widgets should not accept the focus in the property panel
360 /// Flag about value of the control is enabled (can be modified)
361 EnableState myIsValueEnabled;
363 /// The widget value state
367 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
368 bool myIsComputedDefault;
370 /// the default value, which is defined in the XML for this attribute
371 std::string myDefaultValue;
373 /// an XML internal state
376 // an XML state, the value is not stored into model if the widget is in edit mode
377 std::string myIsModifiedInEdit;
379 /// the reset state. If it is false, the reset method of the widget is not performed
381 /// blocked flag of modification of the value state
382 bool myIsValueStateBlocked;
383 /// do not flush updated signal
384 bool myFlushUpdateBlocked;