-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
frontend: Add new component SlidingStackedWidget #12678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
/****************************************************************************** | ||
Copyright (C) 2025 by Taylor Giampaolo <warchamp7@obsproject.com> | ||
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 2 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 "SlidingStackedWidget.hpp" | ||
|
||
#include <QTimer> | ||
#include <QPropertyAnimation> | ||
#include <QParallelAnimationGroup> | ||
#include <QGraphicsOpacityEffect> | ||
|
||
#include "moc_SlidingStackedWidget.cpp" | ||
|
||
SlidingStackedWidget::SlidingStackedWidget(QWidget *parent) : QStackedWidget(parent) {} | ||
|
||
void SlidingStackedWidget::setDuration(int duration) | ||
{ | ||
duration_ = duration; | ||
} | ||
|
||
int SlidingStackedWidget::duration() | ||
{ | ||
// TODO: Add Reduced Motion accessibility option in the future and return 0 here if it's set | ||
return duration_; | ||
} | ||
|
||
void SlidingStackedWidget::setEasingCurve(enum QEasingCurve::Type easingCurve) | ||
{ | ||
easingCurve_ = easingCurve; | ||
} | ||
|
||
void SlidingStackedWidget::setVertical(bool vertical) | ||
{ | ||
vertical_ = vertical; | ||
} | ||
|
||
void SlidingStackedWidget::setWrap(bool wrap) | ||
{ | ||
wrap_ = wrap; | ||
} | ||
|
||
bool SlidingStackedWidget::advanceNext() | ||
{ | ||
int current = currentIndex(); | ||
|
||
if (wrap_ || (current < count() - 1)) { | ||
advanceToIndex(current + 1); | ||
} else { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool SlidingStackedWidget::advancePrevious() | ||
{ | ||
int current = currentIndex(); | ||
|
||
if (wrap_ || (current > 0)) { | ||
advanceToIndex(current - 1); | ||
} else { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void SlidingStackedWidget::setNextPageInstantly() | ||
{ | ||
nextPageInstant = true; | ||
} | ||
|
||
void SlidingStackedWidget::setCurrentIndex(int idx) | ||
{ | ||
advanceToIndex(idx); | ||
} | ||
|
||
void SlidingStackedWidget::setCurrentWidget(QWidget *widget) | ||
{ | ||
advanceToWidget(widget); | ||
} | ||
|
||
void SlidingStackedWidget::advanceToIndex(int idx, enum SlideDirection direction) | ||
{ | ||
if (idx > count() - 1) { | ||
direction = vertical_ ? TOP_TO_BOTTOM : RIGHT_TO_LEFT; | ||
idx = (idx) % count(); | ||
} else if (idx < 0) { | ||
direction = vertical_ ? BOTTOM_TO_TOP : LEFT_TO_RIGHT; | ||
idx = (idx + count()) % count(); | ||
} | ||
|
||
advanceToWidget(widget(idx), direction); | ||
} | ||
|
||
void SlidingStackedWidget::advanceToWidget(QWidget *newWidget, enum SlideDirection direction) | ||
{ | ||
int index = currentIndex(); | ||
int nextIndex = indexOf(newWidget); | ||
|
||
if (isAnimating) { | ||
/* Avoid interrupting animation if trying to advance from and to the same indexes */ | ||
if (index == activeIndex_ && nextIndex == nextIndex_) { | ||
return; | ||
} | ||
|
||
/* Skip rest of animation */ | ||
animGroup->setCurrentTime(animGroup->duration()); | ||
animGroup->stop(); | ||
|
||
/* Allow event loop to tick before advancing again */ | ||
QTimer::singleShot(1, this, [this, newWidget, direction]() { advanceToWidget(newWidget, direction); }); | ||
|
||
return; | ||
} | ||
|
||
isAnimating = true; | ||
int animDuration = nextPageInstant ? 0 : duration(); | ||
|
||
enum SlideDirection directionHint; | ||
|
||
if (index == nextIndex) { | ||
isAnimating = false; | ||
return; | ||
} else if (index < nextIndex) { | ||
directionHint = vertical_ ? BOTTOM_TO_TOP : RIGHT_TO_LEFT; | ||
} else { | ||
directionHint = vertical_ ? TOP_TO_BOTTOM : LEFT_TO_RIGHT; | ||
} | ||
|
||
if (direction == AUTOMATIC) { | ||
direction = directionHint; | ||
} | ||
|
||
int offsetX = frameRect().width(); | ||
int offsetY = frameRect().height(); | ||
|
||
widget(nextIndex)->setGeometry(0, 0, offsetX, offsetY); | ||
if (direction == BOTTOM_TO_TOP) { | ||
offsetX = 0; | ||
offsetY = -offsetY; | ||
} else if (direction == TOP_TO_BOTTOM) { | ||
offsetX = 0; | ||
} else if (direction == RIGHT_TO_LEFT) { | ||
offsetX = -offsetX; | ||
offsetY = 0; | ||
} else if (direction == LEFT_TO_RIGHT) { | ||
offsetY = 0; | ||
} | ||
|
||
QPoint nextPos = widget(nextIndex)->pos(); | ||
QPoint currentPos = widget(index)->pos(); | ||
activePos_ = currentPos; | ||
widget(nextIndex)->move(nextPos.x() - offsetX, nextPos.y() - offsetY); | ||
|
||
widget(nextIndex)->show(); | ||
widget(nextIndex)->raise(); | ||
|
||
QPropertyAnimation *currentPosAnimation = new QPropertyAnimation(widget(index), "pos"); | ||
currentPosAnimation->setDuration(animDuration); | ||
currentPosAnimation->setEasingCurve(easingCurve_); | ||
currentPosAnimation->setStartValue(QPoint(currentPos.x(), currentPos.y())); | ||
currentPosAnimation->setEndValue(QPoint(currentPos.x() + offsetX, currentPos.y() + offsetY)); | ||
|
||
QGraphicsOpacityEffect *currentOpacityEffect = new QGraphicsOpacityEffect(); | ||
widget(index)->setGraphicsEffect(currentOpacityEffect); | ||
|
||
QPropertyAnimation *currentOpacityAnimation = new QPropertyAnimation(currentOpacityEffect, "opacity"); | ||
currentOpacityAnimation->setDuration(animDuration * 0.5); | ||
currentOpacityAnimation->setStartValue(1); | ||
currentOpacityAnimation->setEndValue(0); | ||
|
||
QGraphicsOpacityEffect *nextOpacityEffect = new QGraphicsOpacityEffect(); | ||
nextOpacityEffect->setOpacity(0); | ||
widget(nextIndex)->setGraphicsEffect(nextOpacityEffect); | ||
|
||
QPropertyAnimation *nextOpacityAnimation = new QPropertyAnimation(nextOpacityEffect, "opacity"); | ||
nextOpacityAnimation->setDuration(animDuration * 0.5); | ||
nextOpacityAnimation->setStartValue(0); | ||
nextOpacityAnimation->setEndValue(1); | ||
|
||
QPropertyAnimation *nextPosAnimation = new QPropertyAnimation(widget(nextIndex), "pos"); | ||
nextPosAnimation->setDuration(animDuration); | ||
nextPosAnimation->setEasingCurve(easingCurve_); | ||
nextPosAnimation->setStartValue(QPoint(-offsetX + nextPos.x(), -offsetY + nextPos.y())); | ||
nextPosAnimation->setEndValue(QPoint(nextPos.x(), nextPos.y())); | ||
|
||
animGroup = new QParallelAnimationGroup; | ||
animGroup->addAnimation(currentPosAnimation); | ||
animGroup->addAnimation(nextPosAnimation); | ||
animGroup->addAnimation(currentOpacityAnimation); | ||
animGroup->addAnimation(nextOpacityAnimation); | ||
|
||
QObject::connect(animGroup, &QParallelAnimationGroup::finished, this, &SlidingStackedWidget::animationDoneSlot); | ||
|
||
nextIndex_ = nextIndex; | ||
activeIndex_ = index; | ||
isAnimating = true; | ||
animGroup->start(QAbstractAnimation::DeleteWhenStopped); | ||
|
||
nextPageInstant = false; | ||
} | ||
|
||
void SlidingStackedWidget::animationDoneSlot() | ||
{ | ||
QStackedWidget::setCurrentIndex(nextIndex_); | ||
|
||
widget(activeIndex_)->hide(); | ||
widget(activeIndex_)->move(activePos_); | ||
|
||
isAnimating = false; | ||
|
||
emit animationFinished(); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,73 @@ | ||||||
/****************************************************************************** | ||||||
Copyright (C) 2025 by Taylor Giampaolo <warchamp7@obsproject.com> | ||||||
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 2 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/>. | ||||||
******************************************************************************/ | ||||||
|
||||||
#pragma once | ||||||
|
||||||
#include <QStackedWidget> | ||||||
#include <QEasingCurve> | ||||||
#include <QParallelAnimationGroup> | ||||||
|
||||||
class SlidingStackedWidget : public QStackedWidget { | ||||||
Q_OBJECT | ||||||
|
||||||
public: | ||||||
enum SlideDirection { LEFT_TO_RIGHT, RIGHT_TO_LEFT, TOP_TO_BOTTOM, BOTTOM_TO_TOP, AUTOMATIC }; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, also if it's part of the widget's API, you get additional context.
|
||||||
|
||||||
explicit SlidingStackedWidget(QWidget *parent); | ||||||
|
||||||
void setDuration(int duration); | ||||||
int duration(); | ||||||
void setEasingCurve(enum QEasingCurve::Type easingCurve); | ||||||
void setVertical(bool vertical = true); | ||||||
|
||||||
void setNextPageInstantly(); | ||||||
void advanceToIndex(int idx, enum SlideDirection direction = AUTOMATIC); | ||||||
void advanceToWidget(QWidget *widget, enum SlideDirection direction = AUTOMATIC); | ||||||
|
||||||
void setWrap(bool wrap); | ||||||
|
||||||
public slots: | ||||||
void setCurrentIndex(int idx); | ||||||
void setCurrentWidget(QWidget *widget); | ||||||
|
||||||
bool advanceNext(); | ||||||
bool advancePrevious(); | ||||||
|
||||||
signals: | ||||||
void animationFinished(void); | ||||||
|
||||||
protected: | ||||||
bool nextPageInstant = false; | ||||||
bool isAnimating = false; | ||||||
|
||||||
int duration_ = 340; | ||||||
|
||||||
bool vertical_ = false; | ||||||
bool wrap_ = false; | ||||||
enum QEasingCurve::Type easingCurve_ = QEasingCurve::OutQuad; | ||||||
|
||||||
int activeIndex_ = 0; | ||||||
QPoint activePos_ = QPoint(0, 0); | ||||||
|
||||||
int nextIndex_ = 0; | ||||||
|
||||||
QList<QWidget *> blockedPageList; | ||||||
QParallelAnimationGroup *animGroup = nullptr; | ||||||
|
||||||
protected slots: | ||||||
void animationDoneSlot(void); | ||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a C++-style comment in my opinion, but this is not spelt-out by the code style guidelines that unfortunately can only cover pure C code (as those are based on the Linux kernel guidelines).
The C++ style is part of the language reference doc, but makes functionally no difference, so it's entirely a question of code style.
Right now we don't have any C++ code style guidelines (because the kernel guidelines can only cover C) so we have to introduce some. And in that case I'd vote to use C++-style comments in C++ code as an informal agreement until we have C++ style guidelines in writing.