~ubuntu-branches/ubuntu/edgy/kdenetwork/edgy

« back to all changes in this revision

Viewing changes to kopete/libkopete/ui/kopetelistviewsearchline.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-12-06 12:28:33 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20051206122833-y01rb4gt2emnvz39
Tags: 4:3.5.0-0ubuntu1
* New upstream release
* Remove GCC 3.4 on hppa

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
4
 
 
5
    Copyright (c) 2004      by Richard Smith <kde@metafoo.co.uk>
 
6
 
 
7
    Kopete    (c) 2004      by the Kopete developers <kopete-devel@kde.org>
 
8
 
 
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
    *************************************************************************
 
17
*/
 
18
 
 
19
#include "kopetelistviewsearchline.h"
 
20
#include "kopetelistviewitem.h"
 
21
#include "kopetelistview.h"
 
22
 
 
23
namespace Kopete {
 
24
namespace UI {
 
25
namespace ListView {
 
26
 
 
27
SearchLine::SearchLine( QWidget *parent, ListView *listView, const char *name )
 
28
        : KListViewSearchLine( parent, listView, name )
 
29
{
 
30
}
 
31
 
 
32
SearchLine::SearchLine(QWidget *parent, const char *name)
 
33
        : KListViewSearchLine( parent, 0, name )
 
34
{
 
35
}
 
36
 
 
37
SearchLine::~SearchLine()
 
38
{
 
39
}
 
40
 
 
41
 
 
42
void SearchLine::updateSearch( const QString &s )
 
43
{
 
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.
 
47
        
 
48
        //BEGIN code from KSearchLine::updateSearch
 
49
                if( !listView() )
 
50
                return;
 
51
        
 
52
        search = s.isNull() ? text() : s;
 
53
        
 
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).
 
56
        
 
57
        QListViewItem *currentItem = 0;
 
58
        
 
59
        switch( listView()->selectionMode() )
 
60
        {
 
61
        case KListView::NoSelection:
 
62
                break;
 
63
        case KListView::Single:
 
64
                currentItem = listView()->selectedItem();
 
65
                break;
 
66
        default:
 
67
                for( QListViewItemIterator it(listView(), QListViewItemIterator::Selected | QListViewItemIterator::Visible);
 
68
                     it.current() && !currentItem; ++it )
 
69
                {
 
70
                        if( listView()->itemRect( it.current() ).isValid() )
 
71
                                currentItem = it.current();
 
72
                }
 
73
        }
 
74
        
 
75
        if( keepParentsVisible() )
 
76
                checkItemParentsVisible( listView()->firstChild() );
 
77
        else
 
78
                checkItemParentsNotVisible();
 
79
        
 
80
        if( currentItem )
 
81
                listView()->ensureItemVisible( currentItem );
 
82
        //END code from KSearchLine::updateSearch
 
83
}
 
84
 
 
85
void SearchLine::checkItemParentsNotVisible()
 
86
{
 
87
        //BEGIN code from KSearchLine::checkItemParentsNotVisible
 
88
        QListViewItemIterator it( listView() );
 
89
        for( ; it.current(); ++it )
 
90
        {
 
91
                QListViewItem *item = it.current();
 
92
                if( itemMatches( item, search ) )
 
93
                        setItemVisible( item, true );
 
94
                else
 
95
                        setItemVisible( item, false );
 
96
        }
 
97
        //END code from KSearchLine::checkItemParentsNotVisible
 
98
}
 
99
 
 
100
bool SearchLine::checkItemParentsVisible( QListViewItem *item )
 
101
{
 
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 ) )
 
107
                {
 
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() );
 
117
                        visible = true;
 
118
                }
 
119
                else
 
120
                        setItemVisible( item, false );
 
121
        }
 
122
        return visible;
 
123
        //END code from KSearchLine::checkItemParentsVisible
 
124
}
 
125
 
 
126
void SearchLine::setItemVisible( QListViewItem *it, bool b )
 
127
{
 
128
        if( Item *item = dynamic_cast<Item*>( it ) )
 
129
                item->setSearchMatch( b );
 
130
        else
 
131
                it->setVisible( b );
 
132
}
 
133
 
 
134
} // namespace ListView
 
135
} // namespace UI
 
136
} // namespace Kopete
 
137
 
 
138
#include "kopetelistviewsearchline.moc"