| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include <QtWidgets> |
| 5 |
|
| 6 |
#include "bubble.h" |
| 7 |
|
| 8 |
Bubble::Bubble(const QPointF &position, qreal radius, const QPointF &velocity) |
| 9 |
: position(position), vel(velocity), radius(radius) |
| 10 |
{ |
| 11 |
innerColor = randomColor(); |
| 12 |
outerColor = randomColor(); |
| 13 |
updateBrush(); |
| 14 |
} |
| 15 |
|
| 16 |
//! [0] |
| 17 |
void Bubble::updateCache() |
| 18 |
{ |
| 19 |
delete cache; |
| 20 |
cache = new QImage(qRound(radius * 2 + 2), qRound(radius * 2 + 2), QImage::Format_ARGB32_Premultiplied); |
| 21 |
cache->fill(0x00000000); |
| 22 |
QPainter p(cache); |
| 23 |
p.setRenderHint(QPainter::Antialiasing); |
| 24 |
QPen pen(Qt::white); |
| 25 |
pen.setWidth(2); |
| 26 |
p.setPen(pen); |
| 27 |
p.setBrush(brush); |
| 28 |
p.drawEllipse(1, 1, int(2*radius), int(2*radius)); |
| 29 |
} |
| 30 |
//! [0] |
| 31 |
|
| 32 |
Bubble::~Bubble() |
| 33 |
{ |
| 34 |
delete cache; |
| 35 |
} |
| 36 |
|
| 37 |
void Bubble::updateBrush() |
| 38 |
{ |
| 39 |
QRadialGradient gradient(QPointF(radius, radius), radius, |
| 40 |
QPointF(radius*0.5, radius*0.5)); |
| 41 |
|
| 42 |
gradient.setColorAt(0, QColor(255, 255, 255, 255)); |
| 43 |
gradient.setColorAt(0.25, innerColor); |
| 44 |
gradient.setColorAt(1, outerColor); |
| 45 |
brush = QBrush(gradient); |
| 46 |
updateCache(); |
| 47 |
} |
| 48 |
|
| 49 |
//! [1] |
| 50 |
void Bubble::drawBubble(QPainter *painter) |
| 51 |
{ |
| 52 |
painter->save(); |
| 53 |
painter->translate(position.x() - radius, position.y() - radius); |
| 54 |
painter->setOpacity(0.8); |
| 55 |
painter->drawImage(0, 0, *cache); |
| 56 |
painter->restore(); |
| 57 |
} |
| 58 |
//! [1] |
| 59 |
|
| 60 |
QColor Bubble::randomColor() |
| 61 |
{ |
| 62 |
int red = int(185 + QRandomGenerator::global()->bounded(70)); |
| 63 |
int green = int(185 + QRandomGenerator::global()->bounded(70)); |
| 64 |
int blue = int(205 + QRandomGenerator::global()->bounded(50)); |
| 65 |
int alpha = int(91 + QRandomGenerator::global()->bounded(100)); |
| 66 |
|
| 67 |
return QColor(red, green, blue, alpha); |
| 68 |
} |
| 69 |
|
| 70 |
void Bubble::move(const QRect &bbox) |
| 71 |
{ |
| 72 |
position += vel; |
| 73 |
qreal leftOverflow = position.x() - radius - bbox.left(); |
| 74 |
qreal rightOverflow = position.x() + radius - bbox.right(); |
| 75 |
qreal topOverflow = position.y() - radius - bbox.top(); |
| 76 |
qreal bottomOverflow = position.y() + radius - bbox.bottom(); |
| 77 |
|
| 78 |
if (leftOverflow < 0.0) { |
| 79 |
position.setX(position.x() - 2 * leftOverflow); |
| 80 |
vel.setX(-vel.x()); |
| 81 |
} else if (rightOverflow > 0.0) { |
| 82 |
position.setX(position.x() - 2 * rightOverflow); |
| 83 |
vel.setX(-vel.x()); |
| 84 |
} |
| 85 |
|
| 86 |
if (topOverflow < 0.0) { |
| 87 |
position.setY(position.y() - 2 * topOverflow); |
| 88 |
vel.setY(-vel.y()); |
| 89 |
} else if (bottomOverflow > 0.0) { |
| 90 |
position.setY(position.y() - 2 * bottomOverflow); |
| 91 |
vel.setY(-vel.y()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
QRectF Bubble::rect() |
| 96 |
{ |
| 97 |
return QRectF(position.x() - radius, position.y() - radius, |
| 98 |
2 * radius, 2 * radius); |
| 99 |
} |