|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
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:BSD$ |
|
10 ** You may use this file under the terms of the BSD license as follows: |
|
11 ** |
|
12 ** "Redistribution and use in source and binary forms, with or without |
|
13 ** modification, are permitted provided that the following conditions are |
|
14 ** met: |
|
15 ** * Redistributions of source code must retain the above copyright |
|
16 ** notice, this list of conditions and the following disclaimer. |
|
17 ** * Redistributions in binary form must reproduce the above copyright |
|
18 ** notice, this list of conditions and the following disclaimer in |
|
19 ** the documentation and/or other materials provided with the |
|
20 ** distribution. |
|
21 ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
|
22 ** the names of its contributors may be used to endorse or promote |
|
23 ** products derived from this software without specific prior written |
|
24 ** permission. |
|
25 ** |
|
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
|
37 ** $QT_END_LICENSE$ |
|
38 ** |
|
39 ****************************************************************************/ |
|
40 |
|
41 #include <QtGui> |
|
42 |
|
43 #include "scribblearea.h" |
|
44 |
|
45 //! [0] |
|
46 ScribbleArea::ScribbleArea(QWidget *parent) |
|
47 : QWidget(parent) |
|
48 { |
|
49 setAttribute(Qt::WA_AcceptTouchEvents); |
|
50 setAttribute(Qt::WA_StaticContents); |
|
51 modified = false; |
|
52 |
|
53 myPenColors |
|
54 << QColor("green") |
|
55 << QColor("purple") |
|
56 << QColor("red") |
|
57 << QColor("blue") |
|
58 << QColor("yellow") |
|
59 |
|
60 << QColor("pink") |
|
61 << QColor("orange") |
|
62 << QColor("brown") |
|
63 << QColor("grey") |
|
64 << QColor("black"); |
|
65 } |
|
66 //! [0] |
|
67 |
|
68 //! [1] |
|
69 bool ScribbleArea::openImage(const QString &fileName) |
|
70 //! [1] //! [2] |
|
71 { |
|
72 QImage loadedImage; |
|
73 if (!loadedImage.load(fileName)) |
|
74 return false; |
|
75 |
|
76 QSize newSize = loadedImage.size().expandedTo(size()); |
|
77 resizeImage(&loadedImage, newSize); |
|
78 image = loadedImage; |
|
79 modified = false; |
|
80 update(); |
|
81 return true; |
|
82 } |
|
83 //! [2] |
|
84 |
|
85 //! [3] |
|
86 bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat) |
|
87 //! [3] //! [4] |
|
88 { |
|
89 QImage visibleImage = image; |
|
90 resizeImage(&visibleImage, size()); |
|
91 |
|
92 if (visibleImage.save(fileName, fileFormat)) { |
|
93 modified = false; |
|
94 return true; |
|
95 } else { |
|
96 return false; |
|
97 } |
|
98 } |
|
99 //! [4] |
|
100 |
|
101 //! [9] |
|
102 void ScribbleArea::clearImage() |
|
103 //! [9] //! [10] |
|
104 { |
|
105 image.fill(qRgb(255, 255, 255)); |
|
106 modified = true; |
|
107 update(); |
|
108 } |
|
109 //! [10] |
|
110 |
|
111 //! [12] //! [13] |
|
112 void ScribbleArea::paintEvent(QPaintEvent *event) |
|
113 //! [13] //! [14] |
|
114 { |
|
115 QPainter painter(this); |
|
116 const QRect rect = event->rect(); |
|
117 painter.drawImage(rect.topLeft(), image, rect); |
|
118 } |
|
119 //! [14] |
|
120 |
|
121 //! [15] |
|
122 void ScribbleArea::resizeEvent(QResizeEvent *event) |
|
123 //! [15] //! [16] |
|
124 { |
|
125 if (width() > image.width() || height() > image.height()) { |
|
126 int newWidth = qMax(width() + 128, image.width()); |
|
127 int newHeight = qMax(height() + 128, image.height()); |
|
128 resizeImage(&image, QSize(newWidth, newHeight)); |
|
129 update(); |
|
130 } |
|
131 QWidget::resizeEvent(event); |
|
132 } |
|
133 //! [16] |
|
134 |
|
135 //! [19] |
|
136 void ScribbleArea::resizeImage(QImage *image, const QSize &newSize) |
|
137 //! [19] //! [20] |
|
138 { |
|
139 if (image->size() == newSize) |
|
140 return; |
|
141 |
|
142 QImage newImage(newSize, QImage::Format_RGB32); |
|
143 newImage.fill(qRgb(255, 255, 255)); |
|
144 QPainter painter(&newImage); |
|
145 painter.drawImage(QPoint(0, 0), *image); |
|
146 *image = newImage; |
|
147 } |
|
148 //! [20] |
|
149 |
|
150 //! [21] |
|
151 void ScribbleArea::print() |
|
152 { |
|
153 #ifndef QT_NO_PRINTER |
|
154 QPrinter printer(QPrinter::HighResolution); |
|
155 |
|
156 QPrintDialog *printDialog = new QPrintDialog(&printer, this); |
|
157 //! [21] //! [22] |
|
158 if (printDialog->exec() == QDialog::Accepted) { |
|
159 QPainter painter(&printer); |
|
160 QRect rect = painter.viewport(); |
|
161 QSize size = image.size(); |
|
162 size.scale(rect.size(), Qt::KeepAspectRatio); |
|
163 painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
|
164 painter.setWindow(image.rect()); |
|
165 painter.drawImage(0, 0, image); |
|
166 } |
|
167 #endif // QT_NO_PRINTER |
|
168 } |
|
169 //! [22] |
|
170 |
|
171 bool ScribbleArea::event(QEvent *event) |
|
172 { |
|
173 switch (event->type()) { |
|
174 case QEvent::TouchBegin: |
|
175 case QEvent::TouchUpdate: |
|
176 case QEvent::TouchEnd: |
|
177 { |
|
178 QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints(); |
|
179 foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints) { |
|
180 switch (touchPoint.state()) { |
|
181 case Qt::TouchPointStationary: |
|
182 // don't do anything if this touch point hasn't moved |
|
183 continue; |
|
184 default: |
|
185 { |
|
186 QRectF rect = touchPoint.rect(); |
|
187 if (rect.isEmpty()) { |
|
188 qreal diameter = qreal(50) * touchPoint.pressure(); |
|
189 rect.setSize(QSizeF(diameter, diameter)); |
|
190 } |
|
191 |
|
192 QPainter painter(&image); |
|
193 painter.setPen(Qt::NoPen); |
|
194 painter.setBrush(myPenColors.at(touchPoint.id() % myPenColors.count())); |
|
195 painter.drawEllipse(rect); |
|
196 painter.end(); |
|
197 |
|
198 modified = true; |
|
199 int rad = 2; |
|
200 update(rect.toRect().adjusted(-rad,-rad, +rad, +rad)); |
|
201 } |
|
202 break; |
|
203 } |
|
204 } |
|
205 break; |
|
206 } |
|
207 default: |
|
208 return QWidget::event(event); |
|
209 } |
|
210 return true; |
|
211 } |