util/examples/effects/blurpicker/blurpicker.cpp
changeset 7 f7bc934e204c
equal deleted inserted replaced
3:41300fa6a67c 7:f7bc934e204c
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 "blurpicker.h"
       
    43 
       
    44 #include <QtGui>
       
    45 
       
    46 #include "blureffect.h"
       
    47 
       
    48 #ifndef M_PI
       
    49 #define M_PI 3.14159265358979323846
       
    50 #endif
       
    51 
       
    52 BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index")
       
    53 {
       
    54     setBackgroundBrush(QPixmap(":/images/background.jpg"));
       
    55     setScene(new QGraphicsScene(this));
       
    56 
       
    57     setupScene();
       
    58     setIndex(0);
       
    59 
       
    60     m_animation.setDuration(400);
       
    61     m_animation.setEasingCurve(QEasingCurve::InOutSine);
       
    62 
       
    63     setRenderHint(QPainter::Antialiasing, true);
       
    64     setFrameStyle(QFrame::NoFrame);
       
    65 }
       
    66 
       
    67 qreal BlurPicker::index() const
       
    68 {
       
    69     return m_index;
       
    70 }
       
    71 
       
    72 void BlurPicker::setIndex(qreal index)
       
    73 {
       
    74     m_index = index;
       
    75 
       
    76     qreal baseline = 0;
       
    77     for (int i = 0; i < m_icons.count(); ++i) {
       
    78         QGraphicsItem *icon = m_icons[i];
       
    79         qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count();
       
    80         qreal xs = 170 * sin(a);
       
    81         qreal ys = 100 * cos(a);
       
    82         QPointF pos(xs, ys);
       
    83         pos = QTransform().rotate(-20).map(pos);
       
    84         pos -= QPointF(40, 40);
       
    85         icon->setPos(pos);
       
    86         baseline = qMax(baseline, ys);
       
    87         static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline);
       
    88     }
       
    89 
       
    90     scene()->update();
       
    91 }
       
    92 
       
    93 void BlurPicker::setupScene()
       
    94 {
       
    95     scene()->setSceneRect(-200, -120, 400, 240);
       
    96 
       
    97     QStringList names;
       
    98     names << ":/images/accessories-calculator.png";
       
    99     names << ":/images/accessories-text-editor.png";
       
   100     names << ":/images/help-browser.png";
       
   101     names << ":/images/internet-group-chat.png";
       
   102     names << ":/images/internet-mail.png";
       
   103     names << ":/images/internet-web-browser.png";
       
   104     names << ":/images/office-calendar.png";
       
   105     names << ":/images/system-users.png";
       
   106 
       
   107     for (int i = 0; i < names.count(); i++) {
       
   108         QPixmap pixmap(names[i]);
       
   109         QGraphicsPixmapItem *icon = scene()->addPixmap(pixmap);
       
   110         icon->setZValue(1);
       
   111         icon->setGraphicsEffect(new BlurEffect(icon));
       
   112         m_icons << icon;
       
   113     }
       
   114 
       
   115     QGraphicsPixmapItem *bg = scene()->addPixmap(QPixmap(":/images/background.jpg"));
       
   116     bg->setZValue(0);
       
   117     bg->setPos(-200, -150);
       
   118 }
       
   119 
       
   120 void BlurPicker::keyPressEvent(QKeyEvent *event)
       
   121 {
       
   122     int delta = 0;
       
   123     switch (event->key())
       
   124     {
       
   125     case Qt::Key_Left:
       
   126         delta = -1;
       
   127         break;
       
   128     case  Qt::Key_Right:
       
   129         delta = 1;
       
   130         break;
       
   131     default:
       
   132         break;
       
   133     }
       
   134     if (m_animation.state() == QAbstractAnimation::Stopped && delta) {
       
   135             m_animation.setEndValue(m_index + delta);
       
   136             m_animation.start();
       
   137             event->accept();
       
   138     }
       
   139 }