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