Salome HOME
4042beeb4d9453febee84ee8a757d033b0fb7ae6
[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
9 #ifndef MODULEBASE_OPERATION_H
10 #define MODULEBASE_OPERATION_H
11
12 #include <ModuleBase.h>
13
14 #include <QObject>
15 #include <QString>
16
17 #include <memory>
18
19 class SUIT_Study;
20 class XGUI_Workshop;
21 class ModelAPI_Feature;
22 class ModelAPI_Document;
23
24 /*!
25  \class ModuleBase_Operation
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_Operation: public QObject
41 {
42 Q_OBJECT
43
44 public:
45   /*! Enum describes state of operation */
46   enum OperationState
47   {
48     Waiting,  //!< Operation is not used (it is not run or suspended)
49     Running  //!< Operation is started
50   };
51
52   /*!
53    * Enum describes execution status of operation. Execution status often used after
54    * ending work of operation which was started from this one. In this case this
55    * operation can ask previously started operation whether it finished successfully.
56    */
57   enum ExecStatus
58   {
59     Rejected, //!< Operation has not performed any action (modification of data model for example)
60     Accepted  //!< Operation has performed an actions and must be stopped
61   };
62
63   /*!
64    * Enum describes setting of the operation.
65    */
66   enum Flags
67   {
68     None = 0x00, //!< None options
69     Transaction = 0x01 //!< Automatically open (commit/abort) transaction during start (commit/abort).
70   };
71
72 public:
73   ModuleBase_Operation(const QString& theId = "", QObject* parent = 0);
74   virtual ~ModuleBase_Operation();
75
76   // Operation processing.
77   virtual QString operationId() const;
78
79   virtual bool isPerformedImmediately() const;
80
81   std::shared_ptr<ModelAPI_Feature> feature() const;
82
83   OperationState state() const;
84   bool isRunning() const;
85   virtual bool isValid(ModuleBase_Operation* theOtherOp) const;
86   virtual bool isGranted() const;
87
88   bool setSlot(const QObject* theReceiver, const char* theSlot);
89
90   void setFlags(const int);
91   void clearFlags(const int);
92   bool testFlags(const int) const;
93
94   int execStatus() const;
95
96 signals:
97   void started();
98   void aborted();
99   void committed();
100   void stopped(); //!< operation aborted or committed
101
102   void callSlot();
103
104 public slots:
105   void start();
106   void abort();
107   void commit();
108
109   //true = do nothing, false = abort()
110   //Provided for S/S compatibility with QAction's toggle(bool)
111   void setRunning(bool);
112
113   // Data model operations.
114   void storeReal(double);
115
116 protected:
117   virtual bool isReadyToStart() const;
118
119   virtual void startOperation();
120   virtual void stopOperation();
121   virtual void abortOperation();
122   virtual void commitOperation();
123
124   void setExecStatus(const int);
125   void setState(const OperationState);
126
127   std::shared_ptr<ModelAPI_Document> document() const;
128
129 private:
130   int myFlags;               //!< Operation flags
131   OperationState myState;    //!< Operation state
132   ExecStatus myExecStatus;   //!< Execution status
133
134   //!< Next fields could be extracted into a subclass;
135   QString myOperationId;
136   std::shared_ptr<ModelAPI_Feature> myFeature;
137 };
138
139 #endif