Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #ifndef PartSet_SketcherMgr_H
21 #define PartSet_SketcherMgr_H
22
23 #include "PartSet.h"
24
25 #include "PartSet_Filters.h"
26 #include "PartSet_Tools.h"
27
28 #include <ModelAPI_Feature.h>
29 #include <ModelAPI_Attribute.h>
30 #include <ModelAPI_CompositeFeature.h>
31 #include <ModelAPI_Result.h>
32
33 #include <ModuleBase_ViewerFilters.h>
34 #include <ModuleBase_Definitions.h>
35 #include <ModuleBase_ModelWidget.h>
36
37 #include <GeomAPI_Pln.h>
38 #include <SelectMgr_IndexedMapOfOwner.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopTools_MapOfShape.hxx>
41
42 #include <QObject>
43 #include <QList>
44 #include <QMap>
45
46 class PartSet_Module;
47 class ModuleBase_IViewWindow;
48 class ModuleBase_ModelWidget;
49 class ModuleBase_Operation;
50 class XGUI_OperationMgr;
51 class XGUI_Workshop;
52
53 class AIS_InteractiveObject;
54
55 class QMouseEvent;
56
57 /**
58 * \ingroup Modules
59 * A class for management of sketch operations
60   At the time of the sketcher operation active, only the sketch sub-feature results are
61   displayed in the viewer. After the sketch create/edit operation is finished, the sub-feature
62   are hidden, the sketch feature result is displayed
63 */
64 class PARTSET_EXPORT PartSet_SketcherMgr : public QObject
65 {
66   Q_OBJECT
67   /// Struct to define gp point, with the state is the point is initialized
68   struct Point
69   {
70     /// Constructor
71     Point()
72     {
73       myIsInitialized = false;
74     }
75     /// Destructor
76     ~Point()
77     {
78     }
79
80     /// clear the initialized flag.
81     void clear()
82     {
83       myIsInitialized = false;
84     }
85     /// set the point and switch on the initialized flag
86     /// \param thePoint the point
87     void setValue(const double theX, const double theY)
88     {
89       myIsInitialized = true;
90       myCurX = theX;
91       myCurY = theY;
92     }
93
94     bool myIsInitialized;  /// the state whether the point is set
95     double myCurX, myCurY; /// the point coordinates
96   };
97
98 public:
99   /// Struct to define selection model information to store/restore selection
100   struct SelectionInfo
101   {
102     std::set<AttributePtr> myAttributes; /// the selected attributes
103     std::set<ResultPtr> myResults; /// the selected results
104     TopoDS_Shape myFirstResultShape; /// the first shape of feature result
105     TopTools_MapOfShape myLocalSelectedShapes; /// shapes of local selection
106   };
107   typedef QMap<FeaturePtr, SelectionInfo> FeatureToSelectionMap;
108
109 public:
110   /// Constructor
111   /// \param theModule a pointer to PartSet module
112   PartSet_SketcherMgr(PartSet_Module* theModule);
113
114   virtual ~PartSet_SketcherMgr();
115
116   /// Returns true if the operation is the sketch
117   /// \param theOperation an operation
118   /// \return the boolean result
119   static bool isSketchOperation(ModuleBase_Operation* theOperation);
120
121   /// Returns true if the operation feature belongs to list of granted features of Sketch
122   /// operation. An operation of a sketch should be started before.
123   /// \param theOperation an operation
124   /// \return the boolean result
125   bool isNestedSketchOperation(ModuleBase_Operation* theOperation) const;
126
127   /// Returns true if the feature kind belongs to list of granted features of Sketch
128   /// operation. An operation of a sketch should be started before.
129   /// \param theOperation an operation
130   /// \return the boolean result
131   bool isNestedSketchFeature(const QString& theFeatureKind) const;
132
133   /// Returns true if the operation is a create and nested sketch operationn
134   /// \param theOperation a checked operation
135   /// \param theSketch a sketch feature
136   //// \return boolean value
137   bool isNestedCreateOperation(ModuleBase_Operation* theOperation,
138                                       const CompositeFeaturePtr& /*theSketch*/) const;
139
140   /// Returns true if the operation is an edit nested feature one
141   /// \param theOperation a checked operation
142   //// \return boolean value
143   bool isNestedEditOperation(ModuleBase_Operation* theOperation,
144                                     const CompositeFeaturePtr& /*theSketch*/) const;
145
146   /// Returns whether the current operation is a sketch entity - line, point, arc or circle
147   /// \param theId is an id of object
148   /// \return a boolean value
149   static bool isEntity(const std::string& theId);
150
151   /// Returns whether the feature has external attribute filled with 'true' value
152   /// \param theFeature a feature object
153   /// \return a boolean value
154   static bool isExternalFeature(const FeaturePtr& theFeature);
155
156   /// Returns whether the current operation is a sketch distance - lenght, distance or radius
157   /// \param theOperation the operation
158   /// \return a boolean value
159   static bool isDistanceOperation(ModuleBase_Operation* theOperation);
160
161   /// Returns whether the feature kind is a sketch distance - lenght, distance or radius
162   /// \param theKind the feature kind
163   /// \return a boolean value
164   static bool isDistanceKind(std::string& theKind);
165
166   /// Returns true if a mouse cursor is over viewer window
167   bool isMouseOverWindow() { return myIsMouseOverWindow; }
168
169   /// Returns current Sketch feature/ Returns NULL if there is no launched sketch operation
170   CompositeFeaturePtr activeSketch() const { return myCurrentSketch; }
171
172   /// Starts sketch operation
173   void startSketch(ModuleBase_Operation* );
174
175   /// Stops sketch operation
176   void stopSketch(ModuleBase_Operation* );
177
178   /// Starts sketch operation, connects to the opeation property panel
179   /// \param theOperation a committed operation
180   void startNestedSketch(ModuleBase_Operation* theOperation);
181
182   /// Stop sketch operation, disconnects from the opeation property panel
183   /// \param theOperation a stopped operation
184   void stopNestedSketch(ModuleBase_Operation* theOperation);
185
186   /// Visualizes the operation feature if it is a creation nested feature operation
187   /// \param theOperation a committed operation
188   void commitNestedSketch(ModuleBase_Operation* theOperation);
189
190   /// Append the sketch plane filter into the current viewer
191   /// \param toActivate state whether the filter should be activated/deactivated
192   void activatePlaneFilter(const bool& toActivate);
193
194   /// Commit the operation if it is possible. If the operation is dimention constraint,
195   /// it gives widget editor to input dimention value
196   /// \return true if the operation is stopped after activation
197   bool operationActivatedByPreselection();
198
199   /// Returns True if there are available Undos and the sketch manager allows undo
200   /// \return the boolean result
201   bool canUndo() const;
202
203   //! Returns True if there are available Redos and the sketch manager allows redo
204   /// \return the boolean result
205   bool canRedo() const;
206
207   /// Returns False only if the sketch creating feature can not be visualized.
208   /// \return a boolean value
209   //bool canCommitOperation() const;
210
211   /// Returns whether the object can be erased at the bounds of the active operation.
212   /// Sketch sub-entities can not be erased during the sketch operation
213   /// \param theObject a model object
214   bool canEraseObject(const ObjectPtr& theObject) const;
215
216   /// Returns whether the object can be displayed at the bounds of the active operation.
217   /// Display only current operation results for usual operation and ask the sketcher manager
218   /// if it is a sketch operation
219   /// \param theObject a model object
220   bool canDisplayObject(const ObjectPtr& theObject) const;
221
222   /// Returns whether the constraint object can be displayed. It depends on the sketch check
223   /// box states
224   /// \param theObject a model object
225   /// \param theState the constraint visible state state to be checked
226   /// \param isProcessed an output parameter if it is processed
227   /// \return result value
228   bool canDisplayConstraint(const FeaturePtr& theObject,
229                             const PartSet_Tools::ConstraintVisibleState& theState,
230                             bool& isProcessed) const;
231
232   /// Check the given objects either there are some results of the current sketch. If so,
233   /// it suggests to delete them as there are no functionality to show back hidden sketch objects
234   /// \param theObjects a list of hidden objects
235   //virtual void processHiddenObject(const std::list<ObjectPtr>& theObjects);
236
237   /// Returns true if the mouse is over viewer or property panel value is changed
238   /// \return boolean result
239   bool canDisplayCurrentCreatedFeature() const;
240
241   /// Returns true if the current operation is nested creation or internal reentrant edit
242   /// \param theOperation an operation
243   bool canChangeCursor(ModuleBase_Operation* theOperation) const;
244
245   /// Returns state of constraints showing flag
246   const QMap<PartSet_Tools::ConstraintVisibleState, bool>& showConstraintStates();
247
248   /// Returns true if the object is a current sketch sub feature of a result of the feature
249   /// \param theObject an object
250   /// \return boolean value
251   bool isObjectOfSketch(const ObjectPtr& theObject) const;
252
253   /// Enumeration for selection mode used
254   enum SelectionType {
255     ST_HighlightType, /// Only highlighted objects
256     ST_SelectType, /// Only selected objects
257     ST_SelectAndHighlightType /// Both, highlighted and selected objects
258   };
259
260   /// Saves the current selection in the viewer into an internal container
261   /// It obtains the selected attributes.
262   /// The highlighted objects can be processes as the selected ones
263   /// \param theHighlightedOnly a boolean flag
264   /// \param theCurrentSelection a container filled by the current selection
265   void storeSelection(const SelectionType theType, FeatureToSelectionMap& theCurrentSelection);
266
267   /// Restores previously saved selection state
268   /// \param theCurrentSelection a container filled by the current selection
269   void restoreSelection(FeatureToSelectionMap& theCurrentSelection);
270
271   /// Return error state of the sketch feature, true if the error has happened
272   /// \return boolean value
273   bool sketchSolverError();
274
275   //! Returns the feature error if the current state of the feature in the sketch is not correct
276   //! If the feature is correct, it returns an empty value
277   //! Incorrect states: the feature is sketch, the solver error value
278   //! The feature value is reset, this is the flag of sketch mgr
279   //! \return string value
280   QString getFeatureError(const FeaturePtr& theFeature);
281
282   /// It nullify internal flags concerned to clicked mouse event
283   void clearClickedFlags();
284
285   /// Returns list of strings which contains id's of sketch replication operations
286   static const QStringList& replicationsIdList();
287
288   /// Returns list of strings which contains id's of constraints operations
289   static const QStringList& constraintsIdList();
290
291   /// Returns a list of modes, where the AIS objects should be activated
292   /// \param theModes a list of modes
293   static void sketchSelectionModes(QIntList& theModes);
294
295   /// Create specific for the module presentation
296   /// \param theResult an object for presentation
297   /// \return created presentation or NULL(default value)
298   virtual Handle(AIS_InteractiveObject) createPresentation(const ResultPtr& theResult);
299
300   /// Connects or disconnects to the value changed signal of the property panel widgets
301   /// \param theWidget a property contol widget
302   /// \param isToConnect a boolean value whether connect or disconnect
303   void connectToPropertyPanel(ModuleBase_ModelWidget* theWidget, const bool isToConnect);
304
305   /// Visualize the operation feature if the previous state is modified value in property panel
306   /// \param thePreviousState the previous widget value state
307   void widgetStateChanged(int thePreviousState);
308
309   /// If the current operation is a dimention one, the style of dimension visualization is send for
310   /// the current object
311   /// \param theObject an object to be customized
312   void customizePresentation(const ObjectPtr& theObject);
313
314   /// Update sketch presentations according to the the state
315   /// \param theType a type of sketch visualization style
316   /// \param theState a boolean state
317   void updateBySketchParameters(const PartSet_Tools::ConstraintVisibleState& theType,
318                                 bool theState);
319
320 public slots:
321   /// Process sketch plane selected event
322   void onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln);
323
324 private slots:
325   /// Toggle show constraints
326   void onShowConstraintsToggle(int theType, bool theState);
327   /// Process the enter mouse to the view port. If the current operation is a create of
328   /// a nested sketch feature, it updates internal flags to display the feature on mouse move
329   void onEnterViewPort();
330   /// Process the leave mouse of the view port. If the current operation is a create of
331   /// a nested sketch feature, it hides the feature in the viewer
332   void onLeaveViewPort();
333   /// Listens to the value changed signal and display the current operation feature
334   //void onBeforeValuesChangedInPropertyPanel();
335   /// Listens to the signal about the modification of the values
336   /// have been done in the property panel
337   //void onAfterValuesChangedInPropertyPanel();
338
339   void onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*);
340   void onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*);
341   void onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*);
342   void onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*);
343   void onApplicationStarted();
344   //void onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget);
345
346   void onBeforeContextMenu();
347   void onAfterContextMenu();
348
349 private:
350   /// Launches the operation from current highlighting
351   void launchEditing();
352
353   /// Converts mouse position to 2d coordinates.
354   /// Member myCurrentSketch has to be correctly defined
355   void get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent,
356                   Point& thePoint);
357
358   /// Show distance value editor if it is a distance operation and all attribute references
359   /// are filled by preseletion
360   /// \return true if the value is accepted
361   static bool setDistanceValueByPreselection(ModuleBase_Operation* theOperation,
362                                              ModuleBase_IWorkshop* theWorkshop,
363                                              bool& theCanCommitOperation);
364
365   /// Applyes the current selection to the object in the workshop viewer
366   /// It includes the selection in all modes of activation, even local context - vertexes, edges
367   /// It gets all results of the feature, find an AIS object in the viewer and takes all BRep
368   /// selection owners. If the owner is vertex, the corresponded attribute is seached in
369   /// the feature and if it is in the container of selected attributes, the owner is put in the
370   /// out container. If the owner is edge and the current result is in the container of selected
371   /// results, the owner is put in the out container.
372   /// \param theFeature a feature or result object
373   /// \param theSketch a current sketch feature
374   /// \param theWorkshop a workshop to have an access to AIS context and displayer
375   /// \param theSelection a container of the selection, it has results and attributres for a feature
376   /// \param theOwnersToSelect an out container of found owners
377   static void getSelectionOwners(const FeaturePtr& theFeature,
378                                   const FeaturePtr& theSketch,
379                                   ModuleBase_IWorkshop* theWorkshop,
380                                   const FeatureToSelectionMap& theSelection,
381                                   SelectMgr_IndexedMapOfOwner& theOwnersToSelect);
382
383   /// Returns true if the created feature is visible
384   /// \param
385   bool isVisibleCreatedFeature() const;
386
387   /// Returns a current operation
388   /// \return an operation
389   ModuleBase_Operation* getCurrentOperation() const;
390
391   /// Get the active widget in the property panel
392   ModuleBase_ModelWidget* getActiveWidget() const;
393
394   /// Erase or display the feature of the current operation. If the mouse over the active view or
395   /// a current value is changed by property panel, the feature is displayed otherwise it is hidden
396   /// \param theOperation an operation which feature is to be displayed,
397   ///                     it is nested create operation
398   /// \param isToDisplay a flag about the display or erase the feature
399   void visualizeFeature(const FeaturePtr& theFeature, const bool isEditOperation,
400                         const bool isToDisplay, const bool isFlushRedisplay = true);
401
402 private:
403   /// Updates selection priority of the presentation
404   /// \param theObject object to find a presentation which will be corrected
405   /// \param theFeature a feature of the presentation
406   void updateSelectionPriority(ObjectPtr theObject, FeaturePtr theFeature);
407   /// Returns current workshop
408   XGUI_Workshop* workshop() const;
409   /// Returns operation manager
410   XGUI_OperationMgr* operationMgr() const;
411
412 private:
413   PartSet_Module* myModule;
414
415   bool myPreviousDrawModeEnabled; // the previous selection enabled state in the viewer
416   bool myIsEditLaunching;
417   bool myIsDragging;
418   bool myDragDone;
419   bool myIsMouseOverWindow; /// the state that the mouse over the view
420   /// the state whether the over view state is processed by mouseMove method
421   bool myIsMouseOverViewProcessed;
422   bool myIsPopupMenuActive; /// the state of the popup menu is shown
423   Point myCurrentPoint;
424   //Point myClickedPoint;
425
426   CompositeFeaturePtr myCurrentSketch;
427
428   Handle(PartSet_CirclePointFilter) myCirclePointFilter;
429   Handle(ModuleBase_ShapeInPlaneFilter) myPlaneFilter;
430   FeatureToSelectionMap myCurrentSelection;
431   bool myPreviousUpdateViewerEnabled;
432
433   QMap<PartSet_Tools::ConstraintVisibleState, bool> myIsConstraintsShown;
434 };
435
436
437 #endif