qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
flyoutoverlaywidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
3  Copyright © 2015-2019 by The qTox Project Contributors
4 
5  This file is part of qTox, a Qt-based graphical interface for Tox.
6 
7  qTox is libre software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  qTox is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with qTox. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "flyoutoverlaywidget.h"
22 
23 #include <QBitmap>
24 #include <QHBoxLayout>
25 #include <QPainter>
26 #include <QPropertyAnimation>
27 #include <QTimer>
28 
30  : QWidget(parent)
31 {
32  setContentsMargins(0, 0, 0, 0);
33 
34  animation = new QPropertyAnimation(this, QByteArrayLiteral("flyoutPercent"), this);
35  animation->setKeyValueAt(0, 0.0f);
36  animation->setKeyValueAt(1, 1.0f);
37  animation->setDuration(200);
38 
39  connect(animation, &QAbstractAnimation::finished, this, &FlyoutOverlayWidget::finishedAnimation);
41  hide();
42 }
43 
45 {
46 }
47 
49 {
50  return animation->duration();
51 }
52 
54 {
55  animation->setDuration(timeMs);
56 }
57 
59 {
60  return percent;
61 }
62 
64 {
65  percent = progress;
66 
67  QSize self = size();
68  setMask(QRegion(0, 0, self.width() * progress + 1, self.height()));
69  move(startPos.x() + self.width() - self.width() * percent, startPos.y());
70  setVisible(progress != 0);
71 }
72 
74 {
75  return (percent == 1);
76 }
77 
79 {
80  return (animation->state() == QAbstractAnimation::Running);
81 }
82 
84 {
85  return (isBeingAnimated() && animation->direction() == QAbstractAnimation::Forward);
86 }
87 
89 {
90  if (percent == 1.0f)
91  return;
92 
93  if (animation->state() != QAbstractAnimation::Running)
94  this->startPos = pos();
95 
96  startAnimation(true);
97 }
98 
100 {
101  if (animation->state() != QAbstractAnimation::Running)
102  this->startPos = pos();
103 
104  startAnimation(false);
105 }
106 
108 {
109  bool hide = (animation->direction() == QAbstractAnimation::Backward);
110 
111  // Delay it by a few frames to let the system catch up on rendering
112  if (hide)
113  QTimer::singleShot(50, this, SIGNAL(hidden()));
114 }
115 
117 {
118  setAttribute(Qt::WA_TransparentForMouseEvents, !forward);
119  animation->setDirection(forward ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
120  animation->start();
121  animation->setCurrentTime(animation->duration() * percent);
122 }
FlyoutOverlayWidget::animationDuration
int animationDuration() const
Definition: flyoutoverlaywidget.cpp:48
FlyoutOverlayWidget::FlyoutOverlayWidget
FlyoutOverlayWidget(QWidget *parent=nullptr)
Definition: flyoutoverlaywidget.cpp:29
FlyoutOverlayWidget::animation
QPropertyAnimation * animation
Definition: flyoutoverlaywidget.h:57
flyoutoverlaywidget.h
FlyoutOverlayWidget::flyoutPercent
qreal flyoutPercent
Definition: flyoutoverlaywidget.h:30
FlyoutOverlayWidget::animateHide
void animateHide()
Definition: flyoutoverlaywidget.cpp:99
FlyoutOverlayWidget::isBeingAnimated
bool isBeingAnimated() const
Definition: flyoutoverlaywidget.cpp:78
FlyoutOverlayWidget::animateShow
void animateShow()
Definition: flyoutoverlaywidget.cpp:88
FlyoutOverlayWidget::~FlyoutOverlayWidget
~FlyoutOverlayWidget()
Definition: flyoutoverlaywidget.cpp:44
FlyoutOverlayWidget::isBeingShown
bool isBeingShown() const
Definition: flyoutoverlaywidget.cpp:83
FlyoutOverlayWidget::hidden
void hidden()
FlyoutOverlayWidget::setAnimationDuration
void setAnimationDuration(int timeMs)
Definition: flyoutoverlaywidget.cpp:53
FlyoutOverlayWidget::startPos
QPoint startPos
Definition: flyoutoverlaywidget.h:59
FlyoutOverlayWidget::setFlyoutPercent
void setFlyoutPercent(qreal progress)
Definition: flyoutoverlaywidget.cpp:63
FlyoutOverlayWidget::percent
qreal percent
Definition: flyoutoverlaywidget.h:58
FlyoutOverlayWidget::startAnimation
void startAnimation(bool forward)
Definition: flyoutoverlaywidget.cpp:116
FlyoutOverlayWidget::finishedAnimation
void finishedAnimation()
Definition: flyoutoverlaywidget.cpp:107
FlyoutOverlayWidget::isShown
bool isShown() const
Definition: flyoutoverlaywidget.cpp:73