1 // Copyright (C) 2014-2020 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>
38 #pragma warning(disable: 4100)
41 class Config_WidgetAPI;
42 class Events_InfoMessage;
43 class ModuleBase_IPropertyPanel;
44 class ModuleBase_IWorkshop;
45 class ModuleBase_ViewerPrs;
46 class ModuleBase_WidgetValidator;
49 /**\class ModuleBase_ModelWidget
51 * \brief An abstract custom widget class. This class realization is assumed
52 * to create some controls.
53 * The controls values modification should send signal about values change.
55 * Common interface for widgets in the property panel.
56 * Every widget are able to save/restore data from the model and/or to contain other widgets.
59 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
63 /// State of the widget
64 enum ValueState { Stored, /// modification is finished and applyed to the model
65 ModifiedInPP, /// modification has not been finished and set to the model yet
66 ModifiedInViewer, /// modification performed by viewer events
67 Reset }; /// the value is reset
69 enum EnableState { On, /// the widget value is always enabled
70 Off, /// the widget value is always disabled
71 /// the widget value enable state is defined in preferences
72 DefinedInPreferences };
75 /// \param theParent the parent object
76 /// \param theData the widget configuration. The attribute of the model widget is obtained from
77 /// a low-level API for reading xml definitions of widgets
78 ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
80 virtual ~ModuleBase_ModelWidget();
82 /// Fills the widget with default values. It calls the resetCustom method and change
83 /// the widget state to Reset if the reset is performed.
84 /// \return true if the widget current value is reset
87 /// Returns the state whether the attribute of the feature is initialized
88 /// \param theObject a model feature to be checked
89 /// \return the boolean result
90 bool isInitialized(ObjectPtr theObject) const;
92 /// Fills given container with selection modes if the widget has it
93 /// \param [out] theModuleSelectionModes module additional modes, -1 means all default modes
94 /// \param [out] theModes a container of modes
95 virtual void selectionModes(int& theModuleSelectionModes, QIntList& theModes);
97 /// Appends into container of workshop selection filters
98 /// \param [out] theModuleSelectionFilters module additional modes, -1 means all default modes
99 /// \param [out] theSelectionFilters selection filters
100 virtual void selectionFilters(QIntList& theModuleSelectionFilters,
101 SelectMgr_ListOfFilter& theSelectionFilters);
103 /// Returns true, if default value of the widget should be computed
104 /// on operation's execute, like radius for circle's constraint (can not be zero)
105 bool isComputedDefault() const { return myIsComputedDefault; }
107 /// Returns true, if default value of the widget is defined in the XML and it is not the
109 /// \return the boolean result
110 std::string getDefaultValue() const { return myDefaultValue; }
112 /// Returns true, if widget is internal
113 /// \return the boolean result
114 bool isInternal() const { return myIsInternal; }
116 /// Returns true, if the obligatory value of the widget is
117 /// not defined in the XML or has true value
118 /// \return the boolean result
119 bool isObligatory() const { return myIsObligatory; }
121 /// Returns true, if the widget value is enabled and might be modified manualy.
122 /// It returns false if the application preferences allow having disabled value
123 /// and the internal state tells to disable
124 /// \return the boolean result
125 virtual bool isValueEnabled() const;
127 /// Returns this parameter value in the xml file
128 /// \return the boolean result
129 bool isUseReset() const { return myUseReset; }
131 /// Returns this parameter value in the xml file
132 /// \return the boolean result
133 std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
135 /// Returns this widget value state
136 /// \return the enumeration result
137 ValueState getValueState() const { return myState; }
139 /// Stores the widget value if it is modified
140 void processValueState();
142 /// Returns an attribute error according to the value state
143 /// It exists in all cases excepring the "Store" case
144 Events_InfoMessage getValueStateError() const;
146 /// Defines if it is supposed that the widget should interact with the viewer.
147 virtual bool isViewerSelector() { return false; }
149 /// Defines if it is supported to set the value in this widget
150 /// By default it returns true
151 virtual bool canAcceptFocus() const { return true; };
153 //! Returns the widget error, get it from the attribute validator and state of the widget
154 //! If the feature is correct, it returns an empty value
155 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
156 //! \return string value
157 virtual QString getError(const bool theValueStateChecked = true) const;
159 /// Set the given wrapped value to the current widget
160 /// This value should be processed in the widget according to the needs
161 /// \param theValues the wrapped selection values
162 /// \param theToValidate the boolean value whether the value should be checked by filters
163 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
164 const bool theToValidate)
169 /// Returns values which should be highlighted when the whidget is active
170 /// \param theValues a list of presentations
171 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
173 /// Checks if the selection presentation is valid in widget
174 /// \param theValue a selected presentation in the view
175 /// \return a boolean value
176 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
179 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
180 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
182 /// Restore value from attribute data to the widget's control.
183 /// Emits signals before and after store
184 /// \return True in success
187 /// Saves the internal parameters to the given feature. Emits signals before and after store
188 /// \return True in success
191 /// Set focus to the first control of the current widget.
192 /// The focus policy of the control is checked.
193 /// If the widget has the NonFocus focus policy, it is skipped.
194 /// \return the state whether the widget can accept the focus
195 virtual bool focusTo();
197 /// Select the internal content if it can be selected. It is empty in the default realization
198 virtual void selectContent() {}
200 /// The method called when widget is activated
203 /// The method called when widget is deactivated
204 virtual void deactivate();
206 /// Opportunity to do something after the active widget of the property panel changed
207 virtual void updateAfterDeactivation() {}
208 /// Opportunity to do something after the active widget of the property panel changed
209 virtual void updateAfterActivation() {}
211 /// The method called if widget should be activated always
212 virtual bool needToBeActivated() { return false; }
214 /// Returns list of widget controls
215 /// \return a control list
216 virtual QList<QWidget*> getControls() const = 0;
218 /// Returns the first or the last control that can accept the focus
219 /// \param isFirst if true, the first controls is returned or the last one
220 /// \return a control from a list of controls
221 QWidget* getControlAcceptingFocus(const bool isFirst);
223 /// FocusIn events processing
224 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
226 /// \brief Enables processing of focus event on all controls by the widget
227 /// if this widget is not obligatory and set no-focus policy otherwise
228 virtual void enableFocusProcessing();
230 //! Switch On/Off highlighting of the widget
231 virtual void setHighlighted(bool isHighlighted);
233 /// Returns the attribute name
234 /// \returns the string value
235 std::string attributeID() const
237 return myAttributeID;
240 /// \return Current feature
241 FeaturePtr feature() const
246 /// \return Context for translation
247 virtual std::string context() const {
249 std::string aContext = myFeatureId;
250 if(!aContext.empty() && !myAttributeID.empty()) {
253 aContext += myAttributeID;
258 /// Set feature which is processing by active operation
259 /// \param theFeature a feature object
260 /// \param theToStoreValue a value about necessity to store the widget value to the feature
261 /// \param isUpdateFlushed a flag if update should be flushed on store value
262 virtual void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
263 const bool isUpdateFlushed = true);
265 /// Editing mode depends on mode of current operation. This value is defined by it.
266 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
268 /// \return Current Editing mode
269 bool isEditingMode() const { return myIsEditing; }
271 /// Returns true if the action can be processed. By default it is empty and returns false.
272 /// \param theActionType an action type
273 /// \param isActionEnabled if true, the enable state of the action
274 virtual bool canProcessAction(ModuleBase_ActionType theActionType, bool& isActionEnabled);
276 /// Returns true if the event is processed. The default implementation is empty, returns false.
277 virtual bool processAction(ModuleBase_ActionType theActionType,
278 const ActionParamPtr& theParam = ActionParamPtr());
280 /// Returns list of accessible actions for Undo/Redo commands. By default it returns empty list.
281 /// \param theActionType type of action. It can be ActionUndo or ActionRedo.
282 virtual QList<ActionInfo> actionsList(ModuleBase_ActionType theActionType) const
283 { return QList<ActionInfo>(); }
285 /// Sends Update and Redisplay for the given object
286 /// \param theObj is updating object
287 void updateObject(ObjectPtr theObj);
289 /// Translate passed string with widget context()
290 virtual QString translate(const std::string& theStr) const;
292 /// Emit focus in widget to set this control as active in propety panel
293 void emitFocusInWidget() { emit focusInWidget(this); }
295 /// Finds model widget parent of the given sub widget
296 /// \param theWidget a candidate to be a child of the model widget
297 /// \param theProp a property panel instance
298 /// \return a model widget or NULL
299 static ModuleBase_ModelWidget* findModelWidget(ModuleBase_IPropertyPanel* theProp,
302 /// Returns True if the widget uses feature attribute.
303 /// If not then it means that the widget do not need attribute at all.
304 virtual bool usesAttribute() const { return true; }
306 /// It is called when user press Ok or OkPlus buttons in the parent property panel
307 /// By default this slot does nothing
308 virtual void onFeatureAccepted() {}
310 /// Returns True in case if the widget contains useful information for inspection tool
311 virtual bool isInformative() const { return true; }
313 /// If widgets has several panels then this method has to show a page which contains information
314 /// for current feature. By default does nothing
315 virtual void showInformativePage() {}
317 /// Returns True if data of its attribute was modified
318 virtual bool isModified() const { return false; }
320 virtual void setReadOnly(bool isReadOnly) { setEnabled(!isReadOnly); }
322 virtual bool isReadOnly() const { return !isEnabled(); }
324 /// Returns true if the widget should have access to external parts
325 bool canUseExternalParts() const { return myUseExternalParts; }
328 /// The signal about widget values are to be changed
329 void beforeValuesChanged();
330 /// The signal about widget values changed
331 void valuesChanged();
332 /// The signal about widget values modified
333 void valuesModified();
334 /// The signal about widget values are to be changed
335 void afterValuesChanged();
337 /// The signal about widget values are to be restored
338 void beforeValuesRestored();
339 /// The signal about widget values are to be restored
340 void afterValuesRestored();
342 /// The signal about key release on the control, that corresponds to the attribute
343 /// \param theObject a sender of the event
344 /// \param theEvent key release event
345 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
347 /// The signal is emitted if the enter is clicked in the control of the widget
348 /// \param theObject a sender of the event
349 void enterClicked(QObject* theObject);
351 /// The signal about the widget is get focus
352 /// \param theWidget the model base widget
353 void focusInWidget(ModuleBase_ModelWidget* theWidget);
355 /// The signal about the widget is lost focus
356 /// \param theWidget the model base widget
357 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
359 /// The signal about value state modification
360 void valueStateChanged(int theState);
362 /// The signal is emitted after flush of updates singal for the widget
363 void objectUpdated();
366 /// Sets default value of widget. Normally, widget should fetch this value
367 /// from the xml. However, some widgets derived widgets could define it
368 void setDefaultValue(const std::string& theValue);
369 /// \brief Set the attribute name
370 /// \param theAttribute the string value with attribute name
371 void setAttributeID(const std::string& theAttribute)
373 myAttributeID = theAttribute;
376 /// Sets the current value state. If the value is changed, the signal is emitted
377 /// If the current value state is Blocked, this method do nothing
378 /// \param theState a new state
379 /// \return the previous value state
380 ValueState setValueState(const ValueState& theState);
382 /// Blocks the value state change.
383 /// \param theBlocked a block state
384 /// \return the previous value
385 bool blockValueState(const bool theBlocked);
387 /// Compute the feature default value and fill the controls with it
388 /// or store the control value to the feature
389 virtual void initializeValueByActivate();
391 /// Saves the internal parameters to the given feature
392 /// \return True in success
393 virtual bool storeValueCustom() = 0;
395 /// Restore value from attribute data to the widget's control
396 virtual bool restoreValueCustom() = 0;
398 /// Fills the widget with default values
399 /// \return true if the widget current value is reset
400 virtual bool resetCustom() { return false; };
402 /// The method called when widget is activated
403 virtual void activateCustom() {};
405 //// Returns true if the event is processed. The default implementation is empty, returns false.
406 virtual bool processEnter();
408 //// Returns true if the event is processed. The default implementation is empty, returns false.
409 virtual bool processEscape();
411 //// Returns true if the event is processed. The default implementation is empty, returns false.
412 virtual bool processDelete();
414 /// Returns true if envent is processed. It applyes workshop selection for the widget attribute.
415 virtual bool processSelection();
418 /// Processing of values changed in model widget by store the current value to the feature
419 void onWidgetValuesChanged();
421 /// Changes widget state.
422 void onWidgetValuesModified();
425 /// own validator, by default it is zero
426 ModuleBase_WidgetValidator* myWidgetValidator;
428 /// The attribute name of the model feature
429 std::string myAttributeID;
431 /// A feature which is processing by active operation
432 FeaturePtr myFeature;
435 std::string myFeatureId;
437 /// Flag which shows that current operation is in editing mode
440 /// Flag which shows whether current widget is obligatory
441 /// The non-obligatory widgets should not accept the focus in the property panel
444 /// Flag about value of the control is enabled (can be modified)
445 EnableState myIsValueEnabled;
447 /// The widget value state
451 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
452 bool myIsComputedDefault;
454 /// the default value, which is defined in the XML for this attribute
455 std::string myDefaultValue;
457 /// an XML internal state
460 // an XML state, the value is not stored into model if the widget is in edit mode
461 std::string myIsModifiedInEdit;
463 /// the reset state. If it is false, the reset method of the widget is not performed
465 /// blocked flag of modification of the value state
466 bool myIsValueStateBlocked;
467 /// do not flush updated signal
468 bool myFlushUpdateBlocked;
470 bool myUpdateVisualAttributes;
472 /// A flag which indicates that current widget should have access to external parts
473 bool myUseExternalParts;