Salome HOME
Issue #2319: AttributeError: 'NoneType' object has no attribute 'isVertex' in model...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ModelWidget.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef MODULEBASE_MODELWIDGET_H
22 #define MODULEBASE_MODELWIDGET_H
23
24 #include <ModuleBase.h>
25 #include <ModuleBase_ActionType.h>
26 #include <ModuleBase_OperationFeature.h>
27 #include <ModuleBase_ActionInfo.h>
28 #include <ModelAPI_Feature.h>
29
30 #include <QWidget>
31
32 #include <memory>
33
34 class Config_WidgetAPI;
35 class Events_InfoMessage;
36 class ModuleBase_IPropertyPanel;
37 class ModuleBase_IWorkshop;
38 class ModuleBase_ViewerPrs;
39 class ModuleBase_WidgetValidator;
40 class QKeyEvent;
41
42 /**\class ModuleBase_ModelWidget
43  * \ingroup GUI
44  * \brief An abstract custom widget class. This class realization is assumed 
45  * to create some controls.
46  * The controls values modification should send signal about values change.
47  *
48  * Common interface for widgets in the property panel.
49  * Every widget are able to save/restore data from the model and/or to contain other widgets.
50  *
51  */
52 class MODULEBASE_EXPORT ModuleBase_ModelWidget : public QWidget
53 {
54 Q_OBJECT
55  public:
56    /// State of the widget
57    enum ValueState { Stored, /// modification is finished and applyed to the model
58                      ModifiedInPP, /// modification has not been finished and set to the model yet
59                      ModifiedInViewer, /// modification performed by viewer events
60                      Reset }; /// the value is reset
61
62    enum EnableState { On,  /// the widget value is always enabled
63                       Off, /// the widget value is always disabled
64                       /// the widget value enable state is defined in preferences
65                       DefinedInPreferences };
66
67   /// Constructor
68   /// \param theParent the parent object
69   /// \param theData the widget configuration. The attribute of the model widget is obtained from
70   /// a low-level API for reading xml definitions of widgets
71   ModuleBase_ModelWidget(QWidget* theParent, const Config_WidgetAPI* theData);
72   /// Destructor
73   virtual ~ModuleBase_ModelWidget();
74
75   /// Fills the widget with default values. It calls the resetCustom method and change
76   /// the widget state to Reset if the reset is performed.
77   /// \return true if the widget current value is reset
78   bool reset();
79
80   /// Returns the state whether the attribute of the feature is initialized
81   /// \param theObject a model feature to be checked
82   /// \return the boolean result
83   bool isInitialized(ObjectPtr theObject) const;
84
85   /// Returns true, if default value of the widget should be computed
86   /// on operation's execute, like radius for circle's constraint (can not be zero)
87   bool isComputedDefault() const { return myIsComputedDefault; }
88
89   /// Returns true, if default value of the widget is defined in the XML and it is not the
90   /// computed value
91   /// \return the boolean result
92   std::string getDefaultValue() const { return myDefaultValue; }
93
94   /// Returns true, if widget is internal
95   /// \return the boolean result
96   bool isInternal() const { return myIsInternal; }
97
98   /// Returns true, if the obligatory value of the widget is
99   /// not defined in the XML or has true value
100   /// \return the boolean result
101   bool isObligatory() const { return myIsObligatory; }
102
103   /// Returns true, if the widget value is enabled and might be modified manualy.
104   /// It returns false if the application preferences allow having disabled value
105   /// and the internal state tells to disable
106   /// \return the boolean result
107   virtual bool isValueEnabled() const;
108
109   /// Returns this parameter value in the xml file
110   /// \return the boolean result
111   bool isUseReset() const { return myUseReset; }
112
113   /// Returns this parameter value in the xml file
114   /// \return the boolean result
115   std::string isModifiedInEdit() const { return myIsModifiedInEdit; }
116
117   /// Returns this widget value state
118   /// \return the enumeration result
119   ValueState getValueState() const { return myState; }
120
121   /// Stores the widget value if it is modified
122   void processValueState();
123
124   /// Returns an attribute error according to the value state
125   /// It exists in all cases excepring the "Store" case
126   Events_InfoMessage getValueStateError() const;
127
128   /// Defines if it is supposed that the widget should interact with the viewer.
129   virtual bool isViewerSelector() { return false; }
130
131   /// Defines if it is supported to set the value in this widget
132   /// By default it returns true
133   virtual bool canAcceptFocus() const { return true; };
134
135   //! Returns the widget error, get it from the attribute validator and state of the widget
136   //! If the feature is correct, it returns an empty value
137   //! \param theValueStateChecked the boolean flag if the state of the widget should be checked
138   //! \return string value
139   QString getError(const bool theValueStateChecked = true) const;
140
141   /// Set the given wrapped value to the current widget
142   /// This value should be processed in the widget according to the needs
143   /// \param theValues the wrapped selection values
144   /// \param theToValidate the boolean value whether the value should be checked by filters
145   virtual bool setSelection(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues,
146                             const bool theToValidate)
147   {
148     return false;
149   }
150
151   /// Returns values which should be highlighted when the whidget is active
152   /// \param theValues a list of presentations
153   virtual void getHighlighted(QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues) {};
154
155   /// Checks if the selection presentation is valid in widget
156   /// \param theValue a selected presentation in the view
157   /// \return a boolean value
158   virtual bool isValidSelectionCustom(const std::shared_ptr<ModuleBase_ViewerPrs>& theValue)
159   { return true; }
160
161   /// Returns widget validator, by default it is NULL. To be created in a child if necessary
162   ModuleBase_WidgetValidator* widgetValidator() { return myWidgetValidator; }
163
164   /// Restore value from attribute data to the widget's control.
165   /// Emits signals before and after store
166   /// \return True in success
167   bool restoreValue();
168
169   /// Saves the internal parameters to the given feature. Emits signals before and after store
170   /// \return True in success
171   bool storeValue();
172
173   /// Set focus to the first control of the current widget.
174   /// The focus policy of the control is checked.
175   /// If the widget has the NonFocus focus policy, it is skipped.
176   /// \return the state whether the widget can accept the focus
177   virtual bool focusTo();
178
179   /// Select the internal content if it can be selected. It is empty in the default realization
180   virtual void selectContent() {}
181
182   /// The method called when widget is activated
183   void activate();
184
185   /// The method called when widget is deactivated
186   virtual void deactivate();
187
188   /// The method called if widget should be activated always
189   virtual bool needToBeActiated() { return false; }
190
191   /// Returns list of widget controls
192   /// \return a control list
193   virtual QList<QWidget*> getControls() const = 0;
194
195   /// Returns the first or the last control that can accept the focus
196   /// \param isFirst if true, the first controls is returned or the last one
197   /// \return a control from a list of controls
198   QWidget* getControlAcceptingFocus(const bool isFirst);
199
200   /// FocusIn events processing
201   virtual bool eventFilter(QObject* theObject, QEvent *theEvent);
202
203   /// \brief Enables processing of focus event on all controls by the widget
204   /// if this widget is not obligatory and set no-focus policy otherwise
205   virtual void enableFocusProcessing();
206
207   //! Switch On/Off highlighting of the widget
208   virtual void setHighlighted(bool isHighlighted);
209
210   /// Returns the attribute name
211   /// \returns the string value
212   std::string attributeID() const
213   {
214     return myAttributeID;
215   }
216
217   /// \return Current feature
218   FeaturePtr feature() const
219   {
220     return myFeature;
221   }
222
223   /// \return Context for translation
224   virtual std::string context() const {
225
226     std::string aContext = myFeatureId;
227     if(!aContext.empty() && !myAttributeID.empty()) {
228       aContext += ":";
229     }
230     aContext += myAttributeID;
231
232     return aContext;
233   }
234
235   /// Set feature which is processing by active operation
236   /// \param theFeature a feature object
237   /// \param theToStoreValue a value about necessity to store the widget value to the feature
238   /// \param isUpdateFlushed a flag if update should be flushed on store value
239   void setFeature(const FeaturePtr& theFeature, const bool theToStoreValue = false,
240                   const bool isUpdateFlushed = true);
241
242   /// Editing mode depends on mode of current operation. This value is defined by it.
243   virtual void setEditingMode(bool isEditing) { myIsEditing = isEditing; }
244
245   /// \return Current Editing mode
246   bool isEditingMode() const { return myIsEditing; }
247
248   /// Returns true if the action can be processed. By default it is empty and returns false.
249   /// \param theActionType an action type
250   /// \param isActionEnabled if true, the enable state of the action
251   virtual bool canProcessAction(ModuleBase_ActionType theActionType, bool& isActionEnabled);
252
253   /// Returns true if the event is processed. The default implementation is empty, returns false.
254   virtual bool processAction(ModuleBase_ActionType theActionType);
255
256   /// Returns list of accessible actions for Undo/Redo commands. By default it returns empty list.
257   /// \param theActionType type of action. It can be ActionUndo or ActionRedo.
258   virtual QList<ActionInfo> actionsList(ModuleBase_ActionType theActionType) const
259   { return QList<ActionInfo>(); }
260
261   /// Sends Update and Redisplay for the given object
262   /// \param theObj is updating object
263   void updateObject(ObjectPtr theObj);
264
265   /// Translate passed string with widget context()
266   virtual QString translate(const std::string& theStr) const;
267
268   /// Emit focus in widget to set this control as active in propety panel
269   void emitFocusInWidget() { emit focusInWidget(this); }
270
271   /// Finds model widget parent of the given sub widget
272   /// \param theWidget a candidate to be a child of the model widget
273   /// \param theProp a property panel instance
274   /// \return a model widget or NULL
275   static ModuleBase_ModelWidget* findModelWidget(ModuleBase_IPropertyPanel* theProp,
276                                                  QWidget* theWidget);
277 signals:
278   /// The signal about widget values are to be changed
279   void beforeValuesChanged();
280   /// The signal about widget values changed
281   void valuesChanged();
282   /// The signal about widget values modified
283   void valuesModified();
284   /// The signal about widget values are to be changed
285   void afterValuesChanged();
286
287   /// The signal about widget values are to be restored
288   void beforeValuesRestored();
289   /// The signal about widget values are to be restored
290   void afterValuesRestored();
291
292   /// The signal about key release on the control, that corresponds to the attribute
293   /// \param theObject a sender of the event
294   /// \param theEvent key release event
295   void keyReleased(QObject* theObject, QKeyEvent* theEvent);
296
297   /// The signal is emitted if the enter is clicked in the control of the widget
298   /// \param theObject a sender of the event
299   void enterClicked(QObject* theObject);
300
301   /// The signal about the widget is get focus
302   /// \param theWidget the model base widget
303   void focusInWidget(ModuleBase_ModelWidget* theWidget);
304
305   /// The signal about the widget is lost focus
306   /// \param theWidget the model base widget
307   void focusOutWidget(ModuleBase_ModelWidget* theWidget);
308
309   /// The signal about value state modification
310   void valueStateChanged(int theState);
311
312   /// The signal is emitted after flush of updates singal for the widget
313   void objectUpdated();
314
315 protected:
316   /// Sets default value of widget. Normally, widget should fetch this value
317   /// from the xml. However, some widgets derived widgets could define it
318   void setDefaultValue(const std::string& theValue);
319   /// \brief Set the attribute name
320   /// \param theAttribute the string value with attribute name
321   void setAttributeID(const std::string& theAttribute)
322   {
323     myAttributeID = theAttribute;
324   }
325
326   /// Sets the current value state. If the value is changed, the signal is emitted
327   /// If the current value state is Blocked, this method do nothing
328   /// \param theState a new state
329   /// \return the previous value state
330   ValueState setValueState(const ValueState& theState);
331
332   /// Blocks the value state change.
333   /// \param theBlocked a block state
334   /// \return the previous value
335   bool blockValueState(const bool theBlocked);
336
337   /// Compute the feature default value and fill the controls with it
338   /// or store the control value to the feature
339   virtual void initializeValueByActivate();
340
341   /// Saves the internal parameters to the given feature
342   /// \return True in success
343   virtual bool storeValueCustom() = 0;
344
345   /// Restore value from attribute data to the widget's control
346   virtual bool restoreValueCustom() = 0;
347
348   /// Fills the widget with default values
349   /// \return true if the widget current value is reset
350   virtual bool resetCustom() { return false; };
351
352   /// The method called when widget is activated
353   virtual void activateCustom() {};
354
355     //// Returns true if the event is processed. The default implementation is empty, returns false.
356   virtual bool processEnter();
357
358   //// Returns true if the event is processed. The default implementation is empty, returns false.
359   virtual bool processEscape();
360
361   //// Returns true if the event is processed. The default implementation is empty, returns false.
362   virtual bool processDelete();
363
364 protected slots:
365   /// Processing of values changed in model widget by store the current value to the feature
366   void onWidgetValuesChanged();
367
368   /// Changes widget state.
369   void onWidgetValuesModified();
370
371  protected:
372     /// own validator, by default it is zero
373    ModuleBase_WidgetValidator* myWidgetValidator;
374
375   /// The attribute name of the model feature
376   std::string myAttributeID;
377
378   /// A feature which is processing by active operation
379   FeaturePtr myFeature;
380
381   /// A feature ID
382   std::string myFeatureId;
383
384   /// Flag which shows that current operation is in editing mode
385   bool myIsEditing;
386
387   /// Flag which shows whether current widget is obligatory
388   /// The non-obligatory widgets should not accept the focus in the property panel
389   bool myIsObligatory;
390
391   /// Flag about value of the control is enabled (can be modified)
392   EnableState myIsValueEnabled;
393
394   /// The widget value state
395   ValueState myState;
396
397 private:
398   /// Value should be computed on execute, like radius for circle's constraint (can not be zero)
399   bool myIsComputedDefault;
400
401   /// the default value, which is defined in the XML for this attribute
402   std::string myDefaultValue;
403
404   /// an XML internal state
405   bool myIsInternal;
406
407   // an XML state, the value is not stored into model if the widget is in edit mode
408   std::string myIsModifiedInEdit;
409
410   /// the reset state. If it is false, the reset method of the widget is not performed
411   bool myUseReset;
412   /// blocked flag of modification of the value state
413   bool myIsValueStateBlocked;
414   /// do not flush updated signal
415   bool myFlushUpdateBlocked;
416 };
417
418 #endif