summaryrefslogtreecommitdiff
path: root/core/apps/calibrate/calibrate.cpp
blob: 5f52bdcb6ce7fede0f6938ecb78ff0cbcda3906c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include <math.h>

#include "calibrate.h"

#include <qpe/resource.h>
#include <qpe/qcopenvelope_qws.h>
#include <qapplication.h>

#include <qpixmap.h>
#include <qimage.h>
#include <qpainter.h>
#include <qtimer.h>
#include <qwindowsystem_qws.h>
#include <qgfx_qws.h>


Calibrate::Calibrate( QWidget* parent, const char * name, WFlags wf ) :
		QDialog( parent, name, TRUE, wf | WStyle_Tool | WStyle_Customize | WStyle_StaysOnTop )
{
#ifdef QWS
	showCross = TRUE;
	const int offset = 30;
	QRect desk = qApp->desktop() ->geometry();
	setGeometry( 0, 0, desk.width(), desk.height() );
	crosshair.convertFromImage( Resource::loadImage("launcher/crosshair") );

        splash.convertFromImage( Resource::loadImage("launcher/firstuse").smoothScale( width(), height() ) );
        setBackgroundPixmap( splash );

	cd.screenPoints[ QWSPointerCalibrationData::TopLeft ] = QPoint( offset, offset );
	cd.screenPoints[ QWSPointerCalibrationData::BottomLeft ] = QPoint( offset, qt_screen->deviceHeight() - offset );
	cd.screenPoints[ QWSPointerCalibrationData::BottomRight ] = QPoint( qt_screen->deviceWidth() - offset, qt_screen->deviceHeight() - offset );
	cd.screenPoints[ QWSPointerCalibrationData::TopRight ] = QPoint( qt_screen->deviceWidth() - offset, offset );
	cd.screenPoints[ QWSPointerCalibrationData::Center ] = QPoint( qt_screen->deviceWidth() / 2, qt_screen->deviceHeight() / 2 );
	goodcd = cd;
	reset();

	timer = new QTimer( this );
	connect( timer, SIGNAL( timeout() ), this, SLOT( timeout() ) );
#endif
}

Calibrate::~Calibrate()
{
	store();
}

void Calibrate::show()
{
#ifdef QWS
	grabMouse();
	QWSServer::mouseHandler() ->getCalibration( &goodcd );
	QWSServer::mouseHandler() ->clearCalibration();
	QDialog::show();
#endif
}

void Calibrate::store()
{
#ifdef QWS
	QWSServer::mouseHandler() ->calibrate( &goodcd );
#endif
}

void Calibrate::hide()
{
	if ( isVisible()) {
		store();

		// hack - calibrate is a launcher dialog, but treated like a standalone app
		{
			QCopEnvelope e( "QPE/System", "closing(QString)" );
			e << QString ( "calibrate" );
		}
	}
	QDialog::hide();
}

void Calibrate::reset()
{
#ifdef QWS
	penPos = QPoint();
	location = QWSPointerCalibrationData::TopLeft;
	crossPos = fromDevice( cd.screenPoints[ location ] );
#endif
}

QPoint Calibrate::fromDevice( const QPoint &p )
{
#ifdef QWS
	return qt_screen->mapFromDevice ( p, QSize( qt_screen->deviceWidth ( ), qt_screen->deviceHeight() ) );
#else
        return QPoint();
#endif

}

bool Calibrate::sanityCheck()
{
#ifdef QWS
	QPoint tl = cd.devPoints[QWSPointerCalibrationData::TopLeft];
	QPoint tr = cd.devPoints[QWSPointerCalibrationData::TopRight];
	QPoint bl = cd.devPoints[QWSPointerCalibrationData::BottomLeft];
	QPoint br = cd.devPoints[QWSPointerCalibrationData::BottomRight];

	// not needed anywhere .. just calculate it, so it's there
	cd.devPoints[QWSPointerCalibrationData::Center] = QRect( tl, br ).normalize().center();

	int dlx = QABS( bl. x ( ) - tl. x ( ));
	int dly = QABS( bl. y ( ) - tl. y ( ));
	int drx = QABS( br. x ( ) - tr. x ( ));
	int dry = QABS( br. y ( ) - tr. y ( ));
	int dtx = QABS( tr. x ( ) - tl. x ( ));
	int dty = QABS( tr. y ( ) - tl. y ( ));
	int dbx = QABS( br. x ( ) - bl. x ( ));
	int dby = QABS( br. y ( ) - bl. y ( ));

	int dl = (int) ::sqrt (( dlx * dlx ) + ( dly * dly )); // calculate vector lengths for all sides
	int dr = (int) ::sqrt (( drx * drx ) + ( dry * dry ));
	int dt = (int) ::sqrt (( dtx * dtx ) + ( dty * dty ));
	int db = (int) ::sqrt (( dbx * dbx ) + ( dby * dby ));

	// Calculate leeway for x/y (we do not care if diff1/diff2 is for x or y here !)
	int diff1 = QABS( dl - dr );
	int avg1  = ( dl + dr ) / 2;
	int diff2 = QABS( dt - db );
	int avg2 = ( dt + db ) / 2;

	// Calculate leeway for "real" vector length against "manhattan" vector length
	// This is a check, if the rect is rotated (other then 0/90/180/270)
	// It needs to be performed only for the triange (bl, tl, tr)
	int diff3 = QABS(( dlx + dly + dtx + dty ) - ( dl + dt ));
	int avg3 = (( dlx + dly + dtx + dty ) + ( dl + dt )) / 2;

	if (( diff1 > ( avg1 / 20 )) || // 5% leeway
	    ( diff2 > ( avg2 / 20 )) ||
	    ( diff3 > ( avg3 / 20 )))
		return false;
	else
		return true;
#else
return true;
#endif
}

void Calibrate::moveCrosshair( QPoint pt )
{
	showCross = FALSE;
	repaint( crossPos.x() - 14, crossPos.y() - 14, 28, 28 );
	showCross = TRUE;
	crossPos = pt;
	repaint( crossPos.x() - 14, crossPos.y() - 14, 28, 28 );
}

void Calibrate::paintEvent( QPaintEvent * )
{
	QPainter p( this );
	int y = height() / 3;

	p.drawText( 0, y + height() / 8, width(), height() - y, AlignHCenter,
	            tr( "Touch the crosshairs firmly and\n"
	                "accurately to calibrate your screen." ) );

	if ( !showCross ) return;

#if 0
	if ( crosshair.isNull() ) {
#endif
		p.setPen( QColor( 0, 0, 155 ) );
		p.drawEllipse( crossPos.x()-8, crossPos.y()-8, 16, 16 );
		p.setPen( QColor( 250, 220, 220 ) );
		p.drawRoundRect( crossPos.x()-12, crossPos.y()-12, 24, 24, 75, 75 );
		p.setPen( QColor( 0, 0, 120 ) );
		p.drawRect( crossPos.x() - 1, crossPos.y() - 14, 2, 13 );
		p.drawRect( crossPos.x() - 1, crossPos.y() + 1, 2, 13 );
		p.drawRect( crossPos.x() - 14, crossPos.y() - 1, 13, 2 );
		p.drawRect( crossPos.x() + 1, crossPos.y() - 1, 13, 2 );
#if 0
	}
	else p.drawPixmap( crossPos.x(), crossPos.y(), crosshair );
#endif
}

void Calibrate::mousePressEvent( QMouseEvent *e )
{
#ifdef QWS
	// map to device coordinates
	QPoint devPos = qt_screen->mapToDevice( e->pos(), QSize( qt_screen->width(), qt_screen->height() ) );
	if ( penPos.isNull() )
		penPos = devPos;
	else
		penPos = QPoint( ( penPos.x() + devPos.x() ) / 2,
		                 ( penPos.y() + devPos.y() ) / 2 );
#endif
}

void Calibrate::mouseReleaseEvent( QMouseEvent * )
{
#ifdef QWS
	if ( timer->isActive() )
		return ;

	bool doMove = TRUE;

	cd.devPoints[ location ] = penPos;
	if ( location < QWSPointerCalibrationData::TopRight ) {
		location = (QWSPointerCalibrationData::Location) ( int( location ) + 1 );
	}
	else {
		if ( sanityCheck() ) {
			reset();
			goodcd = cd;
			hide();
			emit accept();
			doMove = FALSE;
		}
		else {
			location = QWSPointerCalibrationData::TopLeft;
		}
	}

	if ( doMove ) {
		QPoint target = fromDevice( cd.screenPoints[ location ] );
		dx = ( target.x() - crossPos.x() ) / 10;
		dy = ( target.y() - crossPos.y() ) / 10;
		timer->start( 30 );
	}
#endif
}

void Calibrate::timeout()
{
#ifdef QWS
	QPoint target = fromDevice( cd.screenPoints[ location ] );

	bool doneX = FALSE;
	bool doneY = FALSE;
	QPoint newPos( crossPos.x() + dx, crossPos.y() + dy );

	if ( QABS( crossPos.x() - target.x() ) <= QABS( dx ) ) {
		newPos.setX( target.x() );
		doneX = TRUE;
	}

	if ( QABS( crossPos.y() - target.y() ) <= QABS( dy ) ) {
		newPos.setY( target.y() );
		doneY = TRUE;
	}

	if ( doneX && doneY ) {
		penPos = QPoint();
		timer->stop();
	}

	moveCrosshair( newPos );
#endif
}

//#endif // _WS_QWS_