| 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 QLISTWIDGET_P_H |
| 5 | #define QLISTWIDGET_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. This header file may change |
| 12 | // from version to version without notice, or even be removed. |
| 13 | // |
| 14 | // We mean it. |
| 15 | // |
| 16 | |
| 17 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 18 | #include <QtCore/qabstractitemmodel.h> |
| 19 | #include <QtWidgets/qabstractitemview.h> |
| 20 | #include <QtWidgets/qlistwidget.h> |
| 21 | #include <private/qlistview_p.h> |
| 22 | #include <private/qwidgetitemdata_p.h> |
| 23 | |
| 24 | #include <array> |
| 25 | |
| 26 | QT_REQUIRE_CONFIG(listwidget); |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | class QListModelLessThan |
| 31 | { |
| 32 | public: |
| 33 | inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const |
| 34 | { return *i1 < *i2; } |
| 35 | }; |
| 36 | |
| 37 | class QListModelGreaterThan |
| 38 | { |
| 39 | public: |
| 40 | inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const |
| 41 | { return *i2 < *i1; } |
| 42 | }; |
| 43 | |
| 44 | class Q_AUTOTEST_EXPORT QListModel : public QAbstractListModel |
| 45 | { |
| 46 | Q_OBJECT |
| 47 | friend class QListWidget; |
| 48 | |
| 49 | public: |
| 50 | QListModel(QListWidget *parent); |
| 51 | ~QListModel(); |
| 52 | |
| 53 | void clear(); |
| 54 | QListWidgetItem *at(int row) const; |
| 55 | void insert(int row, QListWidgetItem *item); |
| 56 | void insert(int row, const QStringList &items); |
| 57 | void remove(QListWidgetItem *item); |
| 58 | QListWidgetItem *take(int row); |
| 59 | void move(int srcRow, int dstRow); |
| 60 | |
| 61 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 62 | |
| 63 | QModelIndex index(const QListWidgetItem *item) const; |
| 64 | QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override; |
| 65 | |
| 66 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 67 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 68 | bool clearItemData(const QModelIndex &index) override; |
| 69 | |
| 70 | QMap<int, QVariant> itemData(const QModelIndex &index) const override; |
| 71 | |
| 72 | bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override; |
| 73 | bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override; |
| 74 | bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override; |
| 75 | |
| 76 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 77 | |
| 78 | void sort(int column, Qt::SortOrder order) override; |
| 79 | void ensureSorted(int column, Qt::SortOrder order, int start, int end); |
| 80 | static bool itemLessThan(const std::pair<QListWidgetItem*,int> &left, |
| 81 | const std::pair<QListWidgetItem*,int> &right); |
| 82 | static bool itemGreaterThan(const std::pair<QListWidgetItem*,int> &left, |
| 83 | const std::pair<QListWidgetItem*,int> &right); |
| 84 | static QList<QListWidgetItem*>::iterator sortedInsertionIterator( |
| 85 | const QList<QListWidgetItem*>::iterator &begin, |
| 86 | const QList<QListWidgetItem*>::iterator &end, |
| 87 | Qt::SortOrder order, QListWidgetItem *item); |
| 88 | |
| 89 | void itemChanged(QListWidgetItem *item, const QList<int> &roles = QList<int>()); |
| 90 | |
| 91 | // dnd |
| 92 | QStringList mimeTypes() const override; |
| 93 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
| 94 | #if QT_CONFIG(draganddrop) |
| 95 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
| 96 | int row, int column, const QModelIndex &parent) override; |
| 97 | Qt::DropActions supportedDropActions() const override; |
| 98 | #endif |
| 99 | |
| 100 | QMimeData *internalMimeData() const; |
| 101 | private: |
| 102 | QList<QListWidgetItem*> items; |
| 103 | |
| 104 | // A cache must be mutable if get-functions should have const modifiers |
| 105 | mutable QModelIndexList cachedIndexes; |
| 106 | }; |
| 107 | |
| 108 | |
| 109 | |
| 110 | class QListWidgetPrivate : public QListViewPrivate |
| 111 | { |
| 112 | Q_DECLARE_PUBLIC(QListWidget) |
| 113 | public: |
| 114 | QListWidgetPrivate() : QListViewPrivate(), sortOrder(Qt::AscendingOrder), sortingEnabled(false) {} |
| 115 | inline QListModel *listModel() const { return qobject_cast<QListModel*>(object: model); } |
| 116 | void setup(); |
| 117 | void clearConnections(); |
| 118 | void emitItemPressed(const QModelIndex &index); |
| 119 | void emitItemClicked(const QModelIndex &index); |
| 120 | void emitItemDoubleClicked(const QModelIndex &index); |
| 121 | void emitItemActivated(const QModelIndex &index); |
| 122 | void emitItemEntered(const QModelIndex &index); |
| 123 | void emitItemChanged(const QModelIndex &index); |
| 124 | void emitCurrentItemChanged(const QModelIndex ¤t, const QModelIndex &previous); |
| 125 | void sort(); |
| 126 | void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
| 127 | Qt::SortOrder sortOrder; |
| 128 | bool sortingEnabled; |
| 129 | |
| 130 | std::array<QMetaObject::Connection, 8> connections; |
| 131 | std::array<QMetaObject::Connection, 2> selectionModelConnections; |
| 132 | }; |
| 133 | |
| 134 | class QListWidgetItemPrivate |
| 135 | { |
| 136 | public: |
| 137 | QListWidgetItemPrivate(QListWidgetItem *item) : q(item), theid(-1) {} |
| 138 | QListWidgetItem *q; |
| 139 | QList<QWidgetItemData> values; |
| 140 | int theid; |
| 141 | }; |
| 142 | |
| 143 | QT_END_NAMESPACE |
| 144 | |
| 145 | #endif // QLISTWIDGET_P_H |
| 146 | |