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 |
|
---|
43 | #include "interfaces.h"
|
---|
44 | #include "paintarea.h"
|
---|
45 |
|
---|
46 | #include <QPainter>
|
---|
47 | #include <QMouseEvent>
|
---|
48 |
|
---|
49 | PaintArea::PaintArea(QWidget *parent) :
|
---|
50 | QWidget(parent),
|
---|
51 | theImage(500, 400, QImage::Format_RGB32),
|
---|
52 | color(Qt::blue),
|
---|
53 | thickness(3),
|
---|
54 | brushInterface(0),
|
---|
55 | lastPos(-1, -1)
|
---|
56 | {
|
---|
57 | setAttribute(Qt::WA_StaticContents);
|
---|
58 | setAttribute(Qt::WA_NoBackground);
|
---|
59 |
|
---|
60 | theImage.fill(qRgb(255, 255, 255));
|
---|
61 | }
|
---|
62 |
|
---|
63 | bool PaintArea::openImage(const QString &fileName)
|
---|
64 | {
|
---|
65 | QImage image;
|
---|
66 | if (!image.load(fileName))
|
---|
67 | return false;
|
---|
68 |
|
---|
69 | setImage(image);
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool PaintArea::saveImage(const QString &fileName, const char *fileFormat)
|
---|
74 | {
|
---|
75 | return theImage.save(fileName, fileFormat);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void PaintArea::setImage(const QImage &image)
|
---|
79 | {
|
---|
80 | theImage = image.convertToFormat(QImage::Format_RGB32);
|
---|
81 | update();
|
---|
82 | updateGeometry();
|
---|
83 | }
|
---|
84 |
|
---|
85 | void PaintArea::insertShape(const QPainterPath &path)
|
---|
86 | {
|
---|
87 | pendingPath = path;
|
---|
88 | #ifndef QT_NO_CURSOR
|
---|
89 | setCursor(Qt::CrossCursor);
|
---|
90 | #endif
|
---|
91 | }
|
---|
92 |
|
---|
93 | void PaintArea::setBrushColor(const QColor &color)
|
---|
94 | {
|
---|
95 | this->color = color;
|
---|
96 | }
|
---|
97 |
|
---|
98 | void PaintArea::setBrushWidth(int width)
|
---|
99 | {
|
---|
100 | thickness = width;
|
---|
101 | }
|
---|
102 |
|
---|
103 | //! [0]
|
---|
104 | void PaintArea::setBrush(BrushInterface *brushInterface, const QString &brush)
|
---|
105 | {
|
---|
106 | this->brushInterface = brushInterface;
|
---|
107 | this->brush = brush;
|
---|
108 | }
|
---|
109 | //! [0]
|
---|
110 |
|
---|
111 | QSize PaintArea::sizeHint() const
|
---|
112 | {
|
---|
113 | return theImage.size();
|
---|
114 | }
|
---|
115 |
|
---|
116 | void PaintArea::paintEvent(QPaintEvent * /* event */)
|
---|
117 | {
|
---|
118 | QPainter painter(this);
|
---|
119 | painter.drawImage(QPoint(0, 0), theImage);
|
---|
120 | }
|
---|
121 |
|
---|
122 | void PaintArea::mousePressEvent(QMouseEvent *event)
|
---|
123 | {
|
---|
124 | if (event->button() == Qt::LeftButton) {
|
---|
125 | if (!pendingPath.isEmpty()) {
|
---|
126 | QPainter painter(&theImage);
|
---|
127 | setupPainter(painter);
|
---|
128 |
|
---|
129 | const QRectF boundingRect = pendingPath.boundingRect();
|
---|
130 | QLinearGradient gradient(boundingRect.topRight(),
|
---|
131 | boundingRect.bottomLeft());
|
---|
132 | gradient.setColorAt(0.0, QColor(color.red(), color.green(),
|
---|
133 | color.blue(), 63));
|
---|
134 | gradient.setColorAt(1.0, QColor(color.red(), color.green(),
|
---|
135 | color.blue(), 191));
|
---|
136 | painter.setBrush(gradient);
|
---|
137 | painter.translate(event->pos() - boundingRect.center());
|
---|
138 | painter.drawPath(pendingPath);
|
---|
139 |
|
---|
140 | pendingPath = QPainterPath();
|
---|
141 | #ifndef QT_NO_CURSOR
|
---|
142 | unsetCursor();
|
---|
143 | #endif
|
---|
144 | update();
|
---|
145 | } else {
|
---|
146 | if (brushInterface) {
|
---|
147 | QPainter painter(&theImage);
|
---|
148 | setupPainter(painter);
|
---|
149 | const QRect rect = brushInterface->mousePress(brush, painter,
|
---|
150 | event->pos());
|
---|
151 | update(rect);
|
---|
152 | }
|
---|
153 |
|
---|
154 | lastPos = event->pos();
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | //! [1]
|
---|
160 | void PaintArea::mouseMoveEvent(QMouseEvent *event)
|
---|
161 | {
|
---|
162 | if ((event->buttons() & Qt::LeftButton) && lastPos != QPoint(-1, -1)) {
|
---|
163 | if (brushInterface) {
|
---|
164 | QPainter painter(&theImage);
|
---|
165 | setupPainter(painter);
|
---|
166 | const QRect rect = brushInterface->mouseMove(brush, painter, lastPos,
|
---|
167 | event->pos());
|
---|
168 | update(rect);
|
---|
169 | }
|
---|
170 |
|
---|
171 | lastPos = event->pos();
|
---|
172 | }
|
---|
173 | }
|
---|
174 | //! [1]
|
---|
175 |
|
---|
176 | void PaintArea::mouseReleaseEvent(QMouseEvent *event)
|
---|
177 | {
|
---|
178 | if (event->button() == Qt::LeftButton && lastPos != QPoint(-1, -1)) {
|
---|
179 | if (brushInterface) {
|
---|
180 | QPainter painter(&theImage);
|
---|
181 | setupPainter(painter);
|
---|
182 | QRect rect = brushInterface->mouseRelease(brush, painter,
|
---|
183 | event->pos());
|
---|
184 | update(rect);
|
---|
185 | }
|
---|
186 |
|
---|
187 | lastPos = QPoint(-1, -1);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | void PaintArea::setupPainter(QPainter &painter)
|
---|
192 | {
|
---|
193 | painter.setRenderHint(QPainter::Antialiasing, true);
|
---|
194 | painter.setPen(QPen(color, thickness, Qt::SolidLine, Qt::RoundCap,
|
---|
195 | Qt::RoundJoin));
|
---|
196 | }
|
---|