1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef MODULEBASE_MODELWIDGET_H
21 #define MODULEBASE_MODELWIDGET_H
23 #include <ModuleBase.h>
24 #include <ModuleBase_ActionType.h>
25 #include <ModuleBase_Definitions.h>
26 #include <ModuleBase_OperationFeature.h>
27 #include <ModuleBase_ActionInfo.h>
28 #include <ModuleBase_ActionParameter.h>
29 #include <ModelAPI_Feature.h>
31 #include <SelectMgr_ListOfFilter.hxx>
37 class Config_WidgetAPI;
38 class Events_InfoMessage;
39 class ModuleBase_IPropertyPanel;
40 class ModuleBase_IWorkshop;
41 class ModuleBase_ViewerPrs;
42 class ModuleBase_WidgetValidator;
45 /**\class ModuleBase_ModelWidget
47 * \brief An abstract custom widget class. This class realization is assumed
48 * to create some controls.
49 * The controls values modification should send signal about values change.
51 * Common interface for widgets in the property panel.
52 * Every widget are able to save/restore data from the model and/or to contain other widgets.
55 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
59 /// State of the widget
60 enum ValueState { Stored, /// modification is finished and applyed to the model
61 ModifiedInPP, /// modification has not been finished and set to the model yet
62 ModifiedInViewer, /// modification performed by viewer events
63 Reset }; /// the value is reset
65 enum EnableState { On, /// the widget value is always enabled
66 Off, /// the widget value is always disabled
67 /// the widget value enable state is defined in preferences
68 DefinedInPreferences };
71 /// \param theParent the parent object
72 /// \param theData the widget configuration. The attribute of the model widget is obtained from
73 /// a low-level API for reading xml definitions of widgets
74 ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
76 virtual ~ModuleBase_ModelWidget();
78 /// Fills the widget with default values. It calls the resetCustom method and change
79 /// the widget state to Reset if the reset is performed.
80 /// \return true if the widget current value is reset
83 /// Returns the state whether the attribute of the feature is initialized
84 /// \param theObject a model feature to be checked
85 /// \return the boolean result
86 bool isInitialized(ObjectPtr theObject) const;
88 /// Fills given container with selection modes if the widget has it
89 /// \param [out] theModuleSelectionModes module additional modes, -1 means all default modes
90 /// \param [out] theModes a container of modes
91 virtual void selectionModes(int& theModuleSelectionModes, QIntList& theModes);
93 /// Appends into container of workshop selection filters
94 /// \param [out] theModuleSelectionFilters module additional modes, -1 means all default modes
95 /// \param [out] theSelectionFilters selection filters
96 virtual void selectionFilters(QIntList& theModuleSelectionFilters,
97 SelectMgr_ListOfFilter& theSelectionFilters);
99 /// Returns true, if default value of the widget should be computed
100 /// on operation's execute, like radius for circle's constraint (can not be zero)
101 bool isComputedDefault() const { return myIsComputedDefault; }
103 /// Returns true, if default value of the widget is defined in the XML and it is not the
105 /// \return the boolean result
106 std::string getDefaultValue() const { return myDefaultValue; }
108 /// Returns true, if widget is internal
109 /// \return the boolean result
110 bool isInternal() const { return myIsInternal; }
112 /// Returns true, if the obligatory value of the widget is
113 /// not defined in the XML or has true value
114 /// \return the boolean result
115 bool isObligatory() const { return myIsObligatory; }
117 /// Returns true, if the widget value is enabled and might be modified manualy.
118 /// It returns false if the application preferences allow having disabled value
119 /// and the internal state tells to disable
120 /// \return the boolean result
121 virtual bool isValueEnabled() const;
123 /// Returns this parameter value in the xml file
124 /// \return the boolean result
125 bool isUseReset() const { return myUseReset; }
127 /// Returns this parameter value in the xml file
128 /// \return the boolean result
129 std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
131 /// Returns this widget value state
132 /// \return the enumeration result
133 ValueState getValueState() const { return myState; }
135 /// Stores the widget value if it is modified
136 void processValueState();
138 /// Returns an attribute error according to the value state
139 /// It exists in all cases excepring the "Store" case
140 Events_InfoMessage getValueStateError() const;
142 /// Defines if it is supposed that the widget should interact with the viewer.
143 virtual bool isViewerSelector() { return false; }
145 /// Defines if it is supported to set the value in this widget
146 /// By default it returns true
147 virtual bool canAcceptFocus() const { return true; };
149 //! Returns the widget error, get it from the attribute validator and state of the widget
150 //! If the feature is correct, it returns an empty value
151 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
152 //! \return string value
153 virtual QString getError(const bool theValueStateChecked = true) const;
155 /// Set the given wrapped value to the current widget
156 /// This value should be processed in the widget according to the needs
157 /// \param theValues the wrapped selection values
158 /// \param theToValidate the boolean value whether the value should be checked by filters
159 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
160 const bool theToValidate)
165 /// Returns values which should be highlighted when the whidget is active
166 /// \param theValues a list of presentations
167 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
169 /// Checks if the selection presentation is valid in widget
170 /// \param theValue a selected presentation in the view
171 /// \return a boolean value
172 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
175 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
176 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
178 /// Restore value from attribute data to the widget's control.
179 /// Emits signals before and after store
180 /// \return True in success
183 /// Saves the internal parameters to the given feature. Emits signals before and after store
184 /// \return True in success
187 /// Set focus to the first control of the current widget.
188 /// The focus policy of the control is checked.
189 /// If the widget has the NonFocus focus policy, it is skipped.
190 /// \return the state whether the widget can accept the focus
191 virtual bool focusTo();
193 /// Select the internal content if it can be selected. It is empty in the default realization
194 virtual void selectContent() {}
196 /// The method called when widget is activated
199 /// The method called when widget is deactivated
200 virtual void deactivate();
202 /// Opportunity to do something after the active widget of the property panel changed
203 virtual void updateAfterDeactivation() {}
204 /// Opportunity to do something after the active widget of the property panel changed
205 virtual void updateAfterActivation() {}
207 /// The method called if widget should be activated always
208 virtual bool needToBeActiated() { return false; }
210 /// Returns list of widget controls
211 /// \return a control list
212 virtual QList<QWidget*> getControls() const = 0;
214 /// Returns the first or the last control that can accept the focus
215 /// \param isFirst if true, the first controls is returned or the last one
216 /// \return a control from a list of controls
217 QWidget* getControlAcceptingFocus(const bool isFirst);
219 /// FocusIn events processing
220 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
222 /// \brief Enables processing of focus event on all controls by the widget
223 /// if this widget is not obligatory and set no-focus policy otherwise
224 virtual void enableFocusProcessing();
226 //! Switch On/Off highlighting of the widget
227 virtual void setHighlighted(bool isHighlighted);
229 /// Returns the attribute name
230 /// \returns the string value
231 std::string attributeID() const
233 return myAttributeID;
236 /// \return Current feature
237 FeaturePtr feature() const
242 /// \return Context for translation
243 virtual std::string context() const {
245 std::string aContext = myFeatureId;
246 if(!aContext.empty() && !myAttributeID.empty()) {
249 aContext += myAttributeID;
254 /// Set feature which is processing by active operation
255 /// \param theFeature a feature object
256 /// \param theToStoreValue a value about necessity to store the widget value to the feature
257 /// \param isUpdateFlushed a flag if update should be flushed on store value
258 void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
259 const bool isUpdateFlushed = true);
261 /// Editing mode depends on mode of current operation. This value is defined by it.
262 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
264 /// \return Current Editing mode
265 bool isEditingMode() const { return myIsEditing; }
267 /// Returns true if the action can be processed. By default it is empty and returns false.
268 /// \param theActionType an action type
269 /// \param isActionEnabled if true, the enable state of the action
270 virtual bool canProcessAction(ModuleBase_ActionType theActionType, bool& isActionEnabled);
272 /// Returns true if the event is processed. The default implementation is empty, returns false.
273 virtual bool processAction(ModuleBase_ActionType theActionType,
274 const ActionParamPtr& theParam = ActionParamPtr());
276 /// Returns list of accessible actions for Undo/Redo commands. By default it returns empty list.
277 /// \param theActionType type of action. It can be ActionUndo or ActionRedo.
278 virtual QList<ActionInfo> actionsList(ModuleBase_ActionType theActionType) const
279 { return QList<ActionInfo>(); }
281 /// Sends Update and Redisplay for the given object
282 /// \param theObj is updating object
283 void updateObject(ObjectPtr theObj);
285 /// Translate passed string with widget context()
286 virtual QString translate(const std::string& theStr) const;
288 /// Emit focus in widget to set this control as active in propety panel
289 void emitFocusInWidget() { emit focusInWidget(this); }
291 /// Finds model widget parent of the given sub widget
292 /// \param theWidget a candidate to be a child of the model widget
293 /// \param theProp a property panel instance
294 /// \return a model widget or NULL
295 static ModuleBase_ModelWidget* findModelWidget(ModuleBase_IPropertyPanel* theProp,
298 /// Returns True if the widget uses feature attribute.
299 /// If not then it means that the widget do not need attribute at all.
300 virtual bool usesAttribute() const { return true; }
302 /// It is called when user press Ok or OkPlus buttons in the parent property panel
303 /// By default this slot does nothing
304 virtual void onFeatureAccepted() {}
307 /// The signal about widget values are to be changed
308 void beforeValuesChanged();
309 /// The signal about widget values changed
310 void valuesChanged();
311 /// The signal about widget values modified
312 void valuesModified();
313 /// The signal about widget values are to be changed
314 void afterValuesChanged();
316 /// The signal about widget values are to be restored
317 void beforeValuesRestored();
318 /// The signal about widget values are to be restored
319 void afterValuesRestored();
321 /// The signal about key release on the control, that corresponds to the attribute
322 /// \param theObject a sender of the event
323 /// \param theEvent key release event
324 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
326 /// The signal is emitted if the enter is clicked in the control of the widget
327 /// \param theObject a sender of the event
328 void enterClicked(QObject* theObject);
330 /// The signal about the widget is get focus
331 /// \param theWidget the model base widget
332 void focusInWidget(ModuleBase_ModelWidget* theWidget);
334 /// The signal about the widget is lost focus
335 /// \param theWidget the model base widget
336 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
338 /// The signal about value state modification
339 void valueStateChanged(int theState);
341 /// The signal is emitted after flush of updates singal for the widget
342 void objectUpdated();
345 /// Sets default value of widget. Normally, widget should fetch this value
346 /// from the xml. However, some widgets derived widgets could define it
347 void setDefaultValue(const std::string& theValue);
348 /// \brief Set the attribute name
349 /// \param theAttribute the string value with attribute name
350 void setAttributeID(const std::string& theAttribute)
352 myAttributeID = theAttribute;
355 /// Sets the current value state. If the value is changed, the signal is emitted
356 /// If the current value state is Blocked, this method do nothing
357 /// \param theState a new state
358 /// \return the previous value state
359 ValueState setValueState(const ValueState& theState);
361 /// Blocks the value state change.
362 /// \param theBlocked a block state
363 /// \return the previous value
364 bool blockValueState(const bool theBlocked);
366 /// Compute the feature default value and fill the controls with it
367 /// or store the control value to the feature
368 virtual void initializeValueByActivate();
370 /// Saves the internal parameters to the given feature
371 /// \return True in success
372 virtual bool storeValueCustom() = 0;
374 /// Restore value from attribute data to the widget's control
375 virtual bool restoreValueCustom() = 0;
377 /// Fills the widget with default values
378 /// \return true if the widget current value is reset
379 virtual bool resetCustom() { return false; };
381 /// The method called when widget is activated
382 virtual void activateCustom() {};
384 //// Returns true if the event is processed. The default implementation is empty, returns false.
385 virtual bool processEnter();
387 //// Returns true if the event is processed. The default implementation is empty, returns false.
388 virtual bool processEscape();
390 //// Returns true if the event is processed. The default implementation is empty, returns false.
391 virtual bool processDelete();
393 /// Returns true if envent is processed. It applyes workshop selection for the widget attribute.
394 virtual bool processSelection();
397 /// Processing of values changed in model widget by store the current value to the feature
398 void onWidgetValuesChanged();
400 /// Changes widget state.
401 void onWidgetValuesModified();
404 /// own validator, by default it is zero
405 ModuleBase_WidgetValidator* myWidgetValidator;
407 /// The attribute name of the model feature
408 std::string myAttributeID;
410 /// A feature which is processing by active operation
411 FeaturePtr myFeature;
414 std::string myFeatureId;
416 /// Flag which shows that current operation is in editing mode
419 /// Flag which shows whether current widget is obligatory
420 /// The non-obligatory widgets should not accept the focus in the property panel
423 /// Flag about value of the control is enabled (can be modified)
424 EnableState myIsValueEnabled;
426 /// The widget value state
430 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
431 bool myIsComputedDefault;
433 /// the default value, which is defined in the XML for this attribute
434 std::string myDefaultValue;
436 /// an XML internal state
439 // an XML state, the value is not stored into model if the widget is in edit mode
440 std::string myIsModifiedInEdit;
442 /// the reset state. If it is false, the reset method of the widget is not performed
444 /// blocked flag of modification of the value state
445 bool myIsValueStateBlocked;
446 /// do not flush updated signal
447 bool myFlushUpdateBlocked;