source: trunk/demos/gradients/gradients.cpp@ 452

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

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

File size: 18.2 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 demonstration applications 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 "gradients.h"
43#include "hoverpoints.h"
44
45ShadeWidget::ShadeWidget(ShadeType type, QWidget *parent)
46 : QWidget(parent), m_shade_type(type), m_alpha_gradient(QLinearGradient(0, 0, 0, 0))
47{
48
49 // Checkers background
50 if (m_shade_type == ARGBShade) {
51 QPixmap pm(20, 20);
52 QPainter pmp(&pm);
53 pmp.fillRect(0, 0, 10, 10, Qt::lightGray);
54 pmp.fillRect(10, 10, 10, 10, Qt::lightGray);
55 pmp.fillRect(0, 10, 10, 10, Qt::darkGray);
56 pmp.fillRect(10, 0, 10, 10, Qt::darkGray);
57 pmp.end();
58 QPalette pal = palette();
59 pal.setBrush(backgroundRole(), QBrush(pm));
60 setAutoFillBackground(true);
61 setPalette(pal);
62
63 } else {
64 setAttribute(Qt::WA_NoBackground);
65
66 }
67
68 QPolygonF points;
69 points << QPointF(0, sizeHint().height())
70 << QPointF(sizeHint().width(), 0);
71
72 m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
73// m_hoverPoints->setConnectionType(HoverPoints::LineConnection);
74 m_hoverPoints->setPoints(points);
75 m_hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
76 m_hoverPoints->setPointLock(1, HoverPoints::LockToRight);
77 m_hoverPoints->setSortType(HoverPoints::XSort);
78
79 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
80
81 connect(m_hoverPoints, SIGNAL(pointsChanged(const QPolygonF &)), this, SIGNAL(colorsChanged()));
82}
83
84
85QPolygonF ShadeWidget::points() const
86{
87 return m_hoverPoints->points();
88}
89
90
91uint ShadeWidget::colorAt(int x)
92{
93 generateShade();
94
95 QPolygonF pts = m_hoverPoints->points();
96 for (int i=1; i < pts.size(); ++i) {
97 if (pts.at(i-1).x() <= x && pts.at(i).x() >= x) {
98 QLineF l(pts.at(i-1), pts.at(i));
99 l.setLength(l.length() * ((x - l.x1()) / l.dx()));
100 return m_shade.pixel(qRound(qMin(l.x2(), (qreal(m_shade.width() - 1)))),
101 qRound(qMin(l.y2(), qreal(m_shade.height() - 1))));
102 }
103 }
104 return 0;
105}
106
107
108void ShadeWidget::setGradientStops(const QGradientStops &stops)
109{
110 if (m_shade_type == ARGBShade) {
111 m_alpha_gradient = QLinearGradient(0, 0, width(), 0);
112
113 for (int i=0; i<stops.size(); ++i) {
114 QColor c = stops.at(i).second;
115 m_alpha_gradient.setColorAt(stops.at(i).first, QColor(c.red(), c.green(), c.blue()));
116 }
117
118 m_shade = QImage();
119 generateShade();
120 update();
121 }
122}
123
124
125void ShadeWidget::paintEvent(QPaintEvent *)
126{
127 generateShade();
128
129 QPainter p(this);
130 p.drawImage(0, 0, m_shade);
131
132 p.setPen(QColor(146, 146, 146));
133 p.drawRect(0, 0, width() - 1, height() - 1);
134}
135
136
137void ShadeWidget::generateShade()
138{
139 if (m_shade.isNull() || m_shade.size() != size()) {
140
141 if (m_shade_type == ARGBShade) {
142 m_shade = QImage(size(), QImage::Format_ARGB32_Premultiplied);
143 m_shade.fill(0);
144
145 QPainter p(&m_shade);
146 p.fillRect(rect(), m_alpha_gradient);
147
148 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
149 QLinearGradient fade(0, 0, 0, height());
150 fade.setColorAt(0, QColor(0, 0, 0, 255));
151 fade.setColorAt(1, QColor(0, 0, 0, 0));
152 p.fillRect(rect(), fade);
153
154 } else {
155 m_shade = QImage(size(), QImage::Format_RGB32);
156 QLinearGradient shade(0, 0, 0, height());
157 shade.setColorAt(1, Qt::black);
158
159 if (m_shade_type == RedShade)
160 shade.setColorAt(0, Qt::red);
161 else if (m_shade_type == GreenShade)
162 shade.setColorAt(0, Qt::green);
163 else
164 shade.setColorAt(0, Qt::blue);
165
166 QPainter p(&m_shade);
167 p.fillRect(rect(), shade);
168 }
169 }
170
171
172}
173
174
175GradientEditor::GradientEditor(QWidget *parent)
176 : QWidget(parent)
177{
178 QVBoxLayout *vbox = new QVBoxLayout(this);
179 vbox->setSpacing(1);
180 vbox->setMargin(1);
181
182 m_red_shade = new ShadeWidget(ShadeWidget::RedShade, this);
183 m_green_shade = new ShadeWidget(ShadeWidget::GreenShade, this);
184 m_blue_shade = new ShadeWidget(ShadeWidget::BlueShade, this);
185 m_alpha_shade = new ShadeWidget(ShadeWidget::ARGBShade, this);
186
187 vbox->addWidget(m_red_shade);
188 vbox->addWidget(m_green_shade);
189 vbox->addWidget(m_blue_shade);
190 vbox->addWidget(m_alpha_shade);
191
192 connect(m_red_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
193 connect(m_green_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
194 connect(m_blue_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
195 connect(m_alpha_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
196}
197
198
199inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
200{
201 return p1.x() < p2.x();
202}
203
204
205void GradientEditor::pointsUpdated()
206{
207 qreal w = m_alpha_shade->width();
208
209 QGradientStops stops;
210
211 QPolygonF points;
212
213 points += m_red_shade->points();
214 points += m_green_shade->points();
215 points += m_blue_shade->points();
216 points += m_alpha_shade->points();
217
218 qSort(points.begin(), points.end(), x_less_than);
219
220 for (int i=0; i<points.size(); ++i) {
221 qreal x = int(points.at(i).x());
222 if (i < points.size() - 1 && x == points.at(i+1).x())
223 continue;
224 QColor color((0x00ff0000 & m_red_shade->colorAt(int(x))) >> 16,
225 (0x0000ff00 & m_green_shade->colorAt(int(x))) >> 8,
226 (0x000000ff & m_blue_shade->colorAt(int(x))),
227 (0xff000000 & m_alpha_shade->colorAt(int(x))) >> 24);
228
229 if (x / w > 1)
230 return;
231
232 stops << QGradientStop(x / w, color);
233 }
234
235 m_alpha_shade->setGradientStops(stops);
236
237 emit gradientStopsChanged(stops);
238}
239
240
241static void set_shade_points(const QPolygonF &points, ShadeWidget *shade)
242{
243 shade->hoverPoints()->setPoints(points);
244 shade->hoverPoints()->setPointLock(0, HoverPoints::LockToLeft);
245 shade->hoverPoints()->setPointLock(points.size() - 1, HoverPoints::LockToRight);
246 shade->update();
247}
248
249void GradientEditor::setGradientStops(const QGradientStops &stops)
250{
251 QPolygonF pts_red, pts_green, pts_blue, pts_alpha;
252
253 qreal h_red = m_red_shade->height();
254 qreal h_green = m_green_shade->height();
255 qreal h_blue = m_blue_shade->height();
256 qreal h_alpha = m_alpha_shade->height();
257
258 for (int i=0; i<stops.size(); ++i) {
259 qreal pos = stops.at(i).first;
260 QRgb color = stops.at(i).second.rgba();
261 pts_red << QPointF(pos * m_red_shade->width(), h_red - qRed(color) * h_red / 255);
262 pts_green << QPointF(pos * m_green_shade->width(), h_green - qGreen(color) * h_green / 255);
263 pts_blue << QPointF(pos * m_blue_shade->width(), h_blue - qBlue(color) * h_blue / 255);
264 pts_alpha << QPointF(pos * m_alpha_shade->width(), h_alpha - qAlpha(color) * h_alpha / 255);
265 }
266
267 set_shade_points(pts_red, m_red_shade);
268 set_shade_points(pts_green, m_green_shade);
269 set_shade_points(pts_blue, m_blue_shade);
270 set_shade_points(pts_alpha, m_alpha_shade);
271
272}
273
274GradientWidget::GradientWidget(QWidget *parent)
275 : QWidget(parent)