source: trunk/examples/widgets/tablet/tabletcanvas.cpp@ 336

Last change on this file since 336 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <math.h>
44
45#include "tabletcanvas.h"
46
47//! [0]
48TabletCanvas::TabletCanvas()
49{
50 resize(500, 500);
51 myBrush = QBrush();
52 myPen = QPen();
53 initImage();
54 setAutoFillBackground(true);
55 deviceDown = false;
56 myColor = Qt::red;
57 myTabletDevice = QTabletEvent::Stylus;
58 alphaChannelType = NoAlpha;
59 colorSaturationType = NoSaturation;
60 lineWidthType = LineWidthPressure;
61}
62
63void TabletCanvas::initImage()
64{
65 QImage newImage = QImage(width(), height(), QImage::Format_ARGB32);
66 QPainter painter(&newImage);
67 painter.fillRect(0, 0, newImage.width(), newImage.height(), Qt::white);
68 if (!image.isNull())
69 painter.drawImage(0, 0, image);
70 painter.end();
71 image = newImage;
72}
73//! [0]
74
75//! [1]
76bool TabletCanvas::saveImage(const QString &file)
77{
78 return image.save(file);
79}
80//! [1]
81
82//! [2]
83bool TabletCanvas::loadImage(const QString &file)
84{
85 bool success = image.load(file);
86
87 if (success) {
88 update();
89 return true;
90 }
91 return false;
92}
93//! [2]
94
95//! [3]
96void TabletCanvas::tabletEvent(QTabletEvent *event)
97{
98
99 switch (event->type()) {
100 case QEvent::TabletPress:
101 if (!deviceDown)
102 deviceDown = true;
103 break;
104 case QEvent::TabletRelease:
105 if (deviceDown)
106 deviceDown = false;
107 break;
108 case QEvent::TabletMove:
109 polyLine[2] = polyLine[1];
110 polyLine[1] = polyLine[0];
111 polyLine[0] = event->pos();
112
113 if (deviceDown) {
114 updateBrush(event);
115 QPainter painter(&image);
116 paintImage(painter, event);
117 }
118 break;
119 default:
120 break;
121 }
122 update();
123}
124//! [3]
125
126//! [4]
127void TabletCanvas::paintEvent(QPaintEvent *)
128{
129 QPainter painter(this);
130 painter.drawImage(QPoint(0, 0), image);
131}
132//! [4]
133
134//! [5]
135void TabletCanvas::paintImage(QPainter &painter, QTabletEvent *event)
136{
137 QPoint brushAdjust(10, 10);
138
139 switch (myTabletDevice) {
140 case QTabletEvent::Stylus:
141 painter.setBrush(myBrush);
142 painter.setPen(myPen);
143 painter.drawLine(polyLine[1], event->pos());
144 break;
145 case QTabletEvent::Airbrush:
146 myBrush.setColor(myColor);
147 myBrush.setStyle(brushPattern(event->pressure()));
148 painter.setPen(Qt::NoPen);
149 painter.setBrush(myBrush);
150
151 for (int i = 0; i < 3; ++i) {
152 painter.drawEllipse(QRect(polyLine[i] - brushAdjust,
153 polyLine[i] + brushAdjust));
154 }
155 break;
156 case QTabletEvent::Puck:
157 case QTabletEvent::FourDMouse:
158 case QTabletEvent::RotationStylus:
159 qWarning("This input device is not supported by the example.");
160 break;
161 default:
162 qWarning("Unknown tablet device.");
163 }
164}
165//! [5]
166
167//! [6]
168Qt::BrushStyle TabletCanvas::brushPattern(qreal value)
169{
170 int pattern = int((value) * 100.0) % 7;
171
172 switch (pattern) {
173 case 0:
174 return Qt::SolidPattern;
175 case 1:
176 return Qt::Dense1Pattern;
177 case 2:
178 return Qt::Dense2Pattern;
179 case 3:
180 return Qt::Dense3Pattern;
181 case 4:
182 return Qt::Dense4Pattern;
183 case 5:
184 return Qt::Dense5Pattern;
185 case 6:
186 return Qt::Dense6Pattern;
187 default:
188 return Qt::Dense7Pattern;
189 }
190}
191//! [6]
192
193//! [7]
194void TabletCanvas::updateBrush(QTabletEvent *event)
195{
196 int hue, saturation, value, alpha;
197 myColor.getHsv(&hue, &saturation, &value, &alpha);
198
199 int vValue = int(((event->yTilt() + 60.0) / 120.0) * 255);
200 int hValue = int(((event->xTilt() + 60.0) / 120.0) * 255);
201//! [7] //! [8]
202
203 switch (alphaChannelType) {
204 case AlphaPressure:
205 myColor.setAlpha(int(event->pressure() * 255.0));
206 break;
207 case AlphaTilt:
208 myColor.setAlpha(maximum(abs(vValue - 127), abs(hValue - 127)));
209 break;
210 default:
211 myColor.setAlpha(255);
212 }
213
214//! [8] //! [9]
215 switch (colorSaturationType) {
216 case SaturationVTilt:
217 myColor.setHsv(hue, vValue, value, alpha);
218 break;
219 case SaturationHTilt:
220 myColor.setHsv(hue, hValue, value, alpha);
221 break;
222 case SaturationPressure:
223 myColor.setHsv(hue, int(event->pressure() * 255.0), value, alpha);
224 break;
225 default:
226 ;
227 }
228
229//! [9] //! [10]
230 switch (lineWidthType) {
231 case LineWidthPressure:
232 myPen.setWidthF(event->pressure() * 10 + 1);
233 break;
234 case LineWidthTilt:
235 myPen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12);
236 break;
237 default:
238 myPen.setWidthF(1);
239 }
240
241//! [10] //! [11]
242 if (event->pointerType() == QTabletEvent::Eraser) {
243 myBrush.setColor(Qt::white);
244 myPen.setColor(Qt::white);
245 myPen.setWidthF(event->pressure() * 10 + 1);
246 } else {
247 myBrush.setColor(myColor);
248 myPen.setColor(myColor);
249 }
250}
251//! [11]
252
253void TabletCanvas::resizeEvent(QResizeEvent *event)
254{
255 initImage();
256 polyLine[0] = polyLine[1] = polyLine[2] = QPoint();
257}
Note: See TracBrowser for help on using the repository browser.