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 QSTANDARDITEMMODEL_P_H |
43 | #define QSTANDARDITEMMODEL_P_H |
44 | |
45 | // |
46 | // W A R N I N G |
47 | // ------------- |
48 | // |
49 | // This file is not part of the Qt API. It exists for the convenience |
50 | // of other Qt classes. This header file may change from version to |
51 | // version without notice, or even be removed. |
52 | // |
53 | // We mean it. |
54 | // |
55 | |
56 | #include "private/qabstractitemmodel_p.h" |
57 | |
58 | #ifndef QT_NO_STANDARDITEMMODEL |
59 | |
60 | #include <private/qwidgetitemdata_p.h> |
61 | #include <QtCore/qlist.h> |
62 | #include <QtCore/qpair.h> |
63 | #include <QtCore/qstack.h> |
64 | #include <QtCore/qvariant.h> |
65 | #include <QtCore/qvector.h> |
66 | |
67 | QT_BEGIN_NAMESPACE |
68 | |
69 | class QStandardItemPrivate |
70 | { |
71 | Q_DECLARE_PUBLIC(QStandardItem) |
72 | public: |
73 | inline QStandardItemPrivate() |
74 | : model(0), |
75 | parent(0), |
76 | rows(0), |
77 | columns(0), |
78 | q_ptr(0), |
79 | lastIndexOf(2) |
80 | { } |
81 | virtual ~QStandardItemPrivate(); |
82 | |
83 | inline int childIndex(int row, int column) const { |
84 | if ((row < 0) || (column < 0) |
85 | || (row >= rowCount()) || (column >= columnCount())) { |
86 | return -1; |
87 | } |
88 | return (row * columnCount()) + column; |
89 | } |
90 | inline int childIndex(const QStandardItem *child) { |
91 | int start = qMax(0, lastIndexOf -2); |
92 | lastIndexOf = children.indexOf(const_cast<QStandardItem*>(child), start); |
93 | if (lastIndexOf == -1 && start != 0) |
94 | lastIndexOf = children.lastIndexOf(const_cast<QStandardItem*>(child), start); |
95 | return lastIndexOf; |
96 | } |
97 | QPair<int, int> position() const; |
98 | void setChild(int row, int column, QStandardItem *item, |
99 | bool emitChanged = false); |
100 | inline int rowCount() const { |
101 | return rows; |
102 | } |
103 | inline int columnCount() const { |
104 | return columns; |
105 | } |
106 | void childDeleted(QStandardItem *child); |
107 | |
108 | void setModel(QStandardItemModel *mod); |
109 | |
110 | inline void setParentAndModel( |
111 | QStandardItem *par, |
112 | QStandardItemModel *mod) { |
113 | setModel(mod); |
114 | parent = par; |
115 | } |
116 | |
117 | void changeFlags(bool enable, Qt::ItemFlags f); |
118 | void setItemData(const QMap<int, QVariant> &roles); |
119 | const QMap<int, QVariant> itemData() const; |
120 | |
121 | bool insertRows(int row, int count, const QList<QStandardItem*> &items); |
122 | bool insertRows(int row, const QList<QStandardItem*> &items); |
123 | bool insertColumns(int column, int count, const QList<QStandardItem*> &items); |
124 | |
125 | void sortChildren(int column, Qt::SortOrder order); |
126 | |
127 | QStandardItemModel *model; |
128 | QStandardItem *parent; |
129 | QVector<QWidgetItemData> values; |
130 | QVector<QStandardItem*> children; |
131 | int rows; |
132 | int columns; |
133 | |
134 | QStandardItem *q_ptr; |
135 | |
136 | int lastIndexOf; |
137 | }; |
138 | |
139 | class QStandardItemModelPrivate : public QAbstractItemModelPrivate |
140 | { |
141 | Q_DECLARE_PUBLIC(QStandardItemModel) |
142 | |
143 | public: |
144 | QStandardItemModelPrivate(); |
145 | virtual ~QStandardItemModelPrivate(); |
146 | |
147 | void init(); |
148 | |
149 | inline QStandardItem *createItem() const { |
150 | return itemPrototype ? itemPrototype->clone() : new QStandardItem; |
151 | } |
152 | |
153 | inline QStandardItem *itemFromIndex(const QModelIndex &index) const { |
154 | Q_Q(const QStandardItemModel); |
155 | if (!index.isValid()) |
156 | return root.data(); |
157 | if (index.model() != q) |
158 | return 0; |
159 | QStandardItem *parent = static_cast<QStandardItem*>(index.internalPointer()); |
160 | if (parent == 0) |
161 | return 0; |
162 | return parent->child(index.row(), index.column()); |
163 | } |
164 | |
165 | void sort(QStandardItem *parent, int column, Qt::SortOrder order); |
166 | void itemChanged(QStandardItem *item); |
167 | void rowsAboutToBeInserted(QStandardItem *parent, int start, int end); |
168 | void columnsAboutToBeInserted(QStandardItem *parent, int start, int end); |
169 | void rowsAboutToBeRemoved(QStandardItem *parent, int start, int end); |
170 | void columnsAboutToBeRemoved(QStandardItem *parent, int start, int end); |
171 | void rowsInserted(QStandardItem *parent, int row, int count); |
172 | void columnsInserted(QStandardItem *parent, int column, int count); |
173 | void rowsRemoved(QStandardItem *parent, int row, int count); |
174 | void columnsRemoved(QStandardItem *parent, int column, int count); |
175 | |
176 | void _q_emitItemChanged(const QModelIndex &topLeft, |
177 | const QModelIndex &bottomRight); |
178 | |
179 | void decodeDataRecursive(QDataStream &stream, QStandardItem *item); |
180 | |
181 | QVector<QStandardItem*> columnHeaderItems; |
182 | QVector<QStandardItem*> rowHeaderItems; |
183 | QScopedPointer<QStandardItem> root; |
184 | const QStandardItem *itemPrototype; |
185 | int sortRole; |
186 | }; |
187 | |
188 | QT_END_NAMESPACE |
189 | |
190 | #endif // QT_NO_STANDARDITEMMODEL |
191 | |
192 | #endif // QSTANDARDITEMMODEL_P_H |
193 | |