We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / contrib / libqxt-2007-10-24 / src / gui / qxttreewidget.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 "qxttreewidget.h"
25 #include "qxtitemdelegate.h"
26 #include "qxttreewidget_p.h"
27 #include <QHeaderView>
28
29 QxtTreeWidgetPrivate::QxtTreeWidgetPrivate()
30 {}
31
32 QxtItemDelegate* QxtTreeWidgetPrivate::delegate() const
33 {
34     QxtItemDelegate* del = dynamic_cast<QxtItemDelegate*>(qxt_p().itemDelegate());
35     Q_ASSERT(del);
36     return del;
37 }
38
39 void QxtTreeWidgetPrivate::informStartEditing(const QModelIndex& index)
40 {
41     QTreeWidgetItem* item = qxt_p().itemFromIndex(index);
42     Q_ASSERT(item);
43     emit qxt_p().itemEditingStarted(item);
44 }
45
46 void QxtTreeWidgetPrivate::informFinishEditing(const QModelIndex& index)
47 {
48     QTreeWidgetItem* item = qxt_p().itemFromIndex(index);
49     Q_ASSERT(item);
50     emit qxt_p().itemEditingFinished(item);
51 }
52
53 void QxtTreeWidgetPrivate::expandCollapse(QTreeWidgetItem* item)
54 {
55     if (item && !item->parent() && delegate()->decorationStyle() != Qxt::NoDecoration)
56         qxt_p().setItemExpanded(item, !qxt_p().isItemExpanded(item));
57 }
58
59 /*!
60     \class QxtTreeWidget QxtTreeWidget
61     \ingroup QxtGui
62     \brief An extended QTreeWidget with additional signals.
63
64     QxtTreeWidget offers an optional top level item decoration
65     and a few most commonly requested signals.
66
67     \image html qxttreewidget.png "QxtTreeWidget with Qxt::Menulike and Qxt::Buttonlike decoration styles, respectively."
68  */
69
70 /*!
71     \fn QxtTreeWidget::itemEditingStarted(QTreeWidgetItem* item)
72
73     This signal is emitted after the editing of \a item has been started.
74
75     \sa itemEditingFinished()
76  */
77
78 /*!
79     \fn QxtTreeWidget::itemEditingFinished(QTreeWidgetItem* item)
80
81     This signal is emitted after the editing of \a item has been finished.
82
83     \sa itemEditingStarted()
84  */
85
86 /*!
87     \fn QxtTreeWidget::itemCheckStateChanged(QxtTreeWidgetItem* item)
88
89     This signal is emitted whenever the check state of \a item has changed.
90
91     \note Use QxtTreeWidgetItem in order to enable this feature.
92
93     \sa QxtTreeWidgetItem, QTreeWidgetItem::checkState()
94  */
95
96 /*!
97     Constructs a new QxtTreeWidget with \a parent.
98  */
99 QxtTreeWidget::QxtTreeWidget(QWidget* parent) : QTreeWidget(parent)
100 {
101     QXT_INIT_PRIVATE(QxtTreeWidget);
102     QxtItemDelegate* delegate = new QxtItemDelegate(this);
103     connect(delegate, SIGNAL(editingStarted(const QModelIndex&)),
104             &qxt_d(), SLOT(informStartEditing(const QModelIndex&)));
105     connect(delegate, SIGNAL(editingFinished(const QModelIndex&)),
106             &qxt_d(), SLOT(informFinishEditing(const QModelIndex&)));
107     connect(this, SIGNAL(itemPressed(QTreeWidgetItem*, int)),
108             &qxt_d(), SLOT(expandCollapse(QTreeWidgetItem*)));
109     setItemDelegate(delegate);
110 }
111
112 /*!
113     Destructs the tree widget.
114  */
115 QxtTreeWidget::~QxtTreeWidget()
116 {}
117
118 /*!
119     \property QxtTreeWidget::decorationStyle
120     \brief This property holds the top level item decoration style
121
122     Top level items are decorated according to this property.
123     The default value is \b Qxt::NoDecoration.
124
125     \note Setting the property to anything else than \b Qxt::NoDecoration
126     hides the header and sets \b QTreeView::rootIsDecorated to \b false
127     (to avoid multiple branch indicators).
128
129     \sa Qxt::DecorationStyle QTreeView::rootIsDecorated
130  */
131 Qxt::DecorationStyle QxtTreeWidget::decorationStyle() const
132 {
133     return qxt_d().delegate()->decorationStyle();
134 }
135
136 void QxtTreeWidget::setDecorationStyle(Qxt::DecorationStyle style)
137 {
138     if (qxt_d().delegate()->decorationStyle() != style)
139     {
140         qxt_d().delegate()->setDecorationStyle(style);
141
142         if (style != Qxt::NoDecoration)
143         {
144             setRootIsDecorated(false);
145             header()->hide();
146         }
147         reset();
148     }
149 }
150
151 /*!
152     \property QxtTreeWidget::elideMode
153     \brief This property holds the text elide mode
154
155     The text of a decorated top level item is elided according to this property.
156     The default value is \b Qt::ElideMiddle.
157
158     \note The property has effect only for decorated top level items.
159
160     \sa decorationStyle, Qt::TextElideMode
161  */
162 Qt::TextElideMode QxtTreeWidget::elideMode() const
163 {
164     return qxt_d().delegate()->elideMode();
165 }
166
167 void QxtTreeWidget::setElideMode(Qt::TextElideMode mode)
168 {
169     if (qxt_d().delegate()->elideMode() != mode)
170     {
171         qxt_d().delegate()->setElideMode(mode);
172         reset();
173     }
174 }