author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
#include <math.h> |
|
43 |
||
44 |
#include "tabletcanvas.h" |
|
45 |
||
46 |
//! [0] |
|
47 |
TabletCanvas::TabletCanvas() |
|
48 |
{ |
|
49 |
resize(500, 500); |
|
50 |
myBrush = QBrush(); |
|
51 |
myPen = QPen(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
initPixmap(); |
0 | 53 |
setAutoFillBackground(true); |
54 |
deviceDown = false; |
|
55 |
myColor = Qt::red; |
|
56 |
myTabletDevice = QTabletEvent::Stylus; |
|
57 |
alphaChannelType = NoAlpha; |
|
58 |
colorSaturationType = NoSaturation; |
|
59 |
lineWidthType = LineWidthPressure; |
|
60 |
} |
|
61 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
void TabletCanvas::initPixmap() |
0 | 63 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
64 |
QPixmap newPixmap = QPixmap(width(), height()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
65 |
newPixmap.fill(Qt::white); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
66 |
QPainter painter(&newPixmap); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
67 |
if (!pixmap.isNull()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
68 |
painter.drawPixmap(0, 0, pixmap); |
0 | 69 |
painter.end(); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
pixmap = newPixmap; |
0 | 71 |
} |
72 |
//! [0] |
|
73 |
||
74 |
//! [1] |
|
75 |
bool TabletCanvas::saveImage(const QString &file) |
|
76 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
return pixmap.save(file); |
0 | 78 |
} |
79 |
//! [1] |
|
80 |
||
81 |
//! [2] |
|
82 |
bool TabletCanvas::loadImage(const QString &file) |
|
83 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
bool success = pixmap.load(file); |
0 | 85 |
|
86 |
if (success) { |
|
87 |
update(); |
|
88 |
return true; |
|
89 |
} |
|
90 |
return false; |
|
91 |
} |
|
92 |
//! [2] |
|
93 |
||
94 |
//! [3] |
|
95 |
void TabletCanvas::tabletEvent(QTabletEvent *event) |
|
96 |
{ |
|
97 |
||
98 |
switch (event->type()) { |
|
99 |
case QEvent::TabletPress: |
|
100 |
if (!deviceDown) { |
|
101 |
deviceDown = true; |
|
102 |
polyLine[0] = polyLine[1] = polyLine[2] = event->pos(); |
|
103 |
} |
|
104 |
break; |
|
105 |
case QEvent::TabletRelease: |
|
106 |
if (deviceDown) |
|
107 |
deviceDown = false; |
|
108 |
break; |
|
109 |
case QEvent::TabletMove: |
|
110 |
polyLine[2] = polyLine[1]; |
|
111 |
polyLine[1] = polyLine[0]; |
|
112 |
polyLine[0] = event->pos(); |
|
113 |
||
114 |
if (deviceDown) { |
|
115 |
updateBrush(event); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
QPainter painter(&pixmap); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
paintPixmap(painter, event); |
0 | 118 |
} |
119 |
break; |
|
120 |
default: |
|
121 |
break; |
|
122 |
} |
|
123 |
update(); |
|
124 |
} |
|
125 |
//! [3] |
|
126 |
||
127 |
//! [4] |
|
128 |
void TabletCanvas::paintEvent(QPaintEvent *) |
|
129 |
{ |
|
130 |
QPainter painter(this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
painter.drawPixmap(0, 0, pixmap); |
0 | 132 |
} |
133 |
//! [4] |
|
134 |
||
135 |
//! [5] |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
void TabletCanvas::paintPixmap(QPainter &painter, QTabletEvent *event) |
0 | 137 |
{ |
138 |
QPoint brushAdjust(10, 10); |
|
139 |
||
140 |
switch (myTabletDevice) { |
|
141 |
case QTabletEvent::Airbrush: |
|
142 |
myBrush.setColor(myColor); |
|
143 |
myBrush.setStyle(brushPattern(event->pressure())); |
|
144 |
painter.setPen(Qt::NoPen); |
|
145 |
painter.setBrush(myBrush); |
|
146 |
||
147 |
for (int i = 0; i < 3; ++i) { |
|
148 |
painter.drawEllipse(QRect(polyLine[i] - brushAdjust, |
|
149 |
polyLine[i] + brushAdjust)); |
|
150 |
} |
|
151 |
break; |
|
152 |
case QTabletEvent::Puck: |
|
153 |
case QTabletEvent::FourDMouse: |
|
154 |
case QTabletEvent::RotationStylus: |
|
155 |
{ |
|
156 |
const QString error(tr("This input device is not supported by the example.")); |
|
157 |
#ifndef QT_NO_STATUSTIP |
|
158 |
QStatusTipEvent status(error); |
|
159 |
QApplication::sendEvent(this, &status); |
|
160 |
#else |
|
161 |
qWarning() << error; |
|
162 |
#endif |
|
163 |
} |
|
164 |
break; |
|
165 |
default: |
|
166 |
{ |
|
167 |
const QString error(tr("Unknown tablet device - treating as stylus")); |
|
168 |
#ifndef QT_NO_STATUSTIP |
|
169 |
QStatusTipEvent status(error); |
|
170 |
QApplication::sendEvent(this, &status); |
|
171 |
#else |
|
172 |
qWarning() << error; |
|
173 |
#endif |
|
174 |
} |
|
175 |
// FALL-THROUGH |
|
176 |
case QTabletEvent::Stylus: |
|
177 |
painter.setBrush(myBrush); |
|
178 |
painter.setPen(myPen); |
|
179 |
painter.drawLine(polyLine[1], event->pos()); |
|
180 |
break; |
|
181 |
} |
|
182 |
} |
|
183 |
//! [5] |
|
184 |
||
185 |
//! [6] |
|
186 |
Qt::BrushStyle TabletCanvas::brushPattern(qreal value) |
|
187 |
{ |
|
188 |
int pattern = int((value) * 100.0) % 7; |
|
189 |
||
190 |
switch (pattern) { |
|
191 |
case 0: |
|
192 |
return Qt::SolidPattern; |
|
193 |
case 1: |
|
194 |
return Qt::Dense1Pattern; |
|
195 |
case 2: |
|
196 |
return Qt::Dense2Pattern; |
|
197 |
case 3: |
|
198 |
return Qt::Dense3Pattern; |
|
199 |
case 4: |
|
200 |
return Qt::Dense4Pattern; |
|
201 |
case 5: |
|
202 |
return Qt::Dense5Pattern; |
|
203 |
case 6: |
|
204 |
return Qt::Dense6Pattern; |
|
205 |
default: |
|
206 |
return Qt::Dense7Pattern; |
|
207 |
} |
|
208 |
} |
|
209 |
//! [6] |
|
210 |
||
211 |
//! [7] |
|
212 |
void TabletCanvas::updateBrush(QTabletEvent *event) |
|
213 |
{ |
|
214 |
int hue, saturation, value, alpha; |
|
215 |
myColor.getHsv(&hue, &saturation, &value, &alpha); |
|
216 |
||
217 |
int vValue = int(((event->yTilt() + 60.0) / 120.0) * 255); |
|
218 |
int hValue = int(((event->xTilt() + 60.0) / 120.0) * 255); |
|
219 |
//! [7] //! [8] |
|
220 |
||
221 |
switch (alphaChannelType) { |
|
222 |
case AlphaPressure: |
|
223 |
myColor.setAlpha(int(event->pressure() * 255.0)); |
|
224 |
break; |
|
225 |
case AlphaTilt: |
|
226 |
myColor.setAlpha(maximum(abs(vValue - 127), abs(hValue - 127))); |
|
227 |
break; |
|
228 |
default: |
|
229 |
myColor.setAlpha(255); |
|
230 |
} |
|
231 |
||
232 |
//! [8] //! [9] |
|
233 |
switch (colorSaturationType) { |
|
234 |
case SaturationVTilt: |
|
235 |
myColor.setHsv(hue, vValue, value, alpha); |
|
236 |
break; |
|
237 |
case SaturationHTilt: |
|
238 |
myColor.setHsv(hue, hValue, value, alpha); |
|
239 |
break; |
|
240 |
case SaturationPressure: |
|
241 |
myColor.setHsv(hue, int(event->pressure() * 255.0), value, alpha); |
|
242 |
break; |
|
243 |
default: |
|
244 |
; |
|
245 |
} |
|
246 |
||
247 |
//! [9] //! [10] |
|
248 |
switch (lineWidthType) { |
|
249 |
case LineWidthPressure: |
|
250 |
myPen.setWidthF(event->pressure() * 10 + 1); |
|
251 |
break; |
|
252 |
case LineWidthTilt: |
|
253 |
myPen.setWidthF(maximum(abs(vValue - 127), abs(hValue - 127)) / 12); |
|
254 |
break; |
|
255 |
default: |
|
256 |
myPen.setWidthF(1); |
|
257 |
} |
|
258 |
||
259 |
//! [10] //! [11] |
|
260 |
if (event->pointerType() == QTabletEvent::Eraser) { |
|
261 |
myBrush.setColor(Qt::white); |
|
262 |
myPen.setColor(Qt::white); |
|
263 |
myPen.setWidthF(event->pressure() * 10 + 1); |
|
264 |
} else { |
|
265 |
myBrush.setColor(myColor); |
|
266 |
myPen.setColor(myColor); |
|
267 |
} |
|
268 |
} |
|
269 |
//! [11] |
|
270 |
||
271 |
void TabletCanvas::resizeEvent(QResizeEvent *) |
|
272 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
273 |
initPixmap(); |
0 | 274 |
polyLine[0] = polyLine[1] = polyLine[2] = QPoint(); |
275 |
} |