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