TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
ttktoastlabel.cpp
Go to the documentation of this file.
1 #include "ttktoastlabel.h"
2 #include "ttkdesktopwrapper.h"
3 
4 #include <QPainter>
5 #include <QPropertyAnimation>
6 
8  : QLabel(parent),
9  m_font(font())
10 {
11  setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
12  setAttribute(Qt::WA_TranslucentBackground);
13  setAttribute(Qt::WA_DeleteOnClose);
14 
15  setFontSize(15);
16  setFontMargin(20, 20);
17 
18  connect(&m_timer, SIGNAL(timeout()), SLOT(closeAnimation()));
19  m_timer.setInterval(TTK_DN_S2MS);
20  m_timer.start();
21 }
22 
23 TTKToastLabel::TTKToastLabel(const QString &text, QWidget *parent)
24  : TTKToastLabel(parent)
25 {
26  setText(text);
27 }
28 
30 {
31  m_timer.stop();
32 }
33 
34 void TTKToastLabel::setFontMargin(int height, int width)
35 {
36  m_margin.setX(height);
37  m_margin.setY(width);
38 }
39 
41 {
42  return m_timer.interval();
43 }
44 
46 {
47  m_font.setPointSize(size);
48  setFont(m_font);
49 }
50 
51 void TTKToastLabel::popup(QWidget *parent)
52 {
53  if(!parent)
54  {
55  const QSize &windowSize = TTKDesktopWrapper::screenGeometry().size();
56  move((windowSize.width() - width()) / 2, windowSize.height() - 200);
57  }
58  else
59  {
60  const QPoint &globalPoint = parent->mapToGlobal(QPoint(0, 0));
61  move(globalPoint.x() + (parent->width() - width()) / 2, globalPoint.y() + (parent->height() - height()) / 2);
62  }
63  show();
64 }
65 
66 void TTKToastLabel::setText(const QString &text)
67 {
68  const QFontMetrics ftm(m_font);
69  setFixedSize(QtFontWidth(ftm, text) + m_margin.x(), ftm.height() + m_margin.y());
70  QLabel::setText(text);
71 }
72 
74 {
75  m_timer.stop();
76 
77  QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
78  animation->setDuration(TTK_DN_S2MS);
79  animation->setStartValue(1);
80  animation->setEndValue(0);
81  animation->start();
82 
83  connect(animation, SIGNAL(finished()), SLOT(close()));
84  connect(animation, SIGNAL(finished()), SIGNAL(finished()));
85 }
86 
87 void TTKToastLabel::paintEvent(QPaintEvent *event)
88 {
89  Q_UNUSED(event);
90 
91  QPainter painter(this);
92  painter.setRenderHint(QPainter::Antialiasing);
93  painter.setPen(Qt::NoPen);
94  painter.setBrush(QColor(0, 0, 0, 175));
95  painter.drawRoundedRect(rect(), 6, 6);
96 
97  painter.setPen(QColor(255, 255, 255));
98  painter.drawText(rect(), Qt::AlignCenter, text());
99 }
void popup(QWidget *parent=nullptr)
void setText(const QString &text)
voidpf void uLong size
Definition: ioapi.h:136
int timerInterval() const
void setFontSize(int size)
virtual void paintEvent(QPaintEvent *event) overridefinal
void closeAnimation()
#define QtFontWidth(p, t)
Font string width.
Definition: ttkqtcompat.h:31
The class of the toast widget.
Definition: ttktoastlabel.h:29
void setFontMargin(int height, int width)
static QRect screenGeometry(int index=0)
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
TTKToastLabel(QWidget *parent=nullptr)