1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #ifndef MODULEBASE_MODELWIDGET_H
22 #define MODULEBASE_MODELWIDGET_H
24 #include <ModuleBase.h>
25 #include <ModuleBase_ActionType.h>
26 #include <ModuleBase_Definitions.h>
27 #include <ModuleBase_OperationFeature.h>
28 #include <ModuleBase_ActionInfo.h>
29 #include <ModuleBase_ActionParameter.h>
30 #include <ModelAPI_Feature.h>
32 #include <SelectMgr_ListOfFilter.hxx>
38 class Config_WidgetAPI;
39 class Events_InfoMessage;
40 class ModuleBase_IPropertyPanel;
41 class ModuleBase_IWorkshop;
42 class ModuleBase_ViewerPrs;
43 class ModuleBase_WidgetValidator;
46 /**\class ModuleBase_ModelWidget
48 * \brief An abstract custom widget class. This class realization is assumed
49 * to create some controls.
50 * The controls values modification should send signal about values change.
52 * Common interface for widgets in the property panel.
53 * Every widget are able to save/restore data from the model and/or to contain other widgets.
56 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
60 /// State of the widget
61 enum ValueState { Stored, /// modification is finished and applyed to the model
62 ModifiedInPP, /// modification has not been finished and set to the model yet
63 ModifiedInViewer, /// modification performed by viewer events
64 Reset }; /// the value is reset
66 enum EnableState { On, /// the widget value is always enabled
67 Off, /// the widget value is always disabled
68 /// the widget value enable state is defined in preferences
69 DefinedInPreferences };
72 /// \param theParent the parent object
73 /// \param theData the widget configuration. The attribute of the model widget is obtained from
74 /// a low-level API for reading xml definitions of widgets
75 ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
77 virtual ~ModuleBase_ModelWidget();
79 /// Fills the widget with default values. It calls the resetCustom method and change
80 /// the widget state to Reset if the reset is performed.
81 /// \return true if the widget current value is reset
84 /// Returns the state whether the attribute of the feature is initialized
85 /// \param theObject a model feature to be checked
86 /// \return the boolean result
87 bool isInitialized(ObjectPtr theObject) const;
89 /// Fills given container with selection modes if the widget has it
90 /// \param [out] theModuleSelectionModes module additional modes, -1 means all default modes
91 /// \param [out] theModes a container of modes
92 virtual void selectionModes(int& theModuleSelectionModes, QIntList& theModes);
94 /// Appends into container of workshop selection filters
95 /// \param [out] theModuleSelectionFilters module additional modes, -1 means all default modes
96 /// \param [out] theSelectionFilters selection filters
97 virtual void selectionFilters(QIntList& theModuleSelectionFilters,
98 SelectMgr_ListOfFilter& theSelectionFilters);
100 /// Returns true, if default value of the widget should be computed
101 /// on operation's execute, like radius for circle's constraint (can not be zero)
102 bool isComputedDefault() const { return myIsComputedDefault; }
104 /// Returns true, if default value of the widget is defined in the XML and it is not the
106 /// \return the boolean result
107 std::string getDefaultValue() const { return myDefaultValue; }
109 /// Returns true, if widget is internal
110 /// \return the boolean result
111 bool isInternal() const { return myIsInternal; }
113 /// Returns true, if the obligatory value of the widget is
114 /// not defined in the XML or has true value
115 /// \return the boolean result
116 bool isObligatory() const { return myIsObligatory; }
118 /// Returns true, if the widget value is enabled and might be modified manualy.
119 /// It returns false if the application preferences allow having disabled value
120 /// and the internal state tells to disable
121 /// \return the boolean result
122 virtual bool isValueEnabled() const;
124 /// Returns this parameter value in the xml file
125 /// \return the boolean result
126 bool isUseReset() const { return myUseReset; }
128 /// Returns this parameter value in the xml file
129 /// \return the boolean result
130 std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
132 /// Returns this widget value state
133 /// \return the enumeration result
134 ValueState getValueState() const { return myState; }
136 /// Stores the widget value if it is modified
137 void processValueState();
139 /// Returns an attribute error according to the value state
140 /// It exists in all cases excepring the "Store" case
141 Events_InfoMessage getValueStateError() const;
143 /// Defines if it is supposed that the widget should interact with the viewer.
144 virtual bool isViewerSelector() { return false; }
146 /// Defines if it is supported to set the value in this widget
147 /// By default it returns true
148 virtual bool canAcceptFocus() const { return true; };
150 //! Returns the widget error, get it from the attribute validator and state of the widget
151 //! If the feature is correct, it returns an empty value
152 //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
153 //! \return string value
154 QString getError(const bool theValueStateChecked = true) const;
156 /// Set the given wrapped value to the current widget
157 /// This value should be processed in the widget according to the needs
158 /// \param theValues the wrapped selection values
159 /// \param theToValidate the boolean value whether the value should be checked by filters
160 virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
161 const bool theToValidate)
166 /// Returns values which should be highlighted when the whidget is active
167 /// \param theValues a list of presentations
168 virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
170 /// Checks if the selection presentation is valid in widget
171 /// \param theValue a selected presentation in the view
172 /// \return a boolean value
173 virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
176 /// Returns widget validator, by default it is NULL. To be created in a child if necessary
177 ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
179 /// Restore value from attribute data to the widget's control.
180 /// Emits signals before and after store
181 /// \return True in success
184 /// Saves the internal parameters to the given feature. Emits signals before and after store
185 /// \return True in success
188 /// Set focus to the first control of the current widget.
189 /// The focus policy of the control is checked.
190 /// If the widget has the NonFocus focus policy, it is skipped.
191 /// \return the state whether the widget can accept the focus
192 virtual bool focusTo();
194 /// Select the internal content if it can be selected. It is empty in the default realization
195 virtual void selectContent() {}
197 /// The method called when widget is activated
200 /// The method called when widget is deactivated
201 virtual void deactivate();
203 /// Opportunity to do something after the active widget of the property panel changed
204 virtual void updateAfterDeactivation() {}
205 /// Opportunity to do something after the active widget of the property panel changed
206 virtual void updateAfterActivation() {}
208 /// The method called if widget should be activated always
209 virtual bool needToBeActiated() { return false; }
211 /// Returns list of widget controls
212 /// \return a control list
213 virtual QList<QWidget*> getControls() const = 0;
215 /// Returns the first or the last control that can accept the focus
216 /// \param isFirst if true, the first controls is returned or the last one
217 /// \return a control from a list of controls
218 QWidget* getControlAcceptingFocus(const bool isFirst);
220 /// FocusIn events processing
221 virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
223 /// \brief Enables processing of focus event on all controls by the widget
224 /// if this widget is not obligatory and set no-focus policy otherwise
225 virtual void enableFocusProcessing();
227 //! Switch On/Off highlighting of the widget
228 virtual void setHighlighted(bool isHighlighted);
230 /// Returns the attribute name
231 /// \returns the string value
232 std::string attributeID() const
234 return myAttributeID;
237 /// \return Current feature
238 FeaturePtr feature() const
243 /// \return Context for translation
244 virtual std::string context() const {
246 std::string aContext = myFeatureId;
247 if(!aContext.empty() && !myAttributeID.empty()) {
250 aContext += myAttributeID;
255 /// Set feature which is processing by active operation
256 /// \param theFeature a feature object
257 /// \param theToStoreValue a value about necessity to store the widget value to the feature
258 /// \param isUpdateFlushed a flag if update should be flushed on store value
259 void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
260 const bool isUpdateFlushed = true);
262 /// Editing mode depends on mode of current operation. This value is defined by it.
263 virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
265 /// \return Current Editing mode
266 bool isEditingMode() const { return myIsEditing; }
268 /// Returns true if the action can be processed. By default it is empty and returns false.
269 /// \param theActionType an action type
270 /// \param isActionEnabled if true, the enable state of the action
271 virtual bool canProcessAction(ModuleBase_ActionType theActionType, bool& isActionEnabled);
273 /// Returns true if the event is processed. The default implementation is empty, returns false.
274 virtual bool processAction(ModuleBase_ActionType theActionType,
275 const ActionParamPtr& theParam = ActionParamPtr());
277 /// Returns list of accessible actions for Undo/Redo commands. By default it returns empty list.
278 /// \param theActionType type of action. It can be ActionUndo or ActionRedo.
279 virtual QList<ActionInfo> actionsList(ModuleBase_ActionType theActionType) const
280 { return QList<ActionInfo>(); }
282 /// Sends Update and Redisplay for the given object
283 /// \param theObj is updating object
284 void updateObject(ObjectPtr theObj);
286 /// Translate passed string with widget context()
287 virtual QString translate(const std::string& theStr) const;
289 /// Emit focus in widget to set this control as active in propety panel
290 void emitFocusInWidget() { emit focusInWidget(this); }
292 /// Finds model widget parent of the given sub widget
293 /// \param theWidget a candidate to be a child of the model widget
294 /// \param theProp a property panel instance
295 /// \return a model widget or NULL
296 static ModuleBase_ModelWidget* findModelWidget(ModuleBase_IPropertyPanel* theProp,
299 /// The signal about widget values are to be changed
300 void beforeValuesChanged();
301 /// The signal about widget values changed
302 void valuesChanged();
303 /// The signal about widget values modified
304 void valuesModified();
305 /// The signal about widget values are to be changed
306 void afterValuesChanged();
308 /// The signal about widget values are to be restored
309 void beforeValuesRestored();
310 /// The signal about widget values are to be restored
311 void afterValuesRestored();
313 /// The signal about key release on the control, that corresponds to the attribute
314 /// \param theObject a sender of the event
315 /// \param theEvent key release event
316 void keyReleased(QObject* theObject, QKeyEvent* theEvent);
318 /// The signal is emitted if the enter is clicked in the control of the widget
319 /// \param theObject a sender of the event
320 void enterClicked(QObject* theObject);
322 /// The signal about the widget is get focus
323 /// \param theWidget the model base widget
324 void focusInWidget(ModuleBase_ModelWidget* theWidget);
326 /// The signal about the widget is lost focus
327 /// \param theWidget the model base widget
328 void focusOutWidget(ModuleBase_ModelWidget* theWidget);
330 /// The signal about value state modification
331 void valueStateChanged(int theState);
333 /// The signal is emitted after flush of updates singal for the widget
334 void objectUpdated();
337 /// Sets default value of widget. Normally, widget should fetch this value
338 /// from the xml. However, some widgets derived widgets could define it
339 void setDefaultValue(const std::string& theValue);
340 /// \brief Set the attribute name
341 /// \param theAttribute the string value with attribute name
342 void setAttributeID(const std::string& theAttribute)
344 myAttributeID = theAttribute;
347 /// Sets the current value state. If the value is changed, the signal is emitted
348 /// If the current value state is Blocked, this method do nothing
349 /// \param theState a new state
350 /// \return the previous value state
351 ValueState setValueState(const ValueState& theState);
353 /// Blocks the value state change.
354 /// \param theBlocked a block state
355 /// \return the previous value
356 bool blockValueState(const bool theBlocked);
358 /// Compute the feature default value and fill the controls with it
359 /// or store the control value to the feature
360 virtual void initializeValueByActivate();
362 /// Saves the internal parameters to the given feature
363 /// \return True in success
364 virtual bool storeValueCustom() = 0;
366 /// Restore value from attribute data to the widget's control
367 virtual bool restoreValueCustom() = 0;
369 /// Fills the widget with default values
370 /// \return true if the widget current value is reset
371 virtual bool resetCustom() { return false; };
373 /// The method called when widget is activated
374 virtual void activateCustom() {};
376 //// Returns true if the event is processed. The default implementation is empty, returns false.
377 virtual bool processEnter();
379 //// Returns true if the event is processed. The default implementation is empty, returns false.
380 virtual bool processEscape();
382 //// Returns true if the event is processed. The default implementation is empty, returns false.
383 virtual bool processDelete();
385 /// Returns true if envent is processed. It applyes workshop selection for the widget attribute.
386 virtual bool processSelection();
389 /// Processing of values changed in model widget by store the current value to the feature
390 void onWidgetValuesChanged();
392 /// Changes widget state.
393 void onWidgetValuesModified();
396 /// own validator, by default it is zero
397 ModuleBase_WidgetValidator* myWidgetValidator;
399 /// The attribute name of the model feature
400 std::string myAttributeID;
402 /// A feature which is processing by active operation
403 FeaturePtr myFeature;
406 std::string myFeatureId;
408 /// Flag which shows that current operation is in editing mode
411 /// Flag which shows whether current widget is obligatory
412 /// The non-obligatory widgets should not accept the focus in the property panel
415 /// Flag about value of the control is enabled (can be modified)
416 EnableState myIsValueEnabled;
418 /// The widget value state
422 /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
423 bool myIsComputedDefault;
425 /// the default value, which is defined in the XML for this attribute
426 std::string myDefaultValue;
428 /// an XML internal state
431 // an XML state, the value is not stored into model if the widget is in edit mode
432 std::string myIsModifiedInEdit;
434 /// the reset state. If it is false, the reset method of the widget is not performed
436 /// blocked flag of modification of the value state
437 bool myIsValueStateBlocked;
438 /// do not flush updated signal
439 bool myFlushUpdateBlocked;