Show invite menu in wlm chat window immediately
[kdenetwork.git] / kopete / libkopete / ui / kopetelistviewsearchline.cpp
blob43bc4025e767c432b8d4b538903e19ce282de943
1 /*
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 *************************************************************************
10 * *
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. *
15 * *
16 *************************************************************************
19 #include "kopetelistviewsearchline.h"
20 #include "kopetelistviewitem.h"
21 #include "kopetelistview.h"
23 namespace Kopete {
24 namespace UI {
25 namespace ListView {
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
44 if( !listView() )
45 return;
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:
58 break;
59 case K3ListView::Single:
60 currentItem = listView()->selectedItem();
61 break;
62 default:
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() );
73 else
74 checkItemParentsNotVisible();
76 if( currentItem )
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 );
90 else
91 setItemVisible( item, false );
93 //END code from KSearchLine::checkItemParentsNotVisible
96 bool SearchLine::checkItemParentsVisible( Q3ListViewItem *item )
98 //BEGIN code from KSearchLine::checkItemParentsVisible
99 bool visible = false;
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() );
113 visible = true;
115 else
116 setItemVisible( item, false );
118 return visible;
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 );
126 else
127 it->setVisible( b );
130 } // namespace ListView
131 } // namespace UI
132 } // namespace Kopete
134 #include "kopetelistviewsearchline.moc"