1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2003 Scott Wheeler <wheeler@kde.org>
4 SPDX-FileCopyrightText: 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KLISTWIDGETSEARCHLINE_H
10#define KLISTWIDGETSEARCHLINE_H
11
12#include <QLineEdit>
13#include <memory>
14
15#include <kitemviews_export.h>
16
17class QListWidget;
18class QListWidgetItem;
19class QModelIndex;
20
21/**
22 * @class KListWidgetSearchLine klistwidgetsearchline.h KListWidgetSearchLine
23 *
24 * This class makes it easy to add a search line for filtering the items in a
25 * listwidget based on a simple text search.
26 *
27 * No changes to the application other than instantiating this class with an
28 * appropriate QListWidget should be needed.
29 */
30class KITEMVIEWS_EXPORT KListWidgetSearchLine : public QLineEdit
31{
32 Q_OBJECT
33
34public:
35 /**
36 * Constructs a KListWidgetSearchLine with \a listWidget being the QListWidget to
37 * be filtered.
38 *
39 * If \a listWidget is null then the widget will be disabled until a listWidget
40 * is set with setListWidget().
41 */
42 explicit KListWidgetSearchLine(QWidget *parent = nullptr, QListWidget *listWidget = nullptr);
43
44 /**
45 * Destroys the KListWidgetSearchLine.
46 */
47 ~KListWidgetSearchLine() override;
48
49 /**
50 * Returns if the search is case sensitive. This defaults to Qt::CaseInsensitive.
51 *
52 * @see setCaseSensitive()
53 */
54 Qt::CaseSensitivity caseSensitive() const;
55
56 /**
57 * Returns the listWidget that is currently filtered by the search.
58 *
59 * @see setListWidget()
60 */
61 QListWidget *listWidget() const;
62
63public Q_SLOTS:
64 /**
65 * Updates search to only make visible the items that match \a s. If
66 * \a s is null then the line edit's text will be used.
67 */
68 virtual void updateSearch(const QString &s = QString());
69
70 /**
71 * Make the search case sensitive or case insensitive.
72 *
73 * @see caseSenstive()
74 */
75 void setCaseSensitivity(Qt::CaseSensitivity cs);
76
77 /**
78 * Sets the QListWidget that is filtered by this search line. If \a lv is null
79 * then the widget will be disabled.
80 *
81 * @see listWidget()
82 */
83 void setListWidget(QListWidget *lv);
84
85 /**
86 * Clear line edit and empty hiddenItems, returning elements to listWidget.
87 */
88 void clear();
89
90protected:
91 /**
92 * Returns true if \a item matches the search \a s. This will be evaluated
93 * based on the value of caseSensitive(). This can be overridden in
94 * subclasses to implement more complicated matching schemes.
95 */
96 virtual bool itemMatches(const QListWidgetItem *item, const QString &s) const;
97 /**
98 * Re-implemented for internal reasons. API not affected.
99 */
100 bool event(QEvent *event) override;
101
102private:
103 friend class KListWidgetSearchLinePrivate;
104 std::unique_ptr<class KListWidgetSearchLinePrivate> const d;
105
106 Q_PRIVATE_SLOT(d, void _k_listWidgetDeleted())
107 Q_PRIVATE_SLOT(d, void _k_queueSearch(const QString &))
108 Q_PRIVATE_SLOT(d, void _k_activateSearch())
109 Q_PRIVATE_SLOT(d, void _k_rowsInserted(const QModelIndex &, int, int))
110 Q_PRIVATE_SLOT(d, void _k_dataChanged(const QModelIndex &, const QModelIndex &))
111};
112
113#endif /* KLISTWIDGETSEARCHLINE_H */
114

source code of kitemviews/src/klistwidgetsearchline.h