1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtGui module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
24 #include "qxtgroupbox.h"
26 #include <QChildEvent>
28 class QxtGroupBoxPrivate : public QxtPrivate<QxtGroupBox>
31 QXT_DECLARE_PUBLIC(QxtGroupBox);
38 QxtGroupBoxPrivate::QxtGroupBoxPrivate() : collapsive(true)
41 void QxtGroupBoxPrivate::init()
43 qxt_p().setCheckable(true);
44 qxt_p().setChecked(true);
45 QObject::connect(&qxt_p(), SIGNAL(toggled(bool)), &qxt_p(), SLOT(setExpanded(bool)));
49 \class QxtGroupBox QxtGroupBox
51 \brief A collapsive and checkable QGroupBox.
53 QxtGroupBox is a checkable group box automatically expanding/collapsing
54 its content according to the check state. QxtGroupBox shows its children
55 when checked and hides its children when unchecked.
57 \image html qxtgroupbox.png "Two QxtGroupBoxes - an expanded and a collapsed - on top of each other."
61 Constructs a new QxtGroupBox with \a parent.
63 QxtGroupBox::QxtGroupBox(QWidget* parent)
66 QXT_INIT_PRIVATE(QxtGroupBox);
71 Constructs a new QxtGroupBox with \a title and \a parent.
73 QxtGroupBox::QxtGroupBox(const QString& title, QWidget* parent)
74 : QGroupBox(title, parent)
76 QXT_INIT_PRIVATE(QxtGroupBox);
81 Destructs the group box.
83 QxtGroupBox::~QxtGroupBox()
87 \property QxtGroupBox::collapsive
88 \brief This property holds whether the group box is collapsive
90 bool QxtGroupBox::isCollapsive() const
92 return qxt_d().collapsive;
95 void QxtGroupBox::setCollapsive(bool enable)
97 if (qxt_d().collapsive != enable)
99 qxt_d().collapsive = enable;
102 else if (!isChecked())
108 Sets the group box \a collapsed.
110 A collapsed group box hides its children.
112 \sa setExpanded(), QGroupBox::toggled()
114 void QxtGroupBox::setCollapsed(bool collapsed)
116 setExpanded(!collapsed);
120 Sets the group box \a expanded.
122 An expanded group box shows its children.
124 \sa setCollapsed(), QGroupBox::toggled()
126 void QxtGroupBox::setExpanded(bool expanded)
128 if (qxt_d().collapsive || expanded)
130 // show/hide direct children
131 foreach (QObject* child, children())
133 if (child->isWidgetType())
134 static_cast<QWidget*>(child)->setVisible(expanded);
140 void QxtGroupBox::childEvent(QChildEvent* event)
142 QObject* child = event->child();
143 if (event->added() && child->isWidgetType())
145 QWidget* widget = static_cast<QWidget*>(child);
146 if (qxt_d().collapsive && !isChecked())