Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / PartSet / PartSet_SketcherReentrantMgr.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 PartSet_SketcherReentrantMgr_H
22 #define PartSet_SketcherReentrantMgr_H
23
24 #include "PartSet.h"
25
26 #include <ModelAPI_Feature.h>
27
28 #include <string>
29 #include <memory>
30
31 #include <QObject>
32
33 class ModuleBase_IWorkshop;
34 class ModuleBase_Operation;
35 class ModuleBase_ModelWidget;
36 class ModuleBase_IViewWindow;
37
38 class ModelAPI_CompositeFeature;
39
40 class QMouseEvent;
41
42 class XGUI_Workshop;
43 class PartSet_Module;
44 class ModuleBase_ViewerPrs;
45 class Events_Message;
46 class ModelAPI_Attribute;
47 class GeomAPI_Pnt2d;
48
49 /// \ingroup PartSet_SketcherReentrantMgr
50 /// It provides reentrant create operations in sketch, that is when all inputs are valid,
51 /// automatic validation of the creation and switch the created entity to edit mode
52 /// ('internal' edit operation), with the ability to simultaneously create the next entity
53 /// of same type (re-entrance of the operation).
54 /// OK valids the current edition and exits from the operation (no re-entrance).
55 /// Cancel removes (undo) the entity currently edited and
56 /// exits from the operation (no re-entrance).
57 class PARTSET_EXPORT PartSet_SketcherReentrantMgr : public QObject
58 {
59 Q_OBJECT
60
61 /// Enumeration to specify the restart operation properties.
62 enum RestartingMode {
63   RM_None, /// the operation should not be restarted
64   RM_Forbided, /// the operation should not be restarted after there is no active widget
65   RM_LastFeatureUsed, /// the operation is restarted and use
66                       /// the previous feature for own initialization
67   RM_EmptyFeatureUsed /// the operation is restarted and does not use the previous feature
68 };
69
70 public:
71   /// Constructor
72   /// \param theWorkshop a workshop instance
73   PartSet_SketcherReentrantMgr(ModuleBase_IWorkshop* theWorkshop);
74   virtual ~PartSet_SketcherReentrantMgr();
75
76 public:
77   /// Returns a first widget of the current opeation if the internal edit operation is active
78   /// or return null. If the current widget of the operation is a viewer selector, it returns null.
79   ModuleBase_ModelWidget* internalActiveWidget() const;
80
81   /// Return true if the current edit operation is an internal
82   bool isInternalEditActive() const;
83
84   /// Stop internal edit if the operation feature is invalid
85   void updateInternalEditActiveState();
86
87   /// if the internal flags allow it and the manager is active, it starts an internal edit operation
88   /// for the created operation.
89   /// \param thePreviousAttributeID an index of the previous active attribute
90   //bool restartOperation(const std::string& thePreviousAttributeID);
91   bool processEnter(const std::string& thePreviousAttributeID);
92
93   /// Resets the internal flags
94   /// \param theOperation a started operation
95   void operationStarted(ModuleBase_Operation* theOperation);
96
97   /// Resets the internal flags
98   /// \param theOperation a started operation
99   /// \return state whether the internal edit operation was active
100   bool operationCommitted(ModuleBase_Operation* theOperation);
101
102   /// Resets the internal flags
103   /// \param theOperation a started operation
104   void operationAborted(ModuleBase_Operation* theOperation);
105
106   /// Return true if the manager processes the mouse move event
107   /// It happens if the current operation is an internal edit operation and the first
108   /// control can be filled by the mouse move event. The operation is restarted.
109   /// \return true if operation is committed.
110   bool processMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent);
111
112   /// Return true if the manager processes the mouse press event
113   /// \return true if the current operation is an internal edit operation.
114   bool processMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent);
115
116   /// Return true if the manager processes the mouse enter event
117   /// It happens if the current operation is an internal edit operation.
118   /// The operation is restarted. If the first widget of the started operation is
119   /// the point 2d, it processes this mouse event
120   /// \return true if operation is committed.
121   bool processMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent);
122
123   /// It is called by the current operation filling with the preselection.
124   /// Returns false if the reentrant mode of the operation is not empty.
125   bool canBeCommittedByPreselection();
126
127   /// Fills reentrant message during restarting operation
128   /// \param theMessage reentrant message
129   void setReentrantMessage(const std::shared_ptr<Events_Message>& theMessage)
130   { myReentrantMessage = theMessage; }
131
132   /// Returnss reentrant message
133   std::shared_ptr<Events_Message> reentrantMessage() const { return myReentrantMessage; }
134
135   /// Put current selection into reentrant message
136   /// \param theMessage a message of reentrant operation
137   void setReentrantPreSelection(const std::shared_ptr<Events_Message>& theMessage);
138
139 private slots:
140   /// SLOT, that is called by a widget activating in the property panel
141   /// If the 'internal' edit operation is started, it activates the first widget selection
142   void onWidgetActivated();
143
144   /// SLOT, that is called by no more widget signal emitted by property panel
145   /// Start an internal edit operation or, if the internal flag is forbided, commits
146   /// the current operation
147   /// \param thePreviousAttributeID an index of the previous active attribute
148   void onNoMoreWidgets(const std::string& thePreviousAttributeID);
149
150   /// Processing of vertex selected. Set an internal reentrant flag to forbiddent state if
151   /// the current feature is a line and there are not obligate widgets anymore
152   void onVertexSelected();
153
154   /// Listens to the signal about the modification of the values
155   /// have been done in the property panel. If the manager has active edit operation and
156   /// the active widget does not process the modification of value, the manager will
157   /// restart current operation and fill a new feature attribute by the value of current
158   /// widget
159   void onAfterValuesChangedInPropertyPanel();
160
161   /// Deactivates selection and filters of the first operation widget if it is an internal
162   /// 'edit' operation
163   void onBeforeStopped();
164
165 private:
166   /// Returns true if the current operation is a sketch or a nested sketch operation
167   bool isActiveMgr() const;
168
169   /// Sets the focus to the last control of the property panel and activates selection
170   /// of the first widget to can select first value of the next create operation
171   /// \param thePreviousAttributeID an index of the previous attribute to set focus to this widget
172   /// \return true if it is started
173   bool startInternalEdit(const std::string& thePreviousAttributeID);
174
175   /// Disconnects this manager from operation signals, deactivate selection of the first control
176   /// in the viewer.
177   void beforeStopInternalEdit();
178
179   /// Commits the current operation and launches a new with the commited operation feature index
180   void restartOperation();
181
182   /// Creates an internal feature and controls to process it
183   void createInternalFeature();
184
185   /// A pair method for an internal creation to remove it and clear all created controls
186   void deleteInternalFeature();
187
188   /// Breaks sequense of automatically resterted operations
189   void resetFlags();
190
191   /// Copy some feature specific attributes from the source to a new feature
192   /// This is type for Circle and Arc features
193   /// \param theSourceFeature a source feature
194   /// \param theNewFeature a new feature
195   /// \param theSketch an active sketch
196   /// \param isTemporary is used to do not create additional features(e.g. coicidence for line)
197   /// \return true is something is copied
198   static bool copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
199                                       const FeaturePtr& theNewFeature,
200                                       const std::shared_ptr<ModelAPI_CompositeFeature>& theSketch,
201                                       const bool isTemporary = false);
202
203   /// Checks whethe the feature of the given operation has kind an arc and the arc type is tangent
204   bool isTangentArc(ModuleBase_Operation* theOperation,
205                     const std::shared_ptr<ModelAPI_CompositeFeature>& /*theSketch*/) const;
206
207   /// Accept All action is enabled if an internal edit is started.
208   /// It updates the state of the button
209   void updateAcceptAllAction();
210
211   /// Returns the workshop
212   XGUI_Workshop* workshop() const;
213
214   /// Returns the workshop module
215   PartSet_Module* module() const;
216
217 private:
218   ModuleBase_IWorkshop* myWorkshop; /// the workshop
219
220   RestartingMode myRestartingMode;  /// automatical restarting mode flag
221   bool myIsFlagsBlocked; /// true when reset of flags should not be perfromed
222   bool myIsInternalEditOperation; /// true when the 'internal' edit is started
223
224   FeaturePtr myPreviousFeature; /// feature of the previous operation, which is restarted
225   FeaturePtr myInternalFeature;
226   QWidget* myInternalWidget;
227   ModuleBase_ModelWidget* myInternalActiveWidget;
228   std::string myNoMoreWidgetsAttribute;
229
230   std::shared_ptr<Events_Message> myReentrantMessage; /// message obtained by operation restart
231   ObjectPtr mySelectedObject; /// cashed selected object
232   std::shared_ptr<ModelAPI_Attribute> mySelectedAttribute; /// cashed selected attribute
233   std::shared_ptr<GeomAPI_Pnt2d> myClickedSketchPoint; /// cashed clicked point
234 };
235
236 #endif