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 Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <QtGui/QtGui>
22 #include <collapsiblewidget.h>
24 /******************************************************************
26 *****************************************************************/
28 ClickableLabel::ClickableLabel( QWidget
* parent
)
33 ClickableLabel::~ClickableLabel()
37 void ClickableLabel::mouseReleaseEvent( QMouseEvent
*e
)
43 ArrowButton::ArrowButton( QWidget
*parent
)
44 : QAbstractButton( parent
)
49 ArrowButton::~ArrowButton()
53 void ArrowButton::paintEvent( QPaintEvent
*event
)
58 int h
= sizeHint().height();
59 opt
.rect
= QRect(0,( height()- h
)/2, h
, h
);
60 opt
.palette
= palette();
61 opt
.state
= QStyle::State_Children
;
63 opt
.state
|= QStyle::State_Open
;
65 style()->drawPrimitive(QStyle::PE_IndicatorBranch
, &opt
, &p
);
70 /******************************************************************
72 *****************************************************************/
74 class CollapsibleWidget::Private
77 QGridLayout
*gridLayout
;
79 ClickableLabel
*label
;
80 ArrowButton
*colButton
;
83 QVBoxLayout
*expanderLayout
;
86 class SettingsContainer::Private
92 /******************************************************************
94 *****************************************************************/
96 SettingsContainer::SettingsContainer(QWidget
*parent
)
97 : QScrollArea( parent
), d(new SettingsContainer::Private
)
99 QWidget
*w
= new QWidget
;
100 QVBoxLayout
*helperLay
= new QVBoxLayout(w
);
101 d
->layout
= new QVBoxLayout
;
102 helperLay
->addLayout( d
->layout
);
103 helperLay
->addStretch(1);
105 setWidgetResizable(true);
108 SettingsContainer::~SettingsContainer()
113 CollapsibleWidget
* SettingsContainer::insertWidget( QWidget
*w
, const QString
& name
)
115 if (w
&& w
->layout()) {
116 QLayout
*lay
= w
->layout();
121 CollapsibleWidget
*cw
= new CollapsibleWidget( name
);
122 d
->layout
->addWidget( cw
);
123 cw
->setInnerWidget( w
);
127 CollapsibleWidget::CollapsibleWidget(QWidget
*parent
)
128 : QWidget(parent
), d(new CollapsibleWidget::Private
)
132 CollapsibleWidget::CollapsibleWidget(const QString
& caption
, QWidget
*parent
)
133 : QWidget(parent
), d(new CollapsibleWidget::Private
)
139 void CollapsibleWidget::init()
142 d
->expanderLayout
= 0;
143 d
->timeline
= new QTimeLine( 150, this );
144 d
->timeline
->setCurveShape( QTimeLine::EaseInOutCurve
);
145 connect( d
->timeline
, SIGNAL(valueChanged(qreal
)),
146 this, SLOT(animateCollapse(qreal
)) );
149 d
->gridLayout
= new QGridLayout( this );
150 d
->gridLayout
->setMargin(0);
152 d
->colButton
= new ArrowButton
;
153 d
->colButton
->setCheckable(true);
155 d
->label
= new ClickableLabel
;
156 d
->label
->setSizePolicy(QSizePolicy::MinimumExpanding
,
157 QSizePolicy::Preferred
);
159 d
->gridLayout
->addWidget(d
->colButton
, 1, 1);
160 d
->gridLayout
->addWidget(d
->label
, 1, 2);
163 connect(d
->label
, SIGNAL(clicked()),
164 d
->colButton
, SLOT(click()));
166 connect(d
->colButton
, SIGNAL(toggled(bool)),
167 SLOT(setExpanded(bool)));
173 CollapsibleWidget::~CollapsibleWidget()
178 QWidget
* CollapsibleWidget::innerWidget() const
180 return d
->innerWidget
;
185 void CollapsibleWidget::setInnerWidget(QWidget
*w
)
194 if ( !isExpanded() ) {
195 d
->innerWidget
->hide();
197 d
->gridLayout
->addWidget( d
->innerWidget
, 2, 2 );
198 d
->gridLayout
->setRowStretch( 2, 1 );
200 if ( !d
->expander
) {
201 d
->expander
= new QWidget( this );
202 d
->gridLayout
->addWidget( d
->expander
, 2, 2 );
203 d
->gridLayout
->setRowStretch( 2, 1 );
204 d
->expanderLayout
= new QVBoxLayout( d
->expander
);
205 d
->expanderLayout
->setMargin( 0 );
206 d
->expanderLayout
->setSpacing( 0 );
207 d
->expander
->setFixedHeight( 0 );
210 d
->innerWidget
->setParent( d
->expander
);
211 d
->innerWidget
->show();
212 d
->expanderLayout
->addWidget( d
->innerWidget
);
217 if ( isExpanded() ) {
222 void CollapsibleWidget::setCaption(const QString
& caption
)
224 d
->label
->setText(QString("<b>%1</b>").arg(caption
));
227 QString
CollapsibleWidget::caption() const
229 return d
->label
->text();
233 void CollapsibleWidget::setExpanded(bool expanded
)
235 if ( !d
->innerWidget
) {
241 d
->innerWidget
->setVisible( false );
245 d
->expander
->setVisible( true );
247 d
->innerWidget
->setVisible( expanded
);
249 d
->colButton
->setChecked( expanded
);
250 d
->timeline
->setDirection( expanded
? QTimeLine::Forward
251 : QTimeLine::Backward
);
252 if (d
->timeline
->state() != QTimeLine::Running
)
253 d
->timeline
->start();
256 void CollapsibleWidget::animateCollapse( qreal showAmount
)
258 int pixels
= d
->innerWidget
->sizeHint().height() * showAmount
;
259 d
->gridLayout
->setRowMinimumHeight( 2, pixels
);
262 d
->gridLayout
->setRowMinimumHeight( 2, pixels
);
264 if ( showAmount
== 1 ) {
265 d
->innerWidget
->setVisible( true );
268 d
->expander
->setFixedHeight( pixels
);
272 bool CollapsibleWidget::isExpanded() const
274 return d
->colButton
->isChecked();
278 #include "collapsiblewidget.moc"