• Skip to content
  • Skip to link menu
KDE 4.6 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 <klocale.h>
00028 #include <QtCore/QTimer>
00029 #include <kdebug.h>
00030 
00031 #define DEFAULT_CASESENSITIVE Qt::CaseInsensitive
00032 
00033 class KListWidgetSearchLine::KListWidgetSearchLinePrivate
00034 {
00035 public:
00036     KListWidgetSearchLinePrivate(KListWidgetSearchLine *parent) :
00037             q( parent ),
00038             listWidget( 0 ),
00039             caseSensitivity( DEFAULT_CASESENSITIVE ),
00040             activeSearch( false ),
00041             queuedSearches( 0 )
00042     {}
00043 
00044     void _k_listWidgetDeleted();
00045     void _k_queueSearch(const QString&);
00046     void _k_activateSearch();
00047     void _k_rowsInserted(const QModelIndex&, int, int);
00048     void _k_dataChanged(const QModelIndex&, const QModelIndex&);
00049 
00050     void init( QListWidget *listWidget = 0 );
00051     void updateHiddenState( int start, int end );
00052 
00053     KListWidgetSearchLine *q;
00054     QListWidget *listWidget;
00055     Qt::CaseSensitivity caseSensitivity;
00056     bool activeSearch;
00057     QString search;
00058     int queuedSearches;
00059 };
00060 
00061 /******************************************************************************
00062  * Public Methods                                                             *
00063  *****************************************************************************/
00064 KListWidgetSearchLine::KListWidgetSearchLine( QWidget *parent, QListWidget *listWidget ) :
00065         KLineEdit( parent ),
00066         d( new KListWidgetSearchLinePrivate(this) )
00067 
00068 {
00069     d->init( listWidget );
00070 }
00071 
00072 KListWidgetSearchLine::~KListWidgetSearchLine()
00073 {
00074     clear(); // returning items back to listWidget
00075     delete d;
00076 }
00077 
00078 Qt::CaseSensitivity KListWidgetSearchLine::caseSensitive() const
00079 {
00080     return d->caseSensitivity;
00081 }
00082 
00083 QListWidget *KListWidgetSearchLine::listWidget() const
00084 {
00085     return d->listWidget;
00086 }
00087 
00088 /******************************************************************************
00089  * Public Slots                                                               *
00090  *****************************************************************************/
00091 void KListWidgetSearchLine::updateSearch( const QString &s )
00092 {
00093     d->search = s.isNull() ? text() : s;
00094     if( d->listWidget ) {
00095         d->updateHiddenState( 0, d->listWidget->count() - 1 );
00096     }
00097 }
00098 
00099 void KListWidgetSearchLine::clear()
00100 {
00101     // Show items back to QListWidget
00102     if ( d->listWidget != 0 ) {
00103         for (int i = 0 ; i < d->listWidget->count(); ++i) {
00104             d->listWidget->item( i )->setHidden( false );
00105         }
00106     }
00107 
00108     d->search = "";
00109     d->queuedSearches = 0;
00110     KLineEdit::clear();
00111 }
00112 
00113 void KListWidgetSearchLine::setCaseSensitivity( Qt::CaseSensitivity cs )
00114 {
00115     d->caseSensitivity = cs;
00116 }
00117 
00118 void KListWidgetSearchLine::setListWidget( QListWidget *lw )
00119 {
00120     if ( d->listWidget != 0 ) {
00121         disconnect( d->listWidget, SIGNAL( destroyed() ),
00122                     this, SLOT( _k_listWidgetDeleted() ) );
00123         d->listWidget->model()->disconnect( this );
00124     }
00125 
00126     d->listWidget = lw;
00127 
00128     if ( lw != 0 ) {
00129         connect( d->listWidget, SIGNAL( destroyed() ),
00130                  this, SLOT( _k_listWidgetDeleted() ) );
00131         connect( d->listWidget->model(), SIGNAL( rowsInserted( const QModelIndex &, int, int ) ),
00132                  this, SLOT( _k_rowsInserted( const QModelIndex &, int, int ) ) );
00133         connect( d->listWidget->model(), SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
00134                  this, SLOT( _k_dataChanged( const QModelIndex &, const QModelIndex & ) ) );
00135         setEnabled( true );
00136     } else
00137         setEnabled( false );
00138 }
00139 
00140 /******************************************************************************
00141  * Protected Methods                                                          *
00142  *****************************************************************************/
00143 bool KListWidgetSearchLine::itemMatches( const QListWidgetItem *item,
00144         const QString &s ) const
00145 {
00146     if ( s.isEmpty() )
00147         return true;
00148 
00149     if ( item == 0 )
00150         return false;
00151 
00152     return ( item->text().indexOf( s, 0,
00153                                    caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive ) >= 0 );
00154 }
00155 
00156 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::init( QListWidget *_listWidget )
00157 {
00158     listWidget = _listWidget;
00159 
00160     connect( q, SIGNAL( textChanged( const QString & ) ),
00161              q, SLOT( _k_queueSearch( const QString & ) ) );
00162 
00163     if ( listWidget != 0 ) {
00164         connect( listWidget, SIGNAL( destroyed() ),
00165                  q, SLOT( _k_listWidgetDeleted() ) );
00166         connect( listWidget->model(), SIGNAL( rowsInserted( const QModelIndex &, int, int ) ),
00167                  q, SLOT( _k_rowsInserted( const QModelIndex &, int, int ) ) );
00168         connect( listWidget->model(), SIGNAL( dataChanged( const QModelIndex &, const QModelIndex & ) ),
00169                  q, SLOT( _k_dataChanged( const QModelIndex &, const QModelIndex & ) ) );
00170         q->setEnabled( true );
00171     } else {
00172         q->setEnabled( false );
00173     }
00174 
00175     q->setClearButtonShown(true);
00176 }
00177 
00178 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::updateHiddenState( int start, int end ) {
00179     if ( !listWidget ) {
00180         return;
00181     }
00182 
00183     QListWidgetItem *currentItem = listWidget->currentItem();
00184 
00185     // Remove Non-Matching items
00186     for( int index = start; index <= end; ++index ) {
00187         QListWidgetItem *item = listWidget->item(index);
00188         if ( ! q->itemMatches( item, search ) ) {
00189             item->setHidden( true );
00190 
00191             if ( item == currentItem ) {
00192                 currentItem = 0; // It's not in listWidget anymore.
00193             }
00194         } else if ( item->isHidden() ) {
00195             item->setHidden( false );
00196         }
00197     }
00198 
00199     if ( listWidget->isSortingEnabled() ) {
00200         listWidget->sortItems();
00201     }
00202 
00203     if ( currentItem != 0 ) {
00204         listWidget->scrollToItem( currentItem );
00205     }
00206 }
00207 
00208 bool KListWidgetSearchLine::event(QEvent *event) {
00209 
00210     if (event->type() == QEvent::KeyPress) {
00211         QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
00212         if(keyEvent->matches(QKeySequence::MoveToNextLine) || keyEvent->matches(QKeySequence::SelectNextLine) ||
00213            keyEvent->matches(QKeySequence::MoveToPreviousLine) || keyEvent->matches(QKeySequence::SelectPreviousLine) ||
00214            keyEvent->matches(QKeySequence::MoveToNextPage) ||  keyEvent->matches(QKeySequence::SelectNextPage) ||
00215            keyEvent->matches(QKeySequence::MoveToPreviousPage) ||  keyEvent->matches(QKeySequence::SelectPreviousPage) ||
00216            keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
00217         {
00218             if(d->listWidget) {
00219                 QApplication::sendEvent(d->listWidget, event);
00220                 return true;
00221             }
00222         }
00223     }
00224     return KLineEdit::event(event);
00225 }
00226 /******************************************************************************
00227  * Protected Slots                                                            *
00228  *****************************************************************************/
00229 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_queueSearch( const QString &s )
00230 {
00231     queuedSearches++;
00232     search = s;
00233     QTimer::singleShot( 200, q, SLOT( _k_activateSearch() ) );
00234 }
00235 
00236 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_activateSearch()
00237 {
00238     queuedSearches--;
00239 
00240     if ( queuedSearches <= 0 ) {
00241         q->updateSearch( search );
00242         queuedSearches = 0;
00243     }
00244 }
00245 
00246 /******************************************************************************
00247  * Private Slots                                                              *
00248  *****************************************************************************/
00249 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_listWidgetDeleted()
00250 {
00251     listWidget = 0;
00252     q->setEnabled( false );
00253 }
00254 
00255 
00256 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_rowsInserted( const QModelIndex &parent, int start, int end )
00257 {
00258     if( parent.isValid() ) {
00259         return;
00260     }
00261 
00262     updateHiddenState( start, end );
00263 }
00264 
00265 void KListWidgetSearchLine::KListWidgetSearchLinePrivate::_k_dataChanged( const QModelIndex & topLeft, const QModelIndex & bottomRight )
00266 {
00267     if( topLeft.parent().isValid() ) {
00268         return;
00269     }
00270 
00271     updateHiddenState( topLeft.row(), bottomRight.row() );
00272 }
00273 
00274 
00275 
00276 #include "klistwidgetsearchline.moc"

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • 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.3
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