author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Wed, 18 Aug 2010 10:37:55 +0300 | |
changeset 33 | 3e2da88830cd |
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 |
||
43 |
#include "scribblearea.h" |
|
44 |
||
45 |
//! [0] |
|
46 |
ScribbleArea::ScribbleArea(QWidget *parent) |
|
47 |
: QWidget(parent) |
|
48 |
{ |
|
49 |
setAttribute(Qt::WA_StaticContents); |
|
50 |
modified = false; |
|
51 |
scribbling = false; |
|
52 |
myPenWidth = 1; |
|
53 |
myPenColor = Qt::blue; |
|
54 |
} |
|
55 |
//! [0] |
|
56 |
||
57 |
//! [1] |
|
58 |
bool ScribbleArea::openImage(const QString &fileName) |
|
59 |
//! [1] //! [2] |
|
60 |
{ |
|
61 |
QImage loadedImage; |
|
62 |
if (!loadedImage.load(fileName)) |
|
63 |
return false; |
|
64 |
||
65 |
QSize newSize = loadedImage.size().expandedTo(size()); |
|
66 |
resizeImage(&loadedImage, newSize); |
|
67 |
image = loadedImage; |
|
68 |
modified = false; |
|
69 |
update(); |
|
70 |
return true; |
|
71 |
} |
|
72 |
//! [2] |
|
73 |
||
74 |
//! [3] |
|
75 |
bool ScribbleArea::saveImage(const QString &fileName, const char *fileFormat) |
|
76 |
//! [3] //! [4] |
|
77 |
{ |
|
78 |
QImage visibleImage = image; |
|
79 |
resizeImage(&visibleImage, size()); |
|
80 |
||
81 |
if (visibleImage.save(fileName, fileFormat)) { |
|
82 |
modified = false; |
|
83 |
return true; |
|
84 |
} else { |
|
85 |
return false; |
|
86 |
} |
|
87 |
} |
|
88 |
//! [4] |
|
89 |
||
90 |
//! [5] |
|
91 |
void ScribbleArea::setPenColor(const QColor &newColor) |
|
92 |
//! [5] //! [6] |
|
93 |
{ |
|
94 |
myPenColor = newColor; |
|
95 |
} |
|
96 |
//! [6] |
|
97 |
||
98 |
//! [7] |
|
99 |
void ScribbleArea::setPenWidth(int newWidth) |
|
100 |
//! [7] //! [8] |
|
101 |
{ |
|
102 |
myPenWidth = newWidth; |
|
103 |
} |
|
104 |
//! [8] |
|
105 |
||
106 |
//! [9] |
|
107 |
void ScribbleArea::clearImage() |
|
108 |
//! [9] //! [10] |
|
109 |
{ |
|
110 |
image.fill(qRgb(255, 255, 255)); |
|
111 |
modified = true; |
|
112 |
update(); |
|
113 |
} |
|
114 |
//! [10] |
|
115 |
||
116 |
//! [11] |
|
117 |
void ScribbleArea::mousePressEvent(QMouseEvent *event) |
|
118 |
//! [11] //! [12] |
|
119 |
{ |
|
120 |
if (event->button() == Qt::LeftButton) { |
|
121 |
lastPoint = event->pos(); |
|
122 |
scribbling = true; |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
void ScribbleArea::mouseMoveEvent(QMouseEvent *event) |
|
127 |
{ |
|
128 |
if ((event->buttons() & Qt::LeftButton) && scribbling) |
|
129 |
drawLineTo(event->pos()); |
|
130 |
} |
|
131 |
||
132 |
void ScribbleArea::mouseReleaseEvent(QMouseEvent *event) |
|
133 |
{ |
|
134 |
if (event->button() == Qt::LeftButton && scribbling) { |
|
135 |
drawLineTo(event->pos()); |
|
136 |
scribbling = false; |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
//! [12] //! [13] |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
void ScribbleArea::paintEvent(QPaintEvent *event) |
0 | 142 |
//! [13] //! [14] |
143 |
{ |
|
144 |
QPainter painter(this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
145 |
QRect dirtyRect = event->rect(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
146 |
painter.drawImage(dirtyRect, image, dirtyRect); |
0 | 147 |
} |
148 |
//! [14] |
|
149 |
||
150 |
//! [15] |
|
151 |
void ScribbleArea::resizeEvent(QResizeEvent *event) |
|
152 |
//! [15] //! [16] |
|
153 |
{ |
|
154 |
if (width() > image.width() || height() > image.height()) { |
|
155 |
int newWidth = qMax(width() + 128, image.width()); |
|
156 |
int newHeight = qMax(height() + 128, image.height()); |
|
157 |
resizeImage(&image, QSize(newWidth, newHeight)); |
|
158 |
update(); |
|
159 |
} |
|
160 |
QWidget::resizeEvent(event); |
|
161 |
} |
|
162 |
//! [16] |
|
163 |
||
164 |
//! [17] |
|
165 |
void ScribbleArea::drawLineTo(const QPoint &endPoint) |
|
166 |
//! [17] //! [18] |
|
167 |
{ |
|
168 |
QPainter painter(&image); |
|
169 |
painter.setPen(QPen(myPenColor, myPenWidth, Qt::SolidLine, Qt::RoundCap, |
|
170 |
Qt::RoundJoin)); |
|
171 |
painter.drawLine(lastPoint, endPoint); |
|
172 |
modified = true; |
|
173 |
||
174 |
int rad = (myPenWidth / 2) + 2; |
|
175 |
update(QRect(lastPoint, endPoint).normalized() |
|
176 |
.adjusted(-rad, -rad, +rad, +rad)); |
|
177 |
lastPoint = endPoint; |
|
178 |
} |
|
179 |
//! [18] |
|
180 |
||
181 |
//! [19] |
|
182 |
void ScribbleArea::resizeImage(QImage *image, const QSize &newSize) |
|
183 |
//! [19] //! [20] |
|
184 |
{ |
|
185 |
if (image->size() == newSize) |
|
186 |
return; |
|
187 |
||
188 |
QImage newImage(newSize, QImage::Format_RGB32); |
|
189 |
newImage.fill(qRgb(255, 255, 255)); |
|
190 |
QPainter painter(&newImage); |
|
191 |
painter.drawImage(QPoint(0, 0), *image); |
|
192 |
*image = newImage; |
|
193 |
} |
|
194 |
//! [20] |
|
195 |
||
196 |
//! [21] |
|
197 |
void ScribbleArea::print() |
|
198 |
{ |
|
199 |
#ifndef QT_NO_PRINTER |
|
200 |
QPrinter printer(QPrinter::HighResolution); |
|
201 |
||
202 |
QPrintDialog *printDialog = new QPrintDialog(&printer, this); |
|
203 |
//! [21] //! [22] |
|
204 |
if (printDialog->exec() == QDialog::Accepted) { |
|
205 |
QPainter painter(&printer); |
|
206 |
QRect rect = painter.viewport(); |
|
207 |
QSize size = image.size(); |
|
208 |
size.scale(rect.size(), Qt::KeepAspectRatio); |
|
209 |
painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); |
|
210 |
painter.setWindow(image.rect()); |
|
211 |
painter.drawImage(0, 0, image); |
|
212 |
} |
|
213 |
#endif // QT_NO_PRINTER |
|
214 |
} |
|
215 |
//! [22] |