0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / tools / View / View_Widget.hxx
1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement. 
15
16 #ifndef View_View_H
17 #define View_View_H
18
19 #include <AIS_InteractiveContext.hxx>
20 #include <AIS_ViewController.hxx>
21 #include <Aspect_VKeyFlags.hxx>
22 #include <V3d_View.hxx>
23 #include <inspector/View_ViewActionType.hxx>
24 #include <inspector/View_Viewer.hxx>
25
26 #include <Standard_WarningsDisable.hxx>
27 #include <QAction>
28 #include <QMap>
29 #include <QString>
30 #include <QToolButton>
31 #include <QWidget>
32 #include <Standard_WarningsRestore.hxx>
33
34 class View_Viewer;
35
36 //! \class View_Widget
37 //! \brief It is a Qt control that visualizes content of OCCT 3D view
38 //! It creates control and actions of manipulating of this view,
39 //! emits signal about selection happening in the view and signal about display mode change and
40 //! changes application cursor depending on an active action.
41 class View_Widget : public QWidget
42 {
43   Q_OBJECT
44
45 public:
46   //! Constructor
47   Standard_EXPORT View_Widget (QWidget* theParent,
48                                const Handle(AIS_InteractiveContext)& theContext,
49                                const bool isFitAllActive);
50
51   //! Destructor
52   virtual ~View_Widget() {}
53
54   //! Returns current viewer
55   View_Viewer* GetViewer() const { return myViewer; }
56
57   //! Sets default size that is used in sizeHint when the widget is firstly show
58   Standard_EXPORT void SetPredefinedSize (int theDefaultWidth, int theDefaultHeight);
59
60   //! Creates V3d view and set Qt control for it
61   Standard_EXPORT void Init();
62
63   //! Returns an action for the given action type
64   //! \param theActionId an action index
65   QAction* ViewAction (const View_ViewActionType theActionId) const { return myViewActions[theActionId]; };
66
67   //! Returns an action widget if exist. Implemented for fit all widget.
68   //! \param theActionId an action index
69   QWidget* GetWidget (const View_ViewActionType theActionId) const { return theActionId == View_ViewActionType_FitAllId ? myFitAllAction : 0; };
70
71   //! \returns 0 - AIS_WireFrame, 1 - AIS_Shaded
72   Standard_EXPORT int DisplayMode() const;
73
74   //! Sets display mode: 0 - AIS_WireFrame, 1 - AIS_Shaded
75   Standard_EXPORT void SetDisplayMode (const int theMode);
76
77   //! Sets enable/disable view and tool bar actions depending on the parameter
78   //! \param theIsEnabled boolean value
79   Standard_EXPORT void SetEnabledView (const bool theIsEnabled);
80
81   //! Returns true if action is checked. It processes fit all action only.
82   //! \param theIsEnabled boolean value
83   bool IsActionChecked (const View_ViewActionType theActionId)
84     { return theActionId == View_ViewActionType_FitAllId && myFitAllAction->isChecked(); }
85
86   //! Sets checked fit all action. Double click on fit all action set the action checked automatically
87   //! \param theIsEnabled boolean value
88   void SetActionChecked (const View_ViewActionType theActionId, const bool isChecked)
89   { if (theActionId == View_ViewActionType_FitAllId) myFitAllAction->setChecked(isChecked); }
90
91   //! Sets initial camera position
92   //! \param theVx direction on Ox
93   //! \param theVy direction on Oy
94   //! \param theVz direction on Oz
95   void SetInitProj (const Standard_Real theVx, const Standard_Real theVy, const Standard_Real theVz)
96   { myHasInitProj = Standard_True; myInitVx = theVx; myInitVy = theVy; myInitVz = theVz; }
97
98   //! Returns paint engine for the OpenGL viewer. Avoid default execution of Qt Widget.
99   virtual QPaintEngine* paintEngine() const Standard_OVERRIDE { return 0; }
100
101   //! Return the recommended size for view. If default size exists, it returns the default size
102   Standard_EXPORT virtual QSize sizeHint() const Standard_OVERRIDE;
103
104   //! Saves state of widget actions
105   //! \param theParameters a view instance
106   //! \param theItems [out] properties
107   //! \param thePrefix preference item prefix
108   Standard_EXPORT static void SaveState (View_Widget* theWidget,
109                                          QMap<QString, QString>& theItems,
110                                          const QString& thePrefix = QString());
111
112   //! Restores state of widget actions
113   //! \param theParameters a view instance
114   //! \param theKey property key
115   //! \param theValue property value
116   //! \param thePrefix preference item prefix
117   //! \return boolean value whether the property is applied to the tree view
118   Standard_EXPORT static bool RestoreState (View_Widget* theWidget,
119                                             const QString& theKey, const QString& theValue,
120                                             const QString& thePrefix = QString());
121
122 signals:
123
124   //! Sends a signal about selection change if the left mouse button is pressed and current action does not process it
125   void selectionChanged();
126
127   //! Sends a signal about display mode change
128   void displayModeClicked();
129
130   //! Sends a signal about checked state is changed
131   //! \param theActionId an action index
132   //! \param theState the checked state
133   void checkedStateChanged (const int theActionId, bool theState);
134
135 public slots:
136
137   //! Fits all the V3d view and redraw view
138   void OnFitAll() { myViewer->GetView()->FitAll(); }
139
140   //! Updates states of widget actions
141   //! 
142   //! - if the state is checked, uncheck all other actions
143   Standard_EXPORT void onCheckedStateChanged (bool isOn);
144
145 protected:
146
147   //! Avoids Qt standard execution of this method, redraw V3d view
148   //! \param an event
149   virtual void paintEvent (QPaintEvent* theEvent) Standard_OVERRIDE;
150
151   //! Avoids Qt standard execution of this method, do mustBeResized for V3d view, Init view if it is the first call
152   //! \param an event
153   virtual void resizeEvent (QResizeEvent* theEvent) Standard_OVERRIDE;
154
155   //! Left, Middle, Right button processing
156   //! \param an event
157   virtual void mousePressEvent (QMouseEvent* theEvent) Standard_OVERRIDE;
158
159   //! Left, Middle, Right button processing
160   //! \param an event
161   virtual void mouseReleaseEvent (QMouseEvent* theEvent) Standard_OVERRIDE;
162
163   //! Left, Middle, Right button processing
164   //! \param an event
165   virtual void mouseMoveEvent (QMouseEvent* theEvent) Standard_OVERRIDE;
166
167 protected:
168
169   //! Creates view actions and fills an internal map
170   void initViewActions();
171
172   //! Empty: template to create popup menu
173   //! \param theX a horizontal position of mouse event
174   //! \param theX a vertical position of mouse event
175   void popup (const Standard_Integer theX, const Standard_Integer theY) { (void)theX; (void)theY; }
176
177 private:
178   //! Creates action and stores it in a map of actions
179   //! \param theActionId an identifier of action in internal map
180   //! \param theIcon an icon name and place according to qrc resource file, e.g. ":/icons/view_fitall.png"
181   //! \param theText an action text
182   //! \param theToolBar a tool bar action text
183   //! \param theStatusBar a status bar action text
184   void createAction (const View_ViewActionType theActionId, const QString& theIcon, const QString& theText,
185                      const char* theSlot, const bool isCheckable = false,
186                      const QString& theToolBar = QString(), const QString& theStatusBar = QString());
187
188 private:
189   //! Converts Qt modifier key to Aspect key flag
190   //! \param theModifierId the event modifier
191   static Aspect_VKeyFlags keyFlag (const int theModifierId);
192
193   //! Converts Qt button key to Aspect key mouse
194   //! \param theButtonId the event button
195   static Aspect_VKeyMouse keyMouse (const int theButtonId);
196
197 private:
198
199   View_Viewer* myViewer; //!< connector to context, V3d viewer and V3d View
200   AIS_ViewController* myController; //!< controller to process view actions
201
202   QToolButton* myFitAllAction; //!< widget for fit all, processed double click to perform action automatically
203   QMap<View_ViewActionType, QAction*> myViewActions; //!< tool bar view actions
204
205   Standard_Boolean myFirst; //!< flag to Init view by the first resize/paint call
206   Standard_Integer myDefaultWidth; //!< default width for the first sizeHint
207   Standard_Integer myDefaultHeight; //!< default height for the first sizeHint
208   Standard_Boolean myViewIsEnabled; //!< flag if the view and tool bar view actions are enabled/disabled
209
210   Standard_Boolean myHasInitProj; //!< is initial camera position defined
211   Standard_Real myInitVx; //!< initial camera position on X
212   Standard_Real myInitVy; //!< initial camera position on Y
213   Standard_Real myInitVz; //!< initial camera position on Z
214 };
215
216 #endif