Salome HOME
Merge branch 'master' of newgeom:newgeom.git into BR_PYTHON_PLUGIN
[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
21 class ModuleBase_ModelWidget;
22 class ModuleBase_OperationDescription;
23 class ModuleBase_IPropertyPanel;
24 class ModuleBase_ISelection;
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(ModuleBase_ISelection* theSelection);
121
122   virtual void setPropertyPanel(ModuleBase_IPropertyPanel* theProp);
123
124   ModuleBase_IPropertyPanel* propertyPanel() const { return myPropertyPanel; }
125
126   /// Activates widgets by preselection if it is accepted
127   virtual bool activateByPreselection();
128
129 signals:
130   void started();  /// the operation is started
131   void aborted();  /// the operation is aborted
132   void committed();  /// the operation is committed
133   void stopped();  /// the operation is aborted or committed
134   void resumed();  /// the operation is resumed
135
136  public slots:
137   /// Starts operation
138   /// Public slot. Verifies whether operation can be started and starts operation.
139   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
140   /// to change behavior of operation. There is no point in using this method. It would
141   /// be better to inherit own operator from base one and redefine startOperation method
142   /// instead.
143   void start();
144   /// Resumes operation
145   /// Public slot. Verifies whether operation can be started and starts operation.
146   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
147   /// to change behavior of operation. There is no point in using this method. It would
148   /// be better to inherit own operator from base one and redefine startOperation method
149   /// instead.
150   void resume();
151   /// Aborts operation
152   /// Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
153   /// Redefine abortOperation method to change behavior of operation instead
154   void abort();
155   /// Commits operation
156   /// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
157   /// Redefine commitOperation method to change behavior of operation instead
158   bool commit();
159
160   /// Alias for start/abort slots
161   /// Public slot. Aborts operation if false, else does nothing.
162   /// Provided for S/S compatibility with QAction's toggle(bool)
163   /// \param theState th flag to abort, if it is true, do nothing, overwise abort
164   void setRunning(bool theState);
165
166   // Data model methods.
167   /// Stores a custom value in model.
168   virtual void storeCustomValue();
169
170   /// Slots which listen the mode widget activation
171   /// \param theWidget the model widget
172   virtual void onWidgetActivated(ModuleBase_ModelWidget* theWidget);
173
174  protected:
175   /// Virtual method called when operation started (see start() method for more description)
176   /// Default impl calls corresponding slot and commits immediately.
177   virtual void startOperation();
178
179   /// Virtual method called when operation stopped - committed or aborted.
180   virtual void stopOperation();
181
182   /// Virtual method called when operation aborted (see abort() method for more description)
183   virtual void abortOperation();
184
185   /// Virtual method called when operation committed (see commit() method for more description)
186   virtual void commitOperation();
187
188   /// Virtual method called after operation committed (see commit() method for more description)
189   virtual void afterCommitOperation();
190
191   /// Send update message by loop
192   void flushUpdated();
193   /// Send created message by loop
194   void flushCreated();
195
196   /// Creates an operation new feature
197   /// \param theFlushMessage the flag whether the create message should be flushed
198   /// \param theCompositeFeature the feature that must be used for adding the created object or null
199   /// \returns the created 
200   virtual FeaturePtr createFeature(const bool theFlushMessage = true, 
201     CompositeFeaturePtr theCompositeFeature = CompositeFeaturePtr());
202
203   /// Verifies whether this operator can be commited.
204   /// \return Returns TRUE if current operation can be committed, e.g. all parameters are filled
205   virtual bool canBeCommitted() const;
206
207   /// Returns pointer to the root document.
208   boost::shared_ptr<ModelAPI_Document> document() const;
209
210   /// Set value to the active widget
211   /// \param theFeature the feature
212   /// \param theX the horizontal coordinate
213   /// \param theY the vertical coordinate
214   /// \return true if the point is set
215   virtual bool setWidgetValue(ObjectPtr theFeature, double theX, double theY);
216
217  protected:
218   FeaturePtr myFeature;  /// the operation feature to be handled
219
220   /// the container to have the operation description
221   ModuleBase_OperationDescription* myDescription;  
222
223   /// Editing feature flag
224   bool myIsEditing;
225
226   /// Modified feature flag
227   bool myIsModified;
228
229   /// List of nested operations IDs
230   QStringList myNestedFeatures;
231
232   /// List of pre-selected object 
233   QList<ModuleBase_ViewerPrs> myPreSelection;
234
235   /// Access to property panel
236   ModuleBase_IPropertyPanel* myPropertyPanel;
237 };
238
239 #endif