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 <qitemdelegate.h>
22#include <private/qlistview_p.h>
23#include <private/qwidgetitemdata_p.h>
24
25QT_REQUIRE_CONFIG(listwidget);
26
27QT_BEGIN_NAMESPACE
28
29class QListModelLessThan
30{
31public:
32 inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const
33 { return *i1 < *i2; }
34};
35
36class QListModelGreaterThan
37{
38public:
39 inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const
40 { return *i2 < *i1; }
41};
42
43class Q_AUTOTEST_EXPORT QListModel : public QAbstractListModel
44{
45 Q_OBJECT
46 friend class QListWidget;
47
48public:
49 QListModel(QListWidget *parent);
50 ~QListModel();
51
52 void clear();
53 QListWidgetItem *at(int row) const;
54 void insert(int row, QListWidgetItem *item);
55 void insert(int row, const QStringList &items);
56 void remove(QListWidgetItem *item);
57 QListWidgetItem *take(int row);
58 void move(int srcRow, int dstRow);
59
60 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
61
62 QModelIndex index(const QListWidgetItem *item) const;
63 QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
64
65 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
66 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
67 bool clearItemData(const QModelIndex &index) override;
68
69 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
70
71 bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
72 bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
73 bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
74
75 Qt::ItemFlags flags(const QModelIndex &index) const override;
76
77 void sort(int column, Qt::SortOrder order) override;
78 void ensureSorted(int column, Qt::SortOrder order, int start, int end);
79 static bool itemLessThan(const QPair<QListWidgetItem*,int> &left,
80 const QPair<QListWidgetItem*,int> &right);
81 static bool itemGreaterThan(const QPair<QListWidgetItem*,int> &left,
82 const QPair<QListWidgetItem*,int> &right);
83 static QList<QListWidgetItem*>::iterator sortedInsertionIterator(
84 const QList<QListWidgetItem*>::iterator &begin,
85 const QList<QListWidgetItem*>::iterator &end,
86 Qt::SortOrder order, QListWidgetItem *item);
87
88 void itemChanged(QListWidgetItem *item, const QList<int> &roles = QList<int>());
89
90 // dnd
91 QStringList mimeTypes() const override;
92 QMimeData *mimeData(const QModelIndexList &indexes) const override;
93#if QT_CONFIG(draganddrop)
94 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
95 int row, int column, const QModelIndex &parent) override;
96 Qt::DropActions supportedDropActions() const override;
97#endif
98
99 QMimeData *internalMimeData() const;
100private:
101 QList<QListWidgetItem*> items;
102
103 // A cache must be mutable if get-functions should have const modifiers
104 mutable QModelIndexList cachedIndexes;
105};
106
107
108
109class QListWidgetPrivate : public QListViewPrivate
110{
111 Q_DECLARE_PUBLIC(QListWidget)
112public:
113 QListWidgetPrivate() : QListViewPrivate(), sortOrder(Qt::AscendingOrder), sortingEnabled(false) {}
114 inline QListModel *listModel() const { return qobject_cast<QListModel*>(object: model); }
115 void setup();
116 void _q_emitItemPressed(const QModelIndex &index);
117 void _q_emitItemClicked(const QModelIndex &index);
118 void _q_emitItemDoubleClicked(const QModelIndex &index);
119 void _q_emitItemActivated(const QModelIndex &index);
120 void _q_emitItemEntered(const QModelIndex &index);
121 void _q_emitItemChanged(const QModelIndex &index);
122 void _q_emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
123 void _q_sort();
124 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
125 Qt::SortOrder sortOrder;
126 bool sortingEnabled;
127};
128
129class QListWidgetItemPrivate
130{
131public:
132 QListWidgetItemPrivate(QListWidgetItem *item) : q(item), theid(-1) {}
133 QListWidgetItem *q;
134 QList<QWidgetItemData> values;
135 int theid;
136};
137
138QT_END_NAMESPACE
139
140#endif // QLISTWIDGET_P_H
141

source code of qtbase/src/widgets/itemviews/qlistwidget_p.h