2
kopetelistviewsearchline.cpp - a widget for performing quick searches on Kopete::ListViews
3
Based on code from KMail, copyright (c) 2004 Till Adam <adam@kde.org>
5
Copyright (c) 2004 by Richard Smith <kde@metafoo.co.uk>
7
Kopete (c) 2004 by the Kopete developers <kopete-devel@kde.org>
9
*************************************************************************
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
16
*************************************************************************
19
#include "kopetelistviewsearchline.h"
20
#include "kopetelistviewitem.h"
21
#include "kopetelistview.h"
27
SearchLine::SearchLine( QWidget *parent, ListView *listView, const char *name )
28
: KListViewSearchLine( parent, listView, name )
32
SearchLine::SearchLine(QWidget *parent, const char *name)
33
: KListViewSearchLine( parent, 0, name )
37
SearchLine::~SearchLine()
42
void SearchLine::updateSearch( const QString &s )
44
// we copy a huge chunk of code here simply in order to override
45
// the way items are shown/hidden. KSearchLine rudely
46
// calls setVisible() on items with no way to customise this behaviour.
48
//BEGIN code from KSearchLine::updateSearch
52
search = s.isNull() ? text() : s;
54
// If there's a selected item that is visible, make sure that it's visible
55
// when the search changes too (assuming that it still matches).
57
QListViewItem *currentItem = 0;
59
switch( listView()->selectionMode() )
61
case KListView::NoSelection:
63
case KListView::Single:
64
currentItem = listView()->selectedItem();
67
for( QListViewItemIterator it(listView(), QListViewItemIterator::Selected | QListViewItemIterator::Visible);
68
it.current() && !currentItem; ++it )
70
if( listView()->itemRect( it.current() ).isValid() )
71
currentItem = it.current();
75
if( keepParentsVisible() )
76
checkItemParentsVisible( listView()->firstChild() );
78
checkItemParentsNotVisible();
81
listView()->ensureItemVisible( currentItem );
82
//END code from KSearchLine::updateSearch
85
void SearchLine::checkItemParentsNotVisible()
87
//BEGIN code from KSearchLine::checkItemParentsNotVisible
88
QListViewItemIterator it( listView() );
89
for( ; it.current(); ++it )
91
QListViewItem *item = it.current();
92
if( itemMatches( item, search ) )
93
setItemVisible( item, true );
95
setItemVisible( item, false );
97
//END code from KSearchLine::checkItemParentsNotVisible
100
bool SearchLine::checkItemParentsVisible( QListViewItem *item )
102
//BEGIN code from KSearchLine::checkItemParentsVisible
103
bool visible = false;
104
for( ; item; item = item->nextSibling() ) {
105
if( ( item->firstChild() && checkItemParentsVisible( item->firstChild() ) ) ||
106
itemMatches( item, search ) )
108
setItemVisible( item, true );
109
// OUCH! this operation just became exponential-time.
110
// however, setting an item visible sets all its descendents
111
// visible too, which we definitely don't want.
112
// plus, in Kopete the nesting is never more than 2 deep,
113
// so this really just doubles the runtime, if that.
114
// this still can be done in O(n) time by a mark-set process,
115
// but that's overkill in our case.
116
checkItemParentsVisible( item->firstChild() );
120
setItemVisible( item, false );
123
//END code from KSearchLine::checkItemParentsVisible
126
void SearchLine::setItemVisible( QListViewItem *it, bool b )
128
if( Item *item = dynamic_cast<Item*>( it ) )
129
item->setSearchMatch( b );
134
} // namespace ListView
136
} // namespace Kopete
138
#include "kopetelistviewsearchline.moc"