1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
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 <math.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 |
|
---|
46 | #include "basictoolsplugin.h"
|
---|
47 |
|
---|
48 | const float Pi = 3.14159f;
|
---|
49 |
|
---|
50 | //! [0]
|
---|
51 | QStringList BasicToolsPlugin::brushes() const
|
---|
52 | {
|
---|
53 | return QStringList() << tr("Pencil") << tr("Air Brush")
|
---|
54 | << tr("Random Letters");
|
---|
55 | }
|
---|
56 | //! [0]
|
---|
57 |
|
---|
58 | //! [1]
|
---|
59 | QRect BasicToolsPlugin::mousePress(const QString &brush, QPainter &painter,
|
---|
60 | const QPoint &pos)
|
---|
61 | {
|
---|
62 | return mouseMove(brush, painter, pos, pos);
|
---|
63 | }
|
---|
64 | //! [1]
|
---|
65 |
|
---|
66 | //! [2]
|
---|
67 | QRect BasicToolsPlugin::mouseMove(const QString &brush, QPainter &painter,
|
---|
68 | const QPoint &oldPos, const QPoint &newPos)
|
---|
69 | {
|
---|
70 | painter.save();
|
---|
71 |
|
---|
72 | int rad = painter.pen().width() / 2;
|
---|
73 | QRect boundingRect = QRect(oldPos, newPos).normalized()
|
---|
74 | .adjusted(-rad, -rad, +rad, +rad);
|
---|
75 | QColor color = painter.pen().color();
|
---|
76 | int thickness = painter.pen().width();
|
---|
77 | QColor transparentColor(color.red(), color.green(), color.blue(), 0);
|
---|
78 | //! [2] //! [3]
|
---|
79 |
|
---|
80 | if (brush == tr("Pencil")) {
|
---|
81 | painter.drawLine(oldPos, newPos);
|
---|
82 | } else if (brush == tr("Air Brush")) {
|
---|
83 | int numSteps = 2 + (newPos - oldPos).manhattanLength() / 2;
|
---|
84 |
|
---|
85 | painter.setBrush(QBrush(color, Qt::Dense6Pattern));
|
---|
86 | painter.setPen(Qt::NoPen);
|
---|
87 |
|
---|
88 | for (int i = 0; i < numSteps; ++i) {
|
---|
89 | int x = oldPos.x() + i * (newPos.x() - oldPos.x()) / (numSteps - 1);
|
---|
90 | int y = oldPos.y() + i * (newPos.y() - oldPos.y()) / (numSteps - 1);
|
---|
91 |
|
---|
92 | painter.drawEllipse(x - (thickness / 2), y - (thickness / 2),
|
---|
93 | thickness, thickness);
|
---|
94 | }
|
---|
95 | } else if (brush == tr("Random Letters")) {
|
---|
96 | QChar ch('A' + (qrand() % 26));
|
---|
97 |
|
---|
98 | QFont biggerFont = painter.font();
|
---|
99 | biggerFont.setBold(true);
|
---|
100 | biggerFont.setPointSize(biggerFont.pointSize() + thickness);
|
---|
101 | painter.setFont(biggerFont);
|
---|
102 |
|
---|
103 | painter.drawText(newPos, QString(ch));
|
---|
104 |
|
---|
105 | QFontMetrics metrics(painter.font());
|
---|
106 | boundingRect = metrics.boundingRect(ch);
|
---|
107 | boundingRect.translate(newPos);
|
---|
108 | boundingRect.adjust(-10, -10, +10, +10);
|
---|
109 | }
|
---|
110 | painter.restore();
|
---|
111 | return boundingRect;
|
---|
112 | }
|
---|
113 | //! [3]
|
---|
114 |
|
---|
115 | //! [4]
|
---|
116 | QRect BasicToolsPlugin::mouseRelease(const QString & /* brush */,
|
---|
117 | QPainter & /* painter */,
|
---|
118 | const QPoint & /* pos */)
|
---|
119 | {
|
---|
120 | return QRect(0, 0, 0, 0);
|
---|
121 | }
|
---|
122 | //! [4]
|
---|
123 |
|
---|
124 | //! [5]
|
---|
125 | QStringList BasicToolsPlugin::shapes() const
|
---|
126 | {
|
---|
127 | return QStringList() << tr("Circle") << tr("Star") << tr("Text...");
|
---|
128 | }
|
---|
129 | //! [5]
|
---|
130 |
|
---|
131 | //! [6]
|
---|
132 | QPainterPath BasicToolsPlugin::generateShape(const QString &shape,
|
---|
133 | QWidget *parent)
|
---|
134 | {
|
---|
135 | QPainterPath path;
|
---|
136 |
|
---|
137 | if (shape == tr("Circle")) {
|
---|
138 | path.addEllipse(0, 0, 50, 50);
|
---|
139 | } else if (shape == tr("Star")) {
|
---|
140 | path.moveTo(90, 50);
|
---|
141 | for (int i = 1; i < 5; ++i) {
|
---|
142 | path.lineTo(50 + 40 * cos(0.8 * i * Pi),
|
---|
143 | 50 + 40 * sin(0.8 * i * Pi));
|
---|
144 | }
|
---|
145 | path.closeSubpath();
|
---|
146 | } else if (shape == tr("Text...")) {
|
---|
147 | QString text = QInputDialog::getText(parent, tr("Text Shape"),
|
---|
148 | tr("Enter text:"),
|
---|
149 | QLineEdit::Normal, tr("Qt"));
|
---|
150 | if (!text.isEmpty()) {
|
---|
151 | QFont timesFont("Times", 50);
|
---|
152 | timesFont.setStyleStrategy(QFont::ForceOutline);
|
---|
153 | path.addText(0, 0, timesFont, text);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | return path;
|
---|
158 | }
|
---|
159 | //! [6]
|
---|
160 |
|
---|
161 | //! [7]
|
---|
162 | QStringList BasicToolsPlugin::filters() const
|
---|
163 | {
|
---|
164 | return QStringList() << tr("Invert Pixels") << tr("Swap RGB")
|
---|
165 | << tr("Grayscale");
|
---|
166 | }
|
---|
167 | //! [7]
|
---|
168 |
|
---|
169 | //! [8]
|
---|
170 | QImage BasicToolsPlugin::filterImage(const QString &filter, const QImage &image,
|
---|
171 | QWidget * /* parent */)
|
---|
172 | {
|
---|
173 | QImage result = image.convertToFormat(QImage::Format_RGB32);
|
---|
174 |
|
---|
175 | if (filter == tr("Invert Pixels")) {
|
---|
176 | result.invertPixels();
|
---|
177 | } else if (filter == tr("Swap RGB")) {
|
---|
178 | result = result.rgbSwapped();
|
---|
179 | } else if (filter == tr("Grayscale")) {
|
---|
180 | for (int y = 0; y < result.height(); ++y) {
|
---|
181 | for (int x = 0; x < result.width(); ++x) {
|
---|
182 | int pixel = result.pixel(x, y);
|
---|
183 | int gray = qGray(pixel);
|
---|
184 | int alpha = qAlpha(pixel);
|
---|
185 | result.setPixel(x, y, qRgba(gray, gray, gray, alpha));
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 | //! [8]
|
---|
192 |
|
---|
193 | QT_BEGIN_NAMESPACE
|
---|
194 | //! [9]
|
---|
195 | Q_EXPORT_PLUGIN2(pnp_basictools, BasicToolsPlugin)
|
---|
196 | //! [9]
|
---|
197 | QT_END_NAMESPACE
|
---|