1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* This file is part of the KDE libraries
Copyright (c) 2010 Timothy Pearson <kb9vqf@pearsoncomputing.net>
Copyright (c) 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
/**
* \todo
* Maybe we should have a common interface for SearchLines, this file
* is so close (it's actually based on) klistviewsearchline! Only few methods
* would be reimplemented.
*/
#include "kiconviewsearchline.h"
#include <tqiconview.h>
#include <klocale.h>
#include <tqtimer.h>
#include <kdebug.h>
#define DEFAULT_CASESENSITIVE false
typedef TQValueList <TQIconViewItem *> QIconViewItemList;
class KIconViewSearchLine::KIconViewSearchLinePrivate
{
public:
KIconViewSearchLinePrivate() :
iconView( 0 ),
caseSensitive( DEFAULT_CASESENSITIVE ),
activeSearch( false ),
hiddenListChanged( 0 ),
queuedSearches( 0 ) {}
TQIconView *iconView;
bool caseSensitive;
bool activeSearch;
TQString search;
int queuedSearches;
int hiddenListChanged;
QIconViewItemList hiddenItems;
};
/******************************************************************************
* Public Methods *
*****************************************************************************/
KIconViewSearchLine::KIconViewSearchLine( TQWidget *parent,
TQIconView *iconView,
const char *name ) :
KLineEdit( parent, name )
{
d = NULL;
init( iconView );
}
KIconViewSearchLine::KIconViewSearchLine( TQWidget *parent, const char *name ) :
KLineEdit( parent, name )
{
d = NULL;
init( NULL );
}
KIconViewSearchLine::~KIconViewSearchLine()
{
clear(); // empty hiddenItems, returning items back to iconView
delete d;
}
bool KIconViewSearchLine::caseSensitive() const
{
return d->caseSensitive;
}
TQIconView *KIconViewSearchLine::iconView() const
{
return d->iconView;
}
/******************************************************************************
* Public Slots *
*****************************************************************************/
void KIconViewSearchLine::updateSearch( const TQString &s )
{
long original_count;
int original_hiddenListChanged;
if( ! d->iconView )
return; // disabled
TQString search = d->search = s.isNull() ? text() : s;
TQIconViewItem *currentItem = d->iconView->currentItem();
TQIconViewItem *item = NULL;
// Remove Non-Matching items, add them them to hidden list
TQIconViewItem *i = d->iconView->firstItem();
while ( i != NULL ) {
item = i;
i = i->nextItem(); // Point to next, otherwise will loose it.
if ( ! itemMatches( item, search ) ) {
hideItem( item );
if ( item == currentItem )
currentItem = NULL; // It's not in iconView anymore.
}
}
// Add Matching items, remove from hidden list
original_count = d->hiddenItems.count();
original_hiddenListChanged = d->hiddenListChanged;
for (QIconViewItemList::iterator it=d->hiddenItems.begin();it!=d->hiddenItems.end();++it) {
item = *it;
if ((original_count != d->hiddenItems.count()) || (original_hiddenListChanged != d->hiddenListChanged)) {
// The list has changed; pointers are now most likely invalid
// ABORT, but restart the search at the beginning
original_count = d->hiddenItems.count();
original_hiddenListChanged = d->hiddenListChanged;
it=d->hiddenItems.begin();
}
else {
if ( itemMatches( item, search ) )
showItem( item );
}
}
d->iconView->sort();
if ( currentItem != NULL )
d->iconView->ensureItemVisible( currentItem );
}
void KIconViewSearchLine::clear()
{
// Clear hidden list, give items back to TQIconView, if it still exists
TQIconViewItem *item = NULL;
QIconViewItemList::iterator it = d->hiddenItems.begin();
while ( it != d->hiddenItems.end() )
{
item = *it;
++it;
if ( item != NULL )
{
if ( d->iconView != NULL )
showItem( item );
else
delete item;
}
}
if ( ! d->hiddenItems.isEmpty() )
kdDebug() << __FILE__ << ":" << __LINE__ <<
"hiddenItems is not empty as it should be. " <<
d->hiddenItems.count() << " items are still there.\n" << endl;
d->search = "";
d->queuedSearches = 0;
KLineEdit::clear();
}
void KIconViewSearchLine::iconDeleted(const TQString &filename) {
// Clear hidden list, give items back to TQIconView, if it still exists
TQIconViewItem *item = NULL;
QIconViewItemList::iterator it = d->hiddenItems.begin();
while ( it != d->hiddenItems.end() )
{
item = *it;
++it;
if ( item != NULL )
{
if (item->text() == filename) {
if (d->iconView != NULL)
showItem( item );
else
delete item;
}
}
}
}
void KIconViewSearchLine::setCaseSensitive( bool cs )
{
d->caseSensitive = cs;
}
void KIconViewSearchLine::setIconView( TQIconView *iv )
{
if ( d->iconView != NULL )
disconnect( d->iconView, TQT_SIGNAL( destroyed() ),
this, TQT_SLOT( iconViewDeleted() ) );
d->iconView = iv;
if ( iv != NULL )
{
connect( d->iconView, TQT_SIGNAL( destroyed() ),
this, TQT_SLOT( iconViewDeleted() ) );
setEnabled( true );
}
else
setEnabled( false );
}
/******************************************************************************
* Protected Methods *
*****************************************************************************/
bool KIconViewSearchLine::itemMatches( const TQIconViewItem *item,
const TQString &s ) const
{
if ( s.isEmpty() )
return true;
if ( item == NULL )
return false;
TQString itemtext = item->text();
return ( itemtext.find( s, 0, caseSensitive() ) >= 0 );
}
void KIconViewSearchLine::init( TQIconView *iconView )
{
delete d;
d = new KIconViewSearchLinePrivate;
d->iconView = iconView;
connect( this, TQT_SIGNAL( textChanged( const TQString & ) ),
this, TQT_SLOT( queueSearch( const TQString & ) ) );
if ( iconView != NULL )
{
connect( iconView, TQT_SIGNAL( destroyed() ),
this, TQT_SLOT( iconViewDeleted() ) );
setEnabled( true );
}
else
setEnabled( false );
}
void KIconViewSearchLine::hideItem( TQIconViewItem *item )
{
if ( ( item == NULL ) || ( d->iconView == NULL ) )
return;
d->hiddenListChanged++;
d->hiddenItems.append( item );
d->iconView->takeItem( item );
}
void KIconViewSearchLine::showItem( TQIconViewItem *item )
{
if ( d->iconView == NULL )
{
kdDebug() << __FILE__ << ":" << __LINE__ <<
"showItem() could not be called while there's no iconView set." <<
endl;
return;
}
d->hiddenListChanged++;
d->iconView->insertItem( item );
d->hiddenItems.remove( item );
item->setText(item->text());
}
/******************************************************************************
* Protected Slots *
*****************************************************************************/
void KIconViewSearchLine::queueSearch( const TQString &s )
{
d->queuedSearches++;
d->search = s;
TQTimer::singleShot( 200, this, TQT_SLOT( activateSearch() ) );
}
void KIconViewSearchLine::activateSearch()
{
d->queuedSearches--;
if ( d->queuedSearches <= 0 )
{
updateSearch( d->search );
d->queuedSearches = 0;
}
else {
TQTimer::singleShot( 200, this, TQT_SLOT( activateSearch() ) );
}
}
/******************************************************************************
* Private Slots *
*****************************************************************************/
void KIconViewSearchLine::iconViewDeleted()
{
d->iconView = NULL;
setEnabled( false );
}
#include "kiconviewsearchline.moc"
|