00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #include "kiconviewsearchline.h"
00028
00029 #include <tqiconview.h>
00030 #include <tdelocale.h>
00031 #include <tqtimer.h>
00032 #include <kdebug.h>
00033
00034 #define DEFAULT_CASESENSITIVE false
00035
00036 typedef TQValueList <TQIconViewItem *> QIconViewItemList;
00037
00038 class TDEIconViewSearchLine::TDEIconViewSearchLinePrivate
00039 {
00040 public:
00041 TDEIconViewSearchLinePrivate() :
00042 iconView( 0 ),
00043 caseSensitive( DEFAULT_CASESENSITIVE ),
00044 activeSearch( false ),
00045 queuedSearches( 0 ) {}
00046
00047 TQIconView *iconView;
00048 bool caseSensitive;
00049 bool activeSearch;
00050 TQString search;
00051 int queuedSearches;
00052 };
00053
00054
00055
00056
00057 TDEIconViewSearchLine::TDEIconViewSearchLine( TQWidget *parent,
00058 TQIconView *iconView,
00059 const char *name ) :
00060 KLineEdit( parent, name )
00061 {
00062 d = NULL;
00063 init( iconView );
00064 }
00065
00066 TDEIconViewSearchLine::TDEIconViewSearchLine( TQWidget *parent, const char *name ) :
00067 KLineEdit( parent, name )
00068 {
00069 d = NULL;
00070 init( NULL );
00071 }
00072
00073 TDEIconViewSearchLine::~TDEIconViewSearchLine()
00074 {
00075 clear();
00076 delete d;
00077 }
00078
00079 bool TDEIconViewSearchLine::caseSensitive() const
00080 {
00081 return d->caseSensitive;
00082 }
00083
00084 TQIconView *TDEIconViewSearchLine::iconView() const
00085 {
00086 return d->iconView;
00087 }
00088
00089
00090
00091
00092 void TDEIconViewSearchLine::updateSearch( const TQString &s )
00093 {
00094 if( ! d->iconView )
00095 return;
00096
00097 d->search = s.isNull() ? text() : s;
00098 TQIconViewItem *currentItem = d->iconView->currentItem();
00099 TQIconViewItem *item = NULL;
00100
00101
00102 TQIconViewItem *i = d->iconView->firstItem();
00103 while ( i != NULL ) {
00104 item = i;
00105 i = i->nextItem();
00106 if ( ! itemMatches( item, d->search ) ) {
00107 hideItem( item );
00108
00109 if ( item == currentItem )
00110 currentItem = NULL;
00111 }
00112 else {
00113 showItem( item );
00114 }
00115 }
00116
00117 d->iconView->sort();
00118
00119 if ( currentItem != NULL )
00120 d->iconView->ensureItemVisible( currentItem );
00121 }
00122
00123 void TDEIconViewSearchLine::clear()
00124 {
00125 if( ! d->iconView )
00126 return;
00127
00128
00129 TQIconViewItem *item = NULL;
00130
00131 TQIconViewItem *i = d->iconView->firstItem();
00132 while ( i != NULL ) {
00133 item = i;
00134 i = i->nextItem();
00135 showItem( item );
00136 }
00137
00138 d->search = "";
00139 d->queuedSearches = 0;
00140 KLineEdit::clear();
00141 }
00142
00143 void TDEIconViewSearchLine::iconDeleted(const TQString &filename) {
00144
00145 }
00146
00147 void TDEIconViewSearchLine::setCaseSensitive( bool cs )
00148 {
00149 d->caseSensitive = cs;
00150 }
00151
00152 void TDEIconViewSearchLine::setIconView( TQIconView *iv )
00153 {
00154 if ( d->iconView != NULL )
00155 disconnect( d->iconView, TQT_SIGNAL( destroyed() ),
00156 this, TQT_SLOT( iconViewDeleted() ) );
00157
00158 d->iconView = iv;
00159
00160 if ( iv != NULL )
00161 {
00162 connect( d->iconView, TQT_SIGNAL( destroyed() ),
00163 this, TQT_SLOT( iconViewDeleted() ) );
00164 setEnabled( true );
00165 }
00166 else
00167 setEnabled( false );
00168 }
00169
00170
00171
00172
00173 bool TDEIconViewSearchLine::itemMatches( const TQIconViewItem *item,
00174 const TQString &s ) const
00175 {
00176 if ( s.isEmpty() )
00177 return true;
00178
00179 if ( item == NULL )
00180 return false;
00181
00182 TQString itemtext = item->text();
00183 return ( itemtext.find( s, 0, caseSensitive() ) >= 0 );
00184 }
00185
00186 void TDEIconViewSearchLine::init( TQIconView *iconView )
00187 {
00188 delete d;
00189 d = new TDEIconViewSearchLinePrivate;
00190
00191 d->iconView = iconView;
00192
00193 connect( this, TQT_SIGNAL( textChanged( const TQString & ) ),
00194 this, TQT_SLOT( queueSearch( const TQString & ) ) );
00195
00196 if ( iconView != NULL )
00197 {
00198 connect( iconView, TQT_SIGNAL( destroyed() ),
00199 this, TQT_SLOT( iconViewDeleted() ) );
00200 setEnabled( true );
00201 }
00202 else
00203 setEnabled( false );
00204 }
00205
00206 void TDEIconViewSearchLine::hideItem( TQIconViewItem *item )
00207 {
00208 if ( ( item == NULL ) || ( d->iconView == NULL ) )
00209 return;
00210
00211 item->setVisible(false);
00212 }
00213
00214 void TDEIconViewSearchLine::showItem( TQIconViewItem *item )
00215 {
00216 if ( d->iconView == NULL )
00217 {
00218 kdDebug() << __FILE__ << ":" << __LINE__ <<
00219 "showItem() could not be called while there's no iconView set." <<
00220 endl;
00221 return;
00222 }
00223
00224 item->setVisible(true);
00225 }
00226
00227
00228
00229
00230 void TDEIconViewSearchLine::queueSearch( const TQString &s )
00231 {
00232 d->queuedSearches++;
00233 d->search = s;
00234 TQTimer::singleShot( 200, this, TQT_SLOT( activateSearch() ) );
00235 }
00236
00237 void TDEIconViewSearchLine::activateSearch()
00238 {
00239 d->queuedSearches--;
00240
00241 if ( d->queuedSearches <= 0 )
00242 {
00243 updateSearch( d->search );
00244 d->queuedSearches = 0;
00245 }
00246 else {
00247 TQTimer::singleShot( 200, this, TQT_SLOT( activateSearch() ) );
00248 }
00249 }
00250
00251
00252
00253
00254 void TDEIconViewSearchLine::iconViewDeleted()
00255 {
00256 d->iconView = NULL;
00257 setEnabled( false );
00258 }
00259
00260 #include "kiconviewsearchline.moc"