File indexing completed on 2024-05-12 04:58:26

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "animatedwidget.h"
0019 
0020 #include <QResizeEvent>
0021 
0022 AnimatedWidget::AnimatedWidget(const Direction &direction, int duration, QWidget* parent)
0023     : QWidget(parent)
0024     , m_direction(direction)
0025     , m_stepHeight(0)
0026     , m_stepY(0)
0027     , m_startY(0)
0028     , m_widget(new QWidget(this))
0029 {
0030     m_timeLine.setDuration(duration);
0031     m_timeLine.setFrameRange(0, 100);
0032     connect(&m_timeLine, &QTimeLine::frameChanged, this, &AnimatedWidget::animateFrame);
0033 
0034     setMaximumHeight(0);
0035 }
0036 
0037 void AnimatedWidget::startAnimation()
0038 {
0039     if (m_timeLine.state() == QTimeLine::Running) {
0040         return;
0041     }
0042 
0043     int shown = 0;
0044     int hidden = 0;
0045 
0046     if (m_direction == Down) {
0047         shown = 0;
0048         hidden = -m_widget->height();
0049     }
0050 
0051     m_widget->move(QPoint(m_widget->pos().x(), hidden));
0052 
0053     m_stepY = (hidden - shown) / 100.0;
0054     m_startY = hidden;
0055     m_stepHeight = m_widget->height() / 100.0;
0056 
0057     m_timeLine.setDirection(QTimeLine::Forward);
0058     m_timeLine.start();
0059 }
0060 
0061 void AnimatedWidget::animateFrame(int frame)
0062 {
0063     setFixedHeight(frame * m_stepHeight);
0064     m_widget->move(pos().x(), m_startY - frame * m_stepY);
0065 }
0066 
0067 void AnimatedWidget::hide()
0068 {
0069     if (m_timeLine.state() == QTimeLine::Running) {
0070         return;
0071     }
0072 
0073     m_timeLine.setDirection(QTimeLine::Backward);
0074     m_timeLine.start();
0075 
0076     connect(&m_timeLine, &QTimeLine::finished, this, &QWidget::close);
0077 
0078     QWidget* p = parentWidget();
0079     if (p) {
0080         p->setFocus();
0081     }
0082 }
0083 
0084 void AnimatedWidget::resizeEvent(QResizeEvent* event)
0085 {
0086     if (event->size().width() != m_widget->width()) {
0087         m_widget->resize(event->size().width(), m_widget->height());
0088     }
0089 
0090     QWidget::resizeEvent(event);
0091 }