examples/opengl/hellogl_es/bubble.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 #include "bubble.h"
       
    45 
       
    46 Bubble::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]
       
    56 void 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 
       
    72 Bubble::~Bubble()
       
    73 {
       
    74     if (cache)
       
    75         delete cache;
       
    76 }
       
    77 
       
    78 void 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]
       
    91 void 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 
       
   101 QColor 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 
       
   111 void 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 
       
   136 QRectF Bubble::rect()
       
   137 {
       
   138     return QRectF(position.x() - radius, position.y() - radius,
       
   139                   2 * radius, 2 * radius);
       
   140 }