1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the examples of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
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
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
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."
39 ****************************************************************************/
46 #include "basictoolsplugin.h"
48 const float Pi = 3.14159f;
51 QStringList BasicToolsPlugin::brushes() const
53 return QStringList() << tr("Pencil") << tr("Air Brush")
54 << tr("Random Letters");
59 QRect BasicToolsPlugin::mousePress(const QString &brush, QPainter &painter,
62 return mouseMove(brush, painter, pos, pos);
67 QRect BasicToolsPlugin::mouseMove(const QString &brush, QPainter &painter,
68 const QPoint &oldPos, const QPoint &newPos)
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);
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;
85 painter.setBrush(QBrush(color, Qt::Dense6Pattern));
86 painter.setPen(Qt::NoPen);
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);
92 painter.drawEllipse(x - (thickness / 2), y - (thickness / 2),
93 thickness, thickness);
95 } else if (brush == tr("Random Letters")) {
96 QChar ch('A' + (qrand() % 26));
98 QFont biggerFont = painter.font();
99 biggerFont.setBold(true);
100 biggerFont.setPointSize(biggerFont.pointSize() + thickness);
101 painter.setFont(biggerFont);
103 painter.drawText(newPos, QString(ch));
105 QFontMetrics metrics(painter.font());
106 boundingRect = metrics.boundingRect(ch);
107 boundingRect.translate(newPos);
108 boundingRect.adjust(-10, -10, +10, +10);
116 QRect BasicToolsPlugin::mouseRelease(const QString & /* brush */,
117 QPainter & /* painter */,
118 const QPoint & /* pos */)
120 return QRect(0, 0, 0, 0);
125 QStringList BasicToolsPlugin::shapes() const
127 return QStringList() << tr("Circle") << tr("Star") << tr("Text...");
132 QPainterPath BasicToolsPlugin::generateShape(const QString &shape,
137 if (shape == tr("Circle")) {
138 path.addEllipse(0, 0, 50, 50);
139 } else if (shape == tr("Star")) {
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));
146 } else if (shape == tr("Text...")) {
147 QString text = QInputDialog::getText(parent, tr("Text Shape"),
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);
162 QStringList BasicToolsPlugin::filters() const
164 return QStringList() << tr("Invert Pixels") << tr("Swap RGB")
170 QImage BasicToolsPlugin::filterImage(const QString &filter, const QImage &image,
171 QWidget * /* parent */)
173 QImage result = image.convertToFormat(QImage::Format_RGB32);
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));
195 Q_EXPORT_PLUGIN2(pnp_basictools, BasicToolsPlugin)