summaryrefslogtreecommitdiffstats
path: root/src/SlidingStackWidget.cc
blob: c4d2f7cfb40d8c3f2d5a2d44225d87caa251aae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * 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
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "SlidingStackWidget.h"

SlidingStackWidget::SlidingStackWidget(QWidget *parent)
    : QStackedWidget(parent)
{
	window_ = parent;

	if (parent == Q_NULLPTR) {
		qDebug() << "Using nullptr for parent";
		window_ = this;
	}

	current_position_ = QPoint(0, 0);
	speed_ = 400;
	now_ = 0;
	next_ = 0;
	active_ = false;
	animation_type_ = QEasingCurve::InOutCirc;
}

SlidingStackWidget::~SlidingStackWidget()
{
}

void SlidingStackWidget::slideInNext()
{
	int now = currentIndex();

	if (now < count() - 1)
		slideInIndex(now + 1);
}

void SlidingStackWidget::slideInPrevious()
{
	int now = currentIndex();

	if (now > 0)
		slideInIndex(now - 1);
}

void SlidingStackWidget::slideInIndex(int index, AnimationDirection direction)
{
	// Take into consideration possible index overflow/undeflow.
	if (index > count() - 1) {
		direction = AnimationDirection::RIGHT_TO_LEFT;
		index = index % count();
	} else if (index < 0) {
		direction = AnimationDirection::LEFT_TO_RIGHT;
		index = (index + count()) % count();
	}

	slideInWidget(widget(index), direction);
}

void SlidingStackWidget::slideInWidget(QWidget *next_widget, AnimationDirection direction)
{
	// If an animation is currenlty executing we should wait for it to finish before
	// another transition can start.
	if (active_)
		return;

	active_ = true;

	int now = currentIndex();
	int next = indexOf(next_widget);

	if (now == next) {
		active_ = false;
		return;
	}

	int offset_x = frameRect().width();

	next_widget->setGeometry(0, 0, offset_x, 0);

	if (direction == AnimationDirection::LEFT_TO_RIGHT) {
		offset_x = -offset_x;
	}

	QPoint pnext = next_widget->pos();
	QPoint pnow = widget(now)->pos();
	current_position_ = pnow;

	// Reposition the next widget outside of the display area.
	next_widget->move(pnext.x() - offset_x, pnext.y());

	// Make the widget visible.
	next_widget->show();
	next_widget->raise();

	// Animate both the next and now widget.
	QPropertyAnimation *animation_now = new QPropertyAnimation(widget(now), "pos");

	animation_now->setDuration(speed_);
	animation_now->setEasingCurve(animation_type_);
	animation_now->setStartValue(QPoint(pnow.x(), pnow.y()));
	animation_now->setEndValue(QPoint(pnow.x() + offset_x, pnow.y()));

	QPropertyAnimation *animation_next = new QPropertyAnimation(next_widget, "pos");

	animation_next->setDuration(speed_);
	animation_next->setEasingCurve(animation_type_);
	animation_next->setStartValue(QPoint(pnext.x() - offset_x, pnext.y()));
	animation_next->setEndValue(QPoint(pnext.x(), pnext.y()));

	QParallelAnimationGroup *animation_group = new QParallelAnimationGroup;

	animation_group->addAnimation(animation_now);
	animation_group->addAnimation(animation_next);

	connect(animation_group, SIGNAL(finished()), this, SLOT(onAnimationFinished()));

	next_ = next;
	now_ = now;
	animation_group->start();
}

void SlidingStackWidget::onAnimationFinished()
{
	setCurrentIndex(next_);

	// The old widget is no longer necessary so we can hide it and
	// move it back to its original position.
	widget(now_)->hide();
	widget(now_)->move(current_position_);

	active_ = false;
	emit animationFinished();
}

int SlidingStackWidget::getWidgetIndex(QWidget *widget)
{
	return indexOf(widget);
}