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