2 Copyright (c) 2003 Scott Wheeler <wheeler@kde.org>
3 Copyright (c) 2005 Rafal Rzepecki <divide@users.sourceforge.net>
4 Copyright (c) 2006 Hamish Rodda <rodda@kde.org>
5 Copyright 2007 Pino Toscano <pino@kde.org>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License version 2 as published by the Free Software Foundation.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
22 #ifndef KTREEVIEWSEARCHLINE_H
23 #define KTREEVIEWSEARCHLINE_H
25 #include <klineedit.h>
31 * This class makes it easy to add a search line for filtering the items in
32 * listviews based on a simple text search.
34 * No changes to the application other than instantiating this class with
35 * appropriate QTreeViews should be needed.
38 class KTreeViewSearchLine
: public KLineEdit
42 Q_PROPERTY( Qt::CaseSensitivity caseSensitity READ caseSensitivity WRITE setCaseSensitivity
)
43 Q_PROPERTY( bool keepParentsVisible READ keepParentsVisible WRITE setKeepParentsVisible
)
48 * Constructs a KTreeViewSearchLine with \a treeView being the QTreeView to
51 * If \a treeView is null then the widget will be disabled until listviews
52 * are set with setTreeView(), setTreeViews() or added with addTreeView().
54 explicit KTreeViewSearchLine( QWidget
*parent
= 0, QTreeView
*treeView
= 0 );
57 * Constructs a KTreeViewSearchLine with \a treeViews being the list of
58 * pointers to QTreeViews to be filtered.
60 * If \a treeViews is empty then the widget will be disabled until listviews
61 * are set with setTreeView(), setTreeViews() or added with addTreeView().
63 KTreeViewSearchLine( QWidget
*parent
, const QList
<QTreeView
*> &treeViews
);
67 * Destroys the KTreeViewSearchLine.
69 virtual ~KTreeViewSearchLine();
72 * Returns true if the search is case sensitive. This defaults to false.
74 * @see setCaseSensitive()
76 Qt::CaseSensitivity
caseSensitivity() const;
79 * Returns the current list of columns that will be searched. If the
80 * returned list is empty all visible columns will be searched.
82 * @see setSearchColumns
84 QList
<int> searchColumns() const;
87 * If this is true (the default) then the parents of matched items will also
90 * @see setKeepParentsVisible()
92 bool keepParentsVisible() const;
95 * Returns the listview that is currently filtered by the search.
96 * If there are multiple listviews filtered, it returns 0.
98 * @see setTreeView(), treeView()
100 QTreeView
*treeView() const;
103 * Returns the list of pointers to listviews that are currently filtered by
106 * @see setTreeViews(), addTreeView(), treeView()
108 QList
<QTreeView
*> treeViews() const;
112 * Adds a QTreeView to the list of listviews filtered by this search line.
113 * If \a treeView is null then the widget will be disabled.
115 * @see treeView(), setTreeViews(), removeTreeView()
117 void addTreeView( QTreeView
*treeView
);
120 * Removes a QTreeView from the list of listviews filtered by this search
121 * line. Does nothing if \a treeView is 0 or is not filtered by the quick search
124 * @see listVew(), setTreeView(), addTreeView()
126 void removeTreeView( QTreeView
*treeView
);
129 * Updates search to only make visible the items that match \a pattern. If
130 * \a s is null then the line edit's text will be used.
132 virtual void updateSearch( const QString
&pattern
= QString() );
135 * Make the search case sensitive or case insensitive.
137 * @see caseSenstivity()
139 void setCaseSensitivity( Qt::CaseSensitivity caseSensitivity
);
142 * When a search is active on a list that's organized into a tree view if
143 * a parent or ancesestor of an item is does not match the search then it
144 * will be hidden and as such so too will any children that match.
146 * If this is set to true (the default) then the parents of matching items
149 * @see keepParentsVisible
151 void setKeepParentsVisible( bool value
);
154 * Sets the list of columns to be searched. The default is to search all,
155 * visible columns which can be restored by passing \a columns as an empty
157 * If listviews to be filtered have different numbers or labels of columns
158 * this method has no effect.
162 void setSearchColumns( const QList
<int> &columns
);
165 * Sets the QTreeView that is filtered by this search line, replacing any
166 * previously filtered listviews. If \a treeView is null then the widget will be
169 * @see treeView(), setTreeViews()
171 void setTreeView( QTreeView
*treeView
);
174 * Sets QTreeViews that are filtered by this search line, replacing any
175 * previously filtered listviews. If \a treeViews is empty then the widget will
178 * @see treeViews(), addTreeView(), setTreeView()
180 void setTreeViews( const QList
<QTreeView
*> &treeViews
);
185 * Returns true if \a item matches the search \a pattern. This will be evaluated
186 * based on the value of caseSensitive(). This can be overridden in
187 * subclasses to implement more complicated matching schemes.
189 virtual bool itemMatches( const QModelIndex
&item
, int row
, const QString
&pattern
) const;
192 * Re-implemented for internal reasons. API not affected.
194 virtual void contextMenuEvent( QContextMenuEvent
* );
197 * Updates search to only make visible appropriate items in \a treeView. If
198 * \a treeView is null then nothing is done.
200 virtual void updateSearch( QTreeView
*treeView
);
203 * Connects signals of this listview to the appropriate slots of the search
206 virtual void connectTreeView( QTreeView
* );
209 * Disconnects signals of a listviews from the search line.
211 virtual void disconnectTreeView( QTreeView
* );
214 * Checks columns in all listviews and decides whether choosing columns to
215 * filter on makes any sense.
217 * Returns false if either of the following is true:
218 * * there are no listviews connected,
219 * * the listviews have different numbers of columns,
220 * * the listviews have only one column,
221 * * the listviews differ in column labels.
223 * Otherwise it returns true.
225 * @see setSearchColumns()
227 virtual bool canChooseColumnsCheck();
231 * When keys are pressed a new search string is created and a timer is
232 * activated. The most recent search is activated when this timer runs out
233 * if another key has not yet been pressed.
235 * This method makes @param search the most recent search and starts the
238 * Together with activateSearch() this makes it such that searches are not
239 * started until there is a short break in the users typing.
241 * @see activateSearch()
243 void queueSearch( const QString
&search
);
246 * When the timer started with queueSearch() expires this slot is called.
247 * If there has been another timer started then this slot does nothing.
248 * However if there are no other pending searches this starts the list view
253 void activateSearch();
259 Q_PRIVATE_SLOT( d
, void rowsInserted( const QModelIndex
&, int, int ) const )
260 Q_PRIVATE_SLOT( d
, void treeViewDeleted( QObject
* ) )
261 Q_PRIVATE_SLOT( d
, void slotColumnActivated( QAction
* ) )
262 Q_PRIVATE_SLOT( d
, void slotAllVisibleColumns() )
266 * Creates a widget featuring a KTreeViewSearchLine, a label with the text
267 * "Search" and a button to clear the search.
269 class KTreeViewSearchLineWidget
: public QWidget
275 * Creates a KTreeViewSearchLineWidget for \a treeView with \a parent as the
278 explicit KTreeViewSearchLineWidget( QWidget
*parent
= 0, QTreeView
*treeView
= 0 );
281 * Destroys the KTreeViewSearchLineWidget
283 ~KTreeViewSearchLineWidget();
286 * Returns a pointer to the search line.
288 KTreeViewSearchLine
*searchLine() const;
292 * Creates the widgets inside of the widget. This is called from the
293 * constructor via a single shot timer so that it it guaranteed to run
294 * after construction is complete. This makes it suitable for overriding in
297 virtual void createWidgets();
301 * Creates the search line. This can be useful to reimplement in cases where
302 * a KTreeViewSearchLine subclass is used.
304 * It is const because it is be called from searchLine(), which to the user
305 * doesn't conceptually alter the widget.
307 virtual KTreeViewSearchLine
*createSearchLine( QTreeView
*treeView
) const;