1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSTANDARDITEMMODEL_H
41#define QSTANDARDITEMMODEL_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtCore/qabstractitemmodel.h>
45#include <QtGui/qbrush.h>
46#include <QtGui/qfont.h>
47#include <QtGui/qicon.h>
48#ifndef QT_NO_DATASTREAM
49#include <QtCore/qdatastream.h>
50#endif
51
52QT_REQUIRE_CONFIG(standarditemmodel);
53
54QT_BEGIN_NAMESPACE
55
56class QStandardItemModel;
57
58class QStandardItemPrivate;
59class Q_GUI_EXPORT QStandardItem
60{
61public:
62 QStandardItem();
63 explicit QStandardItem(const QString &text);
64 QStandardItem(const QIcon &icon, const QString &text);
65 explicit QStandardItem(int rows, int columns = 1);
66 virtual ~QStandardItem();
67
68 virtual QVariant data(int role = Qt::UserRole + 1) const;
69 virtual void multiData(QModelRoleDataSpan roleDataSpan) const;
70 virtual void setData(const QVariant &value, int role = Qt::UserRole + 1);
71 void clearData();
72
73 inline QString text() const {
74 return qvariant_cast<QString>(data(Qt::DisplayRole));
75 }
76 inline void setText(const QString &text);
77
78 inline QIcon icon() const {
79 return qvariant_cast<QIcon>(data(Qt::DecorationRole));
80 }
81 inline void setIcon(const QIcon &icon);
82
83 inline QString toolTip() const {
84 return qvariant_cast<QString>(data(Qt::ToolTipRole));
85 }
86 inline void setToolTip(const QString &toolTip);
87
88#ifndef QT_NO_STATUSTIP
89 inline QString statusTip() const {
90 return qvariant_cast<QString>(data(Qt::StatusTipRole));
91 }
92 inline void setStatusTip(const QString &statusTip);
93#endif
94
95#if QT_CONFIG(whatsthis)
96 inline QString whatsThis() const {
97 return qvariant_cast<QString>(data(Qt::WhatsThisRole));
98 }
99 inline void setWhatsThis(const QString &whatsThis);
100#endif
101
102 inline QSize sizeHint() const {
103 return qvariant_cast<QSize>(data(Qt::SizeHintRole));
104 }
105 inline void setSizeHint(const QSize &sizeHint);
106
107 inline QFont font() const {
108 return qvariant_cast<QFont>(data(Qt::FontRole));
109 }
110 inline void setFont(const QFont &font);
111
112 inline Qt::Alignment textAlignment() const {
113 return Qt::Alignment(qvariant_cast<int>(data(Qt::TextAlignmentRole)));
114 }
115 inline void setTextAlignment(Qt::Alignment textAlignment);
116
117 inline QBrush background() const {
118 return qvariant_cast<QBrush>(data(Qt::BackgroundRole));
119 }
120 inline void setBackground(const QBrush &brush);
121
122 inline QBrush foreground() const {
123 return qvariant_cast<QBrush>(data(Qt::ForegroundRole));
124 }
125 inline void setForeground(const QBrush &brush);
126
127 inline Qt::CheckState checkState() const {
128 return Qt::CheckState(qvariant_cast<int>(data(Qt::CheckStateRole)));
129 }
130 inline void setCheckState(Qt::CheckState checkState);
131
132 inline QString accessibleText() const {
133 return qvariant_cast<QString>(data(Qt::AccessibleTextRole));
134 }
135 inline void setAccessibleText(const QString &accessibleText);
136
137 inline QString accessibleDescription() const {
138 return qvariant_cast<QString>(data(Qt::AccessibleDescriptionRole));
139 }
140 inline void setAccessibleDescription(const QString &accessibleDescription);
141
142 Qt::ItemFlags flags() const;
143 void setFlags(Qt::ItemFlags flags);
144
145 inline bool isEnabled() const {
146 return (flags() & Qt::ItemIsEnabled) != 0;
147 }
148 void setEnabled(bool enabled);
149
150 inline bool isEditable() const {
151 return (flags() & Qt::ItemIsEditable) != 0;
152 }
153 void setEditable(bool editable);
154
155 inline bool isSelectable() const {
156 return (flags() & Qt::ItemIsSelectable) != 0;
157 }
158 void setSelectable(bool selectable);
159
160 inline bool isCheckable() const {
161 return (flags() & Qt::ItemIsUserCheckable) != 0;
162 }
163 void setCheckable(bool checkable);
164
165 inline bool isAutoTristate() const {
166 return (flags() & Qt::ItemIsAutoTristate) != 0;
167 }
168 void setAutoTristate(bool tristate);
169
170 inline bool isUserTristate() const {
171 return (flags() & Qt::ItemIsUserTristate) != 0;
172 }
173 void setUserTristate(bool tristate);
174
175#if QT_CONFIG(draganddrop)
176 inline bool isDragEnabled() const {
177 return (flags() & Qt::ItemIsDragEnabled) != 0;
178 }
179 void setDragEnabled(bool dragEnabled);
180
181 inline bool isDropEnabled() const {
182 return (flags() & Qt::ItemIsDropEnabled) != 0;
183 }
184 void setDropEnabled(bool dropEnabled);
185#endif // QT_CONFIG(draganddrop)
186
187 QStandardItem *parent() const;
188 int row() const;
189 int column() const;
190 QModelIndex index() const;
191 QStandardItemModel *model() const;
192
193 int rowCount() const;
194 void setRowCount(int rows);
195 int columnCount() const;
196 void setColumnCount(int columns);
197
198 bool hasChildren() const;
199 QStandardItem *child(int row, int column = 0) const;
200 void setChild(int row, int column, QStandardItem *item);
201 inline void setChild(int row, QStandardItem *item);
202
203 void insertRow(int row, const QList<QStandardItem*> &items);
204 void insertColumn(int column, const QList<QStandardItem*> &items);
205 void insertRows(int row, const QList<QStandardItem*> &items);
206 void insertRows(int row, int count);
207 void insertColumns(int column, int count);
208
209 void removeRow(int row);
210 void removeColumn(int column);
211 void removeRows(int row, int count);
212 void removeColumns(int column, int count);
213
214 inline void appendRow(const QList<QStandardItem*> &items);
215 inline void appendRows(const QList<QStandardItem*> &items);
216 inline void appendColumn(const QList<QStandardItem*> &items);
217 inline void insertRow(int row, QStandardItem *item);
218 inline void appendRow(QStandardItem *item);
219
220 QStandardItem *takeChild(int row, int column = 0);
221 QList<QStandardItem*> takeRow(int row);
222 QList<QStandardItem*> takeColumn(int column);
223
224 void sortChildren(int column, Qt::SortOrder order = Qt::AscendingOrder);
225
226 virtual QStandardItem *clone() const;
227
228 enum ItemType { Type = 0, UserType = 1000 };
229 virtual int type() const;
230
231#ifndef QT_NO_DATASTREAM
232 virtual void read(QDataStream &in);
233 virtual void write(QDataStream &out) const;
234#endif
235 virtual bool operator<(const QStandardItem &other) const;
236
237protected:
238 QStandardItem(const QStandardItem &other);
239 QStandardItem(QStandardItemPrivate &dd);
240 QStandardItem &operator=(const QStandardItem &other);
241 QScopedPointer<QStandardItemPrivate> d_ptr;
242
243 void emitDataChanged();
244
245private:
246 Q_DECLARE_PRIVATE(QStandardItem)
247 friend class QStandardItemModelPrivate;
248 friend class QStandardItemModel;
249};
250
251inline void QStandardItem::setText(const QString &atext)
252{ setData(atext, Qt::DisplayRole); }
253
254inline void QStandardItem::setIcon(const QIcon &aicon)
255{ setData(aicon, Qt::DecorationRole); }
256
257inline void QStandardItem::setToolTip(const QString &atoolTip)
258{ setData(atoolTip, Qt::ToolTipRole); }
259
260#ifndef QT_NO_STATUSTIP
261inline void QStandardItem::setStatusTip(const QString &astatusTip)
262{ setData(astatusTip, Qt::StatusTipRole); }
263#endif
264
265#if QT_CONFIG(whatsthis)
266inline void QStandardItem::setWhatsThis(const QString &awhatsThis)
267{ setData(awhatsThis, Qt::WhatsThisRole); }
268#endif
269
270inline void QStandardItem::setSizeHint(const QSize &asizeHint)
271{ setData(asizeHint, Qt::SizeHintRole); }
272
273inline void QStandardItem::setFont(const QFont &afont)
274{ setData(afont, Qt::FontRole); }
275
276inline void QStandardItem::setTextAlignment(Qt::Alignment atextAlignment)
277{ setData(int(atextAlignment), Qt::TextAlignmentRole); }
278
279inline void QStandardItem::setBackground(const QBrush &abrush)
280{ setData(abrush, Qt::BackgroundRole); }
281
282inline void QStandardItem::setForeground(const QBrush &abrush)
283{ setData(abrush, Qt::ForegroundRole); }
284
285inline void QStandardItem::setCheckState(Qt::CheckState acheckState)
286{ setData(acheckState, Qt::CheckStateRole); }
287
288inline void QStandardItem::setAccessibleText(const QString &aaccessibleText)
289{ setData(aaccessibleText, Qt::AccessibleTextRole); }
290
291inline void QStandardItem::setAccessibleDescription(const QString &aaccessibleDescription)
292{ setData(aaccessibleDescription, Qt::AccessibleDescriptionRole); }
293
294inline void QStandardItem::setChild(int arow, QStandardItem *aitem)
295{ setChild(arow, 0, aitem); }
296
297inline void QStandardItem::appendRow(const QList<QStandardItem*> &aitems)
298{ insertRow(rowCount(), aitems); }
299
300inline void QStandardItem::appendRows(const QList<QStandardItem*> &aitems)
301{ insertRows(rowCount(), aitems); }
302
303inline void QStandardItem::appendColumn(const QList<QStandardItem*> &aitems)
304{ insertColumn(columnCount(), aitems); }
305
306inline void QStandardItem::insertRow(int arow, QStandardItem *aitem)
307{ insertRow(arow, QList<QStandardItem*>() << aitem); }
308
309inline void QStandardItem::appendRow(QStandardItem *aitem)
310{ insertRow(rowCount(), aitem); }
311
312class QStandardItemModelPrivate;
313
314class Q_GUI_EXPORT QStandardItemModel : public QAbstractItemModel
315{
316 Q_OBJECT
317 Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole)
318
319public:
320 explicit QStandardItemModel(QObject *parent = nullptr);
321 QStandardItemModel(int rows, int columns, QObject *parent = nullptr);
322 ~QStandardItemModel();
323
324 void setItemRoleNames(const QHash<int,QByteArray> &roleNames);
325 QHash<int, QByteArray> roleNames() const override;
326
327 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
328 QModelIndex parent(const QModelIndex &child) const override;
329
330 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
331 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
332 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
333
334 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
335 void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override;
336 bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
337 bool clearItemData(const QModelIndex &index) override;
338
339 QVariant headerData(int section, Qt::Orientation orientation,
340 int role = Qt::DisplayRole) const override;
341 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
342 int role = Qt::EditRole) override;
343
344 bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
345 bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
346 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
347 bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) override;
348
349 Qt::ItemFlags flags(const QModelIndex &index) const override;
350 Qt::DropActions supportedDropActions() const override;
351
352 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
353 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
354
355 void clear();
356
357 using QObject::parent;
358
359 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
360
361 QStandardItem *itemFromIndex(const QModelIndex &index) const;
362 QModelIndex indexFromItem(const QStandardItem *item) const;
363
364 QStandardItem *item(int row, int column = 0) const;
365 void setItem(int row, int column, QStandardItem *item);
366 inline void setItem(int row, QStandardItem *item);
367 QStandardItem *invisibleRootItem() const;
368
369 QStandardItem *horizontalHeaderItem(int column) const;
370 void setHorizontalHeaderItem(int column, QStandardItem *item);
371 QStandardItem *verticalHeaderItem(int row) const;
372 void setVerticalHeaderItem(int row, QStandardItem *item);
373
374 void setHorizontalHeaderLabels(const QStringList &labels);
375 void setVerticalHeaderLabels(const QStringList &labels);
376
377 void setRowCount(int rows);
378 void setColumnCount(int columns);
379
380 void appendRow(const QList<QStandardItem*> &items);
381 void appendColumn(const QList<QStandardItem*> &items);
382 inline void appendRow(QStandardItem *item);
383
384 void insertRow(int row, const QList<QStandardItem*> &items);
385 void insertColumn(int column, const QList<QStandardItem*> &items);
386 inline void insertRow(int row, QStandardItem *item);
387
388 inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
389 inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
390
391 QStandardItem *takeItem(int row, int column = 0);
392 QList<QStandardItem*> takeRow(int row);
393 QList<QStandardItem*> takeColumn(int column);
394
395 QStandardItem *takeHorizontalHeaderItem(int column);
396 QStandardItem *takeVerticalHeaderItem(int row);
397
398 const QStandardItem *itemPrototype() const;
399 void setItemPrototype(const QStandardItem *item);
400
401 QList<QStandardItem*> findItems(const QString &text,
402 Qt::MatchFlags flags = Qt::MatchExactly,
403 int column = 0) const;
404
405 int sortRole() const;
406 void setSortRole(int role);
407
408 QStringList mimeTypes() const override;
409 QMimeData *mimeData(const QModelIndexList &indexes) const override;
410 bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
411
412Q_SIGNALS:
413 void itemChanged(QStandardItem *item);
414
415protected:
416 QStandardItemModel(QStandardItemModelPrivate &dd, QObject *parent = nullptr);
417
418private:
419 friend class QStandardItemPrivate;
420 friend class QStandardItem;
421 Q_DISABLE_COPY(QStandardItemModel)
422 Q_DECLARE_PRIVATE(QStandardItemModel)
423
424 Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &topLeft,
425 const QModelIndex &bottomRight))
426};
427
428inline void QStandardItemModel::setItem(int arow, QStandardItem *aitem)
429{ setItem(arow, 0, aitem); }
430
431inline void QStandardItemModel::appendRow(QStandardItem *aitem)
432{ appendRow(QList<QStandardItem*>() << aitem); }
433
434inline void QStandardItemModel::insertRow(int arow, QStandardItem *aitem)
435{ insertRow(arow, QList<QStandardItem*>() << aitem); }
436
437inline bool QStandardItemModel::insertRow(int arow, const QModelIndex &aparent)
438{ return QAbstractItemModel::insertRow(arow, aparent); }
439inline bool QStandardItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
440{ return QAbstractItemModel::insertColumn(acolumn, aparent); }
441
442#ifndef QT_NO_DATASTREAM
443Q_GUI_EXPORT QDataStream &operator>>(QDataStream &in, QStandardItem &item);
444Q_GUI_EXPORT QDataStream &operator<<(QDataStream &out, const QStandardItem &item);
445#endif
446
447QT_END_NAMESPACE
448
449#endif //QSTANDARDITEMMODEL_H
450