1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2007 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 #ifndef QT_TRACKBALL_H
25 #define QT_TRACKBALL_H
26 #include <wrap/qt/device_to_logical.h>
27
28 /// Transforms the event coordintates (that are device independent)
29 /// into the expected framebuffer coordinates (e.g.in opengl pixels)
30 /// This is necessary because trackball works in the viewport coord systems.
QT2VCG_X(QWidget * qw,QMouseEvent * e)31 inline float QT2VCG_X( QWidget *qw, QMouseEvent *e)
32 {
33 return QTLogicalToDevice(qw,e->x());
34 }
35
36 /// Transforms the event coordintates (that are device independent)
37 /// into the expected framebuffer coordinates (e.g.in opengl pixels)
38 /// This is necessary because trackball works in the viewport coord systems.
39
QT2VCG_Y(QWidget * qw,QMouseEvent * e)40 inline float QT2VCG_Y( QWidget *qw, QMouseEvent *e)
41 {
42 return QTLogicalToDevice(qw,qw->height () - e->y ());
43 }
44
45 /// Takes a QT MouseButton, some QT KeyboardModifiers and returns the equivalent Trackball::Button
QT2VCG(Qt::MouseButton qtbt,Qt::KeyboardModifiers modifiers)46 inline vcg::Trackball::Button QT2VCG (Qt::MouseButton qtbt, Qt::KeyboardModifiers modifiers)
47 {
48 int vcgbt = vcg::Trackball::BUTTON_NONE;
49
50 if (qtbt & Qt::LeftButton) vcgbt |= vcg::Trackball::BUTTON_LEFT;
51 if (qtbt & Qt::RightButton) vcgbt |= vcg::Trackball::BUTTON_RIGHT;
52 if (qtbt & Qt::MidButton) vcgbt |= vcg::Trackball::BUTTON_MIDDLE;
53
54 if (modifiers & Qt::ShiftModifier) vcgbt |= vcg::Trackball::KEY_SHIFT;
55 if (modifiers & Qt::ControlModifier) vcgbt |= vcg::Trackball::KEY_CTRL;
56 if (modifiers & Qt::AltModifier) vcgbt |= vcg::Trackball::KEY_ALT;
57
58 return vcg::Trackball::Button (vcgbt);
59 }
60
61 /// Takes a QT ket and QT KeyboardModifiers and returns the mouse wheel related Trackball::Button
QTWheel2VCG(Qt::KeyboardModifiers modifiers)62 inline vcg::Trackball::Button QTWheel2VCG (Qt::KeyboardModifiers modifiers)
63 {
64 int vcgbt = vcg::Trackball::WHEEL;
65
66 if (modifiers & Qt::ShiftModifier) vcgbt |= vcg::Trackball::KEY_SHIFT;
67 if (modifiers & Qt::ControlModifier) vcgbt |= vcg::Trackball::KEY_CTRL;
68 if (modifiers & Qt::AltModifier) vcgbt |= vcg::Trackball::KEY_ALT;
69
70 return vcg::Trackball::Button (vcgbt);
71 }
72
73 /// Takes some QT KeyboardModifiers and returns the mouse wheel related Trackball::Button
QTKey2VCG(int key,Qt::KeyboardModifiers modifiers)74 inline vcg::Trackball::Button QTKey2VCG (int key, Qt::KeyboardModifiers modifiers)
75 {
76 int vcgbt = 0;
77 switch (key) {
78 case Qt::Key_W :
79 case Qt::Key_Up : vcgbt = vcg::Trackball::KEY_UP ; break;
80 case Qt::Key_A :
81 case Qt::Key_Left : vcgbt = vcg::Trackball::KEY_LEFT ; break;
82 case Qt::Key_S :
83 case Qt::Key_Down : vcgbt = vcg::Trackball::KEY_DOWN ; break;
84 case Qt::Key_D :
85 case Qt::Key_Right: vcgbt = vcg::Trackball::KEY_RIGHT; break;
86 case Qt::Key_R :
87 case Qt::Key_PageUp: vcgbt = vcg::Trackball::KEY_PGUP ; break;
88 case Qt::Key_F :
89 case Qt::Key_PageDown: vcgbt = vcg::Trackball::KEY_PGDOWN; break;
90 default : vcgbt = 0;
91 }
92
93 if (modifiers & Qt::ShiftModifier) vcgbt |= vcg::Trackball::KEY_SHIFT;
94 if (modifiers & Qt::ControlModifier) vcgbt |= vcg::Trackball::KEY_CTRL;
95 if (modifiers & Qt::AltModifier) vcgbt |= vcg::Trackball::KEY_ALT;
96
97 return vcg::Trackball::Button (vcgbt);
98 }
99
100 #endif // QT_TRACKBALL_H
101