1/*
2 This file is part of the KDE libraries
3 Copyright (c) 2003 Scott Wheeler <wheeler@kde.org>
4 Copyright (c) 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#ifndef KLISTWIDGETSEARCHLINE_H
22#define KLISTWIDGETSEARCHLINE_H
23
24#include <klineedit.h>
25
26class QListWidget;
27class QListWidgetItem;
28class QModelIndex;
29
30/**
31 * This class makes it easy to add a search line for filtering the items in a
32 * listwidget based on a simple text search.
33 *
34 * No changes to the application other than instantiating this class with an
35 * appropriate QListWidget should be needed.
36 */
37class KDEUI_EXPORT KListWidgetSearchLine : public KLineEdit
38{
39 Q_OBJECT
40
41public:
42
43 /**
44 * Constructs a KListWidgetSearchLine with \a listWidget being the QListWidget to
45 * be filtered.
46 *
47 * If \a listWidget is null then the widget will be disabled until a listWidget
48 * is set with setListWidget().
49 */
50 explicit KListWidgetSearchLine( QWidget *parent = 0,
51 QListWidget *listWidget = 0 );
52
53 /**
54 * Destroys the KListWidgetSearchLine.
55 */
56 virtual ~KListWidgetSearchLine();
57
58 /**
59 * Returns if the search is case sensitive. This defaults to Qt::CaseInsensitive.
60 *
61 * @see setCaseSensitive()
62 */
63 Qt::CaseSensitivity caseSensitive() const;
64
65 /**
66 * Returns the listWidget that is currently filtered by the search.
67 *
68 * @see setListWidget()
69 */
70 QListWidget *listWidget() const;
71
72
73public Q_SLOTS:
74 /**
75 * Updates search to only make visible the items that match \a s. If
76 * \a s is null then the line edit's text will be used.
77 */
78 virtual void updateSearch( const QString &s = QString() );
79
80 /**
81 * Make the search case sensitive or case insensitive.
82 *
83 * @see caseSenstive()
84 */
85 void setCaseSensitivity( Qt::CaseSensitivity cs );
86
87 /**
88 * Sets the QListWidget that is filtered by this search line. If \a lv is null
89 * then the widget will be disabled.
90 *
91 * @see listWidget()
92 */
93 void setListWidget( QListWidget *lv );
94
95
96 /**
97 * Clear line edit and empty hiddenItems, returning elements to listWidget.
98 */
99 void clear();
100
101protected:
102 /**
103 * Returns true if \a item matches the search \a s. This will be evaluated
104 * based on the value of caseSensitive(). This can be overridden in
105 * subclasses to implement more complicated matching schemes.
106 */
107 virtual bool itemMatches( const QListWidgetItem *item,
108 const QString &s ) const;
109 /**
110 * Re-implemented for internal reasons. API not affected.
111 */
112 virtual bool event(QEvent *event);
113
114private:
115 class KListWidgetSearchLinePrivate;
116 KListWidgetSearchLinePrivate * const d;
117
118 Q_PRIVATE_SLOT(d, void _k_listWidgetDeleted())
119 Q_PRIVATE_SLOT(d, void _k_queueSearch(const QString&))
120 Q_PRIVATE_SLOT(d, void _k_activateSearch())
121 Q_PRIVATE_SLOT(d, void _k_rowsInserted(const QModelIndex&, int, int))
122 Q_PRIVATE_SLOT(d, void _k_dataChanged(const QModelIndex&, const QModelIndex&))
123};
124
125
126#endif /* KLISTWIDGETSEARCHLINE_H */
127