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
23 * \inmodule KItemViews
24 *
25 * \brief This class makes it easy to add a search line for filtering the items in a
26 * listwidget based on a simple text search.
27 *
28 * No changes to the application other than instantiating this class with an
29 * appropriate QListWidget should be needed.
30 */
31class KITEMVIEWS_EXPORT KListWidgetSearchLine : public QLineEdit
32{
33 Q_OBJECT
34
35public:
36 /*!
37 * Constructs a KListWidgetSearchLine with \a listWidget being the QListWidget to
38 * be filtered.
39 *
40 * If \a listWidget is null then the widget will be disabled until a listWidget
41 * is set with setListWidget().
42 */
43 explicit KListWidgetSearchLine(QWidget *parent = nullptr, QListWidget *listWidget = nullptr);
44
45 ~KListWidgetSearchLine() override;
46
47 /*!
48 * Returns if the search is case sensitive.
49 *
50 * This defaults to Qt::CaseInsensitive.
51 *
52 * \sa setCaseSensitivity()
53 */
54 Qt::CaseSensitivity caseSensitive() const;
55
56 /*!
57 * Returns the listWidget that is currently filtered by the search.
58 *
59 * \sa setListWidget()
60 */
61 QListWidget *listWidget() const;
62
63public Q_SLOTS:
64 /*!
65 * Updates search to only make visible the items that match \a s.
66 *
67 * If \a s is null then the line edit's text will be used.
68 */
69 virtual void updateSearch(const QString &s = QString());
70
71 /*!
72 * Make the search case sensitive or case insensitive.
73 *
74 * \sa caseSensitive()
75 */
76 void setCaseSensitivity(Qt::CaseSensitivity cs);
77
78 /*!
79 * Sets the QListWidget that is filtered by this search line.
80 *
81 * If \a lv is null then the widget will be disabled.
82 *
83 * \sa listWidget()
84 */
85 void setListWidget(QListWidget *lv);
86
87 /*!
88 * Clear line edit and empty hiddenItems, returning elements to listWidget.
89 */
90 void clear();
91
92protected:
93 /*!
94 * Returns true if \a item matches the search \a s.
95 *
96 * This will be evaluated based on the value of caseSensitive().
97 *
98 * This can be overridden in subclasses to implement more complicated matching schemes.
99 */
100 virtual bool itemMatches(const QListWidgetItem *item, const QString &s) const;
101
102 bool event(QEvent *event) override;
103
104private:
105 friend class KListWidgetSearchLinePrivate;
106 std::unique_ptr<class KListWidgetSearchLinePrivate> const d;
107
108 Q_PRIVATE_SLOT(d, void _k_listWidgetDeleted())
109 Q_PRIVATE_SLOT(d, void _k_queueSearch(const QString &))
110 Q_PRIVATE_SLOT(d, void _k_activateSearch())
111 Q_PRIVATE_SLOT(d, void _k_rowsInserted(const QModelIndex &, int, int))
112 Q_PRIVATE_SLOT(d, void _k_dataChanged(const QModelIndex &, const QModelIndex &))
113};
114
115#endif /* KLISTWIDGETSEARCHLINE_H */
116

source code of kitemviews/src/klistwidgetsearchline.h