1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: gradients.cpp Example File (demos/gradients/gradients.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">gradients.cpp Example File<br /><small><small>demos/gradients/gradients.cpp</small></small></h1>
20
<pre> #include "gradients.h"
21
#include "hoverpoints.h"
23
ShadeWidget::ShadeWidget(ShadeType type, QWidget *parent)
24
: QWidget(parent), m_shade_type(type), m_alpha_gradient(QLinearGradient(0, 0, 0, 0))
27
// Checkers background
28
if (m_shade_type == ARGBShade) {
30
QPainter pmp(&pm);
31
pmp.fillRect(0, 0, 10, 10, Qt::lightGray);
32
pmp.fillRect(10, 10, 10, 10, Qt::lightGray);
33
pmp.fillRect(0, 10, 10, 10, Qt::darkGray);
34
pmp.fillRect(10, 0, 10, 10, Qt::darkGray);
36
QPalette pal = palette();
37
pal.setBrush(backgroundRole(), QBrush(pm));
41
setAttribute(Qt::WA_NoBackground);
46
points << QPointF(0, sizeHint().height())
47
<< QPointF(sizeHint().width(), 0);
49
m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
50
// m_hoverPoints->setConnectionType(HoverPoints::LineConnection);
51
m_hoverPoints->setPoints(points);
52
m_hoverPoints->setPointLock(0, HoverPoints::LockToLeft);
53
m_hoverPoints->setPointLock(1, HoverPoints::LockToRight);
54
m_hoverPoints->setSortType(HoverPoints::XSort);
56
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
58
connect(m_hoverPoints, SIGNAL(pointsChanged(const QPolygonF &)), this, SIGNAL(colorsChanged()));
61
QPolygonF ShadeWidget::points() const
63
return m_hoverPoints->points();
66
uint ShadeWidget::colorAt(int x)
70
QPolygonF pts = m_hoverPoints->points();
71
for (int i=1; i < pts.size(); ++i) {
72
if (pts.at(i-1).x() <= x && pts.at(i).x() >= x) {
73
QLineF l(pts.at(i-1), pts.at(i));
74
l.setLength(l.length() * ((x - l.x1()) / l.dx()));
75
return m_shade.pixel(qRound(qMin(l.x2(), (double(m_shade.width() - 1)))),
76
qRound(qMin(l.y2(), double(m_shade.height() - 1))));
82
void ShadeWidget::setGradientStops(const QGradientStops &stops)
84
if (m_shade_type == ARGBShade) {
85
m_alpha_gradient = QLinearGradient(0, 0, width(), 0);
87
for (int i=0; i<stops.size(); ++i) {
88
QColor c = stops.at(i).second;
89
m_alpha_gradient.setColorAt(stops.at(i).first, QColor(c.red(), c.green(), c.blue()));
98
void ShadeWidget::paintEvent(QPaintEvent *)
103
p.drawImage(0, 0, m_shade);
105
p.setPen(QColor(146, 146, 146));
106
p.drawRect(0, 0, width() - 1, height() - 1);
109
void ShadeWidget::generateShade()
111
if (m_shade.isNull() || m_shade.size() != size()) {
113
if (m_shade_type == ARGBShade) {
114
m_shade = QImage(size(), QImage::Format_ARGB32_Premultiplied);
117
QPainter p(&m_shade);
118
p.fillRect(rect(), m_alpha_gradient);
120
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
121
QLinearGradient fade(0, 0, 0, height());
122
fade.setColorAt(0, QColor(0, 0, 0, 255));
123
fade.setColorAt(1, QColor(0, 0, 0, 0));
124
p.fillRect(rect(), fade);
127
m_shade = QImage(size(), QImage::Format_RGB32);
128
QLinearGradient shade(0, 0, 0, height());
129
shade.setColorAt(1, Qt::black);
131
if (m_shade_type == RedShade)
132
shade.setColorAt(0, Qt::red);
133
else if (m_shade_type == GreenShade)
134
shade.setColorAt(0, Qt::green);
136
shade.setColorAt(0, Qt::blue);
138
QPainter p(&m_shade);
139
p.fillRect(rect(), shade);
145
GradientEditor::GradientEditor(QWidget *parent)
148
QVBoxLayout *vbox = new QVBoxLayout(this);
149
vbox->setSpacing(1);
150
vbox->setMargin(1);
152
m_red_shade = new ShadeWidget(ShadeWidget::RedShade, this);
153
m_green_shade = new ShadeWidget(ShadeWidget::GreenShade, this);
154
m_blue_shade = new ShadeWidget(ShadeWidget::BlueShade, this);
155
m_alpha_shade = new ShadeWidget(ShadeWidget::ARGBShade, this);
157
vbox->addWidget(m_red_shade);
158
vbox->addWidget(m_green_shade);
159
vbox->addWidget(m_blue_shade);
160
vbox->addWidget(m_alpha_shade);
162
connect(m_red_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
163
connect(m_green_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
164
connect(m_blue_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
165
connect(m_alpha_shade, SIGNAL(colorsChanged()), this, SLOT(pointsUpdated()));
168
inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
170
return p1.x() < p2.x();
173
void GradientEditor::pointsUpdated()
175
double w = m_alpha_shade->width();
177
QGradientStops stops;
181
points += m_red_shade->points();
182
points += m_green_shade->points();
183
points += m_blue_shade->points();
184
points += m_alpha_shade->points();
186
qSort(points.begin(), points.end(), x_less_than);
188
for (int i=0; i<points.size(); ++i) {
189
double x = int(points.at(i).x());
190
if (i < points.size() - 1 && x == points.at(i+1).x())
192
QColor color((0x00ff0000 & m_red_shade->colorAt(int(x))) >> 16,
193
(0x0000ff00 & m_green_shade->colorAt(int(x))) >> 8,
194
(0x000000ff & m_blue_shade->colorAt(int(x))),
195
(0xff000000 & m_alpha_shade->colorAt(int(x))) >> 24);
200
stops << QGradientStop(x / w, color);
203
m_alpha_shade->setGradientStops(stops);
205
emit gradientStopsChanged(stops);
208
static void set_shade_points(const QPolygonF &points, ShadeWidget *shade)
210
shade->hoverPoints()->setPoints(points);
211
shade->hoverPoints()->setPointLock(0, HoverPoints::LockToLeft);
212
shade->hoverPoints()->setPointLock(points.size() - 1, HoverPoints::LockToRight);
216
void GradientEditor::setGradientStops(const QGradientStops &stops)
218
QPolygonF pts_red, pts_green, pts_blue, pts_alpha;
220
double h_red = m_red_shade->height();
221
double h_green = m_green_shade->height();
222
double h_blue = m_blue_shade->height();
223
double h_alpha = m_alpha_shade->height();
225
for (int i=0; i<stops.size(); ++i) {
226
double pos = stops.at(i).first;
227
QRgb color = stops.at(i).second.rgba();
228
pts_red << QPointF(pos * m_red_shade->width(), h_red - qRed(color) * h_red / 255);
229
pts_green << QPointF(pos * m_green_shade->width(), h_green - qGreen(color) * h_green / 255);
230
pts_blue << QPointF(pos * m_blue_shade->width(), h_blue - qBlue(color) * h_blue / 255);
231
pts_alpha << QPointF(pos * m_alpha_shade->width(), h_alpha - qAlpha(color) * h_alpha / 255);
234
set_shade_points(pts_red, m_red_shade);
235
set_shade_points(pts_green, m_green_shade);
236
set_shade_points(pts_blue, m_blue_shade);
237
set_shade_points(pts_alpha, m_alpha_shade);
241
GradientWidget::GradientWidget(QWidget *parent)
244
setWindowTitle("Gradients");
246
m_renderer = new GradientRenderer(this);
248
ArthurGroupBox *mainGroup = new ArthurGroupBox(this);
249
mainGroup->setTitle("Gradients");
251
ArthurGroupBox *editorGroup = new ArthurGroupBox(mainGroup);
252
editorGroup->setAttribute(Qt::WA_ContentsPropagated);
253
editorGroup->setTitle("Color Editor");
254
m_editor = new GradientEditor(editorGroup);
256
ArthurGroupBox *typeGroup = new ArthurGroupBox(mainGroup);
257
typeGroup->setAttribute(Qt::WA_ContentsPropagated);
258
typeGroup->setTitle("Gradient Type");
259
m_linearButton = new QRadioButton("Linear Gradient", typeGroup);
260
m_radialButton = new QRadioButton("Radial Gradient", typeGroup);
261
m_conicalButton = new QRadioButton("Conical Gradient", typeGroup);
263
ArthurGroupBox *spreadGroup = new ArthurGroupBox(mainGroup);
264
spreadGroup->setAttribute(Qt::WA_ContentsPropagated);
265
spreadGroup->setTitle("Spread Method");
266
m_padSpreadButton = new QRadioButton("Pad Spread", spreadGroup);
267
m_reflectSpreadButton = new QRadioButton("Reflect Spread", spreadGroup);
268
m_repeatSpreadButton = new QRadioButton("Repeat Spread", spreadGroup);
270
ArthurGroupBox *defaultsGroup = new ArthurGroupBox(mainGroup);
271
defaultsGroup->setAttribute(Qt::WA_ContentsPropagated);
272
defaultsGroup->setTitle("Defaults");
273
QPushButton *default1Button = new QPushButton("1", defaultsGroup);
274
QPushButton *default2Button = new QPushButton("2", defaultsGroup);
275
QPushButton *default3Button = new QPushButton("3", defaultsGroup);
276
QPushButton *default4Button = new QPushButton("Reset", editorGroup);
278
QPushButton *showSourceButton = new QPushButton(mainGroup);
279
showSourceButton->setText("Show Source");
281
QPushButton *whatsThisButton = new QPushButton(mainGroup);
282
whatsThisButton->setText("What's This?");
283
whatsThisButton->setCheckable(true);
286
QHBoxLayout *mainLayout = new QHBoxLayout(this);
287
mainLayout->addWidget(m_renderer);
288
mainLayout->addWidget(mainGroup);
290
mainGroup->setFixedWidth(180);
291
QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
292
mainGroupLayout->addWidget(editorGroup);
293
mainGroupLayout->addWidget(typeGroup);
294
mainGroupLayout->addWidget(spreadGroup);
295
mainGroupLayout->addWidget(defaultsGroup);
296
mainGroupLayout->addStretch(1);
297
mainGroupLayout->addWidget(showSourceButton);
298
mainGroupLayout->addWidget(whatsThisButton);
300
QVBoxLayout *editorGroupLayout = new QVBoxLayout(editorGroup);
301
editorGroupLayout->addWidget(m_editor);
303
QVBoxLayout *typeGroupLayout = new QVBoxLayout(typeGroup);
304
typeGroupLayout->addWidget(m_linearButton);
305
typeGroupLayout->addWidget(m_radialButton);
306
typeGroupLayout->addWidget(m_conicalButton);
308
QVBoxLayout *spreadGroupLayout = new QVBoxLayout(spreadGroup);
309
spreadGroupLayout->addWidget(m_padSpreadButton);
310
spreadGroupLayout->addWidget(m_repeatSpreadButton);
311
spreadGroupLayout->addWidget(m_reflectSpreadButton);
313
QHBoxLayout *defaultsGroupLayout = new QHBoxLayout(defaultsGroup);
314
defaultsGroupLayout->addWidget(default1Button);
315
defaultsGroupLayout->addWidget(default2Button);
316
defaultsGroupLayout->addWidget(default3Button);
317
editorGroupLayout->addWidget(default4Button);
319
connect(m_editor, SIGNAL(gradientStopsChanged(const QGradientStops &)),
320
m_renderer, SLOT(setGradientStops(const QGradientStops &)));
322
connect(m_linearButton, SIGNAL(clicked()), m_renderer, SLOT(setLinearGradient()));
323
connect(m_radialButton, SIGNAL(clicked()), m_renderer, SLOT(setRadialGradient()));
324
connect(m_conicalButton, SIGNAL(clicked()), m_renderer, SLOT(setConicalGradient()));
326
connect(m_padSpreadButton, SIGNAL(clicked()), m_renderer, SLOT(setPadSpread()));
327
connect(m_reflectSpreadButton, SIGNAL(clicked()), m_renderer, SLOT(setReflectSpread()));
328
connect(m_repeatSpreadButton, SIGNAL(clicked()), m_renderer, SLOT(setRepeatSpread()));
330
connect(default1Button, SIGNAL(clicked()), this, SLOT(setDefault1()));
331
connect(default2Button, SIGNAL(clicked()), this, SLOT(setDefault2()));
332
connect(default3Button, SIGNAL(clicked()), this, SLOT(setDefault3()));
333
connect(default4Button, SIGNAL(clicked()), this, SLOT(setDefault4()));
335
connect(showSourceButton, SIGNAL(clicked()), m_renderer, SLOT(showSource()));
337
connect(whatsThisButton, SIGNAL(clicked(bool)), m_renderer, SLOT(setDescriptionEnabled(bool)));
338
connect(whatsThisButton, SIGNAL(clicked(bool)),
339
m_renderer->hoverPoints(), SLOT(setDisabled(bool)));
340
connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
341
whatsThisButton, SLOT(setChecked(bool)));
342
connect(m_renderer, SIGNAL(descriptionEnabledChanged(bool)),
343
m_renderer->hoverPoints(), SLOT(setDisabled(bool)));
345
m_renderer->loadSourceFile(":res/gradients.cpp");
346
m_renderer->loadDescription(":res/gradients.html");
348
QTimer::singleShot(50, this, SLOT(setDefault1()));
351
void GradientWidget::setDefault(int config)
353
QGradientStops stops;
357
stops << QGradientStop(0.00, QColor::fromRgba(0));
358
stops << QGradientStop(0.04, QColor::fromRgba(0xff131360));
359
stops << QGradientStop(0.08, QColor::fromRgba(0xff202ccc));
360
stops << QGradientStop(0.42, QColor::fromRgba(0xff93d3f9));
361
stops << QGradientStop(0.51, QColor::fromRgba(0xffb3e6ff));
362
stops << QGradientStop(0.73, QColor::fromRgba(0xffffffec));
363
stops << QGradientStop(0.92, QColor::fromRgba(0xff5353d9));
364
stops << QGradientStop(0.96, QColor::fromRgba(0xff262666));
365
stops << QGradientStop(1.00, QColor::fromRgba(0));
366
m_linearButton->animateClick();
367
m_repeatSpreadButton->animateClick();
371
stops << QGradientStop(0.00, QColor::fromRgba(0xffffffff));
372
stops << QGradientStop(0.11, QColor::fromRgba(0xfff9ffa0));
373
stops << QGradientStop(0.13, QColor::fromRgba(0xfff9ff99));
374
stops << QGradientStop(0.14, QColor::fromRgba(0xfff3ff86));
375
stops << QGradientStop(0.49, QColor::fromRgba(0xff93b353));
376
stops << QGradientStop(0.87, QColor::fromRgba(0xff264619));
377
stops << QGradientStop(0.96, QColor::fromRgba(0xff0c1306));
378
stops << QGradientStop(1.00, QColor::fromRgba(0));
379
m_radialButton->animateClick();
380
m_padSpreadButton->animateClick();
384
stops << QGradientStop(0.00, QColor::fromRgba(0));
385
stops << QGradientStop(0.10, QColor::fromRgba(0xffe0cc73));
386
stops << QGradientStop(0.17, QColor::fromRgba(0xffc6a006));
387
stops << QGradientStop(0.46, QColor::fromRgba(0xff600659));
388
stops << QGradientStop(0.72, QColor::fromRgba(0xff0680ac));
389
stops << QGradientStop(0.92, QColor::fromRgba(0xffb9d9e6));
390
stops << QGradientStop(1.00, QColor::fromRgba(0));
391
m_conicalButton->animateClick();
392
m_padSpreadButton->animateClick();
396
stops << QGradientStop(0.00, QColor::fromRgba(0xff000000));
397
stops << QGradientStop(1.00, QColor::fromRgba(0xffffffff));
401
qWarning("bad default: %d\n", config);
406
int h_off = m_renderer->width() / 10;
407
int v_off = m_renderer->height() / 8;
408
pts << QPointF(m_renderer->width() / 2, m_renderer->height() / 2)
409
<< QPointF(m_renderer->width() / 2 - h_off, m_renderer->height() / 2 - v_off);
411
m_editor->setGradientStops(stops);
412
m_renderer->hoverPoints()->setPoints(pts);
413
m_renderer->setGradientStops(stops);
416
GradientRenderer::GradientRenderer(QWidget *parent)
417
: ArthurFrame(parent)
419
m_hoverPoints = new HoverPoints(this, HoverPoints::CircleShape);
420
m_hoverPoints->setPointSize(QSize(20, 20));
421
m_hoverPoints->setConnectionType(HoverPoints::NoConnection);
422
m_hoverPoints->setEditable(false);
424
QVector<QPointF> points;
425
points << QPointF(100, 100) << QPointF(200, 200);
426
m_hoverPoints->setPoints(points);
428
m_spread = QGradient::PadSpread;
429
m_gradientType = Qt::LinearGradientPattern;
432
void GradientRenderer::setGradientStops(const QGradientStops &stops)
438
void GradientRenderer::mousePressEvent(QMouseEvent *)
440
setDescriptionEnabled(false);
443
void GradientRenderer::paint(QPainter *p)
445
QPolygonF pts = m_hoverPoints->points();
449
if (m_gradientType == Qt::LinearGradientPattern) {
450
g = QLinearGradient(pts.at(0), pts.at(1));
452
} else if (m_gradientType == Qt::RadialGradientPattern) {
453
g = QRadialGradient(pts.at(0), qMin(width(), height()) / 3.0, pts.at(1));
456
QLineF l(pts.at(0), pts.at(1));
457
double angle = l.angle(QLineF(0, 0, 1, 0));
460
g = QConicalGradient(pts.at(0), angle);
463
for (int i=0; i<m_stops.size(); ++i)
464
g.setColorAt(m_stops.at(i).first, m_stops.at(i).second);
466
g.setSpread(m_spread);
469
p->setPen(Qt::NoPen);
471
p->drawRect(rect());
474
<p /><address><hr /><div align="center">
475
<table width="100%" cellspacing="0" border="0"><tr class="address">
476
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
477
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
478
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
479
</tr></table></div></address></body>