~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/tools/engineexplorer/ktreeviewsearchline.h

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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>
 
6
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
20
*/
 
21
 
 
22
#ifndef KTREEVIEWSEARCHLINE_H
 
23
#define KTREEVIEWSEARCHLINE_H
 
24
 
 
25
#include <klineedit.h>
 
26
 
 
27
class QModelIndex;
 
28
class QTreeView;
 
29
 
 
30
/**
 
31
 * This class makes it easy to add a search line for filtering the items in
 
32
 * listviews based on a simple text search.
 
33
 *
 
34
 * No changes to the application other than instantiating this class with
 
35
 * appropriate QTreeViews should be needed.
 
36
 */
 
37
 
 
38
class KTreeViewSearchLine : public KLineEdit
 
39
{
 
40
    Q_OBJECT
 
41
 
 
42
    Q_PROPERTY( Qt::CaseSensitivity caseSensitity READ caseSensitivity WRITE setCaseSensitivity )
 
43
    Q_PROPERTY( bool keepParentsVisible READ keepParentsVisible WRITE setKeepParentsVisible )
 
44
 
 
45
 
 
46
  public:
 
47
    /**
 
48
     * Constructs a KTreeViewSearchLine with \a treeView being the QTreeView to
 
49
     * be filtered.
 
50
     *
 
51
     * If \a treeView is null then the widget will be disabled until listviews
 
52
     * are set with setTreeView(), setTreeViews() or added with addTreeView().
 
53
     */
 
54
    explicit KTreeViewSearchLine( QWidget *parent = 0, QTreeView *treeView = 0 );
 
55
 
 
56
    /**
 
57
     * Constructs a KTreeViewSearchLine with \a treeViews being the list of
 
58
     * pointers to QTreeViews to be filtered.
 
59
     *
 
60
     * If \a treeViews is empty then the widget will be disabled until listviews
 
61
     * are set with setTreeView(), setTreeViews() or added with addTreeView().
 
62
     */
 
63
    KTreeViewSearchLine( QWidget *parent, const QList<QTreeView *> &treeViews );
 
64
 
 
65
 
 
66
    /**
 
67
     * Destroys the KTreeViewSearchLine.
 
68
     */
 
69
    virtual ~KTreeViewSearchLine();
 
70
 
 
71
    /**
 
72
     * Returns true if the search is case sensitive.  This defaults to false.
 
73
     *
 
74
     * @see setCaseSensitive()
 
75
     */
 
76
    Qt::CaseSensitivity caseSensitivity() const;
 
77
 
 
78
    /**
 
79
     * Returns the current list of columns that will be searched.  If the
 
80
     * returned list is empty all visible columns will be searched.
 
81
     *
 
82
     * @see setSearchColumns
 
83
     */
 
84
    QList<int> searchColumns() const;
 
85
 
 
86
    /**
 
87
     * If this is true (the default) then the parents of matched items will also
 
88
     * be shown.
 
89
     *
 
90
     * @see setKeepParentsVisible()
 
91
     */
 
92
    bool keepParentsVisible() const;
 
93
 
 
94
    /**
 
95
     * Returns the listview that is currently filtered by the search.
 
96
     * If there are multiple listviews filtered, it returns 0.
 
97
     *
 
98
     * @see setTreeView(), treeView()
 
99
     */
 
100
    QTreeView *treeView() const;
 
101
 
 
102
    /**
 
103
     * Returns the list of pointers to listviews that are currently filtered by
 
104
     * the search.
 
105
     *
 
106
     * @see setTreeViews(), addTreeView(), treeView()
 
107
     */
 
108
    QList<QTreeView *> treeViews() const;
 
109
 
 
110
  public Q_SLOTS:
 
111
    /**
 
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.
 
114
     *
 
115
     * @see treeView(), setTreeViews(), removeTreeView()
 
116
     */
 
117
    void addTreeView( QTreeView *treeView );
 
118
 
 
119
    /**
 
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
 
122
     * line.
 
123
     *
 
124
     * @see listVew(), setTreeView(), addTreeView()
 
125
     */
 
126
    void removeTreeView( QTreeView *treeView );
 
127
 
 
128
    /**
 
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.
 
131
     */
 
132
    virtual void updateSearch( const QString &pattern = QString() );
 
133
 
 
134
    /**
 
135
     * Make the search case sensitive or case insensitive.
 
136
     *
 
137
     * @see caseSenstivity()
 
138
     */
 
139
    void setCaseSensitivity( Qt::CaseSensitivity caseSensitivity );
 
140
 
 
141
    /**
 
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.
 
145
     *
 
146
     * If this is set to true (the default) then the parents of matching items
 
147
     * will be shown.
 
148
     *
 
149
     * @see keepParentsVisible
 
150
     */
 
151
    void setKeepParentsVisible( bool value );
 
152
 
 
153
    /**
 
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
 
156
     * list.
 
157
     * If listviews to be filtered have different numbers or labels of columns
 
158
     * this method has no effect.
 
159
     *
 
160
     * @see searchColumns
 
161
     */
 
162
    void setSearchColumns( const QList<int> &columns );
 
163
 
 
164
    /**
 
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
 
167
     * disabled.
 
168
     *
 
169
     * @see treeView(), setTreeViews()
 
170
     */
 
171
    void setTreeView( QTreeView *treeView );
 
172
 
 
173
    /**
 
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
 
176
     * be disabled.
 
177
     *
 
178
     * @see treeViews(), addTreeView(), setTreeView()
 
179
     */
 
180
    void setTreeViews( const QList<QTreeView *> &treeViews );
 
181
 
 
182
 
 
183
  protected:
 
184
    /**
 
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.
 
188
     */
 
189
    virtual bool itemMatches( const QModelIndex &item, int row, const QString &pattern ) const;
 
190
 
 
191
    /**
 
192
    * Re-implemented for internal reasons.  API not affected.
 
193
    */
 
194
    virtual void contextMenuEvent( QContextMenuEvent* );
 
195
 
 
196
    /**
 
197
     * Updates search to only make visible appropriate items in \a treeView.  If
 
198
     * \a treeView is null then nothing is done.
 
199
     */
 
200
    virtual void updateSearch( QTreeView *treeView );
 
201
 
 
202
    /**
 
203
     * Connects signals of this listview to the appropriate slots of the search
 
204
     * line.
 
205
     */
 
206
    virtual void connectTreeView( QTreeView* );
 
207
 
 
208
    /**
 
209
     * Disconnects signals of a listviews from the search line.
 
210
     */
 
211
    virtual void disconnectTreeView( QTreeView* );
 
212
 
 
213
    /**
 
214
     * Checks columns in all listviews and decides whether choosing columns to
 
215
     * filter on makes any sense.
 
216
     *
 
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.
 
222
     *
 
223
     * Otherwise it returns true.
 
224
     *
 
225
     * @see setSearchColumns()
 
226
     */
 
227
    virtual bool canChooseColumnsCheck();
 
228
 
 
229
  protected Q_SLOTS:
 
230
    /**
 
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.
 
234
     *
 
235
     * This method makes @param search the most recent search and starts the
 
236
     * timer.
 
237
     *
 
238
     * Together with activateSearch() this makes it such that searches are not
 
239
     * started until there is a short break in the users typing.
 
240
     *
 
241
     * @see activateSearch()
 
242
     */
 
243
    void queueSearch( const QString &search );
 
244
 
 
245
    /**
 
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
 
249
     * search.
 
250
     *
 
251
     * @see queueSearch()
 
252
     */
 
253
    void activateSearch();
 
254
 
 
255
  private:
 
256
    class Private;
 
257
    Private* const d;
 
258
 
 
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() )
 
263
};
 
264
 
 
265
/**
 
266
 * Creates a widget featuring a KTreeViewSearchLine, a label with the text
 
267
 * "Search" and a button to clear the search.
 
268
 */
 
269
class KTreeViewSearchLineWidget : public QWidget
 
270
{
 
271
    Q_OBJECT
 
272
 
 
273
  public:
 
274
    /**
 
275
     * Creates a KTreeViewSearchLineWidget for \a treeView with \a parent as the
 
276
     * parent.
 
277
     */
 
278
    explicit KTreeViewSearchLineWidget( QWidget *parent = 0, QTreeView *treeView = 0 );
 
279
 
 
280
    /**
 
281
     * Destroys the KTreeViewSearchLineWidget
 
282
     */
 
283
    ~KTreeViewSearchLineWidget();
 
284
 
 
285
    /**
 
286
     * Returns a pointer to the search line.
 
287
     */
 
288
    KTreeViewSearchLine *searchLine() const;
 
289
 
 
290
  protected Q_SLOTS:
 
291
    /**
 
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
 
295
     * subclasses.
 
296
     */
 
297
    virtual void createWidgets();
 
298
 
 
299
  protected:
 
300
    /**
 
301
     * Creates the search line.  This can be useful to reimplement in cases where
 
302
     * a KTreeViewSearchLine subclass is used.
 
303
     *
 
304
     * It is const because it is be called from searchLine(), which to the user
 
305
     * doesn't conceptually alter the widget.
 
306
     */
 
307
    virtual KTreeViewSearchLine *createSearchLine( QTreeView *treeView ) const;
 
308
 
 
309
  private:
 
310
    class Private;
 
311
    Private* const d;
 
312
};
 
313
 
 
314
#endif