author | sandman <sandman> | 2002-06-25 21:19:29 (UTC) |
---|---|---|
committer | sandman <sandman> | 2002-06-25 21:19:29 (UTC) |
commit | 27d9215054a51c1d605e0f4e77abcee3c96e8270 (patch) (unidiff) | |
tree | 4ece274016a03911beaa9fd2e011440a6af10877 | |
parent | d64bb324d4b617479dab8bf967f954a66f0faa29 (diff) | |
download | opie-27d9215054a51c1d605e0f4e77abcee3c96e8270.zip opie-27d9215054a51c1d605e0f4e77abcee3c96e8270.tar.gz opie-27d9215054a51c1d605e0f4e77abcee3c96e8270.tar.bz2 |
Fix for the calibration routine I broke with my latest "enhancement"
(it is not that trivial to detect all sort of rotations and permutations
of the x and y axis)
-rw-r--r-- | core/apps/calibrate/calibrate.cpp | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/core/apps/calibrate/calibrate.cpp b/core/apps/calibrate/calibrate.cpp index 54fa8ec..1f4fb1b 100644 --- a/core/apps/calibrate/calibrate.cpp +++ b/core/apps/calibrate/calibrate.cpp | |||
@@ -1,219 +1,251 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include "calibrate.h" | 21 | #include "calibrate.h" |
22 | 22 | ||
23 | #include <qpe/resource.h> | 23 | #include <qpe/resource.h> |
24 | 24 | ||
25 | #include <qapplication.h> | 25 | #include <qapplication.h> |
26 | 26 | ||
27 | #if defined(Q_WS_QWS) || defined(_WS_QWS_) | 27 | #if defined(Q_WS_QWS) || defined(_WS_QWS_) |
28 | 28 | ||
29 | #include <qpainter.h> | 29 | #include <qpainter.h> |
30 | #include <qtimer.h> | 30 | #include <qtimer.h> |
31 | #include <qwindowsystem_qws.h> | 31 | #include <qwindowsystem_qws.h> |
32 | #include <qgfx_qws.h> | 32 | #include <qgfx_qws.h> |
33 | 33 | ||
34 | 34 | ||
35 | Calibrate::Calibrate( QWidget* parent, const char * name, WFlags wf ) : | 35 | Calibrate::Calibrate( QWidget* parent, const char * name, WFlags wf ) : |
36 | QDialog( parent, name, TRUE, wf | WStyle_Tool | WStyle_Customize | WStyle_StaysOnTop ) | 36 | QDialog( parent, name, TRUE, wf | WStyle_Tool | WStyle_Customize | WStyle_StaysOnTop ) |
37 | { | 37 | { |
38 | showCross = TRUE; | 38 | showCross = TRUE; |
39 | const int offset = 30; | 39 | const int offset = 30; |
40 | QRect desk = qApp->desktop() ->geometry(); | 40 | QRect desk = qApp->desktop() ->geometry(); |
41 | setGeometry( 0, 0, desk.width(), desk.height() ); | 41 | setGeometry( 0, 0, desk.width(), desk.height() ); |
42 | if ( desk.height() < 250 ) { | 42 | if ( desk.height() < 250 ) { |
43 | int w = desk.height() / 3; | 43 | int w = desk.height() / 3; |
44 | logo.convertFromImage( Resource::loadImage( "launcher/opielogo" ).smoothScale( w, w ) ); | 44 | logo.convertFromImage( Resource::loadImage( "launcher/opielogo" ).smoothScale( w, w ) ); |
45 | } | 45 | } |
46 | else { | 46 | else { |
47 | logo = Resource::loadPixmap( "launcher/opielogo" ); | 47 | logo = Resource::loadPixmap( "launcher/opielogo" ); |
48 | } | 48 | } |
49 | cd.screenPoints[ QWSPointerCalibrationData::TopLeft ] = QPoint( offset, offset ); | 49 | cd.screenPoints[ QWSPointerCalibrationData::TopLeft ] = QPoint( offset, offset ); |
50 | cd.screenPoints[ QWSPointerCalibrationData::BottomLeft ] = QPoint( offset, qt_screen->deviceHeight() - offset ); | 50 | cd.screenPoints[ QWSPointerCalibrationData::BottomLeft ] = QPoint( offset, qt_screen->deviceHeight() - offset ); |
51 | cd.screenPoints[ QWSPointerCalibrationData::BottomRight ] = QPoint( qt_screen->deviceWidth() - offset, qt_screen->deviceHeight() - offset ); | 51 | cd.screenPoints[ QWSPointerCalibrationData::BottomRight ] = QPoint( qt_screen->deviceWidth() - offset, qt_screen->deviceHeight() - offset ); |
52 | cd.screenPoints[ QWSPointerCalibrationData::TopRight ] = QPoint( qt_screen->deviceWidth() - offset, offset ); | 52 | cd.screenPoints[ QWSPointerCalibrationData::TopRight ] = QPoint( qt_screen->deviceWidth() - offset, offset ); |
53 | cd.screenPoints[ QWSPointerCalibrationData::Center ] = QPoint( qt_screen->deviceWidth() / 2, qt_screen->deviceHeight() / 2 ); | 53 | cd.screenPoints[ QWSPointerCalibrationData::Center ] = QPoint( qt_screen->deviceWidth() / 2, qt_screen->deviceHeight() / 2 ); |
54 | goodcd = cd; | 54 | goodcd = cd; |
55 | reset(); | 55 | reset(); |
56 | 56 | ||
57 | timer = new QTimer( this ); | 57 | timer = new QTimer( this ); |
58 | connect( timer, SIGNAL( timeout() ), this, SLOT( timeout() ) ); | 58 | connect( timer, SIGNAL( timeout() ), this, SLOT( timeout() ) ); |
59 | } | 59 | } |
60 | 60 | ||
61 | Calibrate::~Calibrate() | 61 | Calibrate::~Calibrate() |
62 | { | 62 | { |
63 | store(); | 63 | store(); |
64 | } | 64 | } |
65 | 65 | ||
66 | void Calibrate::show() | 66 | void Calibrate::show() |
67 | { | 67 | { |
68 | grabMouse(); | 68 | grabMouse(); |
69 | QWSServer::mouseHandler() ->getCalibration( &goodcd ); | 69 | QWSServer::mouseHandler() ->getCalibration( &goodcd ); |
70 | QWSServer::mouseHandler() ->clearCalibration(); | 70 | QWSServer::mouseHandler() ->clearCalibration(); |
71 | QDialog::show(); | 71 | QDialog::show(); |
72 | } | 72 | } |
73 | 73 | ||
74 | void Calibrate::store() | 74 | void Calibrate::store() |
75 | { | 75 | { |
76 | QWSServer::mouseHandler() ->calibrate( &goodcd ); | 76 | QWSServer::mouseHandler() ->calibrate( &goodcd ); |
77 | } | 77 | } |
78 | 78 | ||
79 | void Calibrate::hide() | 79 | void Calibrate::hide() |
80 | { | 80 | { |
81 | if ( isVisible() ) | 81 | if ( isVisible() ) |
82 | store(); | 82 | store(); |
83 | QDialog::hide(); | 83 | QDialog::hide(); |
84 | } | 84 | } |
85 | 85 | ||
86 | void Calibrate::reset() | 86 | void Calibrate::reset() |
87 | { | 87 | { |
88 | penPos = QPoint(); | 88 | penPos = QPoint(); |
89 | location = QWSPointerCalibrationData::TopLeft; | 89 | location = QWSPointerCalibrationData::TopLeft; |
90 | crossPos = fromDevice( cd.screenPoints[ location ] ); | 90 | crossPos = fromDevice( cd.screenPoints[ location ] ); |
91 | } | 91 | } |
92 | 92 | ||
93 | QPoint Calibrate::fromDevice( const QPoint &p ) | 93 | QPoint Calibrate::fromDevice( const QPoint &p ) |
94 | { | 94 | { |
95 | return qt_screen->mapFromDevice ( p, QSize( qt_screen->deviceWidth ( ), qt_screen->deviceHeight() ) ); | 95 | return qt_screen->mapFromDevice ( p, QSize( qt_screen->deviceWidth ( ), qt_screen->deviceHeight() ) ); |
96 | } | 96 | } |
97 | 97 | ||
98 | bool Calibrate::sanityCheck() | 98 | bool Calibrate::sanityCheck() |
99 | { | 99 | { |
100 | QRect r ( cd.devPoints[ QWSPointerCalibrationData::TopLeft ], cd.devPoints[ QWSPointerCalibrationData::BottomRight ] ); | 100 | QPoint tl = cd.devPoints[QWSPointerCalibrationData::TopLeft]; |
101 | r = r. normalize ( ); // This should also handle rotated TS controllers | 101 | QPoint tr = cd.devPoints[QWSPointerCalibrationData::TopRight]; |
102 | 102 | QPoint bl = cd.devPoints[QWSPointerCalibrationData::BottomLeft]; | |
103 | cd. devPoints [QWSPointerCalibrationData::TopRight] = r. topRight ( ); | 103 | QPoint br = cd.devPoints[QWSPointerCalibrationData::BottomRight]; |
104 | cd. devPoints [QWSPointerCalibrationData::BottomLeft] = r. bottomLeft ( ); | 104 | |
105 | cd. devPoints [QWSPointerCalibrationData::Center] = r. center ( ); | 105 | // not needed anywhere .. just calculate it, so it's there |
106 | 106 | cd. devPoints [QWSPointerCalibrationData::Center] = QRect ( tl, br ). normalize ( ). center ( ); | |
107 | return true; | 107 | |
108 | int dlx = QABS( bl. x ( ) - tl. x ( )); | ||
109 | int dly = QABS( bl. y ( ) - tl. y ( )); | ||
110 | int drx = QABS( br. x ( ) - tr. x ( )); | ||
111 | int dry = QABS( br. y ( ) - tr. y ( )); | ||
112 | int dtx = QABS( tr. x ( ) - tl. x ( )); | ||
113 | int dty = QABS( tr. y ( ) - tl. y ( )); | ||
114 | int dbx = QABS( br. x ( ) - bl. x ( )); | ||
115 | int dby = QABS( br. y ( ) - bl. y ( )); | ||
116 | |||
117 | int dl = (int) ::sqrt (( dlx * dlx ) + ( dly * dly )); // calculate vector lengths for all sides | ||
118 | int dr = (int) ::sqrt (( drx * drx ) + ( dry * dry )); | ||
119 | int dt = (int) ::sqrt (( dtx * dtx ) + ( dty * dty )); | ||
120 | int db = (int) ::sqrt (( dbx * dbx ) + ( dby * dby )); | ||
121 | |||
122 | // Calculate leeway for x/y (we do not care if diff1/diff2 is for x or y here !) | ||
123 | int diff1 = QABS( dl - dr ); | ||
124 | int avg1 = ( dl + dr ) / 2; | ||
125 | int diff2 = QABS( dt - db ); | ||
126 | int avg2 = ( dt + db ) / 2; | ||
127 | |||
128 | // Calculate leeway for "real" vector length against "manhattan" vector length | ||
129 | // This is a check, if the rect is rotated (other then 0/90/180/270) | ||
130 | // It needs to be performed only for the triange (bl, tl, tr) | ||
131 | int diff3 = QABS(( dlx + dly + dtx + dty ) - ( dl + dt )); | ||
132 | int avg3 = (( dlx + dly + dtx + dty ) + ( dl + dt )) / 2; | ||
133 | |||
134 | if (( diff1 > ( avg1 / 20 )) || // 5% leeway | ||
135 | ( diff2 > ( avg2 / 20 )) || | ||
136 | ( diff3 > ( avg3 / 20 ))) | ||
137 | return false; | ||
138 | else | ||
139 | return true; | ||
108 | } | 140 | } |
109 | 141 | ||
110 | void Calibrate::moveCrosshair( QPoint pt ) | 142 | void Calibrate::moveCrosshair( QPoint pt ) |
111 | { | 143 | { |
112 | showCross = FALSE; | 144 | showCross = FALSE; |
113 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); | 145 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); |
114 | showCross = TRUE; | 146 | showCross = TRUE; |
115 | crossPos = pt; | 147 | crossPos = pt; |
116 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); | 148 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); |
117 | } | 149 | } |
118 | 150 | ||
119 | void Calibrate::paintEvent( QPaintEvent * ) | 151 | void Calibrate::paintEvent( QPaintEvent * ) |
120 | { | 152 | { |
121 | QPainter p( this ); | 153 | QPainter p( this ); |
122 | 154 | ||
123 | int y; | 155 | int y; |
124 | 156 | ||
125 | if ( !logo.isNull() ) { | 157 | if ( !logo.isNull() ) { |
126 | y = height() / 2 - logo.height() - 15; | 158 | y = height() / 2 - logo.height() - 15; |
127 | p.drawPixmap( ( width() - logo.width() ) / 2, y, logo ); | 159 | p.drawPixmap( ( width() - logo.width() ) / 2, y, logo ); |
128 | } | 160 | } |
129 | 161 | ||
130 | y = height() / 2 + 15; | 162 | y = height() / 2 + 15; |
131 | 163 | ||
132 | p.drawText( 0, y + height() / 8, width(), height() - y, AlignHCenter, | 164 | p.drawText( 0, y + height() / 8, width(), height() - y, AlignHCenter, |
133 | tr( "Touch the crosshairs firmly and\n" | 165 | tr( "Touch the crosshairs firmly and\n" |
134 | "accurately to calibrate your screen." ) ); | 166 | "accurately to calibrate your screen." ) ); |
135 | 167 | ||
136 | QFont f = p.font(); | 168 | QFont f = p.font(); |
137 | f.setBold( TRUE ); | 169 | f.setBold( TRUE ); |
138 | p.setFont( f ); | 170 | p.setFont( f ); |
139 | p.drawText( 0, y, width(), height() - y, AlignHCenter | WordBreak, | 171 | p.drawText( 0, y, width(), height() - y, AlignHCenter | WordBreak, |
140 | tr( "Welcome to Opie" ) ); | 172 | tr( "Welcome to Opie" ) ); |
141 | 173 | ||
142 | if ( showCross ) { | 174 | if ( showCross ) { |
143 | p.drawRect( crossPos.x() - 1, crossPos.y() - 8, 2, 7 ); | 175 | p.drawRect( crossPos.x() - 1, crossPos.y() - 8, 2, 7 ); |
144 | p.drawRect( crossPos.x() - 1, crossPos.y() + 1, 2, 7 ); | 176 | p.drawRect( crossPos.x() - 1, crossPos.y() + 1, 2, 7 ); |
145 | p.drawRect( crossPos.x() - 8, crossPos.y() - 1, 7, 2 ); | 177 | p.drawRect( crossPos.x() - 8, crossPos.y() - 1, 7, 2 ); |
146 | p.drawRect( crossPos.x() + 1, crossPos.y() - 1, 7, 2 ); | 178 | p.drawRect( crossPos.x() + 1, crossPos.y() - 1, 7, 2 ); |
147 | } | 179 | } |
148 | } | 180 | } |
149 | 181 | ||
150 | void Calibrate::mousePressEvent( QMouseEvent *e ) | 182 | void Calibrate::mousePressEvent( QMouseEvent *e ) |
151 | { | 183 | { |
152 | // map to device coordinates | 184 | // map to device coordinates |
153 | QPoint devPos = qt_screen->mapToDevice( e->pos(), QSize( qt_screen->width(), qt_screen->height() ) ); | 185 | QPoint devPos = qt_screen->mapToDevice( e->pos(), QSize( qt_screen->width(), qt_screen->height() ) ); |
154 | if ( penPos.isNull() ) | 186 | if ( penPos.isNull() ) |
155 | penPos = devPos; | 187 | penPos = devPos; |
156 | else | 188 | else |
157 | penPos = QPoint( ( penPos.x() + devPos.x() ) / 2, | 189 | penPos = QPoint( ( penPos.x() + devPos.x() ) / 2, |
158 | ( penPos.y() + devPos.y() ) / 2 ); | 190 | ( penPos.y() + devPos.y() ) / 2 ); |
159 | } | 191 | } |
160 | 192 | ||
161 | void Calibrate::mouseReleaseEvent( QMouseEvent * ) | 193 | void Calibrate::mouseReleaseEvent( QMouseEvent * ) |
162 | { | 194 | { |
163 | if ( timer->isActive() ) | 195 | if ( timer->isActive() ) |
164 | return ; | 196 | return ; |
165 | 197 | ||
166 | bool doMove = TRUE; | 198 | bool doMove = TRUE; |
167 | 199 | ||
168 | cd.devPoints[ location ] = penPos; | 200 | cd.devPoints[ location ] = penPos; |
169 | if ( location == QWSPointerCalibrationData::TopLeft ) { | 201 | if ( location < QWSPointerCalibrationData::TopRight ) { |
170 | location = QWSPointerCalibrationData::BottomRight; | 202 | location = (QWSPointerCalibrationData::Location) ( int( location ) + 1 ); |
171 | } | 203 | } |
172 | else { | 204 | else { |
173 | if ( sanityCheck() ) { | 205 | if ( sanityCheck() ) { |
174 | reset(); | 206 | reset(); |
175 | goodcd = cd; | 207 | goodcd = cd; |
176 | hide(); | 208 | hide(); |
177 | emit accept(); | 209 | emit accept(); |
178 | doMove = FALSE; | 210 | doMove = FALSE; |
179 | } | 211 | } |
180 | else { | 212 | else { |
181 | location = QWSPointerCalibrationData::TopLeft; | 213 | location = QWSPointerCalibrationData::TopLeft; |
182 | } | 214 | } |
183 | } | 215 | } |
184 | 216 | ||
185 | if ( doMove ) { | 217 | if ( doMove ) { |
186 | QPoint target = fromDevice( cd.screenPoints[ location ] ); | 218 | QPoint target = fromDevice( cd.screenPoints[ location ] ); |
187 | dx = ( target.x() - crossPos.x() ) / 10; | 219 | dx = ( target.x() - crossPos.x() ) / 10; |
188 | dy = ( target.y() - crossPos.y() ) / 10; | 220 | dy = ( target.y() - crossPos.y() ) / 10; |
189 | timer->start( 30 ); | 221 | timer->start( 30 ); |
190 | } | 222 | } |
191 | } | 223 | } |
192 | 224 | ||
193 | void Calibrate::timeout() | 225 | void Calibrate::timeout() |
194 | { | 226 | { |
195 | QPoint target = fromDevice( cd.screenPoints[ location ] ); | 227 | QPoint target = fromDevice( cd.screenPoints[ location ] ); |
196 | 228 | ||
197 | bool doneX = FALSE; | 229 | bool doneX = FALSE; |
198 | bool doneY = FALSE; | 230 | bool doneY = FALSE; |
199 | QPoint newPos( crossPos.x() + dx, crossPos.y() + dy ); | 231 | QPoint newPos( crossPos.x() + dx, crossPos.y() + dy ); |
200 | 232 | ||
201 | if ( QABS( crossPos.x() - target.x() ) <= QABS( dx ) ) { | 233 | if ( QABS( crossPos.x() - target.x() ) <= QABS( dx ) ) { |
202 | newPos.setX( target.x() ); | 234 | newPos.setX( target.x() ); |
203 | doneX = TRUE; | 235 | doneX = TRUE; |
204 | } | 236 | } |
205 | 237 | ||
206 | if ( QABS( crossPos.y() - target.y() ) <= QABS( dy ) ) { | 238 | if ( QABS( crossPos.y() - target.y() ) <= QABS( dy ) ) { |
207 | newPos.setY( target.y() ); | 239 | newPos.setY( target.y() ); |
208 | doneY = TRUE; | 240 | doneY = TRUE; |
209 | } | 241 | } |
210 | 242 | ||
211 | if ( doneX && doneY ) { | 243 | if ( doneX && doneY ) { |
212 | penPos = QPoint(); | 244 | penPos = QPoint(); |
213 | timer->stop(); | 245 | timer->stop(); |
214 | } | 246 | } |
215 | 247 | ||
216 | moveCrosshair( newPos ); | 248 | moveCrosshair( newPos ); |
217 | } | 249 | } |
218 | 250 | ||
219 | #endif // _WS_QWS_ | 251 | #endif // _WS_QWS_ |