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
)
28 : K3ListViewSearchLine( parent
, listView
), searchEmpty(true)
32 SearchLine::~SearchLine()
37 void SearchLine::updateSearch( const QString
&s
)
39 // we copy a huge chunk of code here simply in order to override
40 // the way items are shown/hidden. KSearchLine rudely
41 // calls setVisible() on items with no way to customize this behaviour.
43 //BEGIN code from KSearchLine::updateSearch
47 search
= s
.isNull() ? text() : s
;
48 searchEmpty
= search
.isEmpty();
50 // If there's a selected item that is visible, make sure that it's visible
51 // when the search changes too (assuming that it still matches).
53 Q3ListViewItem
*currentItem
= 0;
55 switch( listView()->selectionMode() )
57 case K3ListView::NoSelection
:
59 case K3ListView::Single
:
60 currentItem
= listView()->selectedItem();
63 for( Q3ListViewItemIterator
it(listView(), Q3ListViewItemIterator::Selected
| Q3ListViewItemIterator::Visible
);
64 it
.current() && !currentItem
; ++it
)
66 if( listView()->itemRect( it
.current() ).isValid() )
67 currentItem
= it
.current();
71 if( keepParentsVisible() )
72 checkItemParentsVisible( listView()->firstChild() );
74 checkItemParentsNotVisible();
77 listView()->ensureItemVisible( currentItem
);
78 //END code from KSearchLine::updateSearch
81 void SearchLine::checkItemParentsNotVisible()
83 //BEGIN code from KSearchLine::checkItemParentsNotVisible
84 Q3ListViewItemIterator
it( listView() );
85 for( ; it
.current(); ++it
)
87 Q3ListViewItem
*item
= it
.current();
88 if( itemMatches( item
, search
) )
89 setItemVisible( item
, true );
91 setItemVisible( item
, false );
93 //END code from KSearchLine::checkItemParentsNotVisible
96 bool SearchLine::checkItemParentsVisible( Q3ListViewItem
*item
)
98 //BEGIN code from KSearchLine::checkItemParentsVisible
100 for( ; item
; item
= item
->nextSibling() ) {
101 if( ( item
->firstChild() && checkItemParentsVisible( item
->firstChild() ) ) ||
102 itemMatches( item
, search
) )
104 setItemVisible( item
, true );
105 // OUCH! this operation just became exponential-time.
106 // however, setting an item visible sets all its descendents
107 // visible too, which we definitely don't want.
108 // plus, in Kopete the nesting is never more than 2 deep,
109 // so this really just doubles the runtime, if that.
110 // this still can be done in O(n) time by a mark-set process,
111 // but that's overkill in our case.
112 checkItemParentsVisible( item
->firstChild() );
116 setItemVisible( item
, false );
119 //END code from KSearchLine::checkItemParentsVisible
122 void SearchLine::setItemVisible( Q3ListViewItem
*it
, bool b
)
124 if( Item
*item
= dynamic_cast<Item
*>( it
) )
125 item
->setSearchMatch( b
, !searchEmpty
);
130 } // namespace ListView
132 } // namespace Kopete
134 #include "kopetelistviewsearchline.moc"