KDE3Support
k3iconviewsearchline.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 "k3iconviewsearchline.h" 00027 00028 #include <Qt3Support/Q3IconDrag> 00029 #include <klocale.h> 00030 #include <QtCore/QTimer> 00031 #include <kdebug.h> 00032 00033 #define DEFAULT_CASESENSITIVE false 00034 00035 typedef QList <Q3IconViewItem *> QIconViewItemList; 00036 00037 class K3IconViewSearchLine::K3IconViewSearchLinePrivate 00038 { 00039 public: 00040 K3IconViewSearchLinePrivate() : 00041 iconView( 0 ), 00042 caseSensitive( DEFAULT_CASESENSITIVE ), 00043 activeSearch( false ), 00044 queuedSearches( 0 ) {} 00045 00046 Q3IconView *iconView; 00047 bool caseSensitive; 00048 bool activeSearch; 00049 QString search; 00050 int queuedSearches; 00051 QIconViewItemList hiddenItems; 00052 }; 00053 00054 /****************************************************************************** 00055 * Public Methods * 00056 *****************************************************************************/ 00057 K3IconViewSearchLine::K3IconViewSearchLine( QWidget *parent, 00058 Q3IconView *iconView ) : 00059 KLineEdit( parent ) 00060 { 00061 d = NULL; 00062 init( iconView ); 00063 } 00064 00065 K3IconViewSearchLine::~K3IconViewSearchLine() 00066 { 00067 clear(); // empty hiddenItems, returning items back to iconView 00068 delete d; 00069 } 00070 00071 bool K3IconViewSearchLine::caseSensitive() const 00072 { 00073 return d->caseSensitive; 00074 } 00075 00076 Q3IconView *K3IconViewSearchLine::iconView() const 00077 { 00078 return d->iconView; 00079 } 00080 00081 /****************************************************************************** 00082 * Public Slots * 00083 *****************************************************************************/ 00084 void K3IconViewSearchLine::updateSearch( const QString &s ) 00085 { 00086 Q3IconView *iv = d->iconView; 00087 if( ! iv ) 00088 return; // disabled 00089 00090 QString search = d->search = s.isNull() ? text() : s; 00091 00092 QIconViewItemList *hi = &(d->hiddenItems); 00093 00094 Q3IconViewItem *currentItem = iv->currentItem(); 00095 00096 Q3IconViewItem *item = NULL; 00097 00098 // Remove Non-Matching items, add them them to hidden list 00099 Q3IconViewItem *i = iv->firstItem(); 00100 while ( i != NULL ) 00101 { 00102 item = i; 00103 i = i->nextItem(); // Point to next, otherwise will loose it. 00104 if ( ! itemMatches( item, search ) ) 00105 { 00106 hideItem( item ); 00107 00108 if ( item == currentItem ) 00109 currentItem = NULL; // It's not in iconView anymore. 00110 } 00111 } 00112 00113 // Add Matching items, remove from hidden list 00114 QIconViewItemList::iterator it = hi->begin(); 00115 while ( it != hi->end() ) 00116 { 00117 item = *it; 00118 ++it; 00119 if ( itemMatches( item, search ) ) 00120 showItem( item ); 00121 } 00122 00123 iv->sort(); 00124 00125 if ( currentItem != NULL ) 00126 iv->ensureItemVisible( currentItem ); 00127 } 00128 00129 void K3IconViewSearchLine::clear() 00130 { 00131 // Clear hidden list, give items back to QIconView, if it still exists 00132 Q3IconViewItem *item = NULL; 00133 QIconViewItemList::iterator it = d->hiddenItems.begin(); 00134 while ( it != d->hiddenItems.end() ) 00135 { 00136 item = *it; 00137 ++it; 00138 if ( item != NULL ) 00139 { 00140 if ( d->iconView != NULL ) 00141 showItem( item ); 00142 else 00143 delete item; 00144 } 00145 } 00146 if ( ! d->hiddenItems.isEmpty() ) 00147 kDebug() << __FILE__ << ":" << __LINE__ << 00148 "hiddenItems is not empty as it should be. " << 00149 d->hiddenItems.count() << " items are still there.\n" << endl; 00150 00151 d->search = ""; 00152 d->queuedSearches = 0; 00153 KLineEdit::clear(); 00154 } 00155 00156 void K3IconViewSearchLine::setCaseSensitive( bool cs ) 00157 { 00158 d->caseSensitive = cs; 00159 } 00160 00161 void K3IconViewSearchLine::setIconView( Q3IconView *iv ) 00162 { 00163 if ( d->iconView != NULL ) 00164 disconnect( d->iconView, SIGNAL( destroyed() ), 00165 this, SLOT( iconViewDeleted() ) ); 00166 00167 d->iconView = iv; 00168 00169 if ( iv != NULL ) 00170 { 00171 connect( d->iconView, SIGNAL( destroyed() ), 00172 this, SLOT( iconViewDeleted() ) ); 00173 setEnabled( true ); 00174 } 00175 else 00176 setEnabled( false ); 00177 } 00178 00179 /****************************************************************************** 00180 * Protected Methods * 00181 *****************************************************************************/ 00182 bool K3IconViewSearchLine::itemMatches( const Q3IconViewItem *item, 00183 const QString &s ) const 00184 { 00185 if ( s.isEmpty() ) 00186 return true; 00187 00188 if ( item == NULL ) 00189 return false; 00190 00191 return ( item->text().indexOf( s, 0, 00192 caseSensitive()?Qt::CaseSensitive:Qt::CaseInsensitive ) >= 0 ); 00193 } 00194 00195 void K3IconViewSearchLine::init( Q3IconView *iconView ) 00196 { 00197 delete d; 00198 d = new K3IconViewSearchLinePrivate; 00199 00200 d->iconView = iconView; 00201 00202 connect( this, SIGNAL( textChanged( const QString & ) ), 00203 this, SLOT( queueSearch( const QString & ) ) ); 00204 00205 if ( iconView != NULL ) 00206 { 00207 connect( iconView, SIGNAL( destroyed() ), 00208 this, SLOT( iconViewDeleted() ) ); 00209 setEnabled( true ); 00210 } else { 00211 setEnabled( false ); 00212 } 00213 00214 setClearButtonShown(true); 00215 } 00216 00217 void K3IconViewSearchLine::hideItem( Q3IconViewItem *item ) 00218 { 00219 if ( ( item == NULL ) || ( d->iconView == NULL ) ) 00220 return; 00221 00222 d->hiddenItems.append( item ); 00223 d->iconView->takeItem( item ); 00224 } 00225 00226 void K3IconViewSearchLine::showItem( Q3IconViewItem *item ) 00227 { 00228 if ( d->iconView == NULL ) 00229 { 00230 kDebug() << __FILE__ << ":" << __LINE__ << 00231 "showItem() could not be called while there's no iconView set." << 00232 endl; 00233 return; 00234 } 00235 d->iconView->insertItem( item ); 00236 d->hiddenItems.removeAll( item ); 00237 } 00238 00239 /****************************************************************************** 00240 * Protected Slots * 00241 *****************************************************************************/ 00242 void K3IconViewSearchLine::queueSearch( const QString &s ) 00243 { 00244 d->queuedSearches++; 00245 d->search = s; 00246 QTimer::singleShot( 200, this, SLOT( activateSearch() ) ); 00247 } 00248 00249 void K3IconViewSearchLine::activateSearch() 00250 { 00251 d->queuedSearches--; 00252 00253 if ( d->queuedSearches <= 0 ) 00254 { 00255 updateSearch( d->search ); 00256 d->queuedSearches = 0; 00257 } 00258 } 00259 00260 /****************************************************************************** 00261 * Private Slots * 00262 *****************************************************************************/ 00263 void K3IconViewSearchLine::iconViewDeleted() 00264 { 00265 d->iconView = NULL; 00266 setEnabled( false ); 00267 } 00268 00269 #include "k3iconviewsearchline.moc"
KDE 4.6 API Reference