-rw-r--r-- | core/apps/calibrate/calibrate.cpp | 282 |
1 files changed, 129 insertions, 153 deletions
diff --git a/core/apps/calibrate/calibrate.cpp b/core/apps/calibrate/calibrate.cpp index 7b60e64..54fa8ec 100644 --- a/core/apps/calibrate/calibrate.cpp +++ b/core/apps/calibrate/calibrate.cpp | |||
@@ -1,243 +1,219 @@ | |||
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 | } else { | 45 | } |
46 | logo = Resource::loadPixmap( "launcher/opielogo" ); | 46 | else { |
47 | } | 47 | logo = Resource::loadPixmap( "launcher/opielogo" ); |
48 | cd.screenPoints[QWSPointerCalibrationData::TopLeft] = QPoint( offset, offset ); | 48 | } |
49 | cd.screenPoints[QWSPointerCalibrationData::BottomLeft] = QPoint( offset, qt_screen->deviceHeight() - offset ); | 49 | cd.screenPoints[ QWSPointerCalibrationData::TopLeft ] = QPoint( offset, offset ); |
50 | cd.screenPoints[QWSPointerCalibrationData::BottomRight] = QPoint( qt_screen->deviceWidth() - offset, qt_screen->deviceHeight() - offset ); | 50 | cd.screenPoints[ QWSPointerCalibrationData::BottomLeft ] = QPoint( offset, qt_screen->deviceHeight() - offset ); |
51 | cd.screenPoints[QWSPointerCalibrationData::TopRight] = QPoint( qt_screen->deviceWidth() - offset, offset ); | 51 | cd.screenPoints[ QWSPointerCalibrationData::BottomRight ] = QPoint( qt_screen->deviceWidth() - offset, qt_screen->deviceHeight() - offset ); |
52 | cd.screenPoints[QWSPointerCalibrationData::Center] = QPoint( qt_screen->deviceWidth()/2, qt_screen->deviceHeight()/2 ); | 52 | cd.screenPoints[ QWSPointerCalibrationData::TopRight ] = QPoint( qt_screen->deviceWidth() - offset, offset ); |
53 | goodcd = cd; | 53 | cd.screenPoints[ QWSPointerCalibrationData::Center ] = QPoint( qt_screen->deviceWidth() / 2, qt_screen->deviceHeight() / 2 ); |
54 | reset(); | 54 | goodcd = cd; |
55 | 55 | reset(); | |
56 | timer = new QTimer( this ); | 56 | |
57 | connect( timer, SIGNAL(timeout()), this, SLOT(timeout()) ); | 57 | timer = new QTimer( this ); |
58 | connect( timer, SIGNAL( timeout() ), this, SLOT( timeout() ) ); | ||
58 | } | 59 | } |
59 | 60 | ||
60 | Calibrate::~Calibrate() | 61 | Calibrate::~Calibrate() |
61 | { | 62 | { |
62 | store(); | 63 | store(); |
63 | } | 64 | } |
64 | 65 | ||
65 | void Calibrate::show() | 66 | void Calibrate::show() |
66 | { | 67 | { |
67 | grabMouse(); | 68 | grabMouse(); |
68 | QWSServer::mouseHandler()->getCalibration(&goodcd); | 69 | QWSServer::mouseHandler() ->getCalibration( &goodcd ); |
69 | QWSServer::mouseHandler()->clearCalibration(); | 70 | QWSServer::mouseHandler() ->clearCalibration(); |
70 | QDialog::show(); | 71 | QDialog::show(); |
71 | } | 72 | } |
72 | 73 | ||
73 | void Calibrate::store() | 74 | void Calibrate::store() |
74 | { | 75 | { |
75 | QWSServer::mouseHandler()->calibrate( &goodcd ); | 76 | QWSServer::mouseHandler() ->calibrate( &goodcd ); |
76 | } | 77 | } |
77 | 78 | ||
78 | void Calibrate::hide() | 79 | void Calibrate::hide() |
79 | { | 80 | { |
80 | if ( isVisible() ) | 81 | if ( isVisible() ) |
81 | store(); | 82 | store(); |
82 | QDialog::hide(); | 83 | QDialog::hide(); |
83 | } | 84 | } |
84 | 85 | ||
85 | void Calibrate::reset() | 86 | void Calibrate::reset() |
86 | { | 87 | { |
87 | penPos = QPoint(); | 88 | penPos = QPoint(); |
88 | location = QWSPointerCalibrationData::TopLeft; | 89 | location = QWSPointerCalibrationData::TopLeft; |
89 | crossPos = fromDevice( cd.screenPoints[location] ); | 90 | crossPos = fromDevice( cd.screenPoints[ location ] ); |
90 | } | 91 | } |
91 | 92 | ||
92 | QPoint Calibrate::fromDevice( const QPoint &p ) | 93 | QPoint Calibrate::fromDevice( const QPoint &p ) |
93 | { | 94 | { |
94 | return qt_screen->mapFromDevice( p, | 95 | return qt_screen->mapFromDevice ( p, QSize( qt_screen->deviceWidth ( ), qt_screen->deviceHeight() ) ); |
95 | QSize(qt_screen->deviceWidth(), qt_screen->deviceHeight()) ); | ||
96 | } | 96 | } |
97 | 97 | ||
98 | bool Calibrate::sanityCheck() | 98 | bool Calibrate::sanityCheck() |
99 | { | 99 | { |
100 | QPoint tl = cd.devPoints[QWSPointerCalibrationData::TopLeft]; | 100 | QRect r ( cd.devPoints[ QWSPointerCalibrationData::TopLeft ], cd.devPoints[ QWSPointerCalibrationData::BottomRight ] ); |
101 | QPoint tr = cd.devPoints[QWSPointerCalibrationData::TopRight]; | 101 | r = r. normalize ( ); // This should also handle rotated TS controllers |
102 | QPoint bl = cd.devPoints[QWSPointerCalibrationData::BottomLeft]; | 102 | |
103 | QPoint br = cd.devPoints[QWSPointerCalibrationData::BottomRight]; | 103 | cd. devPoints [QWSPointerCalibrationData::TopRight] = r. topRight ( ); |
104 | 104 | cd. devPoints [QWSPointerCalibrationData::BottomLeft] = r. bottomLeft ( ); | |
105 | int vl = QABS( tl.y() - bl.y() ); | 105 | cd. devPoints [QWSPointerCalibrationData::Center] = r. center ( ); |
106 | int vr = QABS( tr.y() - br.y() ); | 106 | |
107 | int diff = QABS( vl - vr ); | 107 | return true; |
108 | int avg = ( vl + vr ) / 2; | 108 | } |
109 | if ( diff > avg / 20 ) // 5% leeway | ||
110 | return FALSE; | ||
111 | |||
112 | int ht = QABS( tl.x() - tr.x() ); | ||
113 | int hb = QABS( br.x() - bl.x() ); | ||
114 | diff = QABS( ht - hb ); | ||
115 | avg = ( ht + hb ) / 2; | ||
116 | if ( diff > avg / 20 ) // 5% leeway | ||
117 | return FALSE; | ||
118 | |||
119 | return TRUE; | ||
120 | } | ||
121 | 109 | ||
122 | void Calibrate::moveCrosshair( QPoint pt ) | 110 | void Calibrate::moveCrosshair( QPoint pt ) |
123 | { | 111 | { |
124 | /* | 112 | showCross = FALSE; |
125 | QPainter p( this ); | 113 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); |
126 | p.drawPixmap( crossPos.x()-8, crossPos.y()-8, saveUnder ); | 114 | showCross = TRUE; |
127 | saveUnder = QPixmap::grabWindow( winId(), pt.x()-8, pt.y()-8, 16, 16 ); | 115 | crossPos = pt; |
128 | p.drawRect( pt.x()-1, pt.y()-8, 2, 7 ); | 116 | repaint( crossPos.x() - 8, crossPos.y() - 8, 16, 16 ); |
129 | p.drawRect( pt.x()-1, pt.y()+1, 2, 7 ); | ||
130 | p.drawRect( pt.x()-8, pt.y()-1, 7, 2 ); | ||
131 | p.drawRect( pt.x()+1, pt.y()-1, 7, 2 ); | ||
132 | */ | ||
133 | showCross = FALSE; | ||
134 | repaint( crossPos.x()-8, crossPos.y()-8, 16, 16 ); | ||
135 | showCross = TRUE; | ||
136 | crossPos = pt; | ||
137 | repaint( crossPos.x()-8, crossPos.y()-8, 16, 16 ); | ||
138 | } | 117 | } |
139 | 118 | ||
140 | void Calibrate::paintEvent( QPaintEvent * ) | 119 | void Calibrate::paintEvent( QPaintEvent * ) |
141 | { | 120 | { |
142 | QPainter p( this ); | 121 | QPainter p( this ); |
143 | 122 | ||
144 | int y; | 123 | int y; |
145 | 124 | ||
146 | if ( !logo.isNull() ) { | 125 | if ( !logo.isNull() ) { |
147 | y = height() / 2 - logo.height() - 15; | 126 | y = height() / 2 - logo.height() - 15; |
148 | p.drawPixmap( (width() - logo.width())/2, y, logo ); | 127 | p.drawPixmap( ( width() - logo.width() ) / 2, y, logo ); |
149 | } | 128 | } |
150 | 129 | ||
151 | y = height() / 2 + 15; | 130 | y = height() / 2 + 15; |
152 | 131 | ||
153 | p.drawText( 0, y+height()/8, width(), height() - y, AlignHCenter, | 132 | p.drawText( 0, y + height() / 8, width(), height() - y, AlignHCenter, |
154 | tr("Touch the crosshairs firmly and\n" | 133 | tr( "Touch the crosshairs firmly and\n" |
155 | "accurately to calibrate your screen.") ); | 134 | "accurately to calibrate your screen." ) ); |
156 | 135 | ||
157 | QFont f = p.font(); f.setBold(TRUE); | 136 | QFont f = p.font(); |
158 | p.setFont( f ); | 137 | f.setBold( TRUE ); |
159 | p.drawText( 0, y, width(), height() - y, AlignHCenter|WordBreak, | 138 | p.setFont( f ); |
160 | tr("Welcome to Opie") ); | 139 | p.drawText( 0, y, width(), height() - y, AlignHCenter | WordBreak, |
161 | 140 | tr( "Welcome to Opie" ) ); | |
162 | /* | 141 | |
163 | saveUnder = QPixmap::grabWindow( winId(), crossPos.x()-8, crossPos.y()-8, | 142 | if ( showCross ) { |
164 | 16, 16 ); | 143 | p.drawRect( crossPos.x() - 1, crossPos.y() - 8, 2, 7 ); |
165 | moveCrosshair( crossPos ); | 144 | p.drawRect( crossPos.x() - 1, crossPos.y() + 1, 2, 7 ); |
166 | */ | 145 | p.drawRect( crossPos.x() - 8, crossPos.y() - 1, 7, 2 ); |
167 | if ( showCross ) { | 146 | p.drawRect( crossPos.x() + 1, crossPos.y() - 1, 7, 2 ); |
168 | p.drawRect( crossPos.x()-1, crossPos.y()-8, 2, 7 ); | 147 | } |
169 | p.drawRect( crossPos.x()-1, crossPos.y()+1, 2, 7 ); | ||
170 | p.drawRect( crossPos.x()-8, crossPos.y()-1, 7, 2 ); | ||
171 | p.drawRect( crossPos.x()+1, crossPos.y()-1, 7, 2 ); | ||
172 | } | ||
173 | } | 148 | } |
174 | 149 | ||
175 | void Calibrate::mousePressEvent( QMouseEvent *e ) | 150 | void Calibrate::mousePressEvent( QMouseEvent *e ) |
176 | { | 151 | { |
177 | // map to device coordinates | 152 | // map to device coordinates |
178 | QPoint devPos = qt_screen->mapToDevice( e->pos(), | 153 | QPoint devPos = qt_screen->mapToDevice( e->pos(), QSize( qt_screen->width(), qt_screen->height() ) ); |
179 | QSize(qt_screen->width(), qt_screen->height()) ); | 154 | if ( penPos.isNull() ) |
180 | if ( penPos.isNull() ) | 155 | penPos = devPos; |
181 | penPos = devPos; | 156 | else |
182 | else | 157 | penPos = QPoint( ( penPos.x() + devPos.x() ) / 2, |
183 | penPos = QPoint( (penPos.x() + devPos.x())/2, | 158 | ( penPos.y() + devPos.y() ) / 2 ); |
184 | (penPos.y() + devPos.y())/2 ); | ||
185 | } | 159 | } |
186 | 160 | ||
187 | void Calibrate::mouseReleaseEvent( QMouseEvent * ) | 161 | void Calibrate::mouseReleaseEvent( QMouseEvent * ) |
188 | { | 162 | { |
189 | if ( timer->isActive() ) | 163 | if ( timer->isActive() ) |
190 | return; | 164 | return ; |
191 | 165 | ||
192 | bool doMove = TRUE; | 166 | bool doMove = TRUE; |
193 | 167 | ||
194 | cd.devPoints[location] = penPos; | 168 | cd.devPoints[ location ] = penPos; |
195 | if ( location < QWSPointerCalibrationData::LastLocation ) { | 169 | if ( location == QWSPointerCalibrationData::TopLeft ) { |
196 | location = (QWSPointerCalibrationData::Location)((int)location + 1); | 170 | location = QWSPointerCalibrationData::BottomRight; |
197 | } else { | 171 | } |
198 | if ( sanityCheck() ) { | 172 | else { |
199 | reset(); | 173 | if ( sanityCheck() ) { |
200 | goodcd = cd; | 174 | reset(); |
201 | hide(); | 175 | goodcd = cd; |
202 | emit accept(); | 176 | hide(); |
203 | doMove = FALSE; | 177 | emit accept(); |
204 | } else { | 178 | doMove = FALSE; |
205 | location = QWSPointerCalibrationData::TopLeft; | 179 | } |
206 | } | 180 | else { |
207 | } | 181 | location = QWSPointerCalibrationData::TopLeft; |
208 | 182 | } | |
209 | if ( doMove ) { | 183 | } |
210 | QPoint target = fromDevice( cd.screenPoints[location] ); | 184 | |
211 | dx = (target.x() - crossPos.x())/10; | 185 | if ( doMove ) { |
212 | dy = (target.y() - crossPos.y())/10; | 186 | QPoint target = fromDevice( cd.screenPoints[ location ] ); |
213 | timer->start( 30 ); | 187 | dx = ( target.x() - crossPos.x() ) / 10; |
214 | } | 188 | dy = ( target.y() - crossPos.y() ) / 10; |
189 | timer->start( 30 ); | ||
190 | } | ||
215 | } | 191 | } |
216 | 192 | ||
217 | void Calibrate::timeout() | 193 | void Calibrate::timeout() |
218 | { | 194 | { |
219 | QPoint target = fromDevice( cd.screenPoints[location] ); | 195 | QPoint target = fromDevice( cd.screenPoints[ location ] ); |
220 | 196 | ||
221 | bool doneX = FALSE; | 197 | bool doneX = FALSE; |
222 | bool doneY = FALSE; | 198 | bool doneY = FALSE; |
223 | QPoint newPos( crossPos.x() + dx, crossPos.y() + dy ); | 199 | QPoint newPos( crossPos.x() + dx, crossPos.y() + dy ); |
224 | 200 | ||
225 | if ( QABS(crossPos.x() - target.x()) <= QABS(dx) ) { | 201 | if ( QABS( crossPos.x() - target.x() ) <= QABS( dx ) ) { |
226 | newPos.setX( target.x() ); | 202 | newPos.setX( target.x() ); |
227 | doneX = TRUE; | 203 | doneX = TRUE; |
228 | } | 204 | } |
229 | 205 | ||
230 | if ( QABS(crossPos.y() - target.y()) <= QABS(dy) ) { | 206 | if ( QABS( crossPos.y() - target.y() ) <= QABS( dy ) ) { |
231 | newPos.setY(target.y()); | 207 | newPos.setY( target.y() ); |
232 | doneY = TRUE; | 208 | doneY = TRUE; |
233 | } | 209 | } |
234 | 210 | ||
235 | if ( doneX && doneY ) { | 211 | if ( doneX && doneY ) { |
236 | penPos = QPoint(); | 212 | penPos = QPoint(); |
237 | timer->stop(); | 213 | timer->stop(); |
238 | } | 214 | } |
239 | 215 | ||
240 | moveCrosshair( newPos ); | 216 | moveCrosshair( newPos ); |
241 | } | 217 | } |
242 | 218 | ||
243 | #endif // _WS_QWS_ | 219 | #endif // _WS_QWS_ |