Salome HOME
Merge remote-tracking branch 'remotes/origin/Filters_Development_2'
[modules/shaper.git] / src / ModuleBase / ModuleBase_OperationFeature.h
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef ModuleBase_OperationFeature_H
21 #define ModuleBase_OperationFeature_H
22
23 #include <ModuleBase.h>
24 #include <ModuleBase_Operation.h>
25
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_CompositeFeature.h>
28
29 #include <QObject>
30 #include <QString>
31 #include <QStringList>
32
33 #include <set>
34
35 class ModuleBase_ModelWidget;
36 class ModuleBase_ISelection;
37 class ModuleBase_IViewer;
38 class ModuleBase_IWorkshop;
39 class ModuleBase_ViewerPrs;
40
41 class QKeyEvent;
42
43 /*!
44  * \class ModuleBase_OperationFeature
45  * \ingroup GUI
46  * \brief Base class for all operations
47  *
48  *  Base class for all operations. If you perform an action it is reasonable to create
49  *  operation intended for this. This is a base class for all operations which provides
50  *  mechanism for correct starting operations, starting operations above already started
51  *  ones, committing operations and so on. To create own operation it is reasonable to
52  *  inherit it from this class and redefines virtual methods to provide own behavior
53  *  Main virtual methods are
54  *  - virtual bool      isReadyToStart();
55  *  - virtual void      startOperation();
56  *  - virtual void      abortOperation();
57  *  - virtual void      commitOperation();
58  */
59
60 class MODULEBASE_EXPORT ModuleBase_OperationFeature : public ModuleBase_Operation
61 {
62 Q_OBJECT
63
64  public:
65
66   /// Appends to operation's history id, if it is an "edit" operation (myIsEditing == true)
67   static QString EditSuffix() { return "_E"; }
68   /// Constructor
69   /// \param theId the operation identifier
70   /// \param theParent the QObject parent
71   ModuleBase_OperationFeature(const QString& theId = "", QObject* theParent = 0);
72   /// Destructor
73   virtual ~ModuleBase_OperationFeature();
74
75   /// Returns True id the current operation is launched in editing mode
76   bool isEditOperation() const { return myIsEditing; }
77
78   /// Change the operation mode from create to edit.
79   /// The transaction and the operation name in the model history of transaction are the same.
80   /// It updates the edit state in the widgets of property panel
81   /// \param isEditState boolean state whether the operation should become editing or creating
82   // \param theRestartTransaction if true, the current model transaction is committed and
83   /// the new one is started
84   void setEditOperation(const bool& isEditState/*const bool theRestartTransaction*/);
85
86   /// Returns the operation feature
87   /// \return the feature
88   FeaturePtr feature() const;
89
90   /// Must return True if the operation's feature is valid.
91   /// Since IOperation does not have any feature returns false.
92   virtual bool isValid() const;
93
94   /// Sets the operation feature
95   void setFeature(FeaturePtr theFeature);
96
97   /// Returns True if the current operation works with the given object (feature or result)
98   virtual bool hasObject(ObjectPtr theObj) const;
99
100   /// Returns true if the object is displayed when the operation was started
101   /// \param theObject a feature or result of the operation feature
102   /// \return boolean value whether the object display state was changed
103   virtual bool isDisplayedOnStart(ObjectPtr theObject);
104
105   /// Initialisation of operation with preliminary selection
106   /// \param thePreSelected a container of selected presentations
107   virtual void initSelection(
108                   const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& thePreSelected);
109
110   /// Fill internal map by preselection
111   /// \param theValues a list of preselection
112   void setPreselection(const QList<std::shared_ptr<ModuleBase_ViewerPrs>>& theValues);
113
114   /// \brief Set property pane to the operation
115   /// \param theProp a property panel instance
116   virtual void setPropertyPanel(ModuleBase_IPropertyPanel* theProp);
117
118   // \return Currently installed property panel
119   //ModuleBase_IPropertyPanel* propertyPanel() const { return myPropertyPanel; }
120
121   /// Activates widgets by preselection if it is accepted.
122   /// \param theGreedAttributeId a greed attribute id if there is in the current feature
123   /// \return last filled widget
124   virtual ModuleBase_ModelWidget* activateByPreselection(const std::string& theGreedAttributeId);
125
126   /// If the operation works with feature which is sub-feature of another one
127   /// then this variable has to be initialised by parent feature
128   /// before operation feature creating
129   void setParentFeature(CompositeFeaturePtr theParent);
130
131   /// \return Installed parent feature (can be NULL)
132   CompositeFeaturePtr parentFeature() const;
133
134   /// Stores the previous to the operation current feature
135   /// \param theFeature a feature
136   void setPreviousCurrentFeature(const FeaturePtr& theFeature);
137
138   /// Returns the previous to the operation current feature
139   /// \return theFeature a feature
140   FeaturePtr previousCurrentFeature();
141
142   /// Set whether the operation should be aborted. By default the state is false in operation
143   /// \param theState abort state
144   void setNeedToBeAborted(const bool theState)  { myNeedToBeAborted = theState; }
145
146   /// Returns valid state of the operation
147   /// \return custom validity state (it is almost always true)
148   bool isNeedToBeAborted() const { return myNeedToBeAborted; }
149
150   /// Call this function on launch of a nested operation
151   /// when transaction has to be reopened on resume of main operation
152   /// By default transaction is not reopened on resuming
153   void openTransactionOnResume() { myRestartTransactionOnResume = true; }
154
155  public slots:
156   /// Starts operation
157   /// Public slot. Verifies whether operation can be started and starts operation.
158   /// This slot is not virtual and cannot be redefined. Redefine startOperation method
159   /// to change behavior of operation. There is no point in using this method. It would
160   /// be better to inherit own operator from base one and redefine startOperation method
161   /// instead.
162   /// \return true if the start is successful
163   virtual bool start();
164
165   /// Aborts operation
166   /// Public slot. Aborts operation. This slot is not virtual and cannot be redefined.
167   /// Redefine abortOperation method to change behavior of operation instead
168   void abort();
169
170   /// Commits operation
171   /// Public slot. Commits operation. This slot is not virtual and cannot be redefined.
172   /// Redefine commitOperation method to change behavior of operation instead
173   bool commit();
174
175  protected:
176   /// Displays the feature/results if it is hidden. It will be hided in stopOperation
177   virtual void startOperation();
178
179   /// Hide feature/results if they were hided on start
180   virtual void stopOperation();
181
182   /// Virtual method called after operation resume (see resume() method for more description)
183   virtual void resumeOperation();
184
185   /// Creates an operation new feature
186   /// \param theFlushMessage the flag whether the create message should be flushed
187   /// \returns the created feature
188   virtual FeaturePtr createFeature(const bool theFlushMessage = true);
189
190   /// Removes the preselection information and clears the map of preselection
191   void clearPreselection();
192
193  protected:
194    /// The operation feature to be handled
195   FeaturePtr myFeature;
196
197   /// a list of hidden objects, whic are diplayed by operation start
198   /// and should be hidden by operation stop
199   std::set<ObjectPtr> myVisualizedObjects;
200
201   /// Editing feature flag
202   bool myIsEditing;
203
204   /// State used only if the operation should not be commited
205   bool myNeedToBeAborted;
206
207   /// List of pre-selected object
208   QList<std::shared_ptr<ModuleBase_ViewerPrs>> myPreSelection;
209
210   /// If the operation works with feature which is sub-feature of another one
211   /// then this variable has to be initialised by parent feature
212   /// before operation feature creating
213   CompositeFeaturePtr myParentFeature;
214
215   /// Last current feature before editing operation. It is cashed when Edit operation is started
216   /// in order to restore the document current feature on commit/abort this operation.
217   FeaturePtr myPreviousCurrentFeature;
218
219   bool myRestartTransactionOnResume;
220 };
221
222 #endif