qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
notificationicon.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-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 "notificationicon.h"
21 #include "../pixmapcache.h"
22 #include "src/widget/style.h"
23 
24 #include <QGraphicsScene>
25 #include <QPainter>
26 #include <QTimer>
27 
29  : size(Size)
30 {
31  pmap = PixmapCache::getInstance().get(Style::getImagePath("chatArea/typing.svg"), size);
32 
33  // Timer for the animation, if the Widget is not redrawn, no paint events will
34  // arrive and the timer will not be restarted, so this stops automatically
35  updateTimer.setInterval(1000 / framerate);
36  updateTimer.setSingleShot(true);
37 
38  connect(&updateTimer, &QTimer::timeout, this, &NotificationIcon::updateGradient);
39 }
40 
42 {
43  return QRectF(QPointF(-size.width() / 2.0, -size.height() / 2.0), size);
44 }
45 
46 void NotificationIcon::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
47 {
48  painter->setClipRect(boundingRect());
49 
50  painter->setRenderHint(QPainter::SmoothPixmapTransform);
51  painter->translate(-size.width() / 2.0, -size.height() / 2.0);
52 
53  painter->fillRect(QRect(0, 0, size.width(), size.height()), grad);
54  painter->drawPixmap(0, 0, size.width(), size.height(), pmap);
55 
56  if (!updateTimer.isActive()) {
57  updateTimer.start();
58  }
59 
60  Q_UNUSED(option)
61  Q_UNUSED(widget)
62 }
63 
64 void NotificationIcon::setWidth(qreal width)
65 {
66  Q_UNUSED(width)
67 }
68 
70 {
71  return 3.0;
72 }
73 
75 {
76  // Update for next frame
77  alpha += 0.01;
78 
79  if (alpha + dotWidth >= 1.0) {
80  alpha = 0.0;
81  }
82 
83  grad = QLinearGradient(QPointF(-0.5 * size.width(), 0), QPointF(3.0 / 2.0 * size.width(), 0));
84  grad.setColorAt(0, Qt::lightGray);
85  grad.setColorAt(qMax(0.0, alpha - dotWidth), Qt::lightGray);
86  grad.setColorAt(alpha, Qt::black);
87  grad.setColorAt(qMin(1.0, alpha + dotWidth), Qt::lightGray);
88  grad.setColorAt(1, Qt::lightGray);
89 
90  if (scene() && isVisible()) {
91  scene()->invalidate(sceneBoundingRect());
92  }
93 }
style.h
NotificationIcon::boundingRect
QRectF boundingRect() const override
Definition: notificationicon.cpp:41
Style::getImagePath
static const QString getImagePath(const QString &filename)
Definition: style.cpp:182
NotificationIcon::NotificationIcon
NotificationIcon(QSize size)
Definition: notificationicon.cpp:28
NotificationIcon::setWidth
void setWidth(qreal width) override
Definition: notificationicon.cpp:64
PixmapCache::get
QPixmap get(const QString &filename, QSize size)
Definition: pixmapcache.cpp:22
NotificationIcon::getAscent
qreal getAscent() const override
Definition: notificationicon.cpp:69
NotificationIcon::pmap
QPixmap pmap
Definition: notificationicon.h:47
NotificationIcon::size
QSize size
Definition: notificationicon.h:46
PixmapCache::getInstance
static PixmapCache & getInstance()
Returns the singleton instance.
Definition: pixmapcache.cpp:40
NotificationIcon::dotWidth
qreal dotWidth
Definition: notificationicon.h:51
NotificationIcon::grad
QLinearGradient grad
Definition: notificationicon.h:48
NotificationIcon::framerate
static constexpr int framerate
Definition: notificationicon.h:44
NotificationIcon::alpha
qreal alpha
Definition: notificationicon.h:52
NotificationIcon::updateGradient
void updateGradient()
Definition: notificationicon.cpp:74
NotificationIcon::updateTimer
QTimer updateTimer
Definition: notificationicon.h:49
NotificationIcon::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition: notificationicon.cpp:46
notificationicon.h