examples/effects/lighting/lighting.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 "lighting.h"
       
    43 
       
    44 #include <QtGui>
       
    45 
       
    46 #ifndef M_PI
       
    47 #define M_PI 3.14159265358979323846
       
    48 #endif
       
    49 
       
    50 Lighting::Lighting(QWidget *parent): QGraphicsView(parent), angle(0.0)
       
    51 {
       
    52     setScene(&m_scene);
       
    53 
       
    54     setupScene();
       
    55 
       
    56     QTimer *timer = new QTimer(this);
       
    57     connect(timer, SIGNAL(timeout()), SLOT(animate()));
       
    58     timer->setInterval(30);
       
    59     timer->start();
       
    60 
       
    61     setRenderHint(QPainter::Antialiasing, true);
       
    62     setFrameStyle(QFrame::NoFrame);
       
    63 }
       
    64 
       
    65 void Lighting::setupScene()
       
    66 {
       
    67     m_scene.setSceneRect(-300, -200, 600, 460);
       
    68 
       
    69     QLinearGradient linearGrad(QPointF(-100, -100), QPointF(100, 100));
       
    70     linearGrad.setColorAt(0, QColor(255, 255, 255));
       
    71     linearGrad.setColorAt(1, QColor(192, 192, 255));
       
    72     setBackgroundBrush(linearGrad);
       
    73 
       
    74     QRadialGradient radialGrad(30, 30, 30);
       
    75     radialGrad.setColorAt(0, Qt::yellow);
       
    76     radialGrad.setColorAt(0.2, Qt::yellow);
       
    77     radialGrad.setColorAt(1, Qt::transparent);
       
    78     QPixmap pixmap(60, 60);
       
    79     pixmap.fill(Qt::transparent);
       
    80     QPainter painter(&pixmap);
       
    81     painter.setPen(Qt::NoPen);
       
    82     painter.setBrush(radialGrad);
       
    83     painter.drawEllipse(0, 0, 60, 60);
       
    84     painter.end();
       
    85 
       
    86     m_lightSource = m_scene.addPixmap(pixmap);
       
    87     m_lightSource->setZValue(2);
       
    88 
       
    89     for (int i = -2; i < 3; ++i)
       
    90         for (int j = -2; j < 3; ++j) {
       
    91             QAbstractGraphicsShapeItem *item;
       
    92             if ((i + j) & 1)
       
    93                 item = new QGraphicsEllipseItem(0, 0, 50, 50);
       
    94             else
       
    95                 item = new QGraphicsRectItem(0, 0, 50, 50);
       
    96 
       
    97             item->setPen(QPen(Qt::black));
       
    98             item->setBrush(QBrush(Qt::white));
       
    99             QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
       
   100             effect->setBlurRadius(8);
       
   101             item->setGraphicsEffect(effect);
       
   102             item->setZValue(1);
       
   103             item->setPos(i * 80, j * 80);
       
   104             m_scene.addItem(item);
       
   105             m_items << item;
       
   106         }
       
   107 
       
   108 
       
   109 }
       
   110 
       
   111 void Lighting::animate()
       
   112 {
       
   113     angle += (M_PI / 30);
       
   114     qreal xs = 200 * sin(angle) - 40 + 25;
       
   115     qreal ys = 200 * cos(angle) - 40 + 25;
       
   116     m_lightSource->setPos(xs, ys);
       
   117 
       
   118     for (int i = 0; i < m_items.size(); ++i) {
       
   119         QGraphicsItem *item = m_items.at(i);
       
   120         Q_ASSERT(item);
       
   121         QGraphicsDropShadowEffect *effect = static_cast<QGraphicsDropShadowEffect *>(item->graphicsEffect());
       
   122         Q_ASSERT(effect);
       
   123 
       
   124         QPointF delta(item->x() - xs, item->y() - ys);
       
   125         effect->setOffset(delta.toPoint() / 30);
       
   126 
       
   127         qreal dx = delta.x();
       
   128         qreal dy = delta.y();
       
   129         qreal dd = sqrt(dx * dx + dy * dy);
       
   130         QColor color = effect->color();
       
   131         color.setAlphaF(qBound(0.4, 1 - dd / 200.0, 0.7));
       
   132         effect->setColor(color);
       
   133     }
       
   134 
       
   135     m_scene.update();
       
   136 }
       
   137