Qt 4.8
qtreewidget_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QTREEWIDGET_P_H
43 #define QTREEWIDGET_P_H
44 
45 //
46 // W A R N I N G
47 // -------------
48 //
49 // This file is not part of the Qt API. This header file may change
50 // from version to version without notice, or even be removed.
51 //
52 // We mean it.
53 //
54 
55 #include <QtCore/qabstractitemmodel.h>
56 #include <private/qabstractitemmodel_p.h>
57 #include <QtCore/qpair.h>
58 #include <QtCore/qbasictimer.h>
59 #include <QtGui/qtreewidget.h>
60 #include <private/qtreeview_p.h>
61 #include <QtGui/qheaderview.h>
62 
63 #ifndef QT_NO_TREEWIDGET
64 
66 
67 class QTreeWidgetItem;
69 class QTreeModelPrivate;
70 
72 {
73  Q_OBJECT
74  friend class QTreeWidget;
75  friend class QTreeWidgetPrivate;
76  friend class QTreeWidgetItem;
77  friend class QTreeWidgetItemPrivate;
80 
81 public:
82  explicit QTreeModel(int columns = 0, QTreeWidget *parent = 0);
83  ~QTreeModel();
84 
85  inline QTreeWidget *view() const
87 
88  void clear();
89  void setColumnCount(int columns);
90 
91  QTreeWidgetItem *item(const QModelIndex &index) const;
92  void itemChanged(QTreeWidgetItem *item);
93 
94  QModelIndex index(const QTreeWidgetItem *item, int column) const;
95  QModelIndex index(int row, int column, const QModelIndex &parent) const;
96  QModelIndex parent(const QModelIndex &child) const;
97  int rowCount(const QModelIndex &parent) const;
98  int columnCount(const QModelIndex &parent = QModelIndex()) const;
99  bool hasChildren(const QModelIndex &parent) const;
100 
101  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
102  bool setData(const QModelIndex &index, const QVariant &value, int role);
103 
104  QMap<int, QVariant> itemData(const QModelIndex &index) const;
105 
106  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
107  bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
108  int role);
109 
110  Qt::ItemFlags flags(const QModelIndex &index) const;
111 
112  void sort(int column, Qt::SortOrder order);
113  void ensureSorted(int column, Qt::SortOrder order,
114  int start, int end, const QModelIndex &parent);
115  static bool itemLessThan(const QPair<QTreeWidgetItem*,int> &left,
117  static bool itemGreaterThan(const QPair<QTreeWidgetItem*,int> &left,
118  const QPair<QTreeWidgetItem*,int> &right);
122  Qt::SortOrder order, QTreeWidgetItem *item);
123 
124  bool insertRows(int row, int count, const QModelIndex &);
125  bool insertColumns(int column, int count, const QModelIndex &);
126 
127  bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex());
128 
129  // dnd
130  QStringList mimeTypes() const;
131  QMimeData *mimeData(const QModelIndexList &indexes) const;
132  bool dropMimeData(const QMimeData *data, Qt::DropAction action,
133  int row, int column, const QModelIndex &parent);
134  Qt::DropActions supportedDropActions() const;
135 
136  QMimeData *internalMimeData() const;
137 
138  inline QModelIndex createIndexFromItem(int row, int col, QTreeWidgetItem *item) const
139  { return createIndex(row, col, item); }
140 
141 protected:
142  QTreeModel(QTreeModelPrivate &, QTreeWidget *parent = 0);
143  void emitDataChanged(QTreeWidgetItem *item, int column);
144  void beginInsertItems(QTreeWidgetItem *parent, int row, int count);
145  void endInsertItems();
146  void beginRemoveItems(QTreeWidgetItem *parent, int row, int count);
147  void endRemoveItems();
148  void sortItems(QList<QTreeWidgetItem*> *items, int column, Qt::SortOrder order);
149  void timerEvent(QTimerEvent *);
150 
151 private:
154 
157 
159  mutable bool skipPendingSort; //while doing internal operation we don't care about sorting
160  bool inline executePendingSort() const;
161 
162  bool isChanging() const;
163 
164 private:
166 public:
167  struct SkipSorting
168  {
169  const QTreeModel * const model;
170  const bool previous;
171  SkipSorting(const QTreeModel *m) : model(m), previous(model->skipPendingSort)
172  { model->skipPendingSort = true; }
174  };
175  friend struct SkipSorting;
176 };
177 
179 #include "private/qabstractitemmodel_p.h"
181 
183 {
185 };
186 
188 {
189 public:
191  : q(item), disabled(false), selected(false), rowGuess(-1), policy(QTreeWidgetItem::DontShowIndicatorWhenChildless) {}
192  void propagateDisabled(QTreeWidgetItem *item);
193  void sortChildren(int column, Qt::SortOrder order, bool climb);
198  int rowGuess;
200 };
201 
202 
204 {
207  int column = view()->header()->sortIndicatorSection();
209  QTreeModel *that = const_cast<QTreeModel*>(this);
210  that->sort(column, order);
211  return true;
212  }
213  return false;
214 }
215 
217 {
218  friend class QTreeModel;
220 public:
221  QTreeWidgetPrivate() : QTreeViewPrivate(), explicitSortColumn(-1) {}
222  inline QTreeModel *treeModel() const { return qobject_cast<QTreeModel*>(model); }
223  inline QModelIndex index(const QTreeWidgetItem *item, int column = 0) const
224  { return treeModel()->index(item, column); }
225  inline QTreeWidgetItem *item(const QModelIndex &index) const
226  { return treeModel()->item(index); }
227  void _q_emitItemPressed(const QModelIndex &index);
228  void _q_emitItemClicked(const QModelIndex &index);
229  void _q_emitItemDoubleClicked(const QModelIndex &index);
230  void _q_emitItemActivated(const QModelIndex &index);
231  void _q_emitItemEntered(const QModelIndex &index);
232  void _q_emitItemChanged(const QModelIndex &index);
233  void _q_emitItemExpanded(const QModelIndex &index);
234  void _q_emitItemCollapsed(const QModelIndex &index);
235  void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &index);
236  void _q_sort();
237  void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
238  void _q_selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
239 
240  // used by QTreeWidgetItem::sortChildren to make sure the column argument is used
242 };
243 
245 
246 #endif // QT_NO_TREEWIDGET
247 
248 #endif // QTREEWIDGET_P_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
bool insertRows(int row, int count, const QModelIndex &)
QModelIndex index(const QTreeWidgetItem *item, int column) const
Returns the model index that refers to the tree view item and column.
bool executePendingSort() const
QTreeModel(int columns=0, QTreeWidget *parent=0)
Constructs a tree model with a parent object and the given number of columns.
#define disabled
QTreeModel * treeModel() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QTreeWidget class provides a tree view that uses a predefined tree model.
Definition: qtreewidget.h:260
int sortIndicatorSection() const
Returns the logical index of the section that has a sort indicator.
QVariant headerData(int section, Qt::Orientation orientation, int role) const
Returns the header data corresponding to the given header section, orientation and data role...
QTreeWidgetItem * item(const QModelIndex &index) const
Returns the tree view item corresponding to the index given.
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
bool isActive() const
Returns true if the timer is running and has not been stopped; otherwise returns false.
Definition: qbasictimer.h:62
#define QT_END_INCLUDE_NAMESPACE
This macro is equivalent to QT_BEGIN_NAMESPACE.
Definition: qglobal.h:92
bool hasChildren(const QModelIndex &parent) const
Returns true if parent has any children; otherwise returns false.
QMimeData * mimeData(const QModelIndexList &indexes) const
Returns an object that contains serialized items of data corresponding to the list of indexes specifi...
QTreeWidgetItem * item(const QModelIndex &index) const
void itemChanged(QTreeWidgetItem *item)
void clear()
Removes all items in the model.
QModelIndex index(const QTreeWidgetItem *item, int column=0) const
T * qobject_cast(QObject *object)
Definition: qobject.h:375
QTreeWidgetItem * headerItem
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Returns the data corresponding to the given model index and role.
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
Sets the header data for the item specified by the header section, orientation and data role to the g...
void sort(int column, Qt::SortOrder order)
Sorts the entire tree in the model in the given order, by the values in the given column...
Q_CORE_EXPORT QTextStream & right(QTextStream &s)
void beginInsertItems(QTreeWidgetItem *parent, int row, int count)
SortOrder
Definition: qnamespace.h:189
The QTreeWidgetItemIterator class provides a way to iterate over the items in a QTreeWidget instance...
QMap< int, QVariant > itemData(const QModelIndex &index) const
Returns a map with values for all predefined roles in the model for the item at the given index...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
Qt::DropActions supportedDropActions() const
Returns the drop actions supported by this model.
QList< QTreeWidgetItemIterator * > iterators
static bool itemGreaterThan(const QPair< QTreeWidgetItem *, int > &left, const QPair< QTreeWidgetItem *, int > &right)
Returns true if the value of the left item is greater than the value of the right item...
int rowCount(const QModelIndex &parent) const
Returns the number of rows in the parent model index.
QTreeWidgetItem * q
QTreeWidget * view() const
Definition: qtreewidget_p.h:85
QTreeWidgetItem * rootItem
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QMimeData * internalMimeData() const
QModelIndex createIndexFromItem(int row, int col, QTreeWidgetItem *item) const
unsigned int uint
Definition: qglobal.h:996
QStringList mimeTypes() const
Returns a list of MIME types that can be used to describe a list of model indexes.
void endInsertItems()
int columnCount(const QModelIndex &parent=QModelIndex()) const
Returns the number of columns in the item referred to by the given index.
DropAction
Definition: qnamespace.h:1597
QTreeWidgetItem::ChildIndicatorPolicy policy
QTreeWidgetItemPrivate(QTreeWidgetItem *item)
const QTreeModel *const model
The QMimeData class provides a container for data that records information about its MIME type...
Definition: qmimedata.h:57
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
Handles the data supplied by a drag and drop operation that ended with the given action.
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QAbstractItemModel class provides the abstract interface for item model classes.
void stop()
Stops the timer.
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
Definition: qlist.h:181
The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class...
Definition: qtreewidget.h:63
QModelIndexList cachedIndexes
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
The QItemSelection class manages information about selected items in a model.
The QTimerEvent class contains parameters that describe a timer event.
Definition: qcoreevent.h:341
bool isChanging() const
static QList< QTreeWidgetItem * >::iterator sortedInsertionIterator(const QList< QTreeWidgetItem *>::iterator &begin, const QList< QTreeWidgetItem *>::iterator &end, Qt::SortOrder order, QTreeWidgetItem *item)
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
QHeaderView * header() const
Returns the header for the tree view.
Definition: qtreeview.cpp:302
The QModelIndex class is used to locate data in a data model.
bool setData(const QModelIndex &index, const QVariant &value, int role)
Sets the data for the item specified by the index and role to that referred to by the value...
void beginRemoveItems(QTreeWidgetItem *parent, int row, int count)
#define QT_BEGIN_INCLUDE_NAMESPACE
This macro is equivalent to QT_END_NAMESPACE.
Definition: qglobal.h:91
QBasicTimer sortPendingTimer
The QBasicTimer class provides timer events for objects.
Definition: qbasictimer.h:55
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
bool insertColumns(int column, int count, const QModelIndex &)
void setColumnCount(int columns)
Sets the number of columns in the tree model.
void timerEvent(QTimerEvent *)
This event handler can be reimplemented in a subclass to receive timer events for the object...
static const KeyPair *const end
Orientation
Definition: qnamespace.h:174
static bool itemLessThan(const QPair< QTreeWidgetItem *, int > &left, const QPair< QTreeWidgetItem *, int > &right)
Returns true if the value of the left item is less than the value of the right item.
Qt::SortOrder sortIndicatorOrder() const
Returns the order for the sort indicator.
Qt::ItemFlags flags(const QModelIndex &index) const
Returns the flags for the item referred to the given index.
Q_CORE_EXPORT QTextStream & left(QTextStream &s)
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
void endRemoveItems()
~QTreeModel()
Destroys this tree model.
bool skipPendingSort
The QMap class is a template class that provides a skip-list-based dictionary.
Definition: qdatastream.h:67
SkipSorting(const QTreeModel *m)
void ensureSorted(int column, Qt::SortOrder order, int start, int end, const QModelIndex &parent)
void emitDataChanged(QTreeWidgetItem *item, int column)
Emits the dataChanged() signal for the given item.
void sortItems(QList< QTreeWidgetItem *> *items, int column, Qt::SortOrder order)