File indexing completed on 2024-05-19 04:44:49
0001 /* 0002 Code from Gwenview 0003 SPDX-FileCopyrightText: 2007 Aurélien Gâteau <agateau@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 0007 */ 0008 #include "slidecontainer.h" 0009 0010 // Qt 0011 #include <QEvent> 0012 #include <QPropertyAnimation> 0013 #include <QResizeEvent> 0014 0015 using namespace TextAddonsWidgets; 0016 static const int SLIDE_DURATION = 250; 0017 0018 SlideContainer::SlideContainer(QWidget *parent) 0019 : QFrame(parent) 0020 , mContent(nullptr) 0021 { 0022 setFixedHeight(0); 0023 hide(); 0024 } 0025 0026 QWidget *SlideContainer::content() const 0027 { 0028 return mContent; 0029 } 0030 0031 void SlideContainer::setContent(QWidget *content) 0032 { 0033 if (mContent) { 0034 mContent->setParent(nullptr); 0035 mContent->removeEventFilter(this); 0036 } 0037 mContent = content; 0038 if (mContent) { 0039 mContent->setParent(this); 0040 mContent->installEventFilter(this); 0041 mContent->hide(); 0042 } 0043 } 0044 0045 void SlideContainer::animTo(int newHeight) 0046 { 0047 if (mAnim.data()) { 0048 mAnim.data()->deleteLater(); 0049 disconnect(mAnim.data(), &QPropertyAnimation::finished, this, &SlideContainer::slotAnimFinished); 0050 } 0051 auto anim = new QPropertyAnimation(this, "slideHeight", this); 0052 anim->setDuration(SLIDE_DURATION); 0053 anim->setStartValue(slideHeight()); 0054 anim->setEndValue(newHeight); 0055 mAnim = anim; 0056 anim->start(QAbstractAnimation::DeleteWhenStopped); 0057 connect(anim, &QPropertyAnimation::finished, this, &SlideContainer::slotAnimFinished); 0058 } 0059 0060 void SlideContainer::slideIn() 0061 { 0062 mSlidingOut = false; 0063 show(); 0064 mContent->show(); 0065 mContent->adjustSize(); 0066 delete mAnim.data(); 0067 if (height() == mContent->height()) { 0068 return; 0069 } 0070 animTo(mContent->height()); 0071 } 0072 0073 void SlideContainer::slideOut() 0074 { 0075 if (height() == 0) { 0076 return; 0077 } 0078 mSlidingOut = true; 0079 animTo(0); 0080 } 0081 0082 QSize SlideContainer::sizeHint() const 0083 { 0084 if (mContent) { 0085 return mContent->sizeHint(); 0086 } else { 0087 return {}; 0088 } 0089 } 0090 0091 QSize SlideContainer::minimumSizeHint() const 0092 { 0093 if (mContent) { 0094 return mContent->minimumSizeHint(); 0095 } else { 0096 return {}; 0097 } 0098 } 0099 0100 void SlideContainer::resizeEvent(QResizeEvent *event) 0101 { 0102 if (mContent) { 0103 if (event->oldSize().width() != width()) { 0104 adjustContentGeometry(); 0105 } 0106 } 0107 } 0108 0109 void SlideContainer::adjustContentGeometry() 0110 { 0111 if (mContent) { 0112 mContent->setGeometry(0, height() - mContent->height(), width(), mContent->height()); 0113 } 0114 } 0115 0116 bool SlideContainer::eventFilter(QObject *, QEvent *event) 0117 { 0118 if (event->type() == QEvent::Resize) { 0119 if (!mSlidingOut && height() != 0) { 0120 animTo(mContent->height()); 0121 } 0122 } 0123 return false; 0124 } 0125 0126 int SlideContainer::slideHeight() const 0127 { 0128 return isVisible() ? height() : 0; 0129 } 0130 0131 void SlideContainer::setSlideHeight(int value) 0132 { 0133 setFixedHeight(value); 0134 adjustContentGeometry(); 0135 } 0136 0137 void SlideContainer::slotAnimFinished() 0138 { 0139 if (height() == 0) { 0140 mSlidingOut = false; 0141 hide(); 0142 Q_EMIT slidedOut(); 0143 } else { 0144 Q_EMIT slidedIn(); 0145 } 0146 } 0147 0148 #include "moc_slidecontainer.cpp"