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