not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / randr / collapsiblewidget.cpp
blob50e66ad262b3fe6228cd6bc3f5274363dc84ce81
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 Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <QtGui/QtGui>
21 #include <QTimeLine>
22 #include <collapsiblewidget.h>
24 /******************************************************************
25 * Helper classes
26 *****************************************************************/
28 ClickableLabel::ClickableLabel( QWidget* parent )
29 : QLabel( parent )
33 ClickableLabel::~ClickableLabel()
37 void ClickableLabel::mouseReleaseEvent( QMouseEvent *e )
39 Q_UNUSED( e );
40 emit clicked();
43 ArrowButton::ArrowButton( QWidget *parent )
44 : QAbstractButton( parent )
49 ArrowButton::~ArrowButton()
53 void ArrowButton::paintEvent( QPaintEvent *event )
55 Q_UNUSED( event );
56 QPainter p( this );
57 QStyleOption opt;
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;
62 if (isChecked())
63 opt.state |= QStyle::State_Open;
65 style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, &p);
66 p.end();
70 /******************************************************************
71 * Private classes
72 *****************************************************************/
74 class CollapsibleWidget::Private
76 public:
77 QGridLayout *gridLayout;
78 QWidget *innerWidget;
79 ClickableLabel *label;
80 ArrowButton *colButton;
81 QTimeLine *timeline;
82 QWidget *expander;
83 QVBoxLayout *expanderLayout;
86 class SettingsContainer::Private
88 public:
89 QVBoxLayout *layout;
92 /******************************************************************
93 * Implementation
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);
104 setWidget(w);
105 setWidgetResizable(true);
108 SettingsContainer::~SettingsContainer()
110 delete d;
113 CollapsibleWidget* SettingsContainer::insertWidget( QWidget *w, const QString& name )
115 if (w && w->layout()) {
116 QLayout *lay = w->layout();
117 lay->setMargin(2);
118 lay->setSpacing(0);
121 CollapsibleWidget *cw = new CollapsibleWidget( name );
122 d->layout->addWidget( cw );
123 cw->setInnerWidget( w );
124 return cw;
127 CollapsibleWidget::CollapsibleWidget(QWidget *parent)
128 : QWidget(parent), d(new CollapsibleWidget::Private)
130 init();
132 CollapsibleWidget::CollapsibleWidget(const QString& caption, QWidget *parent)
133 : QWidget(parent), d(new CollapsibleWidget::Private)
135 init();
136 setCaption(caption);
139 void CollapsibleWidget::init()
141 d->expander = 0;
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)) );
148 d->innerWidget = 0;
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)));
169 setExpanded(false);
170 setEnabled(false);
173 CollapsibleWidget::~CollapsibleWidget()
175 delete d;
178 QWidget* CollapsibleWidget::innerWidget() const
180 return d->innerWidget;
183 #define SIMPLE
185 void CollapsibleWidget::setInnerWidget(QWidget *w)
187 if (!w) {
188 return;
191 d->innerWidget = w;
193 #ifdef SIMPLE
194 if ( !isExpanded() ) {
195 d->innerWidget->hide();
197 d->gridLayout->addWidget( d->innerWidget, 2, 2 );
198 d->gridLayout->setRowStretch( 2, 1 );
199 #else
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 );
213 #endif
215 setEnabled( true );
217 if ( isExpanded() ) {
218 setExpanded( true );
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 ) {
236 return;
239 #ifdef SIMPLE
240 if ( !expanded ) {
241 d->innerWidget->setVisible( false );
243 #else
244 if ( expanded ) {
245 d->expander->setVisible( true );
247 d->innerWidget->setVisible( expanded );
248 #endif
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 );
261 #ifdef SIMPLE
262 d->gridLayout->setRowMinimumHeight( 2, pixels );
264 if ( showAmount == 1 ) {
265 d->innerWidget->setVisible( true );
267 #else
268 d->expander->setFixedHeight( pixels );
269 #endif
272 bool CollapsibleWidget::isExpanded() const
274 return d->colButton->isChecked();
278 #include "collapsiblewidget.moc"