Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Sébastien Blin | 68abac9 | 2019-01-02 17:41:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2019-2019 by Savoir-faire Linux * |
Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 3 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> * |
| 4 | * * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 3 of the License, or * |
| 8 | * (at your option) any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 | **************************************************************************/ |
| 18 | |
| 19 | #include "animationhelpers.h" |
| 20 | |
| 21 | #include "ui_animatedoverlay.h" |
| 22 | |
| 23 | #include <QTimer> |
| 24 | #include <QtMath> |
| 25 | |
| 26 | OpacityAnimation::OpacityAnimation(QWidget* target, QObject* parent) |
| 27 | : QObject(parent), |
| 28 | target_(target), |
| 29 | timer_(nullptr), |
| 30 | frameTime_((1.0 / 24.0) * 1000), |
| 31 | startValue_(0.0), |
| 32 | endValue_(1.0), |
| 33 | t_(0), |
| 34 | value_(0), |
| 35 | duration_(1000) |
| 36 | { |
| 37 | timer_ = new QTimer(this); |
| 38 | connect(timer_, SIGNAL(timeout()), this, SLOT(updateAnimation())); |
| 39 | |
| 40 | effect_ = new QGraphicsOpacityEffect(this); |
| 41 | effect_->setOpacity(startValue_); |
| 42 | |
| 43 | target_->setGraphicsEffect(effect_); |
| 44 | target_->setAutoFillBackground(true); |
| 45 | } |
| 46 | |
| 47 | OpacityAnimation::~OpacityAnimation() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | OpacityAnimation::setFPS(const int& fps) |
| 53 | { |
| 54 | frameTime_ = (1.0 / static_cast<double>(fps)) * 1000; |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | OpacityAnimation::setFrameTime(const int& milliseconds) |
| 59 | { |
| 60 | frameTime_ = milliseconds; |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | OpacityAnimation::setDuration(const int& milliseconds) |
| 65 | { |
| 66 | duration_ = milliseconds; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | OpacityAnimation::setStartValue(const double& value) |
| 71 | { |
| 72 | startValue_ = value; |
| 73 | effect_->setOpacity(startValue_); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | OpacityAnimation::setEndValue(const double& value) |
| 78 | { |
| 79 | endValue_ = value; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | OpacityAnimation::start() |
| 84 | { |
| 85 | timer_->start(frameTime_); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | OpacityAnimation::stop() |
| 90 | { |
| 91 | timer_->stop(); |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | OpacityAnimation::updateAnimation() |
| 96 | { |
| 97 | double d = (startValue_ + endValue_) * 0.5; |
| 98 | double a = abs(startValue_ - endValue_) * 0.5; |
| 99 | |
| 100 | t_ += frameTime_; |
| 101 | value_ = a * sin(2 * M_PI * t_ * duration_ * .000001) + d; |
| 102 | effect_->setOpacity(value_); |
| 103 | target_->update(); |
| 104 | } |
| 105 | |
| 106 | AnimatedOverlay::AnimatedOverlay(QColor color, QWidget* parent) : |
| 107 | QWidget(parent), |
| 108 | ui(new Ui::AnimatedOverlay) |
| 109 | { |
| 110 | ui->setupUi(this); |
| 111 | ui->backgroundLabel->setAutoFillBackground(true); |
| 112 | auto values = QString("%1,%2,%3,255") |
| 113 | .arg(color.red()) |
| 114 | .arg(color.green()) |
| 115 | .arg(color.blue()); |
| 116 | ui->backgroundLabel->setStyleSheet("background-color: rgba(" + values + ");"); |
| 117 | |
| 118 | oa_ = new OpacityAnimation(this, this); |
| 119 | oa_->setFPS(16); |
| 120 | oa_->setDuration(1000); |
| 121 | oa_->setStartValue(0.0); |
| 122 | oa_->setEndValue(0.25); |
| 123 | oa_->start(); |
| 124 | } |
| 125 | |
| 126 | AnimatedOverlay::~AnimatedOverlay() |
| 127 | { |
| 128 | disconnect(this); |
| 129 | delete ui; |
| 130 | } |