We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxtgroupbox.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
4 **
5 ** This file is part of the QxtGui module of the Qt eXTension library
6 **
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
9 ** IBM.
10 **
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.
15 **
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.
20 **
21 ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22 **
23 ****************************************************************************/
24 #include "qxtgroupbox.h"
25
26 #include <QChildEvent>
27
28 class QxtGroupBoxPrivate : public QxtPrivate<QxtGroupBox>
29 {
30 public:
31     QXT_DECLARE_PUBLIC(QxtGroupBox);
32
33     QxtGroupBoxPrivate();
34     void init();
35     bool collapsive;
36 };
37
38 QxtGroupBoxPrivate::QxtGroupBoxPrivate() : collapsive(true)
39 {}
40
41 void QxtGroupBoxPrivate::init()
42 {
43     qxt_p().setCheckable(true);
44     qxt_p().setChecked(true);
45     QObject::connect(&qxt_p(), SIGNAL(toggled(bool)), &qxt_p(), SLOT(setExpanded(bool)));
46 }
47
48 /*!
49     \class QxtGroupBox QxtGroupBox
50     \ingroup QxtGui
51     \brief A collapsive and checkable QGroupBox.
52
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.
56
57     \image html qxtgroupbox.png "Two QxtGroupBoxes - an expanded and a collapsed - on top of each other."
58  */
59
60 /*!
61     Constructs a new QxtGroupBox with \a parent.
62  */
63 QxtGroupBox::QxtGroupBox(QWidget* parent)
64         : QGroupBox(parent)
65 {
66     QXT_INIT_PRIVATE(QxtGroupBox);
67     qxt_d().init();
68 }
69
70 /*!
71     Constructs a new QxtGroupBox with \a title and \a parent.
72  */
73 QxtGroupBox::QxtGroupBox(const QString& title, QWidget* parent)
74         : QGroupBox(title, parent)
75 {
76     QXT_INIT_PRIVATE(QxtGroupBox);
77     qxt_d().init();
78 }
79
80 /*!
81     Destructs the group box.
82  */
83 QxtGroupBox::~QxtGroupBox()
84 {}
85
86 /*!
87     \property QxtGroupBox::collapsive
88     \brief This property holds whether the group box is collapsive
89  */
90 bool QxtGroupBox::isCollapsive() const
91 {
92     return qxt_d().collapsive;
93 }
94
95 void QxtGroupBox::setCollapsive(bool enable)
96 {
97     if (qxt_d().collapsive != enable)
98     {
99         qxt_d().collapsive = enable;
100         if (!enable)
101             setExpanded(true);
102         else if (!isChecked())
103             setExpanded(false);
104     }
105 }
106
107 /*!
108     Sets the group box \a collapsed.
109
110     A collapsed group box hides its children.
111
112     \sa setExpanded(), QGroupBox::toggled()
113  */
114 void QxtGroupBox::setCollapsed(bool collapsed)
115 {
116     setExpanded(!collapsed);
117 }
118
119 /*!
120     Sets the group box \a expanded.
121
122     An expanded group box shows its children.
123
124     \sa setCollapsed(), QGroupBox::toggled()
125  */
126 void QxtGroupBox::setExpanded(bool expanded)
127 {
128     if (qxt_d().collapsive || expanded)
129     {
130         // show/hide direct children
131         foreach (QObject* child, children())
132         {
133             if (child->isWidgetType())
134                 static_cast<QWidget*>(child)->setVisible(expanded);
135         }
136         setFlat(!expanded);
137     }
138 }
139
140 void QxtGroupBox::childEvent(QChildEvent* event)
141 {
142     QObject* child = event->child();
143     if (event->added() && child->isWidgetType())
144     {
145         QWidget* widget = static_cast<QWidget*>(child);
146         if (qxt_d().collapsive && !isChecked())
147             widget->hide();
148     }
149 }