Salome HOME
Boolean operations created
[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() const  { return false; }
67
68   /**
69   * Must return True if the given opertation can be launched as nested to current one.
70   * By default it returns false and it has to be redefined for operations which expect
71   * launching of nested operations
72   */
73   virtual bool isValid(ModuleBase_IOperation* theOperation) const { return false; }
74
75   /// Sets a list of model widgets, according to the operation feature xml definition
76   /// \param theXmlRepresentation an xml feature definition
77   /// \param theWidgets a list of widgets
78   //void setModelWidgets(const std::string& theXmlRepresentation,
79   //                     QList<ModuleBase_ModelWidget*> theWidgets);
80
81   /// Returns True if data of its feature was modified during operation
82   virtual bool isModified() const
83   {
84     return myIsModified;
85   }
86
87   /// Returns True id the current operation is launched in editing mode
88   bool isEditOperation() const
89   {
90     return myIsEditing;
91   }
92
93   /// Returns list of nested features
94   QStringList nestedFeatures() const { return myNestedFeatures; }
95
96   /// Sets list of nested features
97   void setNestedFeatures(const QStringList& theList) { myNestedFeatures = theList; }
98
99 signals:
100   void started();  /// the operation is started
101   void aborted();  /// the operation is aborted
102   void committed();  /// the operation is committed
103   void stopped();  /// the operation is aborted or committed
104   void resumed();  /// the operation is resumed
105
106  public slots:
107   /// Starts operation
108   /// Public slot. Verifies whether operation can be started and starts operation.
109   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
110   /// to change behavior of operation. There is no point in using this method. It would
111   /// be better to inherit own operator from base one and redefine startOperation method
112   /// instead.
113   void start();
114   /// Resumes operation
115   /// Public slot. Verifies whether operation can be started and starts operation.
116   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
117   /// to change behavior of operation. There is no point in using this method. It would
118   /// be better to inherit own operator from base one and redefine startOperation method
119   /// instead.
120   void resume();
121   /// Aborts operation
122   /// Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
123   /// Redefine abortOperation method to change behavior of operation instead
124   void abort();
125   /// Commits operation
126   /// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
127   /// Redefine commitOperation method to change behavior of operation instead
128   bool commit();
129
130   /// Alias for start/abort slots
131   /// Public slot. Aborts operation if false, else does nothing.
132   /// Provided for S/S compatibility with QAction's toggle(bool)
133   /// \param theState th flag to abort, if it is true, do nothing, overwise abort
134   void setRunning(bool theState);
135
136   // Data model methods.
137   /// Stores a custom value in model.
138   virtual void storeCustomValue() = 0;
139
140  protected:
141   /// Virtual method called when operation started (see start() method for more description)
142   /// Default impl calls corresponding slot and commits immediately.
143   virtual void startOperation() = 0;
144
145   /// Virtual method called when operation stopped - committed or aborted.
146   virtual void stopOperation() = 0;
147
148   /// Virtual method called when operation aborted (see abort() method for more description)
149   virtual void abortOperation() = 0;
150
151   /// Virtual method called when operation committed (see commit() method for more description)
152   virtual void commitOperation() = 0;
153
154   /// Virtual method called after operation committed (see commit() method for more description)
155   /// it is important that the method is called after the stop() signal is emitted
156   virtual void afterCommitOperation() = 0;
157
158   /// Verifies whether this operator can be commited.
159   /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
160   virtual bool canBeCommitted() const;
161
162   /// Returns pointer to the root document.
163   boost::shared_ptr<ModelAPI_Document> document() const;
164
165   /// Editing feature flag
166   bool myIsEditing;
167
168   /// Modified feature flag
169   bool myIsModified;
170
171  private:
172   ModuleBase_OperationDescription* myDescription;  /// the container to have the operation description
173
174   QStringList myNestedFeatures;
175 };
176
177 #endif