examples/effects/blurpicker/blurpicker.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 "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)
       
    53 {
       
    54     setBackgroundBrush(QPixmap(":/images/background.jpg"));
       
    55     setScene(&m_scene);
       
    56 
       
    57     setupScene();
       
    58     updateIconPositions();
       
    59 
       
    60     connect(&m_timeLine, SIGNAL(valueChanged(qreal)), SLOT(updateIconPositions()));
       
    61     m_timeLine.setDuration(400);
       
    62 
       
    63     setRenderHint(QPainter::Antialiasing, true);
       
    64     setFrameStyle(QFrame::NoFrame);
       
    65 }
       
    66 
       
    67 void BlurPicker::updateIconPositions()
       
    68 {
       
    69     m_index = m_timeLine.currentFrame() / 1000.0;
       
    70 
       
    71     qreal baseline = 0;
       
    72     for (int i = 0; i < m_icons.count(); ++i) {
       
    73         QGraphicsItem *icon = m_icons[i];
       
    74         qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count();
       
    75         qreal xs = 170 * sin(a);
       
    76         qreal ys = 100 * cos(a);
       
    77         QPointF pos(xs, ys);
       
    78         pos = QTransform().rotate(-20).map(pos);
       
    79         pos -= QPointF(40, 40);
       
    80         icon->setPos(pos);
       
    81         baseline = qMax(baseline, ys);
       
    82         static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline);
       
    83     }
       
    84 
       
    85     m_scene.update();
       
    86 }
       
    87 
       
    88 void BlurPicker::setupScene()
       
    89 {
       
    90     m_scene.setSceneRect(-200, -120, 400, 240);
       
    91 
       
    92     QStringList names;
       
    93     names << ":/images/accessories-calculator.png";
       
    94     names << ":/images/accessories-text-editor.png";
       
    95     names << ":/images/help-browser.png";
       
    96     names << ":/images/internet-group-chat.png";
       
    97     names << ":/images/internet-mail.png";
       
    98     names << ":/images/internet-web-browser.png";
       
    99     names << ":/images/office-calendar.png";
       
   100     names << ":/images/system-users.png";
       
   101 
       
   102     for (int i = 0; i < names.count(); i++) {
       
   103         QPixmap pixmap(names[i]);
       
   104         QGraphicsPixmapItem *icon = m_scene.addPixmap(pixmap);
       
   105         icon->setZValue(1);
       
   106         icon->setGraphicsEffect(new BlurEffect(icon));
       
   107         m_icons << icon;
       
   108     }
       
   109 
       
   110     QGraphicsPixmapItem *bg = m_scene.addPixmap(QPixmap(":/images/background.jpg"));
       
   111     bg->setZValue(0);
       
   112     bg->setPos(-200, -150);
       
   113 }
       
   114 
       
   115 void BlurPicker::keyPressEvent(QKeyEvent *event)
       
   116 {
       
   117     if (event->key() == Qt::Key_Left) {
       
   118         if (m_timeLine.state() == QTimeLine::NotRunning) {
       
   119             m_timeLine.setFrameRange(m_index * 1000, m_index * 1000 - 1000);
       
   120             m_timeLine.start();
       
   121             event->accept();
       
   122         }
       
   123     }
       
   124 
       
   125     if (event->key() == Qt::Key_Right) {
       
   126         if (m_timeLine.state() == QTimeLine::NotRunning) {
       
   127             m_timeLine.setFrameRange(m_index * 1000, m_index * 1000 + 1000);
       
   128             m_timeLine.start();
       
   129             event->accept();
       
   130         }
       
   131     }
       
   132 }