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