glVertex  5.5.2
glvertex_qt_glwindowui.h
Go to the documentation of this file.
1 // (c) by Stefan Roettger, licensed under MIT license
2 
5 #ifndef GLVERTEX_QT_GLWINDOWUI_H
6 #define GLVERTEX_QT_GLWINDOWUI_H
7 
8 #include <locale.h>
9 
10 #include <QApplication>
11 
12 #include "glvertex_qt_glwindow.h"
13 
16 {
17 public:
18 
19  lgl_Qt_GLWindowUI(bool core = true)
20  : lgl_Qt_GLWindow(core),
21  buttonDown_(false),
22  spin_(0)
23  {
24  setlocale(LC_NUMERIC, "C");
25  }
26 
27  virtual ~lgl_Qt_GLWindowUI()
28  {}
29 
30 protected:
31 
32  bool buttonDown_;
33  QPoint lastPos_;
34  vec2 lastDelta_;
35  vec2 spin_;
36 
37  void keyPressEvent(QKeyEvent *event)
38  {
39 #ifndef __APPLE__
40  Qt::KeyboardModifier ctrl = Qt::ControlModifier;
41 #else
42  Qt::KeyboardModifier ctrl = Qt::MetaModifier;
43 #endif
44 
45  if (event->key() == Qt::Key_Escape)
46  qApp->quit();
47  else if (event->key()==Qt::Key_Space)
49  else if (event->modifiers() & ctrl)
50  if (event->key()==Qt::Key_R)
51  {
52  lglResetManip();
53  update();
54  }
55  else if (event->key()==Qt::Key_Q)
56  qApp->quit();
57  else
58  lgl_Qt_GLWindow::keyPressEvent(event);
59  else
60  lgl_Qt_GLWindow::keyPressEvent(event);
61 
62  interaction();
63  }
64 
65  void keyReleaseEvent(QKeyEvent *event)
66  {
67  if (event->key() == Qt::Key_Back)
68  qApp->quit();
69  else
70  lgl_Qt_GLWindow::keyReleaseEvent(event);
71 
72  interaction();
73  }
74 
75  void mousePressEvent(QMouseEvent *event)
76  {
77  if (event->buttons() & Qt::LeftButton)
78  buttonDown_ = true;
79  else
80  event->ignore();
81 
82  if (buttonDown_)
83  {
84  double x = (event->x()+0.5)/width();
85  double y = (event->y()+0.5)/height();
86 
87  mouseClick(x, y);
88  }
89 
90  spin_ = vec2(0);
91 
92  lastPos_ = event->pos();
93  lastDelta_ = vec2(0);
94 
95  interaction();
96  }
97 
98  void mouseReleaseEvent(QMouseEvent *event)
99  {
100  if (!(event->buttons() & Qt::LeftButton))
101  {
102  buttonDown_ = false;
103  spin_ = lastDelta_;
104 
105  if (spin_.length() < 3.0/width()) spin_ = vec2(0);
106 
107  if (spin_.norm() > 0) update();
108  }
109  else
110  event->ignore();
111 
112  interaction();
113  }
114 
115  void mouseMoveEvent(QMouseEvent *event)
116  {
117  if (buttonDown_)
118  {
119  double dx = (double)(event->x()-lastPos_.x())/width();
120  double dy = (double)(event->y()-lastPos_.y())/height();
121 
122  mouseMoved(dx, dy);
123 
124  lglManip(-dx, -dy);
125  update();
126 
127  lastPos_ = event->pos();
128  lastDelta_ = vec2(dx, dy);
129  }
130 
131  interaction();
132  }
133 
134  void mouseDoubleClickEvent(QMouseEvent *event)
135  {
136  if (event->buttons() & Qt::LeftButton)
137  {
138  double x = (event->x()+0.5)/width();
139  double y = (event->y()+0.5)/height();
140 
141  doubleClick(x, y);
142  update();
143  }
144 
145  interaction();
146  }
147 
148  void wheelEvent(QWheelEvent *event)
149  {
150 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
151  double numDegrees = event->delta()/16.0;
152 
153  if (event->orientation() == Qt::Vertical)
154 #else
155  double numDegrees = event->angleDelta().y()/16.0;
156 
157  if (numDegrees != 0)
158 #endif
159  {
160  lglManip(0, 0, 1+numDegrees/360.0);
161  update();
162  }
163 
164  event->accept();
165 
166  interaction();
167  }
168 
169  // apply manipulator spin
170  virtual void updateOpenGL(double dt)
171  {
172  if (spin_.norm() > 0)
173  {
174  lglManip(-spin_);
175  update();
176  }
177  }
178 
180  virtual bool isSpinning()
181  {
182  return(spin_.norm() > 0);
183  }
184 
186  virtual void mouseMoved(double dx, double dy)
187  {}
188 
190  virtual void mouseClick(double x, double y)
191  {}
192 
194  virtual void doubleClick(double x, double y)
195  {
197  }
198 
200  virtual void interaction()
201  {}
202 
203 };
204 
205 #endif
lgl_Qt_GLWindowUI::mouseClick
virtual void mouseClick(double x, double y)
reimplement to be called in case of a mouse click
Definition: glvertex_qt_glwindowui.h:190
vec2::norm
double norm() const
get squared vector length
Definition: glslmath.h:136
lglResetManip
void lglResetManip()
reset the manipulator matrix
Definition: glvertex_api.h:320
lgl_Qt_GLWindowUI::doubleClick
virtual void doubleClick(double x, double y)
reimplement to be called in case of a double click
Definition: glvertex_qt_glwindowui.h:194
lgl_Qt_GLWindowUI::mouseMoved
virtual void mouseMoved(double dx, double dy)
reimplement to be called in case of a mouse move
Definition: glvertex_qt_glwindowui.h:186
lglManip
void lglManip(bool on=true)
enable or disable the manipulator matrix
Definition: glvertex_api.h:300
vec2
2D double vector
Definition: glslmath.h:108
lgl_Qt_GLWindowUI::interaction
virtual void interaction()
reimplement to be called in case of a user interaction
Definition: glvertex_qt_glwindowui.h:200
lgl_Qt_GLWindow::lgl_Qt_GLWindow
lgl_Qt_GLWindow(bool core=true)
default ctor
Definition: glvertex_qt_glwindow.h:45
vec2::length
double length() const
get vector length
Definition: glslmath.h:133
lgl_Qt_GLWindow::toggle_animation
void toggle_animation()
toggle animation
Definition: glvertex_qt_glwindow.h:177
lgl_Qt_GLWindow::update
void update()
update
Definition: glvertex_qt_glwindow.h:120
lgl_Qt_GLWindow::toggle_wireframe
void toggle_wireframe()
toggle wireframe mode
Definition: glvertex_qt_glwindow.h:262
lgl_Qt_GLWindowUI
Qt OpenGL window class /w event handling.
Definition: glvertex_qt_glwindowui.h:15
lgl_Qt_GLWindowUI::isSpinning
virtual bool isSpinning()
check for manipulator spin
Definition: glvertex_qt_glwindowui.h:180
lgl_Qt_GLWindow
Qt OpenGL window base class.
Definition: glvertex_qt_glwindow.h:40
glvertex_qt_glwindow.h