source: trunk/examples/tools/plugandpaintplugins/basictools/basictoolsplugin.cpp@ 168

Last change on this file since 168 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.4 KB
Line 
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#include <QtGui>
43
44#include <math.h>
45#include <stdlib.h>
46
47#include "basictoolsplugin.h"
48
49const float Pi = 3.14159f;
50
51//! [0]
52QStringList BasicToolsPlugin::brushes() const
53{
54 return QStringList() << tr("Pencil") << tr("Air Brush")
55 << tr("Random Letters");
56}
57//! [0]
58
59//! [1]
60QRect 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]
68QRect 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]
117QRect 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]
126QStringList BasicToolsPlugin::shapes() const
127{
128 return QStringList() << tr("Circle") << tr("Star") << tr("Text...");
129}
130//! [5]
131
132//! [6]
133QPainterPath 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]
163QStringList BasicToolsPlugin::filters() const
164{
165 return QStringList() << tr("Invert Pixels") << tr("Swap RGB")
166 << tr("Grayscale");
167}
168//! [7]
169
170//! [8]
171QImage 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
194QT_BEGIN_NAMESPACE
195//! [9]
196Q_EXPORT_PLUGIN2(pnp_basictools, BasicToolsPlugin)
197//! [9]
198QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.