• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • kdelibs
  • KDE Home
  • Contact Us
 

KDEUI

klistwidgetsearchline.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002   Copyright (c) 2003 Scott Wheeler <wheeler@kde.org>
00003   Copyright (c) 2004 Gustavo Sverzut Barbieri <gsbarbieri@users.sourceforge.net>
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Library General Public
00007   License version 2 as published by the Free Software Foundation.
00008 
00009   This library is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public License
00015   along with this library; see the file COPYING.LIB.  If not, write to
00016   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017   Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "klistwidgetsearchline.h"
00021 
00022 #include <QtGui/QListWidget>
00023 #include <QtGui/QApplication>
00024 #include <QtGui/QKeyEvent>
00025 #include <QtCore/QEvent>
00026 
00027 #include <QtCore/QTimer>
00028 #include <kdebug.h>
00029 
00030 #define DEFAULT_CASESENSITIVE Qt::CaseInsensitive
00031 
00032 class KListWidgetSearchLine::KListWidgetSearchLinePrivate
00033 {
00034 public:
00035     KListWidgetSearchLinePrivate(KListWidgetSearchLine *parent) :
00036             q( parent ),
00037             listWidget( 0 ),
00038             caseSensitivity( DEFAULT_CASESENSITIVE ),
00039             activeSearch( false ),
00040             queuedSearches( 0 )
00041     {}
00042 
00043     void _k_listWidgetDeleted();
00044     void _k_queueSearch(const QString&);
00045     void _k_activateSearch();
00046     void _k_rowsInserted(const QModelIndex&, int, int);
00047     void _k_dataChanged(const QModelIndex&, const QModelIndex&);
00048 
00049     void init( QListWidget *listWidget = 0 );
00050     void updateHiddenState( int start, int end );
00051 
00052     KListWidgetSearchLine *q;
00053     QListWidget *listWidget;
00054     Qt::CaseSensitivity caseSensitivity;
00055     bool activeSearch;
00056     QString search;
00057     int queuedSearches;
00058 };
00059 
00060 /******************************************************************************
00061  * Public Methods                                                             *
00062  *****************************************************************************/
00063 KListWidgetSearchLine::KListWidgetSearchLine( QWidget *parent, QListWidget *listWidget ) :
00064         KLineEdit( parent ),
00065         d( new KListWidgetSearchLinePrivate(this) )
00066 
00067 {
00068     d->init( listWidget );
00069 }
00070 
00071 KListWidgetSearchLine::~KListWidgetSearchLine()
00072 {
00073     clear(); // returning items back to listWidget
00074     delete d;
00075 }
00076 
00077 Qt::CaseSensitivity KListWidgetSearchLine::caseSensitive() const
00078 {
00079     return d->caseSensitivity;
00080 }
00081 
00082 QListWidget *KListWidgetSearchLine::listWidget() const
00083 {
00084     return d->listWidget;
00085 }
00086 
00087 /******************************************************************************
00088  * Public Slots                                                               *
00089  *****************************************************************************/
00090 void KListWidgetSearchLine::updateSearch( const QString &s )
00091 {
00092     d->search = s.isNull() ? text() : s;
00093     if( d->listWidget ) {
00094         d->updateHiddenState( 0, d->listWidget->count() - 1 );
00095     }
00096 }
00097 
00098 void KListWidgetSearchLine::clear()
00099 {
00100     // Show items back to QListWidget
00101     if ( d->listWidget != 0 ) {
00102         for (int i = 0 ; i < d->listWidget->count(); ++i) {
00103             d->listWidget->item( i )->setHidden( false );
00104         }
00105     }
00106 
00107     d->search = "";
00108     d->queuedSearches = 0;
00109     KLineEdit::clear();
00110 }
00111 
00112 void KListWidgetSearchLine::setCaseSensitivity( Qt::CaseSensitivity cs )
00113 {
00114     d->caseSensitivity = cs;
00115 }
00116 
00117 void KListWidgetSearchLine::setListWidget( QListWidget *lw )
00118 {
00119     if ( d->listWidget != 0 ) {
00120         disconnect( d->listWidget, SIGNAL( destroyed() ),
00121                     this, SLOT( _k_listWidgetDeleted() ) );
00122         d->listWidget->model()->disconnect( this );
00123     }
00124 
00125     d->listWidget = lw;
00126 
00127     if ( lw != 0 ) {
00128         connect( d->listWidget, SIGNAL( destroyed() ),
00129                  this, SLOT( _k_listWidgetDeleted() ) );
00130         connect( d->listWidget->model(), SIGNAL( rowsInserted( const QModelIndex &, int, int ) ),
00131                  this, SLOT( _k_rowsInserted( const QModelIndex &, int, int ) ) );
00132         connect( d->listWidget->model(), SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
00133                  this, SLOT( _k_dataChanged( const QModelIndex &, const QModelIndex & ) ) );
00134         setEnabled( true );
00135     } else
00136         setEnabled( false );
00137 }
00138 
00139 /******************************************************************************
00140  * Protected Methods                                                          *
00141  *****************************************************************************/
00142 bool KListWidgetSearchLine::itemMatches( const QListWidgetItem *item,
00143         const QString &s ) const
00144 {
00145     if ( s.isEmpty() )
00146         return true;
00147 
00148     if ( item == 0 )
00149         return false;
00150 
00151     return ( item->text().indexOf( s, 0,
00152                                    caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive ) >= 0 );
00153 }
00154 
00155 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::init( QListWidget *_listWidget )
00156 {
00157     listWidget = _listWidget;
00158 
00159     connect( q, SIGNAL( textChanged( const QString & ) ),
00160              q, SLOT( _k_queueSearch( const QString & ) ) );
00161 
00162     if ( listWidget != 0 ) {
00163         connect( listWidget, SIGNAL( destroyed() ),
00164                  q, SLOT( _k_listWidgetDeleted() ) );
00165         connect( listWidget->model(), SIGNAL( rowsInserted( const QModelIndex &, int, int ) ),
00166                  q, SLOT( _k_rowsInserted( const QModelIndex &, int, int ) ) );
00167         connect( listWidget->model(), SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
00168                  q, SLOT( _k_dataChanged( const QModelIndex &, const QModelIndex & ) ) );
00169         q->setEnabled( true );
00170     } else {
00171         q->setEnabled( false );
00172     }
00173 
00174     q->setClearButtonShown(true);
00175 }
00176 
00177 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::updateHiddenState( int start, int end ) {
00178     if ( !listWidget ) {
00179         return;
00180     }
00181 
00182     QListWidgetItem *currentItem = listWidget->currentItem();
00183 
00184     // Remove Non-Matching items
00185     for( int index = start; index <= end; ++index ) {
00186         QListWidgetItem *item = listWidget->item(index);
00187         if ( ! q->itemMatches( item, search ) ) {
00188             item->setHidden( true );
00189 
00190             if ( item == currentItem ) {
00191                 currentItem = 0; // It's not in listWidget anymore.
00192             }
00193         } else if ( item->isHidden() ) {
00194             item->setHidden( false );
00195         }
00196     }
00197 
00198     if ( listWidget->isSortingEnabled() ) {
00199         listWidget->sortItems();
00200     }
00201 
00202     if ( currentItem != 0 ) {
00203         listWidget->scrollToItem( currentItem );
00204     }
00205 }
00206 
00207 bool KListWidgetSearchLine::event(QEvent *event) {
00208 
00209     if (event->type() == QEvent::KeyPress) {
00210         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
00211         if(keyEvent->matches(QKeySequence::MoveToNextLine) || keyEvent->matches(QKeySequence::SelectNextLine) ||
00212            keyEvent->matches(QKeySequence::MoveToPreviousLine) || keyEvent->matches(QKeySequence::SelectPreviousLine) ||
00213            keyEvent->matches(QKeySequence::MoveToNextPage) ||  keyEvent->matches(QKeySequence::SelectNextPage) ||
00214            keyEvent->matches(QKeySequence::MoveToPreviousPage) ||  keyEvent->matches(QKeySequence::SelectPreviousPage) ||
00215            keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
00216         {
00217             if(d->listWidget) {
00218                 QApplication::sendEvent(d->listWidget, event);
00219                 return true;
00220             }
00221         }
00222     }
00223     return KLineEdit::event(event);
00224 }
00225 /******************************************************************************
00226  * Protected Slots                                                            *
00227  *****************************************************************************/
00228 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_queueSearch( const QString &s )
00229 {
00230     queuedSearches++;
00231     search = s;
00232     QTimer::singleShot( 200, q, SLOT( _k_activateSearch() ) );
00233 }
00234 
00235 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_activateSearch()
00236 {
00237     queuedSearches--;
00238 
00239     if ( queuedSearches <= 0 ) {
00240         q->updateSearch( search );
00241         queuedSearches = 0;
00242     }
00243 }
00244 
00245 /******************************************************************************
00246  * Private Slots                                                              *
00247  *****************************************************************************/
00248 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_listWidgetDeleted()
00249 {
00250     listWidget = 0;
00251     q->setEnabled( false );
00252 }
00253 
00254 
00255 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_rowsInserted( const QModelIndex &parent, int start, int end )
00256 {
00257     if( parent.isValid() ) {
00258         return;
00259     }
00260 
00261     updateHiddenState( start, end );
00262 }
00263 
00264 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_dataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
00265 {
00266     if( topLeft.parent().isValid() ) {
00267         return;
00268     }
00269 
00270     updateHiddenState( topLeft.row(), bottomRight.row() );
00271 }
00272 
00273 
00274 
00275 #include "klistwidgetsearchline.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.7.5
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