kde-workspace/kcontrol/randr/collapsiblewidget.cpp

278 lines
6.5 KiB
C++
Raw Normal View History

2014-11-13 19:30:51 +02:00
/*
This file is part of the KDE libraries
Copyright (C) 2005 Daniel Molkentin <molkentin@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <QtGui/QtGui>
#include <QTimeLine>
#include <collapsiblewidget.h>
/******************************************************************
* Helper classes
*****************************************************************/
ClickableLabel::ClickableLabel( QWidget* parent )
: QLabel( parent )
{
}
ClickableLabel::~ClickableLabel()
{
}
void ClickableLabel::mouseReleaseEvent( QMouseEvent *e )
{
Q_UNUSED( e );
emit clicked();
}
ArrowButton::ArrowButton( QWidget *parent )
: QAbstractButton( parent )
2014-11-13 19:30:51 +02:00
{
}
ArrowButton::~ArrowButton()
{
}
void ArrowButton::paintEvent(QPaintEvent *event)
2014-11-13 19:30:51 +02:00
{
Q_UNUSED(event);
QPainter p(this);
QStyleOption opt;
int h = sizeHint().height();
opt.rect = QRect(0,( height()- h )/2, h, h);
opt.palette = palette();
opt.state = QStyle::State_Children;
if (isChecked()) {
opt.state |= QStyle::State_Open;
}
style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, &p);
p.end();
2014-11-13 19:30:51 +02:00
}
/******************************************************************
* Private classes
*****************************************************************/
class CollapsibleWidget::Private
{
public:
2014-11-13 19:30:51 +02:00
QGridLayout *gridLayout;
QWidget *innerWidget;
ClickableLabel *label;
ArrowButton *colButton;
QTimeLine *timeline;
QWidget *expander;
QVBoxLayout *expanderLayout;
};
class SettingsContainer::Private
{
public:
2014-11-13 19:30:51 +02:00
QVBoxLayout *layout;
};
/******************************************************************
* Implementation
*****************************************************************/
SettingsContainer::SettingsContainer(QWidget *parent)
: QScrollArea(parent),
d(new SettingsContainer::Private())
2014-11-13 19:30:51 +02:00
{
QWidget *w = new QWidget;
QVBoxLayout *helperLay = new QVBoxLayout(w);
helperLay->setMargin(0);
d->layout = new QVBoxLayout;
helperLay->addLayout( d->layout);
helperLay->addStretch(1);
setWidget(w);
setWidgetResizable(true);
2014-11-13 19:30:51 +02:00
}
SettingsContainer::~SettingsContainer()
{
delete d;
2014-11-13 19:30:51 +02:00
}
CollapsibleWidget* SettingsContainer::insertWidget(QWidget *w, const QString& name)
2014-11-13 19:30:51 +02:00
{
if (w && w->layout()) {
QLayout *lay = w->layout();
lay->setMargin(2);
lay->setSpacing(2);
}
CollapsibleWidget *cw = new CollapsibleWidget(name);
d->layout->addWidget(cw);
cw->setInnerWidget(w);
return cw;
2014-11-13 19:30:51 +02:00
}
CollapsibleWidget::CollapsibleWidget(QWidget *parent)
: QWidget(parent),
d(new CollapsibleWidget::Private())
2014-11-13 19:30:51 +02:00
{
init();
2014-11-13 19:30:51 +02:00
}
2014-11-13 19:30:51 +02:00
CollapsibleWidget::CollapsibleWidget(const QString& caption, QWidget *parent)
: QWidget(parent),
d(new CollapsibleWidget::Private())
2014-11-13 19:30:51 +02:00
{
init();
setCaption(caption);
2014-11-13 19:30:51 +02:00
}
void CollapsibleWidget::init()
{
d->expander = 0;
d->expanderLayout = 0;
d->timeline = new QTimeLine(150, this);
d->timeline->setEasingCurve(QEasingCurve(QEasingCurve::InOutSine));
connect(d->timeline, SIGNAL(valueChanged(qreal)), this, SLOT(animateCollapse(qreal)));
2014-11-13 19:30:51 +02:00
d->innerWidget = 0;
d->gridLayout = new QGridLayout(this);
d->gridLayout->setMargin(0);
2014-11-13 19:30:51 +02:00
d->colButton = new ArrowButton;
d->colButton->setCheckable(true);
2014-11-13 19:30:51 +02:00
d->label = new ClickableLabel;
d->label->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
2014-11-13 19:30:51 +02:00
d->gridLayout->addWidget(d->colButton, 1, 1);
d->gridLayout->addWidget(d->label, 1, 2);
2014-11-13 19:30:51 +02:00
connect(d->label, SIGNAL(clicked()), d->colButton, SLOT(click()));
2014-11-13 19:30:51 +02:00
connect(d->colButton, SIGNAL(toggled(bool)), SLOT(setExpanded(bool)));
2014-11-13 19:30:51 +02:00
setExpanded(false);
setEnabled(false);
2014-11-13 19:30:51 +02:00
}
CollapsibleWidget::~CollapsibleWidget()
{
delete d;
2014-11-13 19:30:51 +02:00
}
QWidget* CollapsibleWidget::innerWidget() const
{
return d->innerWidget;
2014-11-13 19:30:51 +02:00
}
#define SIMPLE
void CollapsibleWidget::setInnerWidget(QWidget *w)
{
if (!w) {
return;
}
2014-11-13 19:30:51 +02:00
d->innerWidget = w;
2014-11-13 19:30:51 +02:00
#ifdef SIMPLE
if (!isExpanded()) {
d->innerWidget->hide();
}
d->gridLayout->addWidget(d->innerWidget, 2, 2);
d->gridLayout->setRowStretch(2, 1);
2014-11-13 19:30:51 +02:00
#else
if ( !d->expander ) {
d->expander = new QWidget(this);
d->gridLayout->addWidget(d->expander, 2, 2);
d->gridLayout->setRowStretch( 2, 1 );
d->expanderLayout = new QVBoxLayout(d->expander);
d->expanderLayout->setMargin(0);
d->expanderLayout->setSpacing(0);
d->expander->setFixedHeight(0);
}
d->innerWidget->setParent(d->expander);
d->innerWidget->show();
d->expanderLayout->addWidget(d->innerWidget);
2014-11-13 19:30:51 +02:00
#endif
setEnabled(true);
2014-11-13 19:30:51 +02:00
if (isExpanded()) {
setExpanded(true);
}
2014-11-13 19:30:51 +02:00
}
void CollapsibleWidget::setCaption(const QString& caption)
{
d->label->setText(QString("<b>%1</b>").arg(caption));
2014-11-13 19:30:51 +02:00
}
QString CollapsibleWidget::caption() const
{
return d->label->text();
2014-11-13 19:30:51 +02:00
}
void CollapsibleWidget::setExpanded(bool expanded)
{
if (!d->innerWidget) {
return;
}
2014-11-13 19:30:51 +02:00
#ifdef SIMPLE
if (!expanded) {
d->innerWidget->setVisible(false);
}
2014-11-13 19:30:51 +02:00
#else
if (expanded) {
d->expander->setVisible(true);
}
d->innerWidget->setVisible(expanded);
2014-11-13 19:30:51 +02:00
#endif
d->colButton->setChecked(expanded);
d->timeline->setDirection(expanded ? QTimeLine::Forward : QTimeLine::Backward );
if (d->timeline->state() != QTimeLine::Running) {
d->timeline->start();
}
2014-11-13 19:30:51 +02:00
}
void CollapsibleWidget::animateCollapse( qreal showAmount )
{
int pixels = d->innerWidget->sizeHint().height() * showAmount;
d->gridLayout->setRowMinimumHeight(2, pixels);
2014-11-13 19:30:51 +02:00
#ifdef SIMPLE
d->gridLayout->setRowMinimumHeight(2, pixels);
2014-11-13 19:30:51 +02:00
if (showAmount == 1) {
d->innerWidget->setVisible(true);
}
2014-11-13 19:30:51 +02:00
#else
d->expander->setFixedHeight(pixels);
2014-11-13 19:30:51 +02:00
#endif
}
bool CollapsibleWidget::isExpanded() const
{
return d->colButton->isChecked();
2014-11-13 19:30:51 +02:00
}
2015-02-27 09:28:46 +00:00
#include "moc_collapsiblewidget.cpp"