2015-03-25 09:48:26 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2013 by Maxim Biro <nurupo.contributions@gmail.com>
|
|
|
|
|
|
|
|
This file is part of Tox Qt GUI.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "flyoutoverlaywidget.h"
|
|
|
|
|
|
|
|
#include <QPropertyAnimation>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QBitmap>
|
2015-03-25 21:43:40 +08:00
|
|
|
#include <QTimer>
|
2015-03-25 09:48:26 +08:00
|
|
|
|
|
|
|
FlyoutOverlayWidget::FlyoutOverlayWidget(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
|
|
|
animation = new QPropertyAnimation(this, QByteArrayLiteral("flyoutPercent"), this);
|
|
|
|
animation->setKeyValueAt(0, 0.0f);
|
|
|
|
animation->setKeyValueAt(1, 1.0f);
|
|
|
|
animation->setDuration(200);
|
|
|
|
|
|
|
|
connect(animation, &QAbstractAnimation::finished, this, &FlyoutOverlayWidget::finishedAnimation);
|
|
|
|
setFlyoutPercent(0);
|
2015-04-02 22:31:58 +08:00
|
|
|
hide();
|
2015-03-25 09:48:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
FlyoutOverlayWidget::~FlyoutOverlayWidget()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int FlyoutOverlayWidget::animationDuration() const
|
|
|
|
{
|
|
|
|
return animation->duration();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlyoutOverlayWidget::setAnimationDuration(int timeMs)
|
|
|
|
{
|
|
|
|
animation->setDuration(timeMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal FlyoutOverlayWidget::flyoutPercent() const
|
|
|
|
{
|
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlyoutOverlayWidget::setFlyoutPercent(qreal progress)
|
|
|
|
{
|
|
|
|
percent = progress;
|
|
|
|
|
|
|
|
QSize self = size();
|
|
|
|
setMask(QRegion(0, 0, self.width() * progress + 1, self.height()));
|
|
|
|
move(startPos.x() + self.width() - self.width() * percent, startPos.y());
|
|
|
|
setVisible (progress != 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlyoutOverlayWidget::isShown() const
|
|
|
|
{
|
|
|
|
return (percent == 1);
|
|
|
|
}
|
|
|
|
|
2015-04-02 22:56:16 +08:00
|
|
|
bool FlyoutOverlayWidget::isBeingAnimated() const
|
|
|
|
{
|
|
|
|
return (animation->state() == QAbstractAnimation::Running);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FlyoutOverlayWidget::isBeingShown() const
|
|
|
|
{
|
|
|
|
return (isBeingAnimated() && animation->direction() == QAbstractAnimation::Forward);
|
|
|
|
}
|
|
|
|
|
2015-03-25 09:48:26 +08:00
|
|
|
void FlyoutOverlayWidget::animateShow()
|
|
|
|
{
|
2015-04-02 22:56:16 +08:00
|
|
|
if (percent == 1.0f)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (animation->state() != QAbstractAnimation::Running)
|
|
|
|
this->startPos = pos();
|
|
|
|
|
|
|
|
startAnimation(true);
|
2015-03-25 09:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlyoutOverlayWidget::animateHide()
|
|
|
|
{
|
2015-04-02 22:56:16 +08:00
|
|
|
if (animation->state() != QAbstractAnimation::Running)
|
|
|
|
this->startPos = pos();
|
|
|
|
|
|
|
|
startAnimation(false);
|
2015-03-25 09:48:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlyoutOverlayWidget::finishedAnimation()
|
|
|
|
{
|
|
|
|
bool hide = (animation->direction() == QAbstractAnimation::Backward);
|
2015-03-25 21:43:40 +08:00
|
|
|
|
|
|
|
// Delay it by a few frames to let the system catch up on rendering
|
|
|
|
if (hide)
|
|
|
|
QTimer::singleShot(50, this, &FlyoutOverlayWidget::hidden);
|
|
|
|
|
2015-03-25 09:48:26 +08:00
|
|
|
}
|
2015-04-02 22:56:16 +08:00
|
|
|
|
|
|
|
void FlyoutOverlayWidget::startAnimation(bool forward)
|
|
|
|
{
|
2015-04-02 23:10:26 +08:00
|
|
|
setAttribute(Qt::WA_TransparentForMouseEvents, !forward);
|
2015-04-02 22:56:16 +08:00
|
|
|
animation->setDirection(forward ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
|
|
|
|
animation->start();
|
|
|
|
animation->setCurrentTime(animation->duration() * percent);
|
|
|
|
|
|
|
|
}
|