MRPT  2.0.3
CQtGlCanvasBase.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "gui-precomp.h" // Precompiled headers
11 
13 
14 #if MRPT_HAS_Qt5
15 #include <QMouseEvent>
16 
17 using namespace mrpt;
18 using namespace mrpt::gui;
19 
20 CQtGlCanvasBase::CQtGlCanvasBase(QWidget* parent)
21  : QOpenGLWidget(parent), mrpt::gui::CGlCanvasBase()
22 {
23 #if MRPT_HAS_OPENGL_GLUT
24  m_mainViewport = getOpenGLSceneRef()->getViewport("main");
25  setMouseTracking(true);
26 #else
27  std::cerr << "[mrpt::gui::CQtGlCanvasBase] *Warning*: MRPT built without "
28  "OpenGL support."
29  << std::endl;
30 #endif
31 }
32 
33 void CQtGlCanvasBase::initializeGL()
34 {
35 #if MRPT_HAS_OPENGL_GLUT
36  clearColors();
37 
38  QOpenGLWidget::initializeGL();
39 #endif
40 }
41 
42 void CQtGlCanvasBase::paintGL() { renderCanvas(); }
43 void CQtGlCanvasBase::resizeGL(int width, int height)
44 {
45 #if MRPT_HAS_OPENGL_GLUT
46  if (height == 0) height = 1;
47  glViewport(0, 0, width, height);
48 
49  QOpenGLWidget::resizeGL(width, height);
50 #endif
51 }
52 
53 opengl::COpenGLViewport::Ptr CQtGlCanvasBase::mainViewport() const
54 {
55  return m_mainViewport;
56 }
57 
58 float CQtGlCanvasBase::getCameraZoomDistance() const
59 {
60  mrpt::opengl::CCamera& cam = m_mainViewport->getCamera();
61  return cam.getZoomDistance();
62 }
63 
64 void CQtGlCanvasBase::mousePressEvent(QMouseEvent* event)
65 {
66  setMousePos(event->pos().x(), event->pos().y());
67  setMouseClicked(true);
68 
69  m_isPressLMouseButton = (event->button() == Qt::LeftButton);
70  m_isPressRMouseButton = (event->button() == Qt::RightButton);
71 
72  QOpenGLWidget::mousePressEvent(event);
73 }
74 
75 void CQtGlCanvasBase::mouseMoveEvent(QMouseEvent* event)
76 {
77  int X = event->pos().x();
78  int Y = event->pos().y();
79  updateLastPos(X, Y);
80 
81  if (m_isPressLMouseButton || m_isPressRMouseButton)
82  {
83  // Proxy variables to cache the changes:
84  CamaraParams params = cameraParams();
85 
86  if (m_isPressLMouseButton)
87  {
88  if (event->modifiers() == Qt::ShiftModifier)
89  updateZoom(params, X, Y);
90 
91  else if (event->modifiers() == Qt::ControlModifier)
92  updateRotate(params, X, Y);
93 
94  else if (event->modifiers() == Qt::NoModifier)
95  updateOrbitCamera(params, X, Y);
96  }
97  else
98  updatePan(params, X, Y);
99 
100  setMousePos(X, Y);
101  setCameraParams(params);
102 
103  updateCamerasParams();
104  update();
105  }
106 
107  QOpenGLWidget::mouseMoveEvent(event);
108 }
109 
110 void CQtGlCanvasBase::mouseReleaseEvent(QMouseEvent* event)
111 {
112  setMouseClicked(false);
113  m_isPressLMouseButton = false;
114  m_isPressRMouseButton = false;
115  QOpenGLWidget::mouseReleaseEvent(event);
116 }
117 
118 void CQtGlCanvasBase::wheelEvent(QWheelEvent* event)
119 {
120  CamaraParams params = cameraParams();
121  updateZoom(params, event->delta());
122  setCameraParams(params);
123 
124  updateCamerasParams();
125 
126  update();
127  QOpenGLWidget::wheelEvent(event);
128 }
129 
130 void CQtGlCanvasBase::renderError(const std::string& err_msg)
131 {
132  Q_UNUSED(err_msg);
133 }
134 
135 void CQtGlCanvasBase::updateCamerasParams()
136 {
137  mrpt::opengl::CCamera& cam = m_mainViewport->getCamera();
138  updateCameraParams(cam);
139 }
140 
141 void CQtGlCanvasBase::insertToMap(const opengl::CRenderizable::Ptr& newObject)
142 {
143  assert(m_mainViewport);
144  m_mainViewport->insert(newObject);
145 }
146 
147 void CQtGlCanvasBase::removeFromMap(const opengl::CRenderizable::Ptr& newObject)
148 {
149  assert(m_mainViewport);
150  m_mainViewport->removeObject(newObject);
151 }
152 
153 bool CQtGlCanvasBase::isPressLMouseButton() const
154 {
155  return m_isPressLMouseButton;
156 }
157 
158 bool CQtGlCanvasBase::isPressRMouseButton() const
159 {
160  return m_isPressRMouseButton;
161 }
162 
163 void CQtGlCanvasBase::unpressMouseButtons()
164 {
165  m_isPressLMouseButton = false;
166  m_isPressRMouseButton = false;
167 }
168 
169 #endif // MRPT_HAS_Qt5
mrpt::vision::TStereoCalibParams params
float getZoomDistance() const
Definition: CCamera.h:64
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Classes for creating GUI windows for 2D and 3D visualization.
Definition: about_box.h:14
This base class implements a working with opengl::Camera and a OpenGL canvas, and it&#39;s used in gui::C...
Definition: CGlCanvasBase.h:24
A camera: if added to a scene, the viewpoint defined by this camera will be used instead of the camer...
Definition: CCamera.h:33



Page generated by Doxygen 1.8.14 for MRPT 2.0.3 Git: 8e9e8af54 Wed May 13 17:41:24 2020 +0200 at miƩ may 13 17:55:54 CEST 2020