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/qtwidgetsglobal.h>
44#include <QtWidgets/qlistview.h>
45#include <QtCore/qvariant.h>
46#include <QtCore/qvector.h>
47#include <QtCore/qitemselectionmodel.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(role: Qt::DisplayRole).toString(); }
86 inline void setText(const QString &text);
87
88 inline QIcon icon() const
89 { return qvariant_cast<QIcon>(v: data(role: Qt::DecorationRole)); }
90 inline void setIcon(const QIcon &icon);
91
92 inline QString statusTip() const
93 { return data(role: Qt::StatusTipRole).toString(); }
94 inline void setStatusTip(const QString &statusTip);
95
96#ifndef QT_NO_TOOLTIP
97 inline QString toolTip() const
98 { return data(role: 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(role: Qt::WhatsThisRole).toString(); }
105 inline void setWhatsThis(const QString &whatsThis);
106#endif
107
108 inline QFont font() const
109 { return qvariant_cast<QFont>(v: data(role: Qt::FontRole)); }
110 inline void setFont(const QFont &font);
111
112 inline int textAlignment() const
113 { return data(role: Qt::TextAlignmentRole).toInt(); }
114 inline void setTextAlignment(int alignment)
115 { setData(role: Qt::TextAlignmentRole, value: alignment); }
116
117#if QT_DEPRECATED_SINCE(5, 13)
118 QT_DEPRECATED_X ("Use QListWidgetItem::background() instead")
119 inline QColor backgroundColor() const
120 { return qvariant_cast<QColor>(v: data(role: Qt::BackgroundRole)); }
121#endif
122 // no QT_DEPRECATED_SINCE because it is a virtual function
123 QT_DEPRECATED_X ("Use QListWidgetItem::setBackground() instead")
124 virtual void setBackgroundColor(const QColor &color)
125 { setData(role: Qt::BackgroundRole, value: color); }
126
127 inline QBrush background() const
128 { return qvariant_cast<QBrush>(v: data(role: Qt::BackgroundRole)); }
129 inline void setBackground(const QBrush &brush)
130 { setData(role: Qt::BackgroundRole, value: brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
131
132#if QT_DEPRECATED_SINCE(5, 13)
133 QT_DEPRECATED_X ("Use QListWidgetItem::foreground() instead")
134 inline QColor textColor() const
135 { return qvariant_cast<QColor>(v: data(role: Qt::ForegroundRole)); }
136 QT_DEPRECATED_X ("Use QListWidgetItem::setForeground() instead")
137 inline void setTextColor(const QColor &color)
138 { setData(role: Qt::ForegroundRole, value: color); }
139#endif
140
141 inline QBrush foreground() const
142 { return qvariant_cast<QBrush>(v: data(role: Qt::ForegroundRole)); }
143 inline void setForeground(const QBrush &brush)
144 { setData(role: Qt::ForegroundRole, value: brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
145
146 inline Qt::CheckState checkState() const
147 { return static_cast<Qt::CheckState>(data(role: Qt::CheckStateRole).toInt()); }
148 inline void setCheckState(Qt::CheckState state)
149 { setData(role: Qt::CheckStateRole, value: static_cast<int>(state)); }
150
151 inline QSize sizeHint() const
152 { return qvariant_cast<QSize>(v: data(role: Qt::SizeHintRole)); }
153 inline void setSizeHint(const QSize &size)
154 { setData(role: Qt::SizeHintRole, value: size.isValid() ? QVariant(size) : QVariant()); }
155
156 virtual QVariant data(int role) const;
157 virtual void setData(int role, const QVariant &value);
158
159 virtual bool operator<(const QListWidgetItem &other) const;
160
161#ifndef QT_NO_DATASTREAM
162 virtual void read(QDataStream &in);
163 virtual void write(QDataStream &out) const;
164#endif
165 QListWidgetItem &operator=(const QListWidgetItem &other);
166
167 inline int type() const { return rtti; }
168
169private:
170 QListModel *listModel() const;
171 int rtti;
172 QVector<void *> dummy;
173 QListWidget *view;
174 QListWidgetItemPrivate *d;
175 Qt::ItemFlags itemFlags;
176};
177
178inline void QListWidgetItem::setText(const QString &atext)
179{ setData(role: Qt::DisplayRole, value: atext); }
180
181inline void QListWidgetItem::setIcon(const QIcon &aicon)
182{ setData(role: Qt::DecorationRole, value: aicon); }
183
184inline void QListWidgetItem::setStatusTip(const QString &astatusTip)
185{ setData(role: Qt::StatusTipRole, value: astatusTip); }
186
187#ifndef QT_NO_TOOLTIP
188inline void QListWidgetItem::setToolTip(const QString &atoolTip)
189{ setData(role: Qt::ToolTipRole, value: atoolTip); }
190#endif
191
192#if QT_CONFIG(whatsthis)
193inline void QListWidgetItem::setWhatsThis(const QString &awhatsThis)
194{ setData(role: Qt::WhatsThisRole, value: awhatsThis); }
195#endif
196
197inline void QListWidgetItem::setFont(const QFont &afont)
198{ setData(role: Qt::FontRole, value: afont); }
199
200#ifndef QT_NO_DATASTREAM
201Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item);
202Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &in, QListWidgetItem &item);
203#endif
204
205class QListWidgetPrivate;
206
207class Q_WIDGETS_EXPORT QListWidget : public QListView
208{
209 Q_OBJECT
210 Q_PROPERTY(int count READ count)
211 Q_PROPERTY(int currentRow READ currentRow WRITE setCurrentRow NOTIFY currentRowChanged USER true)
212 Q_PROPERTY(bool sortingEnabled READ isSortingEnabled WRITE setSortingEnabled)
213
214 friend class QListWidgetItem;
215 friend class QListModel;
216public:
217 explicit QListWidget(QWidget *parent = nullptr);
218 ~QListWidget();
219
220 void setSelectionModel(QItemSelectionModel *selectionModel) override;
221
222 QListWidgetItem *item(int row) const;
223 int row(const QListWidgetItem *item) const;
224 void insertItem(int row, QListWidgetItem *item);
225 void insertItem(int row, const QString &label);
226 void insertItems(int row, const QStringList &labels);
227 inline void addItem(const QString &label) { insertItem(row: count(), label); }
228 inline void addItem(QListWidgetItem *item);
229 inline void addItems(const QStringList &labels) { insertItems(row: count(), labels); }
230 QListWidgetItem *takeItem(int row);
231 int count() const;
232
233 QListWidgetItem *currentItem() const;
234 void setCurrentItem(QListWidgetItem *item);
235 void setCurrentItem(QListWidgetItem *item, QItemSelectionModel::SelectionFlags command);
236
237 int currentRow() const;
238 void setCurrentRow(int row);
239 void setCurrentRow(int row, QItemSelectionModel::SelectionFlags command);
240
241 QListWidgetItem *itemAt(const QPoint &p) const;
242 inline QListWidgetItem *itemAt(int x, int y) const;
243 QRect visualItemRect(const QListWidgetItem *item) const;
244
245 void sortItems(Qt::SortOrder order = Qt::AscendingOrder);
246 void setSortingEnabled(bool enable);
247 bool isSortingEnabled() const;
248
249 void editItem(QListWidgetItem *item);
250 void openPersistentEditor(QListWidgetItem *item);
251 void closePersistentEditor(QListWidgetItem *item);
252 using QAbstractItemView::isPersistentEditorOpen;
253 bool isPersistentEditorOpen(QListWidgetItem *item) const;
254
255 QWidget *itemWidget(QListWidgetItem *item) const;
256 void setItemWidget(QListWidgetItem *item, QWidget *widget);
257 inline void removeItemWidget(QListWidgetItem *item);
258
259#if QT_DEPRECATED_SINCE(5, 13)
260 QT_DEPRECATED_X ("Use QListWidgetItem::isSelected() instead")
261 bool isItemSelected(const QListWidgetItem *item) const;
262 QT_DEPRECATED_X ("Use QListWidgetItem::setSelected() instead")
263 void setItemSelected(const QListWidgetItem *item, bool select);
264#endif
265 QList<QListWidgetItem*> selectedItems() const;
266 QList<QListWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags) const;
267
268#if QT_DEPRECATED_SINCE(5, 13)
269 QT_DEPRECATED_X ("Use QListWidgetItem::isHidden() instead")
270 bool isItemHidden(const QListWidgetItem *item) const;
271 QT_DEPRECATED_X ("Use QListWidgetItem::setHidden() instead")
272 void setItemHidden(const QListWidgetItem *item, bool hide);
273#endif
274#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
275protected:
276#endif
277#if QT_CONFIG(draganddrop)
278 void dropEvent(QDropEvent *event) override;
279#endif
280public Q_SLOTS:
281 void scrollToItem(const QListWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible);
282 void clear();
283
284Q_SIGNALS:
285 void itemPressed(QListWidgetItem *item);
286 void itemClicked(QListWidgetItem *item);
287 void itemDoubleClicked(QListWidgetItem *item);
288 void itemActivated(QListWidgetItem *item);
289 void itemEntered(QListWidgetItem *item);
290 // ### Qt 6: add changed roles
291 void itemChanged(QListWidgetItem *item);
292
293 void currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
294 void currentTextChanged(const QString &currentText);
295 void currentRowChanged(int currentRow);
296
297 void itemSelectionChanged();
298
299protected:
300 bool event(QEvent *e) override;
301 virtual QStringList mimeTypes() const;
302#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
303 virtual QMimeData *mimeData(const QList<QListWidgetItem *> &items) const;
304#else
305 virtual QMimeData *mimeData(const QList<QListWidgetItem*> items) const;
306#endif
307#if QT_CONFIG(draganddrop)
308 virtual bool dropMimeData(int index, const QMimeData *data, Qt::DropAction action);
309 virtual Qt::DropActions supportedDropActions() const;
310#endif
311
312#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
313public:
314#else
315protected:
316#endif
317 QList<QListWidgetItem*> items(const QMimeData *data) const;
318
319 QModelIndex indexFromItem(const QListWidgetItem *item) const;
320#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
321 QModelIndex indexFromItem(QListWidgetItem *item) const; // ### Qt 6: remove
322#endif
323 QListWidgetItem *itemFromIndex(const QModelIndex &index) const;
324
325private:
326 void setModel(QAbstractItemModel *model) override;
327 Qt::SortOrder sortOrder() const;
328
329 Q_DECLARE_PRIVATE(QListWidget)
330 Q_DISABLE_COPY(QListWidget)
331
332 Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index))
333 Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index))
334 Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index))
335 Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index))
336 Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index))
337 Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index))
338 Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current))
339 Q_PRIVATE_SLOT(d_func(), void _q_sort())
340 Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight))
341};
342
343inline void QListWidget::removeItemWidget(QListWidgetItem *aItem)
344{ setItemWidget(item: aItem, widget: nullptr); }
345
346inline void QListWidget::addItem(QListWidgetItem *aitem)
347{ insertItem(row: count(), item: aitem); }
348
349inline QListWidgetItem *QListWidget::itemAt(int ax, int ay) const
350{ return itemAt(p: QPoint(ax, ay)); }
351
352inline void QListWidgetItem::setHidden(bool ahide)
353{ if (view) view->setRowHidden(row: view->row(item: this), hide: ahide); }
354
355inline bool QListWidgetItem::isHidden() const
356{ return (view ? view->isRowHidden(row: view->row(item: this)) : false); }
357
358QT_END_NAMESPACE
359
360#endif // QLISTWIDGET_H
361

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