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 demonstration applications of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "gradients.h"
43 #include "hoverpoints.h"
45 ShadeWidget::ShadeWidget(ShadeType type, QWidget *parent)
46 : QWidget(parent), m_shade_type(type), m_alpha_gradient(QLinearGradient(0, 0, 0, 0))
49 // Checkers background
50 if (m_shade_type == ARGBShade) {
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);
58 QPalette pal = palette();
59 pal.setBrush(backgroundRole(), QBrush(pm));
60 setAutoFillBackground(true);
64 setAttribute(Qt::WA_NoBackground);
69 points << QPointF(0, sizeHint().height())
70 << QPointF(sizeHint().width(), 0);
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);
79 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
81 connect(m_hoverPoints, SIGNAL(pointsChanged(QPolygonF)), this, SIGNAL(colorsChanged()));
85 QPolygonF ShadeWidget::points() const
87 return m_hoverPoints->points();
91 uint ShadeWidget::colorAt(int x)
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))));
108 void ShadeWidget::setGradientStops(const QGradientStops &stops)
110 if (m_shade_type == ARGBShade) {
111 m_alpha_gradient = QLinearGradient(0, 0, width(), 0);
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()));
125 void ShadeWidget::paintEvent(QPaintEvent *)
130 p.drawImage(0, 0, m_shade);
132 p.setPen(QColor(146, 146, 146));
133 p.drawRect(0, 0, width() - 1, height() - 1);
137 void ShadeWidget::generateShade()
139 if (m_shade.isNull() || m_shade.size() != size()) {
141 if (m_shade_type == ARGBShade) {
142 m_shade = QImage(size(), QImage::Format_ARGB32_Premultiplied);
145 QPainter p(&m_shade);
146 p.fillRect(rect(), m_alpha_gradient);
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);
155 m_shade = QImage(size(), QImage::Format_RGB32);
156 QLinearGradient shade(0, 0, 0, height());
157 shade.setColorAt(1, Qt::black);
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);
164 shade.setColorAt(0, Qt::blue);
166 QPainter p(&m_shade);
167 p.fillRect(rect(), shade);
175 GradientEditor::GradientEditor(QWidget *parent)
178 QVBoxLayout *vbox = new QVBoxLayout(this);
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);
187 vbox->addWidget(m_red_shade);
188 vbox->addWidget(m_green_shade);
189 vbox->addWidget(m_blue_shade);
190 vbox->addWidget(m_alpha_shade);
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()));
199 inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
201 return p1.x() < p2.x();
205 void GradientEditor::pointsUpdated()
207 qreal w = m_alpha_shade->width();
209 QGradientStops stops;
213 points += m_red_shade->points();
214 points += m_green_shade->points();
215 points += m_blue_shade->points();
216 points += m_alpha_shade->points();
218 qSort(points.begin(), points.end(), x_less_than);
220 for (int i=0; i<points.size(); ++i) {
221 qreal x = int(points.at(i).x());
222 if (i+1 < points.size() && x == points.at(i+1).x())
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);
232 stops << QGradientStop(x / w, color);
235 m_alpha_shade->setGradientStops(stops);
237 emit gradientStopsChanged(stops);
241 static void set_shade_points(const QPolygonF &points, ShadeWidget *shade)
243 shade->hoverPoints()->setPoints(points);
244 shade->hoverPoints()->setPointLock(0, HoverPoints::LockToLeft);
245 shade->hoverPoints()->setPointLock(points.size() - 1, HoverPoints::LockToRight);
249 void GradientEditor::setGradientStops(const QGradientStops &stops)
251 QPolygonF pts_red, pts_green, pts_blue, pts_alpha;
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();
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);
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);
274 GradientWidget::GradientWidget(QWidget *parent)
277 setWindowTitle(tr("Gradients"));
279 m_renderer = new GradientRenderer(this);
281 QGroupBox *mainGroup = new QGroupBox(this);
282 mainGroup->setTitle(tr("Gradients"));
284 QGroupBox *editorGroup = new QGroupBox(mainGroup);
285 editorGroup->setTitle(tr("Color Editor"));
286 m_editor = new GradientEditor(editorGroup);
288 QGroupBox *typeGroup = new QGroupBox(mainGroup);
289 typeGroup->setTitle(tr("Gradient Type"));
290 m_linearButton = new QRadioButton(tr("Linear Gradient"), typeGroup);
291 m_radialButton = new QRadioButton(tr("Radial Gradient"), typeGroup);
292 m_conicalButton = new QRadioButton(tr("Conical Gradient"), typeGroup);
294 QGroupBox *spreadGroup = new QGroupBox(mainGroup);
295 spreadGroup->setTitle(tr("Spread Method"));
296 m_padSpreadButton = new QRadioButton(tr("Pad Spread"), spreadGroup);
297 m_reflectSpreadButton = new QRadioButton(tr("Reflect Spread"), spreadGroup);
298 m_repeatSpreadButton = new QRadioButton(tr("Repeat Spread"), spreadGroup);
300 QGroupBox *defaultsGroup = new QGroupBox(mainGroup);
301 defaultsGroup->setTitle(tr("Defaults"));
302 QPushButton *default1Button = new QPushButton(tr("1"), defaultsGroup);
303 QPushButton *default2Button = new QPushButton(tr("2"), defaultsGroup);
304 QPushButton *default3Button = new QPushButton(tr("3"), defaultsGroup);
305 QPushButton *default4Button = new QPushButton(tr("Reset"), editorGroup);
307 QPushButton *showSourceButton = new QPushButton(mainGroup);
308 showSourceButton->setText(tr("Show Source"));
309 #ifdef QT_OPENGL_SUPPORT
310 QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
311 enableOpenGLButton->setText(tr("Use OpenGL"));
312 enableOpenGLButton->setCheckable(true);
313 enableOpenGLButton->setChecked(m_renderer->usesOpenGL());
314 if (!QGLFormat::hasOpenGL())
315 enableOpenGLButton->hide();
317 QPushButton *whatsThisButton = new QPushButton(mainGroup);
318 whatsThisButton->setText(tr("What's This?"));
319 whatsThisButton->setCheckable(true);
322 QHBoxLayout *mainLayout = new QHBoxLayout(this);
323 mainLayout->addWidget(m_renderer);
324 mainLayout->addWidget(mainGroup);
326 mainGroup->setFixedWidth(180);
327 QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
328 mainGroupLayout->addWidget(editorGroup);
329 mainGroupLayout->addWidget(typeGroup);
330 mainGroupLayout->addWidget(spreadGroup);
331 mainGroupLayout->addWidget(defaultsGroup);
332 mainGroupLayout->addStretch(1);
333 mainGroupLayout->addWidget(showSourceButton);
334 #ifdef QT_OPENGL_SUPPORT
335 mainGroupLayout->addWidget(enableOpenGLButton);
337 mainGroupLayout->addWidget(whatsThisButton);
339 QVBoxLayout *editorGroupLayout = new QVBoxLayout(editorGroup);
340 editorGroupLayout->addWidget(m_editor);
342 QVBoxLayout *typeGroupLayout = new QVBoxLayout(typeGroup);
343 typeGroupLayout->addWidget(m_linearButton);
344 typeGroupLayout->addWidget(m_radialButton);
345 typeGroupLayout->addWidget(m_conicalButton);
347 QVBoxLayout *spreadGroupLayout = new QVBoxLayout(spreadGroup);
348 spreadGroupLayout->addWidget(m_padSpreadButton);
349 spreadGroupLayout->addWidget(m_repeatSpreadButton);
350 spreadGroupLayout->addWidget(m_reflectSpreadButton);
352 QHBoxLayout *defaultsGroupLayout = new QHBoxLayout(defaultsGroup);
353 defaultsGroupLayout->addWidget(default1Button);
354 defaultsGroupLayout->addWidget(default2Button);
355 defaultsGroupLayout->addWidget(default3Button);
356 editorGroupLayout->addWidget(default4Button);
358 connect(m_editor, SIGNAL(gradientStopsChanged(QGradientStops)),
359 m_renderer, SLOT(setGradientStops(QGradientStops)));
361 connect(m_linearButton, SIGNAL(clicked()), m_renderer, SLOT(setLinearGradient()));
362 connect(m_radialButton, SIGNAL(clicked()), m_renderer, SLOT(setRadialGradient()));
363 connect(m_conicalButton, SIGNAL(clicked()), m_renderer, SLOT(setConicalGradient()));
365 connect(m_padSpreadButton, SIGNAL(clicked()), m_renderer, SLOT(setPadSpread()));
366 connect(m_reflectSpreadButton, SIGNAL(clicked()), m_renderer, SLOT(setReflectSpread()));
367 connect(m_repeatSpreadButton, SIGNAL(clicked()), m_renderer, SLOT(setRepeatSpread()));
369 connect(default1Button, SIGNAL(clicked()), this, SLOT(setDefault1()));
370 connect(default2Button, SIGNAL(clicked()), this, SLOT(setDefault2()));
371 connect(default3Button, SIGNAL(clicked()), this, SLOT(setDefault3()));
372 connect(default4Button, SIGNAL(clicked()), this, SLOT(setDefault4()));
374 connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource()));
375 #ifdef QT_OPENGL_SUPPORT
376 connect(enableOpenGLButton, SIGNAL(clicked(bool)), m_renderer, SLOT(enableOpenGL(bool)));
378 connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool)));
379 connect(whatsThisButton, SIGNAL(clicked(bool)),
380 m_renderer->hoverPoints(), SLOT(setDisabled(bool)));
381 connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
382 whatsThisButton, SLOT(setChecked(bool)));
383 connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
384 m_renderer->hoverPoints(), SLOT(setDisabled(bool)));
386 m_renderer->loadSourceFile(":res/gradients/gradients.cpp");
387 m_renderer->loadDescription(":res/gradients/gradients.html");
389 QTimer::singleShot(50, this, SLOT(setDefault1()));
392 void GradientWidget::setDefault(int config)
394 QGradientStops stops;
398 stops << QGradientStop(0.00, QColor::fromRgba(0));
399 stops << QGradientStop(0.04, QColor::fromRgba(0xff131360));
400 stops << QGradientStop(0.08, QColor::fromRgba(0xff202ccc));
401 stops << QGradientStop(0.42, QColor::fromRgba(0xff93d3f9));
402 stops << QGradientStop(0.51, QColor::fromRgba(0xffb3e6ff));
403 stops << QGradientStop(0.73, QColor::fromRgba(0xffffffec));
404 stops << QGradientStop(0.92, QColor::fromRgba(0xff5353d9));
405 stops << QGradientStop(0.96, QColor::fromRgba(0xff262666));
406 stops << QGradientStop(1.00, QColor::fromRgba(0));
407 m_linearButton->animateClick();
408 m_repeatSpreadButton->animateClick();
412 stops << QGradientStop(0.00, QColor::fromRgba(0xffffffff));
413 stops << QGradientStop(0.11, QColor::fromRgba(0xfff9ffa0));
414 stops << QGradientStop(0.13, QColor::fromRgba(0xfff9ff99));
415 stops << QGradientStop(0.14, QColor::fromRgba(0xfff3ff86));
416 stops << QGradientStop(0.49, QColor::fromRgba(0xff93b353));
417 stops << QGradientStop(0.87, QColor::fromRgba(0xff264619));
418 stops << QGradientStop(0.96, QColor::fromRgba(0xff0c1306));
419 stops << QGradientStop(1.00, QColor::fromRgba(0));
420 m_radialButton->animateClick();
421 m_padSpreadButton->animateClick();
425 stops << QGradientStop(0.00, QColor::fromRgba(0));
426 stops << QGradientStop(0.10, QColor::fromRgba(0xffe0cc73));
427 stops << QGradientStop(0.17, QColor::fromRgba(0xffc6a006));
428 stops << QGradientStop(0.46, QColor::fromRgba(0xff600659));
429 stops << QGradientStop(0.72, QColor::fromRgba(0xff0680ac));
430 stops << QGradientStop(0.92, QColor::fromRgba(0xffb9d9e6));
431 stops << QGradientStop(1.00, QColor::fromRgba(0));
432 m_conicalButton->animateClick();
433 m_padSpreadButton->animateClick();
437 stops << QGradientStop(0.00, QColor::fromRgba(0xff000000));
438 stops << QGradientStop(1.00, QColor::fromRgba(0xffffffff));
442 qWarning("bad default: %d\n", config);
447 int h_off = m_renderer->width() / 10;
448 int v_off = m_renderer->height() / 8;
449 pts << QPointF(m_renderer->width() / 2, m_renderer->height() / 2)
450 << QPointF(m_renderer->width() / 2 - h_off, m_renderer->height() / 2 - v_off);
452 m_editor->setGradientStops(stops);
453 m_renderer->hoverPoints()->setPoints(pts);
454 m_renderer->setGradientStops(stops);
458 GradientRenderer::GradientRenderer(QWidget *parent)
459 : ArthurFrame(parent)
461 m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
462 m_hoverPoints->setPointSize(QSize(20, 20));
463 m_hoverPoints->setConnectionType(HoverPoints::NoConnection);
464 m_hoverPoints->setEditable(false);
466 QVector<QPointF> points;
467 points << QPointF(100, 100) << QPointF(200, 200);
468 m_hoverPoints->setPoints(points);
470 m_spread = QGradient::PadSpread;
471 m_gradientType = Qt::LinearGradientPattern;
474 void GradientRenderer::setGradientStops(const QGradientStops &stops)
481 void GradientRenderer::mousePressEvent(QMouseEvent *)
483 setDescriptionEnabled(false);
486 void GradientRenderer::paint(QPainter *p)
488 QPolygonF pts = m_hoverPoints->points();
492 if (m_gradientType == Qt::LinearGradientPattern) {
493 g = QLinearGradient(pts.at(0), pts.at(1));
495 } else if (m_gradientType == Qt::RadialGradientPattern) {
496 g = QRadialGradient(pts.at(0), qMin(width(), height()) / 3.0, pts.at(1));
499 QLineF l(pts.at(0), pts.at(1));
500 qreal angle = l.angle(QLineF(0, 0, 1, 0));
503 g = QConicalGradient(pts.at(0), angle);
506 for (int i=0; i<m_stops.size(); ++i)
507 g.setColorAt(m_stops.at(i).first, m_stops.at(i).second);
509 g.setSpread(m_spread);
512 p->setPen(Qt::NoPen);