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

source code of qtbase/src/gui/itemmodels/qstandarditemmodel.h