kaddressbook

incsearchwidget.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qapplication.h>
00025 #include <qcombobox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qtimer.h>
00029 #include <qtoolbutton.h>
00030 #include <qtooltip.h>
00031 #include <qwhatsthis.h>
00032 
00033 #include <kdialog.h>
00034 #include <kiconloader.h>
00035 #include <klineedit.h>
00036 #include <klocale.h>
00037 
00038 #include "incsearchwidget.h"
00039 
00040 IncSearchWidget::IncSearchWidget( QWidget *parent, const char *name )
00041     : QWidget( parent, name )
00042 {
00043   QHBoxLayout *layout = new QHBoxLayout( this, 2, KDialog::spacingHint() );
00044 
00045   QToolButton *button = new QToolButton( this );
00046   button->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum );
00047   button->setPixmap( SmallIcon( QApplication::reverseLayout() ? "clear_left" : "locationbar_erase" ) );
00048   button->setAccel( QKeySequence( CTRL+ALT+Key_S ) );
00049   button->setAutoRaise( true );
00050   QToolTip::add( button, i18n( "Reset" ) );
00051   layout->addWidget( button );
00052 
00053   QLabel *label = new QLabel( i18n( "Search:" ), this, "kde toolbar widget" );
00054   label->setAlignment( QLabel::AlignVCenter | QLabel::AlignRight );
00055   layout->addWidget( label );
00056 
00057   mSearchText = new KLineEdit( this );
00058   mSearchText->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Preferred );
00059   QWhatsThis::add( mSearchText, i18n( "The incremental search<p>Enter some text here will start the search for the contact, which matches the search pattern best. The part of the contact, which will be used for matching, depends on the field selection." ) );
00060   label->setBuddy( mSearchText );
00061   layout->addWidget( mSearchText );
00062 
00063   label = new QLabel( i18n( "as in 'Search in:'", "&in:" ), this, "kde toolbar widget" );
00064   label->setAlignment( QLabel::AlignVCenter | QLabel::AlignRight );
00065   layout->addWidget( label );
00066 
00067   mFieldCombo = new QComboBox( false, this );
00068   layout->addWidget( mFieldCombo );
00069   label->setBuddy(mFieldCombo);
00070 
00071   QToolTip::add( mFieldCombo, i18n( "Select incremental search field" ) );
00072   QWhatsThis::add( mFieldCombo, i18n( "Here you can choose the field, which shall be used for incremental search." ) );
00073 
00074   mInputTimer = new QTimer( this );
00075 
00076   connect( mInputTimer, SIGNAL( timeout() ),
00077            SLOT( timeout() ) );
00078   connect( mSearchText, SIGNAL( textChanged( const QString& ) ),
00079            SLOT( announceDoSearch() ) );
00080   connect( mSearchText, SIGNAL( returnPressed() ),
00081            SLOT( announceDoSearch() ) );
00082   connect( mFieldCombo, SIGNAL( activated( const QString& ) ),
00083            SLOT( announceDoSearch() ) );
00084   connect( button, SIGNAL( clicked() ),
00085            mSearchText, SLOT( clear() ) );
00086   connect( button, SIGNAL( clicked() ),
00087            SLOT( announceDoSearch() ) );
00088 
00089   initFields();
00090 
00091   mSearchText->installEventFilter( this );
00092 
00093   setFocusProxy( mSearchText );
00094 }
00095 
00096 IncSearchWidget::~IncSearchWidget()
00097 {
00098 }
00099 
00100 void IncSearchWidget::announceDoSearch()
00101 {
00102   if ( mInputTimer->isActive() )
00103     mInputTimer->stop();
00104 
00105   mInputTimer->start( 0, true );
00106 }
00107 
00108 void IncSearchWidget::timeout()
00109 {
00110   emit doSearch( mSearchText->text() );
00111 }
00112 
00113 void IncSearchWidget::initFields()
00114 {
00115   mFieldList = KABC::Field::allFields();
00116 
00117   mFieldCombo->clear();
00118   mFieldCombo->insertItem( i18n( "Visible Fields" ) );
00119   mFieldCombo->insertItem( i18n( "All Fields" ) );
00120 
00121   KABC::Field::List::ConstIterator it;
00122   for ( it = mFieldList.begin(); it != mFieldList.end(); ++it )
00123     mFieldCombo->insertItem( (*it)->label() );
00124 
00125   announceDoSearch();
00126 }
00127 
00128 KABC::Field::List IncSearchWidget::currentFields() const
00129 {
00130   KABC::Field::List fieldList;
00131 
00132   if ( mFieldCombo->currentItem() == 0 )
00133     fieldList = mViewFields;
00134   else if ( mFieldCombo->currentItem() > 1 )
00135     fieldList.append( mFieldList[ mFieldCombo->currentItem() - 2 ] );
00136 
00137   return fieldList;
00138 }
00139 
00140 void IncSearchWidget::setCurrentItem( int pos )
00141 {
00142   mFieldCombo->setCurrentItem( pos );
00143 }
00144 
00145 int IncSearchWidget::currentItem() const
00146 {
00147   return mFieldCombo->currentItem();
00148 }
00149 
00150 void IncSearchWidget::setViewFields( const KABC::Field::List &fields )
00151 {
00152   mViewFields = fields;
00153 }
00154 
00155 void IncSearchWidget::clear()
00156 {
00157   mSearchText->clear();
00158 }
00159 
00160 void IncSearchWidget::keyPressEvent( QKeyEvent *event )
00161 {
00162   if ( event->key() == Qt::Key_Up ) {
00163     event->accept();
00164     emit scrollUp();
00165   } else if ( event->key() == Qt::Key_Down ) {
00166     event->accept();
00167     emit scrollDown();
00168   }
00169 }
00170 
00171 #include "incsearchwidget.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys