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_H
41#define QLISTWIDGET_H
42
43#include <QtWidgets/qlistview.h>
44#include <QtWidgets/qtwidgetsglobal.h>
45#include <QtCore/qlist.h>
46#include <QtCore/qitemselectionmodel.h>
47#include <QtCore/qvariant.h>
48
49QT_REQUIRE_CONFIG(listwidget);
50
51QT_BEGIN_NAMESPACE
52
53class QListWidget;
54class QListModel;
55class QWidgetItemData;
56class QListWidgetItemPrivate;
57
58class Q_WIDGETS_EXPORT QListWidgetItem
59{
60 friend class QListModel;
61 friend class QListWidget;
62public:
63 enum ItemType { Type = 0, UserType = 1000 };
64 explicit QListWidgetItem(QListWidget *listview = nullptr, int type = Type);
65 explicit QListWidgetItem(const QString &text, QListWidget *listview = nullptr, int type = Type);
66 explicit QListWidgetItem(const QIcon &icon, const QString &text,
67 QListWidget *listview = nullptr, int type = Type);
68 QListWidgetItem(const QListWidgetItem &other);
69 virtual ~QListWidgetItem();
70
71 virtual QListWidgetItem *clone() const;
72
73 inline QListWidget *listWidget() const { return view; }
74
75 void setSelected(bool select);
76 bool isSelected() const;
77
78 inline void setHidden(bool hide);
79 inline bool isHidden() const;
80
81 inline Qt::ItemFlags flags() const { return itemFlags; }
82 void setFlags(Qt::ItemFlags flags);
83
84 inline QString text() const
85 { return data(Qt::DisplayRole).toString(); }
86 inline void setText(const QString &text);
87
88 inline QIcon icon() const
89 { return qvariant_cast<QIcon>(data(Qt::DecorationRole)); }
90 inline void setIcon(const QIcon &icon);
91
92 inline QString statusTip() const
93 { return data(Qt::StatusTipRole).toString(); }
94 inline void setStatusTip(const QString &statusTip);
95
96#if QT_CONFIG(tooltip)
97 inline QString toolTip() const
98 { return data(Qt::ToolTipRole).toString(); }
99 inline void setToolTip(const QString &toolTip);
100#endif
101
102#if QT_CONFIG(whatsthis)
103 inline QString whatsThis() const
104 { return data(Qt::WhatsThisRole).toString(); }
105 inline void setWhatsThis(const QString &whatsThis);
106#endif
107
108 inline QFont font() const
109 { return qvariant_cast<QFont>(data(Qt::FontRole)); }
110 inline void setFont(const QFont &font);
111
112 inline int textAlignment() const
113 { return data(Qt::TextAlignmentRole).toInt(); }
114 inline void setTextAlignment(int alignment)
115 { setData(Qt::TextAlignmentRole, alignment); }
116
117 inline QBrush background() const
118 { return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }
119 inline void setBackground(const QBrush &brush)
120 { setData(Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
121
122 inline QBrush foreground() const
123 { return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); }
124 inline void setForeground(const QBrush &brush)
125 { setData(Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
126
127 inline Qt::CheckState checkState() const
128 { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }
129 inline void setCheckState(Qt::CheckState state)
130 { setData(Qt::CheckStateRole, static_cast<int>(state)); }
131
132 inline QSize sizeHint() const
133 { return qvariant_cast<QSize>(data(Qt::SizeHintRole)); }
134 inline void setSizeHint(const QSize &size)
135 { setData(Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); }
136
137 virtual QVariant data(int role) const;
138 virtual void setData(int role, const QVariant &value);
139
140 virtual bool operator<(const QListWidgetItem &other) const;
141
142#ifndef QT_NO_DATASTREAM
143 virtual void read(QDataStream &in);
144 virtual void write(QDataStream &out) const;
145#endif
146 QListWidgetItem &operator=(const QListWidgetItem &other);
147
148 inline int type() const { return rtti; }
149
150private:
151 QListModel *listModel() const;
152 int rtti;
153 QListWidget *view;
154 QListWidgetItemPrivate *d;
155 Qt::ItemFlags itemFlags;
156};
157
158inline void QListWidgetItem::setText(const QString &atext)
159{ setData(Qt::DisplayRole, atext); }
160
161inline void QListWidgetItem::setIcon(const QIcon &aicon)
162{ setData(Qt::DecorationRole, aicon); }
163
164inline void QListWidgetItem::setStatusTip(const QString &astatusTip)
165{ setData(Qt::StatusTipRole, astatusTip); }
166
167#if QT_CONFIG(tooltip)
168inline void QListWidgetItem::setToolTip(const QString &atoolTip)
169{ setData(Qt::ToolTipRole, atoolTip); }
170#endif
171
172#if QT_CONFIG(whatsthis)
173inline void QListWidgetItem::setWhatsThis(const QString &awhatsThis)
174{ setData(Qt::WhatsThisRole, awhatsThis); }
175#endif
176
177inline void QListWidgetItem::setFont(const QFont &afont)
178{ setData(Qt::FontRole, afont); }
179
180#ifndef QT_NO_DATASTREAM
181Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item);
182Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &in, QListWidgetItem &item);
183#endif
184
185class QListWidgetPrivate;
186
187class Q_WIDGETS_EXPORT QListWidget : public QListView
188{
189 Q_OBJECT
190 Q_PROPERTY(int count READ count)
191 Q_PROPERTY(int currentRow READ currentRow WRITE setCurrentRow NOTIFY currentRowChanged USER true)
192 Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
193
194 friend class QListWidgetItem;
195 friend class QListModel;
196public:
197 explicit QListWidget(QWidget *parent = nullptr);
198 ~QListWidget();
199
200 void setSelectionModel(QItemSelectionModel *selectionModel) override;
201
202 QListWidgetItem *item(int row) const;
203 int row(const QListWidgetItem *item) const;
204 void insertItem(int row, QListWidgetItem *item);
205 void insertItem(int row, const QString &label);
206 void insertItems(int row, const QStringList &labels);
207 inline void addItem(const QString &label) { insertItem(count(), label); }
208 inline void addItem(QListWidgetItem *item);
209 inline void addItems(const QStringList &labels) { insertItems(count(), labels); }
210 QListWidgetItem *takeItem(int row);
211 int count() const;
212
213 QListWidgetItem *currentItem() const;
214 void setCurrentItem(QListWidgetItem *item);
215 void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command);
216
217 int currentRow() const;
218 void setCurrentRow(int row);
219 void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command);
220
221 QListWidgetItem *itemAt(const QPoint &p) const;
222 inline QListWidgetItem *itemAt(int x, int y) const;
223 QRect visualItemRect(const QListWidgetItem *item) const;
224
225 void sortItems(Qt::SortOrder order = Qt::AscendingOrder);
226 void setSortingEnabled(bool enable);
227 bool isSortingEnabled() const;
228
229 void editItem(QListWidgetItem *item);
230 void openPersistentEditor(QListWidgetItem *item);
231 void closePersistentEditor(QListWidgetItem *item);
232 using QAbstractItemView::isPersistentEditorOpen;
233 bool isPersistentEditorOpen(QListWidgetItem *item) const;
234
235 QWidget *itemWidget(QListWidgetItem *item) const;
236 void setItemWidget(QListWidgetItem *item, QWidget *widget);
237 inline void removeItemWidget(QListWidgetItem *item);
238
239 QList<QListWidgetItem*> selectedItems() const;
240 QList<QListWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags) const;
241
242 QList<QListWidgetItem*> items(const QMimeData *data) const;
243
244 QModelIndex indexFromItem(const QListWidgetItem *item) const;
245 QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
246
247protected:
248#if QT_CONFIG(draganddrop)
249 void dropEvent(QDropEvent *event) override;
250#endif
251public Q_SLOTS:
252 void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible);
253 void clear();
254
255Q_SIGNALS:
256 void itemPressed(QListWidgetItem *item);
257 void itemClicked(QListWidgetItem *item);
258 void itemDoubleClicked(QListWidgetItem *item);
259 void itemActivated(QListWidgetItem *item);
260 void itemEntered(QListWidgetItem *item);
261 void itemChanged(QListWidgetItem *item);
262
263 void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
264 void currentTextChanged(const QString &currentText);
265 void currentRowChanged(int currentRow);
266
267 void itemSelectionChanged();
268
269protected:
270 bool event(QEvent *e) override;
271 virtual QStringList mimeTypes() const;
272 virtual QMimeData *mimeData(const QList<QListWidgetItem *> &items) const;
273#if QT_CONFIG(draganddrop)
274 virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action);
275 virtual Qt::DropActions supportedDropActions() const;
276#endif
277
278private:
279 void setModel(QAbstractItemModel *model) override;
280 Qt::SortOrder sortOrder() const;
281
282 Q_DECLARE_PRIVATE(QListWidget)
283 Q_DISABLE_COPY(QListWidget)
284
285 Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
286 Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
287 Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
288 Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
289 Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
290 Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
291 Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
292 Q_PRIVATE_SLOT(d_func(), void _q_sort())
293 Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
294};
295
296inline void QListWidget::removeItemWidget(QListWidgetItem *aItem)
297{ setItemWidget(aItem, nullptr); }
298
299inline void QListWidget::addItem(QListWidgetItem *aitem)
300{ insertItem(count(), aitem); }
301
302inline QListWidgetItem *QListWidget::itemAt(int ax, int ay) const
303{ return itemAt(QPoint(ax, ay)); }
304
305inline void QListWidgetItem::setHidden(bool ahide)
306{ if (view) view->setRowHidden(view->row(this), ahide); }
307
308inline bool QListWidgetItem::isHidden() const
309{ return (view ? view->isRowHidden(view->row(this)) : false); }
310
311QT_END_NAMESPACE
312
313#endif // QLISTWIDGET_H
314