qstandarditemmodel.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #ifndef QSTANDARDITEMMODEL_H
  40. #define QSTANDARDITEMMODEL_H
  41. #include <QtCore/qabstractitemmodel.h>
  42. #include <QtGui/qbrush.h>
  43. #include <QtGui/qfont.h>
  44. #include <QtGui/qicon.h>
  45. #ifndef QT_NO_DATASTREAM
  46. #include <QtCore/qdatastream.h>
  47. #endif
  48. QT_BEGIN_NAMESPACE
  49. #ifndef QT_NO_STANDARDITEMMODEL
  50. template <class T> class QList;
  51. class QStandardItemModel;
  52. class QStandardItemPrivate;
  53. class Q_GUI_EXPORT QStandardItem
  54. {
  55. public:
  56. QStandardItem();
  57. explicit QStandardItem(const QString &text);
  58. QStandardItem(const QIcon &icon, const QString &text);
  59. explicit QStandardItem(int rows, int columns = 1);
  60. virtual ~QStandardItem();
  61. virtual QVariant data(int role = Qt::UserRole + 1) const;
  62. virtual void setData(const QVariant &value, int role = Qt::UserRole + 1);
  63. inline QString text() const {
  64. return qvariant_cast<QString>(data(Qt::DisplayRole));
  65. }
  66. inline void setText(const QString &text);
  67. inline QIcon icon() const {
  68. return qvariant_cast<QIcon>(data(Qt::DecorationRole));
  69. }
  70. inline void setIcon(const QIcon &icon);
  71. #ifndef QT_NO_TOOLTIP
  72. inline QString toolTip() const {
  73. return qvariant_cast<QString>(data(Qt::ToolTipRole));
  74. }
  75. inline void setToolTip(const QString &toolTip);
  76. #endif
  77. #ifndef QT_NO_STATUSTIP
  78. inline QString statusTip() const {
  79. return qvariant_cast<QString>(data(Qt::StatusTipRole));
  80. }
  81. inline void setStatusTip(const QString &statusTip);
  82. #endif
  83. #ifndef QT_NO_WHATSTHIS
  84. inline QString whatsThis() const {
  85. return qvariant_cast<QString>(data(Qt::WhatsThisRole));
  86. }
  87. inline void setWhatsThis(const QString &whatsThis);
  88. #endif
  89. inline QSize sizeHint() const {
  90. return qvariant_cast<QSize>(data(Qt::SizeHintRole));
  91. }
  92. inline void setSizeHint(const QSize &sizeHint);
  93. inline QFont font() const {
  94. return qvariant_cast<QFont>(data(Qt::FontRole));
  95. }
  96. inline void setFont(const QFont &font);
  97. inline Qt::Alignment textAlignment() const {
  98. return Qt::Alignment(qvariant_cast<int>(data(Qt::TextAlignmentRole)));
  99. }
  100. inline void setTextAlignment(Qt::Alignment textAlignment);
  101. inline QBrush background() const {
  102. return qvariant_cast<QBrush>(data(Qt::BackgroundRole));
  103. }
  104. inline void setBackground(const QBrush &brush);
  105. inline QBrush foreground() const {
  106. return qvariant_cast<QBrush>(data(Qt::ForegroundRole));
  107. }
  108. inline void setForeground(const QBrush &brush);
  109. inline Qt::CheckState checkState() const {
  110. return Qt::CheckState(qvariant_cast<int>(data(Qt::CheckStateRole)));
  111. }
  112. inline void setCheckState(Qt::CheckState checkState);
  113. inline QString accessibleText() const {
  114. return qvariant_cast<QString>(data(Qt::AccessibleTextRole));
  115. }
  116. inline void setAccessibleText(const QString &accessibleText);
  117. inline QString accessibleDescription() const {
  118. return qvariant_cast<QString>(data(Qt::AccessibleDescriptionRole));
  119. }
  120. inline void setAccessibleDescription(const QString &accessibleDescription);
  121. Qt::ItemFlags flags() const;
  122. void setFlags(Qt::ItemFlags flags);
  123. inline bool isEnabled() const {
  124. return (flags() & Qt::ItemIsEnabled) != 0;
  125. }
  126. void setEnabled(bool enabled);
  127. inline bool isEditable() const {
  128. return (flags() & Qt::ItemIsEditable) != 0;
  129. }
  130. void setEditable(bool editable);
  131. inline bool isSelectable() const {
  132. return (flags() & Qt::ItemIsSelectable) != 0;
  133. }
  134. void setSelectable(bool selectable);
  135. inline bool isCheckable() const {
  136. return (flags() & Qt::ItemIsUserCheckable) != 0;
  137. }
  138. void setCheckable(bool checkable);
  139. inline bool isAutoTristate() const {
  140. return (flags() & Qt::ItemIsAutoTristate) != 0;
  141. }
  142. void setAutoTristate(bool tristate);
  143. inline bool isUserTristate() const {
  144. return (flags() & Qt::ItemIsUserTristate) != 0;
  145. }
  146. void setUserTristate(bool tristate);
  147. #if QT_DEPRECATED_SINCE(5, 6)
  148. QT_DEPRECATED bool isTristate() const { return isAutoTristate(); }
  149. QT_DEPRECATED void setTristate(bool tristate);
  150. #endif
  151. #ifndef QT_NO_DRAGANDDROP
  152. inline bool isDragEnabled() const {
  153. return (flags() & Qt::ItemIsDragEnabled) != 0;
  154. }
  155. void setDragEnabled(bool dragEnabled);
  156. inline bool isDropEnabled() const {
  157. return (flags() & Qt::ItemIsDropEnabled) != 0;
  158. }
  159. void setDropEnabled(bool dropEnabled);
  160. #endif // QT_NO_DRAGANDDROP
  161. QStandardItem *parent() const;
  162. int row() const;
  163. int column() const;
  164. QModelIndex index() const;
  165. QStandardItemModel *model() const;
  166. int rowCount() const;
  167. void setRowCount(int rows);
  168. int columnCount() const;
  169. void setColumnCount(int columns);
  170. bool hasChildren() const;
  171. QStandardItem *child(int row, int column = 0) const;
  172. void setChild(int row, int column, QStandardItem *item);
  173. inline void setChild(int row, QStandardItem *item);
  174. void insertRow(int row, const QList<QStandardItem*> &items);
  175. void insertColumn(int column, const QList<QStandardItem*> &items);
  176. void insertRows(int row, const QList<QStandardItem*> &items);
  177. void insertRows(int row, int count);
  178. void insertColumns(int column, int count);
  179. void removeRow(int row);
  180. void removeColumn(int column);
  181. void removeRows(int row, int count);
  182. void removeColumns(int column, int count);
  183. inline void appendRow(const QList<QStandardItem*> &items);
  184. inline void appendRows(const QList<QStandardItem*> &items);
  185. inline void appendColumn(const QList<QStandardItem*> &items);
  186. inline void insertRow(int row, QStandardItem *item);
  187. inline void appendRow(QStandardItem *item);
  188. QStandardItem *takeChild(int row, int column = 0);
  189. QList<QStandardItem*> takeRow(int row);
  190. QList<QStandardItem*> takeColumn(int column);
  191. void sortChildren(int column, Qt::SortOrder order = Qt::AscendingOrder);
  192. virtual QStandardItem *clone() const;
  193. enum ItemType { Type = 0, UserType = 1000 };
  194. virtual int type() const;
  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. protected:
  201. QStandardItem(const QStandardItem &other);
  202. QStandardItem(QStandardItemPrivate &dd);
  203. QStandardItem &operator=(const QStandardItem &other);
  204. QScopedPointer<QStandardItemPrivate> d_ptr;
  205. void emitDataChanged();
  206. private:
  207. Q_DECLARE_PRIVATE(QStandardItem)
  208. friend class QStandardItemModelPrivate;
  209. friend class QStandardItemModel;
  210. };
  211. inline void QStandardItem::setText(const QString &atext)
  212. { setData(atext, Qt::DisplayRole); }
  213. inline void QStandardItem::setIcon(const QIcon &aicon)
  214. { setData(aicon, Qt::DecorationRole); }
  215. #ifndef QT_NO_TOOLTIP
  216. inline void QStandardItem::setToolTip(const QString &atoolTip)
  217. { setData(atoolTip, Qt::ToolTipRole); }
  218. #endif
  219. #ifndef QT_NO_STATUSTIP
  220. inline void QStandardItem::setStatusTip(const QString &astatusTip)
  221. { setData(astatusTip, Qt::StatusTipRole); }
  222. #endif
  223. #ifndef QT_NO_WHATSTHIS
  224. inline void QStandardItem::setWhatsThis(const QString &awhatsThis)
  225. { setData(awhatsThis, Qt::WhatsThisRole); }
  226. #endif
  227. inline void QStandardItem::setSizeHint(const QSize &asizeHint)
  228. { setData(asizeHint, Qt::SizeHintRole); }
  229. inline void QStandardItem::setFont(const QFont &afont)
  230. { setData(afont, Qt::FontRole); }
  231. inline void QStandardItem::setTextAlignment(Qt::Alignment atextAlignment)
  232. { setData(int(atextAlignment), Qt::TextAlignmentRole); }
  233. inline void QStandardItem::setBackground(const QBrush &abrush)
  234. { setData(abrush, Qt::BackgroundRole); }
  235. inline void QStandardItem::setForeground(const QBrush &abrush)
  236. { setData(abrush, Qt::ForegroundRole); }
  237. inline void QStandardItem::setCheckState(Qt::CheckState acheckState)
  238. { setData(acheckState, Qt::CheckStateRole); }
  239. inline void QStandardItem::setAccessibleText(const QString &aaccessibleText)
  240. { setData(aaccessibleText, Qt::AccessibleTextRole); }
  241. inline void QStandardItem::setAccessibleDescription(const QString &aaccessibleDescription)
  242. { setData(aaccessibleDescription, Qt::AccessibleDescriptionRole); }
  243. inline void QStandardItem::setChild(int arow, QStandardItem *aitem)
  244. { setChild(arow, 0, aitem); }
  245. inline void QStandardItem::appendRow(const QList<QStandardItem*> &aitems)
  246. { insertRow(rowCount(), aitems); }
  247. inline void QStandardItem::appendRows(const QList<QStandardItem*> &aitems)
  248. { insertRows(rowCount(), aitems); }
  249. inline void QStandardItem::appendColumn(const QList<QStandardItem*> &aitems)
  250. { insertColumn(columnCount(), aitems); }
  251. inline void QStandardItem::insertRow(int arow, QStandardItem *aitem)
  252. { insertRow(arow, QList<QStandardItem*>() << aitem); }
  253. inline void QStandardItem::appendRow(QStandardItem *aitem)
  254. { insertRow(rowCount(), aitem); }
  255. class QStandardItemModelPrivate;
  256. class Q_GUI_EXPORT QStandardItemModel : public QAbstractItemModel
  257. {
  258. Q_OBJECT
  259. Q_PROPERTY(int sortRole READ sortRole WRITE setSortRole)
  260. public:
  261. explicit QStandardItemModel(QObject *parent = Q_NULLPTR);
  262. QStandardItemModel(int rows, int columns, QObject *parent = Q_NULLPTR);
  263. ~QStandardItemModel();
  264. void setItemRoleNames(const QHash<int,QByteArray> &roleNames);
  265. QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  266. QModelIndex parent(const QModelIndex &child) const Q_DECL_OVERRIDE;
  267. int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  268. int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  269. bool hasChildren(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
  270. // Qt 6: Remove
  271. QModelIndex sibling(int row, int column, const QModelIndex &idx) const Q_DECL_OVERRIDE;
  272. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  273. bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) Q_DECL_OVERRIDE;
  274. QVariant headerData(int section, Qt::Orientation orientation,
  275. int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
  276. bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value,
  277. int role = Qt::EditRole) Q_DECL_OVERRIDE;
  278. bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
  279. bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
  280. bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
  281. bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
  282. Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
  283. Qt::DropActions supportedDropActions() const Q_DECL_OVERRIDE;
  284. QMap<int, QVariant> itemData(const QModelIndex &index) const Q_DECL_OVERRIDE;
  285. bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) Q_DECL_OVERRIDE;
  286. void clear();
  287. using QObject::parent;
  288. void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) Q_DECL_OVERRIDE;
  289. QStandardItem *itemFromIndex(const QModelIndex &index) const;
  290. QModelIndex indexFromItem(const QStandardItem *item) const;
  291. QStandardItem *item(int row, int column = 0) const;
  292. void setItem(int row, int column, QStandardItem *item);
  293. inline void setItem(int row, QStandardItem *item);
  294. QStandardItem *invisibleRootItem() const;
  295. QStandardItem *horizontalHeaderItem(int column) const;
  296. void setHorizontalHeaderItem(int column, QStandardItem *item);
  297. QStandardItem *verticalHeaderItem(int row) const;
  298. void setVerticalHeaderItem(int row, QStandardItem *item);
  299. void setHorizontalHeaderLabels(const QStringList &labels);
  300. void setVerticalHeaderLabels(const QStringList &labels);
  301. void setRowCount(int rows);
  302. void setColumnCount(int columns);
  303. void appendRow(const QList<QStandardItem*> &items);
  304. void appendColumn(const QList<QStandardItem*> &items);
  305. inline void appendRow(QStandardItem *item);
  306. void insertRow(int row, const QList<QStandardItem*> &items);
  307. void insertColumn(int column, const QList<QStandardItem*> &items);
  308. inline void insertRow(int row, QStandardItem *item);
  309. inline bool insertRow(int row, const QModelIndex &parent = QModelIndex());
  310. inline bool insertColumn(int column, const QModelIndex &parent = QModelIndex());
  311. QStandardItem *takeItem(int row, int column = 0);
  312. QList<QStandardItem*> takeRow(int row);
  313. QList<QStandardItem*> takeColumn(int column);
  314. QStandardItem *takeHorizontalHeaderItem(int column);
  315. QStandardItem *takeVerticalHeaderItem(int row);
  316. const QStandardItem *itemPrototype() const;
  317. void setItemPrototype(const QStandardItem *item);
  318. QList<QStandardItem*> findItems(const QString &text,
  319. Qt::MatchFlags flags = Qt::MatchExactly,
  320. int column = 0) const;
  321. int sortRole() const;
  322. void setSortRole(int role);
  323. QStringList mimeTypes() const Q_DECL_OVERRIDE;
  324. QMimeData *mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE;
  325. bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) Q_DECL_OVERRIDE;
  326. Q_SIGNALS:
  327. void itemChanged(QStandardItem *item);
  328. protected:
  329. QStandardItemModel(QStandardItemModelPrivate &dd, QObject *parent = Q_NULLPTR);
  330. private:
  331. friend class QStandardItemPrivate;
  332. friend class QStandardItem;
  333. Q_DISABLE_COPY(QStandardItemModel)
  334. Q_DECLARE_PRIVATE(QStandardItemModel)
  335. Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &topLeft,
  336. const QModelIndex &bottomRight))
  337. };
  338. inline void QStandardItemModel::setItem(int arow, QStandardItem *aitem)
  339. { setItem(arow, 0, aitem); }
  340. inline void QStandardItemModel::appendRow(QStandardItem *aitem)
  341. { appendRow(QList<QStandardItem*>() << aitem); }
  342. inline void QStandardItemModel::insertRow(int arow, QStandardItem *aitem)
  343. { insertRow(arow, QList<QStandardItem*>() << aitem); }
  344. inline bool QStandardItemModel::insertRow(int arow, const QModelIndex &aparent)
  345. { return QAbstractItemModel::insertRow(arow, aparent); }
  346. inline bool QStandardItemModel::insertColumn(int acolumn, const QModelIndex &aparent)
  347. { return QAbstractItemModel::insertColumn(acolumn, aparent); }
  348. #ifndef QT_NO_DATASTREAM
  349. Q_GUI_EXPORT QDataStream &operator>>(QDataStream &in, QStandardItem &item);
  350. Q_GUI_EXPORT QDataStream &operator<<(QDataStream &out, const QStandardItem &item);
  351. #endif
  352. #endif // QT_NO_STANDARDITEMMODEL
  353. QT_END_NAMESPACE
  354. #endif //QSTANDARDITEMMODEL_H