Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / examples / painting / gradients / gradients.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 demonstration applications of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "gradients.h"
43 #include "hoverpoints.h"
44
45 ShadeWidget::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(QPolygonF)), this, SIGNAL(colorsChanged()));
82 }
83
84
85 QPolygonF ShadeWidget::points() const
86 {
87     return m_hoverPoints->points();
88 }
89
90
91 uint 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
108 void 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
125 void 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
137 void 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
175 GradientEditor::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
199 inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
200 {
201     return p1.x() < p2.x();
202 }
203
204
205 void 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+1 < points.size() && 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
241 static 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
249 void 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
274 GradientWidget::GradientWidget(QWidget *parent)
275     : QWidget(parent)
276 {
277     setWindowTitle(tr("Gradients"));
278
279     m_renderer = new GradientRenderer(this);
280
281     QGroupBox *mainGroup = new QGroupBox(this);
282     mainGroup->setTitle(tr("Gradients"));
283
284     QGroupBox *editorGroup = new QGroupBox(mainGroup);
285     editorGroup->setTitle(tr("Color Editor"));
286     m_editor = new GradientEditor(editorGroup);
287
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);
293
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);
299
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);
306
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();
316 #endif
317     QPushButton *whatsThisButton = new QPushButton(mainGroup);
318     whatsThisButton->setText(tr("What's This?"));
319     whatsThisButton->setCheckable(true);
320
321     // Layouts
322     QHBoxLayout *mainLayout = new QHBoxLayout(this);
323     mainLayout->addWidget(m_renderer);
324     mainLayout->addWidget(mainGroup);
325
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);
336 #endif
337     mainGroupLayout->addWidget(whatsThisButton);
338
339     QVBoxLayout *editorGroupLayout = new QVBoxLayout(editorGroup);
340     editorGroupLayout->addWidget(m_editor);
341
342     QVBoxLayout *typeGroupLayout = new QVBoxLayout(typeGroup);
343     typeGroupLayout->addWidget(m_linearButton);
344     typeGroupLayout->addWidget(m_radialButton);
345     typeGroupLayout->addWidget(m_conicalButton);
346
347     QVBoxLayout *spreadGroupLayout = new QVBoxLayout(spreadGroup);
348     spreadGroupLayout->addWidget(m_padSpreadButton);
349     spreadGroupLayout->addWidget(m_repeatSpreadButton);
350     spreadGroupLayout->addWidget(m_reflectSpreadButton);
351
352     QHBoxLayout *defaultsGroupLayout = new QHBoxLayout(defaultsGroup);
353     defaultsGroupLayout->addWidget(default1Button);
354     defaultsGroupLayout->addWidget(default2Button);
355     defaultsGroupLayout->addWidget(default3Button);
356     editorGroupLayout->addWidget(default4Button);
357
358     connect(m_editor, SIGNAL(gradientStopsChanged(QGradientStops)),
359             m_renderer, SLOT(setGradientStops(QGradientStops)));
360
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()));
364
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()));
368
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()));
373
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)));
377 #endif    
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)));
385
386     m_renderer->loadSourceFile(":res/gradients/gradients.cpp");
387     m_renderer->loadDescription(":res/gradients/gradients.html");
388
389     QTimer::singleShot(50, this, SLOT(setDefault1()));
390 }
391
392 void GradientWidget::setDefault(int config)
393 {
394     QGradientStops stops;
395     QPolygonF points;
396     switch (config) {
397     case 1:
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();
409         break;
410
411     case 2:
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();
422         break;
423
424     case 3:
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();
434         break;
435
436     case 4:
437         stops << QGradientStop(0.00, QColor::fromRgba(0xff000000));
438         stops << QGradientStop(1.00, QColor::fromRgba(0xffffffff));
439         break;
440
441     default:
442         qWarning("bad default: %d\n", config);
443         break;
444     }
445
446     QPolygonF pts;
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);
451
452     m_editor->setGradientStops(stops);
453     m_renderer->hoverPoints()->setPoints(pts);
454     m_renderer->setGradientStops(stops);
455 }
456
457
458 GradientRenderer::GradientRenderer(QWidget *parent)
459     : ArthurFrame(parent)
460 {
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);
465
466     QVector<QPointF> points;
467     points << QPointF(100, 100) << QPointF(200, 200);
468     m_hoverPoints->setPoints(points);
469
470     m_spread = QGradient::PadSpread;
471     m_gradientType = Qt::LinearGradientPattern;
472 }
473
474 void GradientRenderer::setGradientStops(const QGradientStops &stops)
475 {
476     m_stops = stops;
477     update();
478 }
479
480
481 void GradientRenderer::mousePressEvent(QMouseEvent *)
482 {
483     setDescriptionEnabled(false);
484 }
485
486 void GradientRenderer::paint(QPainter *p)
487 {
488     QPolygonF pts = m_hoverPoints->points();
489
490     QGradient g;
491
492     if (m_gradientType == Qt::LinearGradientPattern) {
493         g = QLinearGradient(pts.at(0), pts.at(1));
494
495     } else if (m_gradientType == Qt::RadialGradientPattern) {
496         g = QRadialGradient(pts.at(0), qMin(width(), height()) / 3.0, pts.at(1));
497
498     } else {
499         QLineF l(pts.at(0), pts.at(1));
500         qreal angle = l.angle(QLineF(0, 0, 1, 0));
501         if (l.dy() > 0)
502             angle = 360 - angle;
503         g = QConicalGradient(pts.at(0), angle);
504     }
505
506     for (int i=0; i<m_stops.size(); ++i)
507         g.setColorAt(m_stops.at(i).first, m_stops.at(i).second);
508
509     g.setSpread(m_spread);
510
511     p->setBrush(g);
512     p->setPen(Qt::NoPen);
513
514     p->drawRect(rect());
515
516 }