• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kdeui

kiconviewsearchline.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (c) 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00026 #include "kiconviewsearchline.h"
00027 
00028 #include <qiconview.h>
00029 #include <klocale.h>
00030 #include <qtimer.h>
00031 #include <kdebug.h>
00032 
00033 #define DEFAULT_CASESENSITIVE false
00034 
00035 typedef QValueList <QIconViewItem *> QIconViewItemList;
00036 
00037 class KIconViewSearchLine::KIconViewSearchLinePrivate
00038 {
00039 public:
00040   KIconViewSearchLinePrivate() :
00041     iconView( 0 ),
00042     caseSensitive( DEFAULT_CASESENSITIVE ),
00043     activeSearch( false ),
00044     queuedSearches( 0 ) {}
00045 
00046   QIconView *iconView;
00047   bool caseSensitive;
00048   bool activeSearch;
00049   QString search;
00050   int queuedSearches;
00051   QIconViewItemList hiddenItems;
00052 };
00053 
00054 /******************************************************************************
00055  * Public Methods                                                             *
00056  *****************************************************************************/
00057 KIconViewSearchLine::KIconViewSearchLine( QWidget *parent,
00058                       QIconView *iconView,
00059                       const char *name ) :
00060   KLineEdit( parent, name )
00061 {
00062   d = NULL;
00063   init( iconView );
00064 }
00065 
00066 KIconViewSearchLine::KIconViewSearchLine( QWidget *parent, const char *name ) :
00067   KLineEdit( parent, name )
00068 {
00069   d = NULL;
00070   init( NULL );
00071 }
00072 
00073 KIconViewSearchLine::~KIconViewSearchLine()
00074 {
00075   clear(); // empty hiddenItems, returning items back to iconView
00076   delete d;
00077 }
00078 
00079 bool KIconViewSearchLine::caseSensitive() const
00080 {
00081   return d->caseSensitive;
00082 }
00083 
00084 QIconView *KIconViewSearchLine::iconView() const
00085 {
00086   return d->iconView;
00087 }
00088 
00089 /******************************************************************************
00090  * Public Slots                                                               *
00091  *****************************************************************************/
00092 void KIconViewSearchLine::updateSearch( const QString &s )
00093 {
00094   QIconView *iv = d->iconView;
00095   if( ! iv )
00096     return; // disabled
00097 
00098   QString search = d->search = s.isNull() ? text() : s;
00099 
00100   QIconViewItemList *hi = &(d->hiddenItems);
00101 
00102   QIconViewItem *currentItem = iv->currentItem();
00103 
00104   QIconViewItem *item = NULL;
00105 
00106   // Remove Non-Matching items, add them them to hidden list
00107   QIconViewItem *i = iv->firstItem();
00108   while ( i != NULL )
00109     {
00110       item = i;
00111       i = i->nextItem(); // Point to next, otherwise will loose it.
00112       if ( ! itemMatches( item, search ) )
00113     {
00114       hideItem( item );
00115 
00116       if ( item == currentItem )
00117         currentItem = NULL; // It's not in iconView anymore.
00118     }
00119     }
00120 
00121     // Add Matching items, remove from hidden list
00122     QIconViewItemList::iterator it = hi->begin();
00123     while ( it != hi->end() )
00124       {
00125     item = *it;
00126     ++it;
00127     if ( itemMatches( item, search ) )
00128       showItem( item );
00129       }
00130 
00131     iv->sort();
00132 
00133     if ( currentItem != NULL )
00134       iv->ensureItemVisible( currentItem );
00135 }
00136 
00137 void KIconViewSearchLine::clear()
00138 {
00139   // Clear hidden list, give items back to QIconView, if it still exists
00140   QIconViewItem *item = NULL;
00141   QIconViewItemList::iterator it = d->hiddenItems.begin();
00142   while ( it != d->hiddenItems.end() )
00143     {
00144       item = *it;
00145       ++it;
00146       if ( item != NULL )
00147     {
00148       if ( d->iconView != NULL )
00149         showItem( item );
00150       else
00151         delete item;
00152     }
00153     }
00154   if ( ! d->hiddenItems.isEmpty() )
00155     kdDebug() << __FILE__ << ":" << __LINE__ <<
00156       "hiddenItems is not empty as it should be. " <<
00157       d->hiddenItems.count() << " items are still there.\n" << endl;
00158 
00159   d->search = "";
00160   d->queuedSearches = 0;
00161   KLineEdit::clear();
00162 }
00163 
00164 void KIconViewSearchLine::setCaseSensitive( bool cs )
00165 {
00166   d->caseSensitive = cs;
00167 }
00168 
00169 void KIconViewSearchLine::setIconView( QIconView *iv )
00170 {
00171   if ( d->iconView != NULL )
00172     disconnect( d->iconView, SIGNAL( destroyed() ),
00173         this,        SLOT(   iconViewDeleted() ) );
00174 
00175   d->iconView = iv;
00176 
00177   if ( iv != NULL )
00178     {
00179       connect( d->iconView, SIGNAL( destroyed() ),
00180            this,        SLOT(   iconViewDeleted() ) );
00181       setEnabled( true );
00182     }
00183   else
00184     setEnabled( false );
00185 }
00186 
00187 /******************************************************************************
00188  * Protected Methods                                                          *
00189  *****************************************************************************/
00190 bool KIconViewSearchLine::itemMatches( const QIconViewItem *item,
00191                        const QString &s ) const
00192 {
00193   if ( s.isEmpty() )
00194     return true;
00195 
00196   if ( item == NULL )
00197     return false;
00198 
00199   return ( item->text().find( s, 0, caseSensitive() ) >= 0 );
00200 }
00201 
00202 void KIconViewSearchLine::init( QIconView *iconView )
00203 {
00204   delete d;
00205   d = new KIconViewSearchLinePrivate;
00206 
00207   d->iconView = iconView;
00208 
00209   connect( this, SIGNAL( textChanged( const QString & ) ),
00210        this, SLOT(   queueSearch( const QString & ) ) );
00211 
00212   if ( iconView != NULL )
00213     {
00214       connect( iconView, SIGNAL( destroyed() ),
00215            this,     SLOT(   iconViewDeleted() ) );
00216       setEnabled( true );
00217     }
00218   else
00219     setEnabled( false );
00220 }
00221 
00222 void KIconViewSearchLine::hideItem( QIconViewItem *item )
00223 {
00224   if ( ( item == NULL ) || ( d->iconView == NULL ) )
00225     return;
00226 
00227   d->hiddenItems.append( item );
00228   d->iconView->takeItem( item );
00229 }
00230 
00231 void KIconViewSearchLine::showItem( QIconViewItem *item )
00232 {
00233   if ( d->iconView == NULL )
00234     {
00235       kdDebug() << __FILE__ << ":" << __LINE__ <<
00236     "showItem() could not be called while there's no iconView set." <<
00237     endl;
00238       return;
00239     }
00240   d->iconView->insertItem( item );
00241   d->hiddenItems.remove( item );
00242 }
00243 
00244 /******************************************************************************
00245  * Protected Slots                                                            *
00246  *****************************************************************************/
00247 void KIconViewSearchLine::queueSearch( const QString &s )
00248 {
00249   d->queuedSearches++;
00250   d->search = s;
00251   QTimer::singleShot( 200, this, SLOT( activateSearch() ) );
00252 }
00253 
00254 void KIconViewSearchLine::activateSearch()
00255 {
00256   d->queuedSearches--;
00257 
00258   if ( d->queuedSearches <= 0 )
00259     {
00260       updateSearch( d->search );
00261       d->queuedSearches = 0;
00262     }
00263 }
00264 
00265 /******************************************************************************
00266  * Private Slots                                                              *
00267  *****************************************************************************/
00268 void KIconViewSearchLine::iconViewDeleted()
00269 {
00270   d->iconView = NULL;
00271   setEnabled( false );
00272 }
00273 
00274 #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

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal