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
61QT_REQUIRE_CONFIG(listwidget);
62
63QT_BEGIN_NAMESPACE
64
65class QListModelLessThan
66{
67public:
68 inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const
69 { return *i1 < *i2; }
70};
71
72class QListModelGreaterThan
73{
74public:
75 inline bool operator()(QListWidgetItem *i1, QListWidgetItem *i2) const
76 { return *i2 < *i1; }
77};
78
79class Q_AUTOTEST_EXPORT QListModel : public QAbstractListModel
80{
81 Q_OBJECT
82 friend class QListWidget;
83
84public:
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 bool clearItemData(const QModelIndex &index) override;
104
105 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
106
107 bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
108 bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
109 bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
110
111 Qt::ItemFlags flags(const QModelIndex &index) const override;
112
113 void sort(int column, Qt::SortOrder order) override;
114 void ensureSorted(int column, Qt::SortOrder order, int start, int end);
115 static bool itemLessThan(const QPair<QListWidgetItem*,int> &left,
116 const QPair<QListWidgetItem*,int> &right);
117 static bool itemGreaterThan(const QPair<QListWidgetItem*,int> &left,
118 const QPair<QListWidgetItem*,int> &right);
119 static QList<QListWidgetItem*>::iterator sortedInsertionIterator(
120 const QList<QListWidgetItem*>::iterator &begin,
121 const QList<QListWidgetItem*>::iterator &end,
122 Qt::SortOrder order, QListWidgetItem *item);
123
124 void itemChanged(QListWidgetItem *item, const QList<int> &roles = QList<int>());
125
126 // dnd
127 QStringList mimeTypes() const override;
128 QMimeData *mimeData(const QModelIndexList &indexes) const override;
129#if QT_CONFIG(draganddrop)
130 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
131 int row, int column, const QModelIndex &parent) override;
132 Qt::DropActions supportedDropActions() const override;
133#endif
134
135 QMimeData *internalMimeData() const;
136private:
137 QList<QListWidgetItem*> items;
138
139 // A cache must be mutable if get-functions should have const modifiers
140 mutable QModelIndexList cachedIndexes;
141};
142
143
144
145class QListWidgetPrivate : public QListViewPrivate
146{
147 Q_DECLARE_PUBLIC(QListWidget)
148public:
149 QListWidgetPrivate() : QListViewPrivate(), sortOrder(Qt::AscendingOrder), sortingEnabled(false) {}
150 inline QListModel *listModel() const { return qobject_cast<QListModel*>(model); }
151 void setup();
152 void _q_emitItemPressed(const QModelIndex &index);
153 void _q_emitItemClicked(const QModelIndex &index);
154 void _q_emitItemDoubleClicked(const QModelIndex &index);
155 void _q_emitItemActivated(const QModelIndex &index);
156 void _q_emitItemEntered(const QModelIndex &index);
157 void _q_emitItemChanged(const QModelIndex &index);
158 void _q_emitCurrentItemChanged(const QModelIndex &current, const QModelIndex &previous);
159 void _q_sort();
160 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
161 Qt::SortOrder sortOrder;
162 bool sortingEnabled;
163};
164
165class QListWidgetItemPrivate
166{
167public:
168 QListWidgetItemPrivate(QListWidgetItem *item) : q(item), theid(-1) {}
169 QListWidgetItem *q;
170 QList<QWidgetItemData> values;
171 int theid;
172};
173
174QT_END_NAMESPACE
175
176#endif // QLISTWIDGET_P_H
177