source: trunk/examples/effects/blurpicker/blurpicker.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "blurpicker.h"
42
43#include <QtGui>
44
45#include "blureffect.h"
46
47#ifndef M_PI
48#define M_PI 3.14159265358979323846
49#endif
50
51BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index")
52{
53 setBackgroundBrush(QPixmap(":/images/background.jpg"));
54 setScene(new QGraphicsScene(this));
55
56 setupScene();
57 setIndex(0);
58
59 m_animation.setDuration(400);
60 m_animation.setEasingCurve(QEasingCurve::InOutSine);
61
62 setRenderHint(QPainter::Antialiasing, true);
63 setFrameStyle(QFrame::NoFrame);
64}
65
66qreal BlurPicker::index() const
67{
68 return m_index;
69}
70
71void BlurPicker::setIndex(qreal index)
72{
73 m_index = index;
74
75 qreal baseline = 0;
76 for (int i = 0; i < m_icons.count(); ++i) {
77 QGraphicsItem *icon = m_icons[i];
78 qreal a = ((i + m_index) * 2 * M_PI) / m_icons.count();
79 qreal xs = 170 * sin(a);
80 qreal ys = 100 * cos(a);
81 QPointF pos(xs, ys);
82 pos = QTransform().rotate(-20).map(pos);
83 pos -= QPointF(40, 40);
84 icon->setPos(pos);
85 baseline = qMax(baseline, ys);
86 static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline);
87 }
88
89 scene()->update();
90}
91
92void BlurPicker::setupScene()
93{
94 scene()->setSceneRect(-200, -120, 400, 240);
95
96 QStringList names;
97 names << ":/images/accessories-calculator.png";
98 names << ":/images/accessories-text-editor.png";
99 names << ":/images/help-browser.png";
100 names << ":/images/internet-group-chat.png";
101 names << ":/images/internet-mail.png";
102 names << ":/images/internet-web-browser.png";
103 names << ":/images/office-calendar.png";
104 names << ":/images/system-users.png";
105
106 for (int i = 0; i < names.count(); i++) {
107 QPixmap pixmap(names[i]);
108 QGraphicsPixmapItem *icon = scene()->addPixmap(pixmap);
109 icon->setZValue(1);
110 icon->setGraphicsEffect(new BlurEffect(icon));
111 m_icons << icon;
112 }
113
114 QGraphicsPixmapItem *bg = scene()->addPixmap(QPixmap(":/images/background.jpg"));
115 bg->setZValue(0);
116 bg->setPos(-200, -150);
117}
118
119void BlurPicker::keyPressEvent(QKeyEvent *event)
120{
121 int delta = 0;
122 switch (event->key())
123 {
124 case Qt::Key_Left:
125 delta = -1;
126 break;
127 case Qt::Key_Right:
128 delta = 1;
129 break;
130 default:
131 break;
132 }
133 if (m_animation.state() == QAbstractAnimation::Stopped && delta) {
134 m_animation.setEndValue(m_index + delta);
135 m_animation.start();
136 event->accept();
137 }
138}
Note: See TracBrowser for help on using the repository browser.