|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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: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 |
|
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 } |