KTextAddons

slidecontainer.cpp
1/*
2Code from Gwenview
3SPDX-FileCopyrightText: 2007 Aurélien Gâteau <agateau@kde.org>
4
5SPDX-License-Identifier: GPL-2.0-or-later
6
7*/
8#include "slidecontainer.h"
9
10// Qt
11#include <QEvent>
12#include <QPropertyAnimation>
13#include <QResizeEvent>
14
15using namespace TextAddonsWidgets;
16static const int SLIDE_DURATION = 250;
17
18SlideContainer::SlideContainer(QWidget *parent)
19 : QFrame(parent)
20 , mContent(nullptr)
21{
22 setFixedHeight(0);
23 hide();
24}
25
27{
28 return mContent;
29}
30
32{
33 if (mContent) {
34 mContent->setParent(nullptr);
35 mContent->removeEventFilter(this);
36 }
37 mContent = content;
38 if (mContent) {
39 mContent->setParent(this);
40 mContent->installEventFilter(this);
41 mContent->hide();
42 }
43}
44
45void SlideContainer::animTo(int newHeight)
46{
47 if (mAnim.data()) {
48 mAnim.data()->deleteLater();
49 disconnect(mAnim.data(), &QPropertyAnimation::finished, this, &SlideContainer::slotAnimFinished);
50 }
51 auto anim = new QPropertyAnimation(this, "slideHeight", this);
52 anim->setDuration(SLIDE_DURATION);
53 anim->setStartValue(slideHeight());
54 anim->setEndValue(newHeight);
55 mAnim = anim;
57 connect(anim, &QPropertyAnimation::finished, this, &SlideContainer::slotAnimFinished);
58}
59
61{
62 mSlidingOut = false;
63 show();
64 mContent->show();
65 mContent->adjustSize();
66 delete mAnim.data();
67 if (height() == mContent->height()) {
68 return;
69 }
70 animTo(mContent->height());
71}
72
74{
75 if (height() == 0) {
76 return;
77 }
78 mSlidingOut = true;
79 animTo(0);
80}
81
82QSize SlideContainer::sizeHint() const
83{
84 if (mContent) {
85 return mContent->sizeHint();
86 } else {
87 return {};
88 }
89}
90
91QSize SlideContainer::minimumSizeHint() const
92{
93 if (mContent) {
94 return mContent->minimumSizeHint();
95 } else {
96 return {};
97 }
98}
99
100void SlideContainer::resizeEvent(QResizeEvent *event)
101{
102 if (mContent) {
103 if (event->oldSize().width() != width()) {
104 adjustContentGeometry();
105 }
106 }
107}
108
109void SlideContainer::adjustContentGeometry()
110{
111 if (mContent) {
112 mContent->setGeometry(0, height() - mContent->height(), width(), mContent->height());
113 }
114}
115
116bool SlideContainer::eventFilter(QObject *, QEvent *event)
117{
118 if (event->type() == QEvent::Resize) {
119 if (!mSlidingOut && height() != 0) {
120 animTo(mContent->height());
121 }
122 }
123 return false;
124}
125
126int SlideContainer::slideHeight() const
127{
128 return isVisible() ? height() : 0;
129}
130
131void SlideContainer::setSlideHeight(int value)
132{
133 setFixedHeight(value);
134 adjustContentGeometry();
135}
136
137void SlideContainer::slotAnimFinished()
138{
139 if (height() == 0) {
140 mSlidingOut = false;
141 hide();
142 Q_EMIT slidedOut();
143 } else {
144 Q_EMIT slidedIn();
145 }
146}
147
148#include "moc_slidecontainer.cpp"
void slideIn()
Slides the content widget in.
void setContent(QWidget *content)
Defines the content widget.
QWidget * content() const
Returns the content widget.
void slideOut()
Slides the content widget out.
virtual bool event(QEvent *e) override
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
T * data() const const
void hide()
void setFixedHeight(int h)
void show()
bool isVisible() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:51:28 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.