source: trunk/examples/opengl/hellogl_es/bubble.cpp@ 641

Last change on this file since 641 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 4.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "bubble.h"
45
46Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity)
47 : position(position), vel(velocity), radius(radius)
48{
49 innerColor = randomColor();
50 outerColor = randomColor();
51 cache = 0;
52 updateBrush();
53}
54
55//! [0]
56void Bubble::updateCache()
57{
58 if (cache)
59 delete cache;
60 cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32);
61 cache->fill(0x00000000);
62 QPainter p(cache);
63 p.setRenderHint(QPainter::HighQualityAntialiasing);
64 QPen pen(Qt::white);
65 pen.setWidth(2);
66 p.setPen(pen);
67 p.setBrush(brush);
68 p.drawEllipse(0, 0, int(2*radius), int(2*radius));
69}
70//! [0]
71
72Bubble::~Bubble()
73{
74 if (cache)
75 delete cache;
76}
77
78void Bubble::updateBrush()
79{
80 QRadialGradient gradient(QPointF(radius, radius), radius,
81 QPointF(radius*0.5, radius*0.5));
82
83 gradient.setColorAt(0, QColor(255, 255, 255, 255));
84 gradient.setColorAt(0.25, innerColor);
85 gradient.setColorAt(1, outerColor);
86 brush = QBrush(gradient);
87 updateCache();
88}
89
90//! [1]
91void Bubble::drawBubble(QPainter *painter)
92{
93 painter->save();
94 painter->translate(position.x() - radius, position.y() - radius);
95 painter->setOpacity(0.8);
96 painter->drawImage(0, 0, *cache);
97 painter->restore();
98}
99//! [1]
100
101QColor Bubble::randomColor()
102{
103 int red = int(185 + 70.0*qrand()/(RAND_MAX+1.0));
104 int green = int(185 + 70.0*qrand()/(RAND_MAX+1.0));
105 int blue = int(205 + 50.0*qrand()/(RAND_MAX+1.0));
106 int alpha = int(91 + 100.0*qrand()/(RAND_MAX+1.0));
107
108 return QColor(red, green, blue, alpha);
109}
110
111void Bubble::move(const QRect &bbox)
112{
113 position += vel;
114 qreal leftOverflow = position.x() - radius - bbox.left();
115 qreal rightOverflow = position.x() + radius - bbox.right();
116 qreal topOverflow = position.y() - radius - bbox.top();
117 qreal bottomOverflow = position.y() + radius - bbox.bottom();
118
119 if (leftOverflow < 0.0) {
120 position.setX(position.x() - 2 * leftOverflow);
121 vel.setX(-vel.x());
122 } else if (rightOverflow > 0.0) {
123 position.setX(position.x() - 2 * rightOverflow);
124 vel.setX(-vel.x());
125 }
126
127 if (topOverflow < 0.0) {
128 position.setY(position.y() - 2 * topOverflow);
129 vel.setY(-vel.y());
130 } else if (bottomOverflow > 0.0) {
131 position.setY(position.y() - 2 * bottomOverflow);
132 vel.setY(-vel.y());
133 }
134}
135
136QRectF Bubble::rect()
137{
138 return QRectF(position.x() - radius, position.y() - radius,
139 2 * radius, 2 * radius);
140}
Note: See TracBrowser for help on using the repository browser.