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 "qxttreewidget.h"
25 #include "qxtitemdelegate.h"
26 #include "qxttreewidget_p.h"
27 #include <QHeaderView>
29 QxtTreeWidgetPrivate::QxtTreeWidgetPrivate()
32 QxtItemDelegate* QxtTreeWidgetPrivate::delegate() const
34 QxtItemDelegate* del = dynamic_cast<QxtItemDelegate*>(qxt_p().itemDelegate());
39 void QxtTreeWidgetPrivate::informStartEditing(const QModelIndex& index)
41 QTreeWidgetItem* item = qxt_p().itemFromIndex(index);
43 emit qxt_p().itemEditingStarted(item);
46 void QxtTreeWidgetPrivate::informFinishEditing(const QModelIndex& index)
48 QTreeWidgetItem* item = qxt_p().itemFromIndex(index);
50 emit qxt_p().itemEditingFinished(item);
53 void QxtTreeWidgetPrivate::expandCollapse(QTreeWidgetItem* item)
55 if (item && !item->parent() && delegate()->decorationStyle() != Qxt::NoDecoration)
56 qxt_p().setItemExpanded(item, !qxt_p().isItemExpanded(item));
60 \class QxtTreeWidget QxtTreeWidget
62 \brief An extended QTreeWidget with additional signals.
64 QxtTreeWidget offers an optional top level item decoration
65 and a few most commonly requested signals.
67 \image html qxttreewidget.png "QxtTreeWidget with Qxt::Menulike and Qxt::Buttonlike decoration styles, respectively."
71 \fn QxtTreeWidget::itemEditingStarted(QTreeWidgetItem* item)
73 This signal is emitted after the editing of \a item has been started.
75 \sa itemEditingFinished()
79 \fn QxtTreeWidget::itemEditingFinished(QTreeWidgetItem* item)
81 This signal is emitted after the editing of \a item has been finished.
83 \sa itemEditingStarted()
87 \fn QxtTreeWidget::itemCheckStateChanged(QxtTreeWidgetItem* item)
89 This signal is emitted whenever the check state of \a item has changed.
91 \note Use QxtTreeWidgetItem in order to enable this feature.
93 \sa QxtTreeWidgetItem, QTreeWidgetItem::checkState()
97 Constructs a new QxtTreeWidget with \a parent.
99 QxtTreeWidget::QxtTreeWidget(QWidget* parent) : QTreeWidget(parent)
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);
113 Destructs the tree widget.
115 QxtTreeWidget::~QxtTreeWidget()
119 \property QxtTreeWidget::decorationStyle
120 \brief This property holds the top level item decoration style
122 Top level items are decorated according to this property.
123 The default value is \b Qxt::NoDecoration.
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).
129 \sa Qxt::DecorationStyle QTreeView::rootIsDecorated
131 Qxt::DecorationStyle QxtTreeWidget::decorationStyle() const
133 return qxt_d().delegate()->decorationStyle();
136 void QxtTreeWidget::setDecorationStyle(Qxt::DecorationStyle style)
138 if (qxt_d().delegate()->decorationStyle() != style)
140 qxt_d().delegate()->setDecorationStyle(style);
142 if (style != Qxt::NoDecoration)
144 setRootIsDecorated(false);
152 \property QxtTreeWidget::elideMode
153 \brief This property holds the text elide mode
155 The text of a decorated top level item is elided according to this property.
156 The default value is \b Qt::ElideMiddle.
158 \note The property has effect only for decorated top level items.
160 \sa decorationStyle, Qt::TextElideMode
162 Qt::TextElideMode QxtTreeWidget::elideMode() const
164 return qxt_d().delegate()->elideMode();
167 void QxtTreeWidget::setElideMode(Qt::TextElideMode mode)
169 if (qxt_d().delegate()->elideMode() != mode)
171 qxt_d().delegate()->setElideMode(mode);