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