Fixed coding style and added the d->hovered bool to record whether the scrollhandlers...
[krunner.git] / collapsiblewidget.cpp
blobadd94deccd934a1b6118ab6953731e550a057027
1 /*
2 This file is part of the KDE libraries
3 Copyright (C) 2005 Daniel Molkentin <molkentin@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <QtGui>
21 #include <QTimeLine>
22 #include <collapsiblewidget.h>
24 /******************************************************************
25 * Private classes
26 *****************************************************************/
28 class CollapsibleWidget::Private
30 public:
31 bool expanded;
32 QGridLayout *gridLayout;
33 QWidget *innerWidget;
34 QTimeLine *timeline;
37 class SettingsContainer::Private
39 public:
40 QVBoxLayout *layout;
43 /******************************************************************
44 * Implementation
45 *****************************************************************/
47 SettingsContainer::SettingsContainer(QWidget *parent)
48 : QScrollArea( parent ), d(new SettingsContainer::Private)
50 QWidget *w = new QWidget;
51 QVBoxLayout *helperLay = new QVBoxLayout(w);
52 d->layout = new QVBoxLayout;
53 helperLay->addLayout( d->layout );
54 helperLay->addStretch(1);
55 setWidget(w);
56 setWidgetResizable(true);
59 SettingsContainer::~SettingsContainer()
61 delete d;
64 CollapsibleWidget* SettingsContainer::insertWidget( QWidget *w, const QString& name )
66 Q_UNUSED( name )
68 if ( w && w->layout() ) {
69 QLayout *lay = w->layout();
70 lay->setMargin(2);
71 lay->setSpacing(0);
74 CollapsibleWidget *cw = new CollapsibleWidget( this );
75 d->layout->addWidget( cw );
76 cw->setInnerWidget( w );
77 return cw;
80 CollapsibleWidget::CollapsibleWidget(QWidget *parent)
81 : QWidget(parent), d(new CollapsibleWidget::Private)
83 init();
86 void CollapsibleWidget::init()
88 d->expanded = false;
89 d->innerWidget = 0;
91 d->timeline = new QTimeLine( 150, this );
92 d->timeline->setCurveShape( QTimeLine::EaseInOutCurve );
93 connect( d->timeline, SIGNAL(valueChanged(qreal)),
94 this, SLOT(animateCollapse(qreal)) );
95 connect( d->timeline, SIGNAL(finished()),
96 this, SLOT(signalCompletion()) );
98 d->gridLayout = new QGridLayout( this );
99 d->gridLayout->setMargin(0);
101 setExpanded(false);
102 setEnabled(false);
105 CollapsibleWidget::~CollapsibleWidget()
107 delete d;
110 QWidget* CollapsibleWidget::innerWidget() const
112 return d->innerWidget;
115 void CollapsibleWidget::setInnerWidget(QWidget *w)
117 if (!w) {
118 return;
121 d->innerWidget = w;
123 if ( !isExpanded() ) {
124 d->innerWidget->hide();
126 d->gridLayout->addWidget( d->innerWidget, 2, 2 );
127 d->gridLayout->setRowStretch( 2, 1 );
129 setEnabled( true );
131 if ( isExpanded() ) {
132 setExpanded( true );
136 void CollapsibleWidget::setExpanded(bool expanded)
138 if ( !d->innerWidget ) {
139 return;
142 d->expanded = expanded;
144 if ( !expanded ) {
145 d->innerWidget->setVisible( false );
148 d->timeline->setDirection( expanded ? QTimeLine::Forward
149 : QTimeLine::Backward );
150 d->timeline->start();
153 void CollapsibleWidget::animateCollapse( qreal showAmount )
155 int pixels = d->innerWidget->sizeHint().height() * showAmount;
157 d->gridLayout->setRowMinimumHeight( 2, pixels );
159 if ( showAmount == 1 ) {
160 d->innerWidget->setVisible( true );
164 void CollapsibleWidget::signalCompletion()
166 if ( isExpanded() ) {
167 emit expandCompleted();
168 } else {
169 emit collapseCompleted();
173 bool CollapsibleWidget::isExpanded() const
175 return d->expanded;
178 #include "collapsiblewidget.moc"