1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.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 #include "animatedwidget.h"
19 
20 #include <QResizeEvent>
21 
AnimatedWidget(const Direction & direction,int duration,QWidget * parent)22 AnimatedWidget::AnimatedWidget(const Direction &direction, int duration, QWidget* parent)
23     : QWidget(parent)
24     , m_direction(direction)
25     , m_stepHeight(0)
26     , m_stepY(0)
27     , m_startY(0)
28     , m_widget(new QWidget(this))
29 {
30     m_timeLine.setDuration(duration);
31     m_timeLine.setFrameRange(0, 100);
32     connect(&m_timeLine, &QTimeLine::frameChanged, this, &AnimatedWidget::animateFrame);
33 
34     setMaximumHeight(0);
35 }
36 
startAnimation()37 void AnimatedWidget::startAnimation()
38 {
39     if (m_timeLine.state() == QTimeLine::Running) {
40         return;
41     }
42 
43     int shown = 0;
44     int hidden = 0;
45 
46     if (m_direction == Down) {
47         shown = 0;
48         hidden = -m_widget->height();
49     }
50 
51     m_widget->move(QPoint(m_widget->pos().x(), hidden));
52 
53     m_stepY = (hidden - shown) / 100.0;
54     m_startY = hidden;
55     m_stepHeight = m_widget->height() / 100.0;
56 
57     m_timeLine.setDirection(QTimeLine::Forward);
58     m_timeLine.start();
59 }
60 
animateFrame(int frame)61 void AnimatedWidget::animateFrame(int frame)
62 {
63     setFixedHeight(frame * m_stepHeight);
64     m_widget->move(pos().x(), m_startY - frame * m_stepY);
65 }
66 
hide()67 void AnimatedWidget::hide()
68 {
69     if (m_timeLine.state() == QTimeLine::Running) {
70         return;
71     }
72 
73     m_timeLine.setDirection(QTimeLine::Backward);
74     m_timeLine.start();
75 
76     connect(&m_timeLine, &QTimeLine::finished, this, &QWidget::close);
77 
78     QWidget* p = parentWidget();
79     if (p) {
80         p->setFocus();
81     }
82 }
83 
resizeEvent(QResizeEvent * event)84 void AnimatedWidget::resizeEvent(QResizeEvent* event)
85 {
86     if (event->size().width() != m_widget->width()) {
87         m_widget->resize(event->size().width(), m_widget->height());
88     }
89 
90     QWidget::resizeEvent(event);
91 }
92