• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdeui
 

kdeui

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

kdeui

Skip menu "kdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeui

Skip menu "kdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeui by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |