1 // Created on: 2017-06-16
2 // Created by: Natalia ERMOLAEVA
3 // Copyright (c) 2017 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
19 #include <AIS_InteractiveContext.hxx>
20 #include <V3d_View.hxx>
21 #include <inspector/View_ViewActionType.hxx>
22 #include <inspector/View_Viewer.hxx>
24 #include <Standard_WarningsDisable.hxx>
29 #include <Standard_WarningsRestore.hxx>
35 //! \class View_Widget
36 //! \brief It is a Qt control that visualizes content of OCCT 3D view
37 //! It creates control and actions of manipulating of this view,
38 //! emits signal about selection happenning in the view and signal about display mode change and
39 //! changes application cursor depending on an active action.
40 class View_Widget : public QWidget
45 //! Enumeration defines manipulating actions of the widget
46 enum View_CurrentAction3d
48 View_CurrentAction3d_Nothing, //!< Empty action
49 View_CurrentAction3d_DynamicZooming, //!< Zoom action
50 View_CurrentAction3d_WindowZooming, //!< Zoom action using rectangle
51 View_CurrentAction3d_DynamicPanning, //!< Panning action
52 View_CurrentAction3d_DynamicRotation //!< Rotation action
55 //! Enumeration defines cursor kind
58 View_CursorMode_DefaultCursor, //!< default Qt cursor
59 View_CursorMode_HandCursor, //!< hand cursor
60 View_CursorMode_PanCursor, //!< panning cursor
61 View_CursorMode_ZoomCursor, //!< zoom cursor
62 View_CursorMode_RotationCursor //!< onRotate cursor
65 //! Enumeration defines drag mode
68 View_DragMode_ButtonDown, // theState == -1 button down
69 View_DragMode_ButtonMove, // theState == 0 move
70 View_DragMode_ButtonUp // theState == 1 button up
76 Standard_EXPORT View_Widget (QWidget* theParent);
79 virtual ~View_Widget() {}
81 //! Returns current viewer
82 View_Viewer* GetViewer() const { return myViewer; }
84 //! Sets default size that is used in sizeHint when the widget is firstly show
85 Standard_EXPORT void SetPredefinedSize (int theDefaultWidth, int theDefaultHeight);
87 //! Creates V3d view and set Qt control for it
88 Standard_EXPORT void Init();
90 //! Returns an action for the given action type
91 //! \param theActionId an action indes
92 QAction* GetViewAction (const View_ViewActionType theActionId) const { return myViewActions[theActionId]; };
94 //! \returns 0 - AIS_WireFrame, 1 - AIS_Shaded
95 Standard_EXPORT int GetDisplayMode() const;
97 //! Enable/disable view and tool bar actions depending on the parameter
98 //! \param theIsEnabled boolean value
99 Standard_EXPORT void SetEnabledView (const bool theIsEnabled);
101 //! Get paint engine for the OpenGL viewer. Avoid default execution of Qt Widget.
102 virtual QPaintEngine* paintEngine() const Standard_OVERRIDE { return 0; }
104 //! Recommended size for view. If default size exists, it returns the default size
105 Standard_EXPORT virtual QSize sizeHint() const Standard_OVERRIDE;
109 //! Sends a signal about selection change if the left mouse button is pressed and current action does not process it
110 void selectionChanged();
112 //! Sends a signal about display mode change
113 void displayModeClicked();
117 //! Fits all the V3d view and redraw view
118 void OnFitAll() { myViewer->GetView()->FitAll(); }
120 //! Stores state about fitting all to use it by the mouse move
121 void OnFitArea() { myCurrentMode = View_CurrentAction3d_WindowZooming; }
123 //! Stores state about zoom to use it by the mouse move
124 void OnZoom() { myCurrentMode = View_CurrentAction3d_DynamicZooming; }
126 //! Stores state about pan to use it by the mouse move
127 void OnPan() { myCurrentMode = View_CurrentAction3d_DynamicPanning; }
129 //! Stores state about onRotate to use it by the mouse move
130 void OnRotate() { myCurrentMode = View_CurrentAction3d_DynamicRotation; }
132 //! Updates states of tool actions:
133 //! - if the action is display mode, it changes an icon for action(wireframe or shading)
134 //! - if the state is checked, uncheck all other actions
135 //! \param isOn boolean value about check
136 Standard_EXPORT void OnUpdateToggled (bool isOn);
140 //! Avoid Qt standard execution of this method, redraw V3d view
142 virtual void paintEvent (QPaintEvent* theEvent) Standard_OVERRIDE;
144 //! Avoid Qt standard execution of this method, do mustBeResized for V3d view, Init view if it is the first call
146 virtual void resizeEvent (QResizeEvent* theEvent) Standard_OVERRIDE;
148 //! Left, Middle, Right button processing
150 virtual void mousePressEvent (QMouseEvent* theEvent) Standard_OVERRIDE;
152 //! Left, Middle, Right button processing
154 virtual void mouseReleaseEvent (QMouseEvent* theEvent) Standard_OVERRIDE;
156 //! Left, Middle, Right button processing
158 virtual void mouseMoveEvent (QMouseEvent* theEvent) Standard_OVERRIDE;
162 //! Creates view actions and fills an internal map
163 void initViewActions();
165 //! Creates cursors and fills an internal map
168 //! Sets widget cursor by the action type
169 //! \param theMode an active action mode
170 void activateCursor (const View_CurrentAction3d theMode);
172 //! Activates cursor of the active operation, perform drag, onRotate depending on mode,
173 //! stores the point position in xmin/xmax and ymin/ymax
174 //! \param theFlags an event buttons and modifiers
175 //! \param thePoint a clicked point
176 void processLeftButtonDown (const int theFlags, const QPoint thePoint);
178 //! Activates cursor of the active operation and performs dynamic pan if it is active
179 //! \param theFlags an event buttons and modifiers
180 //! \param thePoint a clicked point
181 void processMiddleButtonDown (const int theFlags, const QPoint thePoint);
183 //! Activates cursor of the active operation, build popup menu
184 //! \param theFlags an event buttons and modifiers
185 //! \param thePoint a clicked point
186 void processRightButtonDown (const int theFlags, const QPoint thePoint);
188 //! Performs the active operation or performs Input/Drag event processing, emits selection changed signal
189 //! \param theFlags an event buttons and modifiers
190 //! \param thePoint a clicked point
191 void processLeftButtonUp (const int theFlags, const QPoint thePoint);
193 //! Changes the active operation to None
194 //! \param theFlags an event buttons and modifiers
195 //! \param thePoint a clicked point
196 void processMiddleButtonUp (const int theFlags, const QPoint thePoint);
198 //! Calls popup menu build and changes the active operation to None
199 //! \param theFlags an event buttons and modifiers
200 //! \param thePoint a clicked point
201 void processRightButtonUp (const int theFlags, const QPoint thePoint);
203 //! Performs active operation or draws rectangle of zoom
204 //! \param theFlags an event buttons and modifiers
205 //! \param thePoint a clicked point
206 void processMouseMove (const int theFlags, const QPoint thePoint);
208 //! Performs selection: store clicked point by botton down, call Select by button up
209 //! Emits signal about selection changed
210 //! \param theX a horizontal position of mouse event
211 //! \param theX a vertical position of mouse event
212 //! \param theState a mouse button state: down, move or up
213 void processDragEvent (const Standard_Integer theX, const Standard_Integer theY, const View_DragMode& theState);
215 //! Performs selection in context without parameter, it means the selection of picked object
216 //! \param theX a horizontal position of mouse event
217 //! \param theX a vertical position of mouse event
218 void processInputEvent (const Standard_Integer theX, const Standard_Integer theY);
220 //! Calls MoveTo of the context for the parameter coordinates
221 //! \param theX a horizontal position of mouse event
222 //! \param theX a vertical position of mouse event
223 void processMoveEvent (const Standard_Integer theX, const Standard_Integer theY);
225 //! Empty: template to process mouse move with multi selection key pressed
226 //! \param theX a horizontal position of mouse event
227 //! \param theX a vertical position of mouse event
228 void processMoveMultiEvent (const Standard_Integer theX, const Standard_Integer theY)
229 { (void)theX; (void)theY; }
231 //! Performs selection: store clicked point by botton down, call ShiftSelect by button move
232 //! Emits signal about selection changed
233 //! \param theX a horizontal position of mouse event
234 //! \param theX a vertical position of mouse event
235 //! \param theState a mouse button state: down, move or up
236 void processDragMultiEvent (const Standard_Integer theX, const Standard_Integer theY, const View_DragMode& theState);
238 //! Performs shift selection in context without parameter, it means the selection of picked object
239 //! \param theX a horizontal position of mouse event
240 //! \param theX a vertical position of mouse event
241 void processInputMultiEvent (const Standard_Integer theX, const Standard_Integer theY);
243 //! Empty: template to create popup menu
244 //! \param theX a horizontal position of mouse event
245 //! \param theX a vertical position of mouse event
246 void popup (const Standard_Integer theX, const Standard_Integer theY) { (void)theX; (void)theY; }
248 //! Draws Qt rectangle for the given area (e.g. for panning operation)
249 //! \param theMinX a minimal X coordinate
250 //! \param theMinY a minimal Y coordinate
251 //! \param theMinZ a minimal Z coordinate
252 //! \param theMaxX a maximum X coordinate
253 //! \param theMaxY a maximum Y coordinate
254 //! \param theMaxZ a maximum Z coordinate
255 //! \param theToDraw state whether the rectangle should be visualized or hidden
256 void drawRectangle (const Standard_Integer theMinX, const Standard_Integer theMinY, const Standard_Integer theMaxX,
257 const Standard_Integer theMaxY, const Standard_Boolean theToDraw);
260 //! Creates action and stores it in a map of actions
261 //! \param theActionId an identifier of action in internal map
262 //! \param theIcon an icon name and place according to qrc resource file, e.g. ":/icons/view_fitall.png"
263 //! \param theText an action text
264 //! \param theToolBar a tool bar action text
265 //! \param theStatusBar a status bar action text
266 void createAction (const View_ViewActionType theActionId, const QString& theIcon, const QString& theText,
267 const char* theSlot, const bool isCheckable = false,
268 const QString& theToolBar = QString(), const QString& theStatusBar = QString());
270 //! Sets active action cursor for application
271 //! \param theMode a cursor mode
272 void setActiveCursor (const View_CursorMode& theMode);
276 View_Viewer* myViewer; //!< connector to context, V3d viewer and V3d View
277 QMap<View_ViewActionType, QAction*> myViewActions; //!< tool bar view actions
278 QMap<View_CursorMode, QCursor> myCursors; //!< possible cursors for view actions
280 View_CurrentAction3d myCurrentMode; //!< an active action mode for viewer
281 bool myFirst; //!< flag to Init view by the first resize/paint call
282 int myDefaultWidth; //!< default width for the first sizeHint
283 int myDefaultHeight; //!< default height for the first sizeHint
284 bool myViewIsEnabled; //!< flag if the view and tool bar view actions are enabled/disabled
285 Standard_Integer myXmin; //!< cached X minimal position by mouse press event
286 Standard_Integer myYmin; //!< cached Y minimal position by mouse press event
287 Standard_Integer myXmax; //!< cached X maximum position by mouse press event
288 Standard_Integer myYmax; //!< cached Y maximum position by mouse press event
289 Standard_Integer myDragButtonDownX; //!< cached X button down by drag event
290 Standard_Integer myDragButtonDownY; //!< cached Y button down by drag event
291 Standard_Integer myDragMultiButtonDownX; //!< cached X button down by multi drag event
292 Standard_Integer myDragMultiButtonDownY; //!< cached Y button down by multi drag event
293 Standard_Boolean myIsRectVisible; //!< true if rectangle is visible now
294 QRubberBand* myRectBand; //!< selection rectangle rubber band