Salome HOME
Isssue #149 #147 Restart operation issue resolved
[modules/shaper.git] / src / ModuleBase / ModuleBase_IOperation.h
1 /*
2  * ModuleBase_IOperation.h
3  *
4  *  Created on: May 5, 2014
5  *      Author: nds
6  */
7
8 #ifndef ModuleBase_IOperation_H
9 #define ModuleBase_IOperation_H
10
11 #include <ModuleBase.h>
12
13 #include <QObject>
14 #include <QString>
15 #include <QList>
16 #include <QStringList>
17
18 #include <boost/shared_ptr.hpp>
19
20 class ModelAPI_Document;
21 class ModuleBase_OperationDescription;
22 //class ModuleBase_ModelWidget;
23
24 /*!
25  \class ModuleBase_IOperation
26  * \brief Base class for all operations
27  *
28  *  Base class for all operations. If you perform an action it is reasonable to create
29  *  operation intended for this. This is a base class for all operations which provides
30  *  mechanism for correct starting operations, starting operations above already started
31  *  ones, committing operations and so on. To create own operation it is reasonable to
32  *  inherit it from this class and redefines virtual methods to provide own behavior
33  *  Main virtual methods are
34  *  - virtual bool      isReadyToStart();
35  *  - virtual void      startOperation();
36  *  - virtual void      abortOperation();
37  *  - virtual void      commitOperation();
38  */
39
40 class MODULEBASE_EXPORT ModuleBase_IOperation : public QObject
41 {
42 Q_OBJECT
43
44  public:
45   /// Constructor
46   /// Constructs an empty operation. Constructor should work very fast because many
47   /// operators may be created after starting workshop but only several from them
48   /// may be used. As result this constructor stores given workshop in myApp field
49   /// and set Waiting status.
50   /// \param theId the operation identifier
51   /// \param theParent the QObject parent
52   ModuleBase_IOperation(const QString& theId = "", QObject* theParent = 0);
53   /// Destructor
54   virtual ~ModuleBase_IOperation();
55
56   /// Returns the operation description
57   /// /returns the instance of the description class
58   ModuleBase_OperationDescription* getDescription() const;
59
60   /**
61   * Must return true if this operation can be launched as nested for any current operation
62   * and it is not necessary to check this operation on validity. By default 
63   * the operation is not granted.
64   * The method has to be redefined for granted operations.
65   */
66   virtual bool isGranted(ModuleBase_IOperation* theOperation) const  { return false; }
67
68   /**
69   * Must return True if the operation's feature is valid.
70   * Since IOperation does not have any feature returns false.
71   */
72   virtual bool isValid() const { return false; }
73
74   /// Sets a list of model widgets, according to the operation feature xml definition
75   /// \param theXmlRepresentation an xml feature definition
76   /// \param theWidgets a list of widgets
77   //void setModelWidgets(const std::string& theXmlRepresentation,
78   //                     QList<ModuleBase_ModelWidget*> theWidgets);
79
80   /// Returns True if data of its feature was modified during operation
81   virtual bool isModified() const
82   {
83     return myIsModified;
84   }
85
86   /// Returns True id the current operation is launched in editing mode
87   bool isEditOperation() const
88   {
89     return myIsEditing;
90   }
91
92   /// Returns list of nested features
93   QStringList nestedFeatures() const { return myNestedFeatures; }
94
95   /// Sets list of nested features
96   void setNestedFeatures(const QStringList& theList) { myNestedFeatures = theList; }
97
98 signals:
99   void started();  /// the operation is started
100   void aborted();  /// the operation is aborted
101   void committed();  /// the operation is committed
102   void stopped();  /// the operation is aborted or committed
103   void resumed();  /// the operation is resumed
104
105  public slots:
106   /// Starts operation
107   /// Public slot. Verifies whether operation can be started and starts operation.
108   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
109   /// to change behavior of operation. There is no point in using this method. It would
110   /// be better to inherit own operator from base one and redefine startOperation method
111   /// instead.
112   void start();
113   /// Resumes operation
114   /// Public slot. Verifies whether operation can be started and starts operation.
115   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
116   /// to change behavior of operation. There is no point in using this method. It would
117   /// be better to inherit own operator from base one and redefine startOperation method
118   /// instead.
119   void resume();
120   /// Aborts operation
121   /// Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
122   /// Redefine abortOperation method to change behavior of operation instead
123   void abort();
124   /// Commits operation
125   /// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
126   /// Redefine commitOperation method to change behavior of operation instead
127   bool commit();
128
129   /// Alias for start/abort slots
130   /// Public slot. Aborts operation if false, else does nothing.
131   /// Provided for S/S compatibility with QAction's toggle(bool)
132   /// \param theState th flag to abort, if it is true, do nothing, overwise abort
133   void setRunning(bool theState);
134
135   // Data model methods.
136   /// Stores a custom value in model.
137   virtual void storeCustomValue() = 0;
138
139  protected:
140   /// Virtual method called when operation started (see start() method for more description)
141   /// Default impl calls corresponding slot and commits immediately.
142   virtual void startOperation() = 0;
143
144   /// Virtual method called when operation stopped - committed or aborted.
145   virtual void stopOperation() = 0;
146
147   /// Virtual method called when operation aborted (see abort() method for more description)
148   virtual void abortOperation() = 0;
149
150   /// Virtual method called when operation committed (see commit() method for more description)
151   virtual void commitOperation() = 0;
152
153   /// Virtual method called after operation committed (see commit() method for more description)
154   /// it is important that the method is called after the stop() signal is emitted
155   virtual void afterCommitOperation() = 0;
156
157   /// Verifies whether this operator can be commited.
158   /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
159   virtual bool canBeCommitted() const;
160
161   /// Returns pointer to the root document.
162   boost::shared_ptr<ModelAPI_Document> document() const;
163
164   /// Editing feature flag
165   bool myIsEditing;
166
167   /// Modified feature flag
168   bool myIsModified;
169
170  private:
171   ModuleBase_OperationDescription* myDescription;  /// the container to have the operation description
172
173   QStringList myNestedFeatures;
174 };
175
176 #endif