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