qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
spinner.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "spinner.h"
21 #include "../pixmapcache.h"
22 
23 #include <QDebug>
24 #include <QGraphicsScene>
25 #include <QPainter>
26 #include <QTime>
27 #include <QVariantAnimation>
28 
29 #include <math.h>
30 
31 Spinner::Spinner(const QString& img, QSize Size, qreal speed)
32  : size(Size)
33  , rotSpeed(speed)
34 {
36 
37  // Timer for the animation, if the Widget is not redrawn, no paint events will
38  // arrive and the timer will not be restarted, so this stops automatically
39  timer.setInterval(1000 / framerate);
40  timer.setSingleShot(true);
41 
42  blendAnimation = new QVariantAnimation(this);
43  blendAnimation->setStartValue(0.0);
44  blendAnimation->setEndValue(1.0);
45  blendAnimation->setDuration(350);
46  blendAnimation->setEasingCurve(QEasingCurve::InCubic);
47  blendAnimation->start(QAbstractAnimation::DeleteWhenStopped);
48  connect(blendAnimation, &QVariantAnimation::valueChanged, this,
49  [this](const QVariant& val) { alpha = val.toDouble(); });
50 
51  QObject::connect(&timer, &QTimer::timeout, this, &Spinner::timeout);
52 }
53 
54 QRectF Spinner::boundingRect() const
55 {
56  return QRectF(QPointF(-size.width() / 2.0, -size.height() / 2.0), size);
57 }
58 
59 void Spinner::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
60 {
61  painter->setClipRect(boundingRect());
62 
63  QTransform trans = QTransform().rotate(curRot)
64  .translate(-size.width() / 2.0, -size.height() / 2.0);
65  painter->setOpacity(alpha);
66  painter->setTransform(trans, true);
67  painter->setRenderHint(QPainter::SmoothPixmapTransform);
68  painter->drawPixmap(0, 0, pmap);
69 
70  if (!timer.isActive()) {
71  timer.start(); // update bounding rectangle for next frame
72  }
73 
74  Q_UNUSED(option)
75  Q_UNUSED(widget)
76 }
77 
78 void Spinner::setWidth(qreal width)
79 {
80  Q_UNUSED(width)
81 }
82 
83 qreal Spinner::getAscent() const
84 {
85  return 0.0;
86 }
87 
89 {
90  // Use global time, so the animations are synced
91  float angle = QTime::currentTime().msecsSinceStartOfDay() / 1000.0f * rotSpeed;
92  // limit to the range [0.0 - 360.0]
93  curRot = remainderf(angle, 360.0f);
94 
95  if (scene()) {
96  scene()->invalidate(sceneBoundingRect());
97  }
98 }
Spinner::getAscent
qreal getAscent() const override
Definition: spinner.cpp:83
Spinner::curRot
qreal curRot
Definition: spinner.h:50
spinner.h
Spinner::alpha
qreal alpha
Definition: spinner.h:52
Spinner::blendAnimation
QVariantAnimation * blendAnimation
Definition: spinner.h:53
Spinner::framerate
static constexpr int framerate
Definition: spinner.h:46
PixmapCache::get
QPixmap get(const QString &filename, QSize size)
Definition: pixmapcache.cpp:22
Spinner::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition: spinner.cpp:59
Spinner::timeout
void timeout()
Definition: spinner.cpp:88
Spinner::rotSpeed
qreal rotSpeed
Definition: spinner.h:49
PixmapCache::getInstance
static PixmapCache & getInstance()
Returns the singleton instance.
Definition: pixmapcache.cpp:40
Spinner::setWidth
void setWidth(qreal width) override
Definition: spinner.cpp:78
Spinner::timer
QTimer timer
Definition: spinner.h:51
Spinner::Spinner
Spinner(const QString &img, QSize size, qreal speed)
Definition: spinner.cpp:31
Spinner::size
QSize size
Definition: spinner.h:47
Spinner::boundingRect
QRectF boundingRect() const override
Definition: spinner.cpp:54
Spinner::pmap
QPixmap pmap
Definition: spinner.h:48