Salome HOME
Issues #248, #235 The obtaining of the point from the TopoDS_Vertex shape during...
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.h
1 /*
2  * ModuleBase_Operation.h
3  *
4  *  Created on: Apr 2, 2014
5  *      Author: sbh
6  */
7
8 #ifndef ModuleBase_Operation_H
9 #define ModuleBase_Operation_H
10
11 #include <ModuleBase.h>
12 #include <ModuleBase_ViewerPrs.h>
13
14 #include <ModelAPI_CompositeFeature.h>
15 #include <ModelAPI_Document.h>
16
17 #include <QObject>
18 #include <QString>
19 #include <QStringList>
20
21 class ModuleBase_ModelWidget;
22 class ModuleBase_OperationDescription;
23 class ModuleBase_IPropertyPanel;
24 class ModuleBase_ISelection;
25 class ModuleBase_IViewer;
26 class ModuleBase_WidgetValueFeature;
27
28 class QKeyEvent;
29
30 /*!
31  \class ModuleBase_Operation
32  * \brief Base class for all operations
33  *
34  *  Base class for all operations. If you perform an action it is reasonable to create
35  *  operation intended for this. This is a base class for all operations which provides
36  *  mechanism for correct starting operations, starting operations above already started
37  *  ones, committing operations and so on. To create own operation it is reasonable to
38  *  inherit it from this class and redefines virtual methods to provide own behavior
39  *  Main virtual methods are
40  *  - virtual bool      isReadyToStart();
41  *  - virtual void      startOperation();
42  *  - virtual void      abortOperation();
43  *  - virtual void      commitOperation();
44  */
45
46 class MODULEBASE_EXPORT ModuleBase_Operation : public QObject
47 {
48 Q_OBJECT
49
50  public:
51   /// Constructor
52   /// \param theId the operation identifier
53   /// \param theParent the QObject parent
54   ModuleBase_Operation(const QString& theId = "", QObject* theParent = 0);
55   /// Destructor
56   virtual ~ModuleBase_Operation();
57
58   /// Returns the operation description
59   /// /returns the instance of the description class
60   ModuleBase_OperationDescription* getDescription() const { return myDescription; }
61
62   /**
63   * Must return true if this operation can be launched as nested for any current operation
64   * and it is not necessary to check this operation on validity. By default 
65   * the operation is not granted.
66   * The method has to be redefined for granted operations.
67   */
68   virtual bool isGranted(ModuleBase_Operation* theOperation) const  { return false; }
69
70   /// Sets a list of model widgets, according to the operation feature xml definition
71   /// \param theXmlRepresentation an xml feature definition
72   /// \param theWidgets a list of widgets
73   //void setModelWidgets(const std::string& theXmlRepresentation,
74   //                     QList<ModuleBase_ModelWidget*> theWidgets);
75
76   /// Returns True if data of its feature was modified during operation
77   virtual bool isModified() const { return myIsModified; }
78
79   /// Returns True id the current operation is launched in editing mode
80   bool isEditOperation() const { return myIsEditing; }
81
82   /// Returns list of nested features
83   QStringList nestedFeatures() const { return myNestedFeatures; }
84
85   /// Sets list of nested features
86   void setNestedFeatures(const QStringList& theList) { myNestedFeatures = theList; }
87
88
89   // Returns operations Id from it's description
90   QString id() const;
91
92   /// Returns the operation feature
93   /// \return the feature
94   FeaturePtr feature() const;
95
96   /**
97   * Must return True if the operation's feature is valid.
98   * Since IOperation does not have any feature returns false.
99   */
100   virtual bool isValid() const;
101
102   /// Returns whether the nested operations are enabled.
103   /// The state can depend on the operation current state.
104   /// \return enabled state
105   virtual bool isNestedOperationsEnabled() const;
106
107   /// Sets the operation feature
108   void setFeature(FeaturePtr theFeature);
109
110   /// Returns True if the current operation works with the given object (feature or result)
111   virtual bool hasObject(ObjectPtr theObj) const;
112
113   virtual void keyReleased(const int theKey) {};
114
115   /// If operation needs to redisplay its result during operation
116   /// then this method has to return True
117   virtual bool hasPreview() const { return false; }
118
119   /// Initialisation of operation with preliminary selection
120   /// \param theSelected the list of selected presentations
121   /// \param theHighlighted the list of highlighted presentations
122   /// \param theViewer a viewer to have the viewer the eye position
123   virtual void initSelection(ModuleBase_ISelection* theSelection,
124                              ModuleBase_IViewer* /* theViewer*/);
125
126   virtual void setPropertyPanel(ModuleBase_IPropertyPanel* theProp);
127
128   ModuleBase_IPropertyPanel* propertyPanel() const { return myPropertyPanel; }
129
130   /// Activates widgets by preselection if it is accepted
131   virtual bool activateByPreselection();
132
133 signals:
134   void started();  /// the operation is started
135   void aborted();  /// the operation is aborted
136   void committed();  /// the operation is committed
137   void stopped();  /// the operation is aborted or committed
138   void resumed();  /// the operation is resumed
139
140  public slots:
141   /// Starts operation
142   /// Public slot. Verifies whether operation can be started and starts operation.
143   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
144   /// to change behavior of operation. There is no point in using this method. It would
145   /// be better to inherit own operator from base one and redefine startOperation method
146   /// instead.
147   void start();
148   /// Resumes operation
149   /// Public slot. Verifies whether operation can be started and starts operation.
150   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
151   /// to change behavior of operation. There is no point in using this method. It would
152   /// be better to inherit own operator from base one and redefine startOperation method
153   /// instead.
154   void resume();
155   /// Aborts operation
156   /// Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
157   /// Redefine abortOperation method to change behavior of operation instead
158   void abort();
159   /// Commits operation
160   /// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
161   /// Redefine commitOperation method to change behavior of operation instead
162   bool commit();
163
164   /// Alias for start/abort slots
165   /// Public slot. Aborts operation if false, else does nothing.
166   /// Provided for S/S compatibility with QAction's toggle(bool)
167   /// \param theState th flag to abort, if it is true, do nothing, overwise abort
168   void setRunning(bool theState);
169
170   // Data model methods.
171   /// Stores a custom value in model.
172   virtual void storeCustomValue();
173
174   /// Slots which listen the mode widget activation
175   /// \param theWidget the model widget
176   virtual void onWidgetActivated(ModuleBase_ModelWidget* theWidget);
177
178  protected:
179   /// Virtual method called when operation started (see start() method for more description)
180   /// Default impl calls corresponding slot and commits immediately.
181   virtual void startOperation();
182
183   /// Virtual method called when operation stopped - committed or aborted.
184   virtual void stopOperation();
185
186   /// Virtual method called when operation aborted (see abort() method for more description)
187   virtual void abortOperation();
188
189   /// Virtual method called when operation committed (see commit() method for more description)
190   virtual void commitOperation();
191
192   /// Virtual method called after operation committed (see commit() method for more description)
193   virtual void afterCommitOperation();
194
195   /// Send update message by loop
196   void flushUpdated();
197   /// Send created message by loop
198   void flushCreated();
199
200   /// Creates an operation new feature
201   /// \param theFlushMessage the flag whether the create message should be flushed
202   /// \param theCompositeFeature the feature that must be used for adding the created object or null
203   /// \returns the created 
204   virtual FeaturePtr createFeature(const bool theFlushMessage = true, 
205     CompositeFeaturePtr theCompositeFeature = CompositeFeaturePtr());
206
207   /// Verifies whether this operator can be commited.
208   /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
209   virtual bool canBeCommitted() const;
210
211   /// Returns pointer to the root document.
212   boost::shared_ptr<ModelAPI_Document> document() const;
213
214   /// Set value to the active widget
215   /// \param theFeature the feature
216   /// \param theX the horizontal coordinate
217   /// \param theY the vertical coordinate
218   /// \return true if the point is set
219   virtual bool setWidgetValue(ObjectPtr theFeature, double theX, double theY);
220
221   /// Return a widget value point by the selection and the viewer position
222   /// The default realization returns false
223   /// \param thePrs the presentation
224   /// \param theViewer a viewer to have the viewer the eye position
225   /// \param theX the horizontal coordinate
226   /// \param theY the vertical coordinate
227   /// \return true if the point exits in the selection
228   virtual bool getViewerPoint(ModuleBase_ViewerPrs thePrs,
229                                    ModuleBase_IViewer* theViewer,
230                                    double& theX, double& theY);
231
232   // Removes the preselection information and clears the map of preselection
233   void clearPreselection();
234
235  protected:
236   FeaturePtr myFeature;  /// the operation feature to be handled
237
238   /// the container to have the operation description
239   ModuleBase_OperationDescription* myDescription;  
240
241   /// Editing feature flag
242   bool myIsEditing;
243
244   /// Modified feature flag
245   bool myIsModified;
246
247   /// List of nested operations IDs
248   QStringList myNestedFeatures;
249
250   /// List of pre-selected object 
251   QList<ModuleBase_WidgetValueFeature*> myPreSelection;
252
253   /// Access to property panel
254   ModuleBase_IPropertyPanel* myPropertyPanel;
255 };
256
257 #endif