File indexing completed on 2024-04-14 07:31:19

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2003 Scott Wheeler <wheeler@kde.org>
0004     SPDX-FileCopyrightText: 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KLISTWIDGETSEARCHLINE_H
0010 #define KLISTWIDGETSEARCHLINE_H
0011 
0012 #include <QLineEdit>
0013 #include <memory>
0014 
0015 #include <kitemviews_export.h>
0016 
0017 class QListWidget;
0018 class QListWidgetItem;
0019 class QModelIndex;
0020 
0021 /**
0022  * @class KListWidgetSearchLine klistwidgetsearchline.h KListWidgetSearchLine
0023  *
0024  * This class makes it easy to add a search line for filtering the items in a
0025  * listwidget based on a simple text search.
0026  *
0027  * No changes to the application other than instantiating this class with an
0028  * appropriate QListWidget should be needed.
0029  */
0030 class KITEMVIEWS_EXPORT KListWidgetSearchLine : public QLineEdit
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     /**
0036      * Constructs a KListWidgetSearchLine with \a listWidget being the QListWidget to
0037      * be filtered.
0038      *
0039      * If \a listWidget is null then the widget will be disabled until a listWidget
0040      * is set with setListWidget().
0041      */
0042     explicit KListWidgetSearchLine(QWidget *parent = nullptr, QListWidget *listWidget = nullptr);
0043 
0044     /**
0045      * Destroys the KListWidgetSearchLine.
0046      */
0047     ~KListWidgetSearchLine() override;
0048 
0049     /**
0050      * Returns if the search is case sensitive.  This defaults to Qt::CaseInsensitive.
0051      *
0052      * @see setCaseSensitive()
0053      */
0054     Qt::CaseSensitivity caseSensitive() const;
0055 
0056     /**
0057      * Returns the listWidget that is currently filtered by the search.
0058      *
0059      * @see setListWidget()
0060      */
0061     QListWidget *listWidget() const;
0062 
0063 public Q_SLOTS:
0064     /**
0065      * Updates search to only make visible the items that match \a s.  If
0066      * \a s is null then the line edit's text will be used.
0067      */
0068     virtual void updateSearch(const QString &s = QString());
0069 
0070     /**
0071      * Make the search case sensitive or case insensitive.
0072      *
0073      * @see caseSenstive()
0074      */
0075     void setCaseSensitivity(Qt::CaseSensitivity cs);
0076 
0077     /**
0078      * Sets the QListWidget that is filtered by this search line.  If \a lv is null
0079      * then the widget will be disabled.
0080      *
0081      * @see listWidget()
0082      */
0083     void setListWidget(QListWidget *lv);
0084 
0085     /**
0086      * Clear line edit and empty hiddenItems, returning elements to listWidget.
0087      */
0088     void clear();
0089 
0090 protected:
0091     /**
0092      * Returns true if \a item matches the search \a s.  This will be evaluated
0093      * based on the value of caseSensitive().  This can be overridden in
0094      * subclasses to implement more complicated matching schemes.
0095      */
0096     virtual bool itemMatches(const QListWidgetItem *item, const QString &s) const;
0097     /**
0098      * Re-implemented for internal reasons.  API not affected.
0099      */
0100     bool event(QEvent *event) override;
0101 
0102 private:
0103     friend class KListWidgetSearchLinePrivate;
0104     std::unique_ptr<class KListWidgetSearchLinePrivate> const d;
0105 
0106     Q_PRIVATE_SLOT(d, void _k_listWidgetDeleted())
0107     Q_PRIVATE_SLOT(d, void _k_queueSearch(const QString &))
0108     Q_PRIVATE_SLOT(d, void _k_activateSearch())
0109     Q_PRIVATE_SLOT(d, void _k_rowsInserted(const QModelIndex &, int, int))
0110     Q_PRIVATE_SLOT(d, void _k_dataChanged(const QModelIndex &, const QModelIndex &))
0111 };
0112 
0113 #endif /* KLISTWIDGETSEARCHLINE_H */