author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 12 Mar 2010 15:46:37 +0200 | |
branch | RCL_3 |
changeset 5 | d3bac044e0f0 |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
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 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
||
44 |
#include "scribblearea.h" |
|
45 |
||
46 |
//! [0] |
|
47 |
ScribbleArea::ScribbleArea(QWidget *parent) |
|
48 |
: QWidget(parent) |
|
49 |
{ |
|
50 |
setAttribute(Qt::WA_AcceptTouchEvents); |
|
51 |
setAttribute(Qt::WA_StaticContents); |
|
52 |
modified = false; |
|
53 |
||
54 |
myPenColors |
|
55 |
<< QColor("green") |
|
56 |
<< QColor("purple") |
|
57 |
<< QColor("red") |
|
58 |
<< QColor("blue") |
|
59 |
<< QColor("yellow") |
|
60 |
||
61 |
<< QColor("pink") |
|
62 |
<< QColor("orange") |
|
63 |
<< QColor("brown") |
|
64 |
<< QColor("grey") |
|
65 |
<< QColor("black"); |
|
66 |
} |
|
67 |
//! [0] |
|
68 |
||
69 |
//! [1] |
|
70 |
bool ScribbleArea::openImage(const QString &fileName) |
|
71 |
//! [1] //! [2] |
|
72 |
{ |
|
73 |
QImage loadedImage; |
|
74 |
if (!loadedImage.load(fileName)) |
|
75 |
return false; |
|
76 |
||
77 |
QSize newSize = loadedImage.size().expandedTo(size()); |
|
78 |
resizeImage(&loadedImage, newSize); |
|
79 |
image = loadedImage; |
|
80 |
modified = false; |
|
81 |
update(); |
|
82 |
return true; |
|
83 |
} |
|
84 |
//! [2] |
|
85 |
||
86 |
//! [3] |
|
87 |
bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat) |
|
88 |
//! [3] //! [4] |
|
89 |
{ |
|
90 |
QImage visibleImage = image; |
|
91 |
resizeImage(&visibleImage, size()); |
|
92 |
||
93 |
if (visibleImage.save(fileName, fileFormat)) { |
|
94 |
modified = false; |
|
95 |
return true; |
|
96 |
} else { |
|
97 |
return false; |
|
98 |
} |
|
99 |
} |
|
100 |
//! [4] |
|
101 |
||
102 |
//! [9] |
|
103 |
void ScribbleArea::clearImage() |
|
104 |
//! [9] //! [10] |
|
105 |
{ |
|
106 |
image.fill(qRgb(255, 255, 255)); |
|
107 |
modified = true; |
|
108 |
update(); |
|
109 |
} |
|
110 |
//! [10] |
|
111 |
||
112 |
//! [12] //! [13] |
|
113 |
void ScribbleArea::paintEvent(QPaintEvent *event) |
|
114 |
//! [13] //! [14] |
|
115 |
{ |
|
116 |
QPainter painter(this); |
|
117 |
const QRect rect = event->rect(); |
|
118 |
painter.drawImage(rect.topLeft(), image, rect); |
|
119 |
} |
|
120 |
//! [14] |
|
121 |
||
122 |
//! [15] |
|
123 |
void ScribbleArea::resizeEvent(QResizeEvent *event) |
|
124 |
//! [15] //! [16] |
|
125 |
{ |
|
126 |
if (width() > image.width() || height() > image.height()) { |
|
127 |
int newWidth = qMax(width() + 128, image.width()); |
|
128 |
int newHeight = qMax(height() + 128, image.height()); |
|
129 |
resizeImage(&image, QSize(newWidth, newHeight)); |
|
130 |
update(); |
|
131 |
} |
|
132 |
QWidget::resizeEvent(event); |
|
133 |
} |
|
134 |
//! [16] |
|
135 |
||
136 |
//! [19] |
|
137 |
void ScribbleArea::resizeImage(QImage *image, const QSize &newSize) |
|
138 |
//! [19] //! [20] |
|
139 |
{ |
|
140 |
if (image->size() == newSize) |
|
141 |
return; |
|
142 |
||
143 |
QImage newImage(newSize, QImage::Format_RGB32); |
|
144 |
newImage.fill(qRgb(255, 255, 255)); |
|
145 |
QPainter painter(&newImage); |
|
146 |
painter.drawImage(QPoint(0, 0), *image); |
|
147 |
*image = newImage; |
|
148 |
} |
|
149 |
//! [20] |
|
150 |
||
151 |
//! [21] |
|
152 |
void ScribbleArea::print() |
|
153 |
{ |
|
154 |
#ifndef QT_NO_PRINTER |
|
155 |
QPrinter printer(QPrinter::HighResolution); |
|
156 |
||
157 |
QPrintDialog *printDialog = new QPrintDialog(&printer, this); |
|
158 |
//! [21] //! [22] |
|
159 |
if (printDialog->exec() == QDialog::Accepted) { |
|
160 |
QPainter painter(&printer); |
|
161 |
QRect rect = painter.viewport(); |
|
162 |
QSize size = image.size(); |
|
163 |
size.scale(rect.size(), Qt::KeepAspectRatio); |
|
164 |
painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
|
165 |
painter.setWindow(image.rect()); |
|
166 |
painter.drawImage(0, 0, image); |
|
167 |
} |
|
168 |
#endif // QT_NO_PRINTER |
|
169 |
} |
|
170 |
//! [22] |
|
171 |
||
172 |
bool ScribbleArea::event(QEvent *event) |
|
173 |
{ |
|
174 |
switch (event->type()) { |
|
175 |
case QEvent::TouchBegin: |
|
176 |
case QEvent::TouchUpdate: |
|
177 |
case QEvent::TouchEnd: |
|
178 |
{ |
|
179 |
QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
|
180 |
foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) { |
|
181 |
switch (touchPoint.state()) { |
|
182 |
case Qt::TouchPointStationary: |
|
183 |
// don't do anything if this touch point hasn't moved |
|
184 |
continue; |
|
185 |
default: |
|
186 |
{ |
|
187 |
QRectF rect = touchPoint.rect(); |
|
188 |
if (rect.isEmpty()) { |
|
189 |
qreal diameter = qreal(50) * touchPoint.pressure(); |
|
190 |
rect.setSize(QSizeF(diameter, diameter)); |
|
191 |
} |
|
192 |
||
193 |
QPainter painter(&image); |
|
194 |
painter.setPen(Qt::NoPen); |
|
195 |
painter.setBrush(myPenColors.at(touchPoint.id() % myPenColors.count())); |
|
196 |
painter.drawEllipse(rect); |
|
197 |
painter.end(); |
|
198 |
||
199 |
modified = true; |
|
200 |
int rad = 2; |
|
201 |
update(rect.toRect().adjusted(-rad,-rad, +rad, +rad)); |
|
202 |
} |
|
203 |
break; |
|
204 |
} |
|
205 |
} |
|
206 |
break; |
|
207 |
} |
|
208 |
default: |
|
209 |
return QWidget::event(event); |
|
210 |
} |
|
211 |
return true; |
|
212 |
} |