source: trunk/examples/qws/mousecalibration/calibration.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "calibration.h"
43
44#include <QWSPointerCalibrationData>
45#include <QPainter>
46#include <QFile>
47#include <QTimer>
48#include <QApplication>
49#include <QDesktopWidget>
50#include <QMouseEvent>
51#include <QScreen>
52#include <QWSServer>
53
54//! [0]
55Calibration::Calibration()
56{
57 QRect desktop = QApplication::desktop()->geometry();
58 desktop.moveTo(QPoint(0, 0));
59 setGeometry(desktop);
60
61 setFocusPolicy(Qt::StrongFocus);
62 setFocus();
63 setModal(true);
64//! [0]
65
66//! [1]
67 int width = qt_screen->deviceWidth();
68 int height = qt_screen->deviceHeight();
69
70 int dx = width / 10;
71 int dy = height / 10;
72
73 QPoint *points = data.screenPoints;
74 points[QWSPointerCalibrationData::TopLeft] = QPoint(dx, dy);
75 points[QWSPointerCalibrationData::BottomLeft] = QPoint(dx, height - dy);
76 points[QWSPointerCalibrationData::BottomRight] = QPoint(width - dx, height - dy);
77 points[QWSPointerCalibrationData::TopRight] = QPoint(width - dx, dy);
78 points[QWSPointerCalibrationData::Center] = QPoint(width / 2, height / 2);
79//! [1]
80
81//! [2]
82 pressCount = 0;
83}
84//! [2]
85
86//! [3]
87Calibration::~Calibration()
88{
89}
90//! [3]
91
92//! [4]
93int Calibration::exec()
94{
95 QWSServer::mouseHandler()->clearCalibration();
96 grabMouse();
97 activateWindow();
98 int ret = QDialog::exec();
99 releaseMouse();
100 return ret;
101}
102//! [4]
103
104//! [5]
105void Calibration::paintEvent(QPaintEvent*)
106{
107 QPainter p(this);
108 p.fillRect(rect(), Qt::white);
109
110 QPoint point = data.screenPoints[pressCount];
111
112 // Map to logical coordinates in case the screen is transformed
113 QSize screenSize(qt_screen->deviceWidth(), qt_screen->deviceHeight());
114 point = qt_screen->mapFromDevice(point, screenSize);
115
116 p.fillRect(point.x() - 6, point.y() - 1, 13, 3, Qt::black);
117 p.fillRect(point.x() - 1, point.y() - 6, 3, 13, Qt::black);
118}
119//! [5]
120
121//! [6]
122void Calibration::mouseReleaseEvent(QMouseEvent *event)
123{
124 // Map from device coordinates in case the screen is transformed
125 QSize screenSize(qt_screen->width(), qt_screen->height());
126 QPoint p = qt_screen->mapToDevice(event->pos(), screenSize);
127
128 data.devPoints[pressCount] = p;
129
130 if (++pressCount < 5)
131 repaint();
132 else
133 accept();
134}
135//! [6]
136
137//! [7]
138void Calibration::accept()
139{
140 Q_ASSERT(pressCount == 5);
141 QWSServer::mouseHandler()->calibrate(&data);
142 QDialog::accept();
143}
144//! [7]
145
Note: See TracBrowser for help on using the repository browser.