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 QtWidgets 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 QLISTWIDGET_P_H
41 #define QLISTWIDGET_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API. This header file may change
48 // from version to version without notice, or even be removed.
49 //
50 // We mean it.
51 //
52 
53 #include <QtWidgets/private/qtwidgetsglobal_p.h>
54 #include <QtCore/qabstractitemmodel.h>
55 #include <QtWidgets/qabstractitemview.h>
56 #include <QtWidgets/qlistwidget.h>
57 #include <qitemdelegate.h>
58 #include <private/qlistview_p.h>
59 #include <private/qwidgetitemdata_p.h>
60 
61 QT_REQUIRE_CONFIG(listwidget);
62 
63 QT_BEGIN_NAMESPACE
64 
65 class QListModelLessThan
66 {
67 public:
operator()68     inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const
69         { return *i1 < *i2; }
70 };
71 
72 class QListModelGreaterThan
73 {
74 public:
operator()75     inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const
76         { return *i2 < *i1; }
77 };
78 
79 class Q_AUTOTEST_EXPORT QListModel : public QAbstractListModel
80 {
81     Q_OBJECT
82     friend class QListWidget;
83 
84 public:
85     QListModel(QListWidget *parent);
86     ~QListModel();
87 
88     void clear();
89     QListWidgetItem *at(int row) const;
90     void insert(int row, QListWidgetItem *item);
91     void insert(int row, const QStringList &items);
92     void remove(QListWidgetItem *item);
93     QListWidgetItem *take(int row);
94     void move(int srcRow, int dstRow);
95 
96     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
97 
98     QModelIndex index(const QListWidgetItem *item) const;
99     QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override;
100 
101     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
102     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
103 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
104     bool clearItemData(const QModelIndex &index) override;
105 #endif
106 
107     QMap<int, QVariant> itemData(const QModelIndex &index) const override;
108 
109     bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
110     bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
111     bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
112 
113     Qt::ItemFlags flags(const QModelIndex &index) const override;
114 
115     void sort(int column, Qt::SortOrder order) override;
116     void ensureSorted(int column, Qt::SortOrder order, int start, int end);
117     static bool itemLessThan(const QPair<QListWidgetItem*,int> &left,
118                              const QPair<QListWidgetItem*,int> &right);
119     static bool itemGreaterThan(const QPair<QListWidgetItem*,int> &left,
120                                 const QPair<QListWidgetItem*,int> &right);
121     static QList<QListWidgetItem*>::iterator sortedInsertionIterator(
122         const QList<QListWidgetItem*>::iterator &begin,
123         const QList<QListWidgetItem*>::iterator &end,
124         Qt::SortOrder order, QListWidgetItem *item);
125 
126     void itemChanged(QListWidgetItem *item, const QVector<int> &roles = QVector<int>());
127 
128     // dnd
129     QStringList mimeTypes() const override;
130     QMimeData *mimeData(const QModelIndexList &indexes) const override;
131 #if QT_CONFIG(draganddrop)
132     bool dropMimeData(const QMimeData *data, Qt::DropAction action,
133                       int row, int column, const QModelIndex &parent) override;
134     Qt::DropActions supportedDropActions() const override;
135 #endif
136 
137     QMimeData *internalMimeData()  const;
138 private:
139     QList<QListWidgetItem*> items;
140 
141     // A cache must be mutable if get-functions should have const modifiers
142     mutable QModelIndexList cachedIndexes;
143 };
144 
145 
146 
147 class QListWidgetPrivate : public QListViewPrivate
148 {
Q_DECLARE_PUBLIC(QListWidget)149     Q_DECLARE_PUBLIC(QListWidget)
150 public:
151     QListWidgetPrivate() : QListViewPrivate(), sortOrder(Qt::AscendingOrder), sortingEnabled(false) {}
listModel()152     inline QListModel *listModel() const { return qobject_cast<QListModel*>(model); }
153     void setup();
154     void _q_emitItemPressed(const QModelIndex &index);
155     void _q_emitItemClicked(const QModelIndex &index);
156     void _q_emitItemDoubleClicked(const QModelIndex &index);
157     void _q_emitItemActivated(const QModelIndex &index);
158     void _q_emitItemEntered(const QModelIndex &index);
159     void _q_emitItemChanged(const QModelIndex &index);
160     void _q_emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
161     void _q_sort();
162     void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
163     Qt::SortOrder sortOrder;
164     bool sortingEnabled;
165 };
166 
167 class QListWidgetItemPrivate
168 {
169 public:
QListWidgetItemPrivate(QListWidgetItem * item)170     QListWidgetItemPrivate(QListWidgetItem *item) : q(item), theid(-1) {}
171     QListWidgetItem *q;
172     QVector<QWidgetItemData> values;
173     int theid;
174 };
175 
176 QT_END_NAMESPACE
177 
178 #endif // QLISTWIDGET_P_H
179