1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "blurpicker.h" |
52 | |
53 | #include <QtWidgets> |
54 | #include <QtCore/qmath.h> |
55 | #include <qmath.h> |
56 | #include "blureffect.h" |
57 | |
58 | BlurPicker::BlurPicker(QWidget *parent): QGraphicsView(parent), m_index(0.0), m_animation(this, "index" ) |
59 | { |
60 | setBackgroundBrush(QPixmap(":/images/background.jpg" )); |
61 | setScene(new QGraphicsScene(this)); |
62 | |
63 | setupScene(); |
64 | setIndex(0); |
65 | |
66 | m_animation.setDuration(400); |
67 | m_animation.setEasingCurve(QEasingCurve::InOutSine); |
68 | |
69 | setRenderHint(hint: QPainter::Antialiasing, enabled: true); |
70 | setFrameStyle(QFrame::NoFrame); |
71 | } |
72 | |
73 | qreal BlurPicker::index() const |
74 | { |
75 | return m_index; |
76 | } |
77 | |
78 | void BlurPicker::setIndex(qreal index) |
79 | { |
80 | m_index = index; |
81 | |
82 | qreal baseline = 0; |
83 | const qreal iconAngle = 2 * M_PI / m_icons.count(); |
84 | for (int i = 0; i < m_icons.count(); ++i) { |
85 | QGraphicsItem *icon = m_icons[i]; |
86 | qreal a = (i + m_index) * iconAngle; |
87 | qreal xs = 170 * qSin(v: a); |
88 | qreal ys = 100 * qCos(v: a); |
89 | QPointF pos(xs, ys); |
90 | pos = QTransform().rotate(a: -20).map(p: pos); |
91 | pos -= QPointF(40, 40); |
92 | icon->setPos(pos); |
93 | baseline = qMax(a: baseline, b: ys); |
94 | static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline); |
95 | } |
96 | |
97 | scene()->update(); |
98 | } |
99 | |
100 | void BlurPicker::setupScene() |
101 | { |
102 | scene()->setSceneRect(x: -200, y: -120, w: 400, h: 240); |
103 | |
104 | QStringList names; |
105 | names << ":/images/accessories-calculator.png" ; |
106 | names << ":/images/accessories-text-editor.png" ; |
107 | names << ":/images/help-browser.png" ; |
108 | names << ":/images/internet-group-chat.png" ; |
109 | names << ":/images/internet-mail.png" ; |
110 | names << ":/images/internet-web-browser.png" ; |
111 | names << ":/images/office-calendar.png" ; |
112 | names << ":/images/system-users.png" ; |
113 | |
114 | for (int i = 0; i < names.count(); i++) { |
115 | QPixmap pixmap(names[i]); |
116 | QGraphicsPixmapItem *icon = scene()->addPixmap(pixmap); |
117 | icon->setZValue(1); |
118 | icon->setGraphicsEffect(new BlurEffect(icon)); |
119 | m_icons << icon; |
120 | } |
121 | |
122 | QGraphicsPixmapItem *bg = scene()->addPixmap(pixmap: QPixmap(":/images/background.jpg" )); |
123 | bg->setZValue(0); |
124 | bg->setPos(ax: -200, ay: -150); |
125 | } |
126 | |
127 | void BlurPicker::keyPressEvent(QKeyEvent *event) |
128 | { |
129 | int delta = 0; |
130 | switch (event->key()) |
131 | { |
132 | case Qt::Key_Left: |
133 | delta = -1; |
134 | break; |
135 | case Qt::Key_Right: |
136 | delta = 1; |
137 | break; |
138 | default: |
139 | break; |
140 | } |
141 | if (m_animation.state() == QAbstractAnimation::Stopped && delta) { |
142 | m_animation.setEndValue(m_index + delta); |
143 | m_animation.start(); |
144 | event->accept(); |
145 | } |
146 | } |
147 | |
148 | void BlurPicker::resizeEvent(QResizeEvent * /* event */) |
149 | { |
150 | } |
151 | |
152 | void BlurPicker::mousePressEvent(QMouseEvent *event) |
153 | { |
154 | int delta = 0; |
155 | if(event->x() > (width() / 2)) |
156 | { |
157 | delta = 1; |
158 | } |
159 | else |
160 | { |
161 | delta = -1; |
162 | } |
163 | |
164 | if (m_animation.state() == QAbstractAnimation::Stopped && delta) { |
165 | m_animation.setEndValue(m_index + delta); |
166 | m_animation.start(); |
167 | event->accept(); |
168 | } |
169 | } |
170 | |