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 TQt, and distribute the resulting executable,
00021     without including the source code for TQt in the source distribution.
00022 */
00023 
00024 #include <tqapplication.h>
00025 #include <tqcombobox.h>
00026 #include <tqlabel.h>
00027 #include <tqlayout.h>
00028 #include <tqtimer.h>
00029 #include <tqtoolbutton.h>
00030 #include <tqtooltip.h>
00031 #include <tqwhatsthis.h>
00032 
00033 #include <kdialog.h>
00034 #include <kiconloader.h>
00035 #include <klineedit.h>
00036 #include <tdelocale.h>
00037 
00038 #include "incsearchwidget.h"
00039 
00040 IncSearchWidget::IncSearchWidget( TQWidget *parent, const char *name )
00041     : TQWidget( parent, name )
00042 {
00043   TQHBoxLayout *layout = new TQHBoxLayout( this, 2, KDialog::spacingHint() );
00044 
00045   TQToolButton *button = new TQToolButton( this );
00046   button->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
00047   button->setPixmap( SmallIcon( TQApplication::reverseLayout() ? "clear_left" : "locationbar_erase" ) );
00048   button->setAccel( TQKeySequence( CTRL+ALT+Key_S ) );
00049   button->setAutoRaise( true );
00050   TQToolTip::add( button, i18n( "Reset" ) );
00051   layout->addWidget( button );
00052 
00053   TQLabel *label = new TQLabel( i18n( "Search:" ), this, "tde toolbar widget" );
00054   label->setAlignment( TQLabel::AlignVCenter | TQLabel::AlignRight );
00055   layout->addWidget( label );
00056 
00057   mSearchText = new KLineEdit( this );
00058   mSearchText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Preferred );
00059   TQWhatsThis::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 TQLabel( i18n( "as in 'Search in:'", "&in:" ), this, "tde toolbar widget" );
00064   label->setAlignment( TQLabel::AlignVCenter | TQLabel::AlignRight );
00065   layout->addWidget( label );
00066 
00067   mFieldCombo = new TQComboBox( false, this );
00068   layout->addWidget( mFieldCombo );
00069   label->setBuddy(mFieldCombo);
00070 
00071   TQToolTip::add( mFieldCombo, i18n( "Select incremental search field" ) );
00072   TQWhatsThis::add( mFieldCombo, i18n( "Here you can choose the field, which shall be used for incremental search." ) );
00073 
00074   mInputTimer = new TQTimer( this );
00075 
00076   connect( mInputTimer, TQT_SIGNAL( timeout() ),
00077            TQT_SLOT( timeout() ) );
00078   connect( mSearchText, TQT_SIGNAL( textChanged( const TQString& ) ),
00079            TQT_SLOT( announceDoSearch() ) );
00080   connect( mSearchText, TQT_SIGNAL( returnPressed() ),
00081            TQT_SLOT( announceDoSearch() ) );
00082   connect( mFieldCombo, TQT_SIGNAL( activated( const TQString& ) ),
00083            TQT_SLOT( announceDoSearch() ) );
00084   connect( button, TQT_SIGNAL( clicked() ),
00085            mSearchText, TQT_SLOT( clear() ) );
00086   connect( button, TQT_SIGNAL( clicked() ),
00087            TQT_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 = TDEABC::Field::allFields();
00116 
00117   mFieldCombo->clear();
00118   mFieldCombo->insertItem( i18n( "Visible Fields" ) );
00119   mFieldCombo->insertItem( i18n( "All Fields" ) );
00120 
00121   TDEABC::Field::List::ConstIterator it;
00122   for ( it = mFieldList.begin(); it != mFieldList.end(); ++it )
00123     mFieldCombo->insertItem( (*it)->label() );
00124 
00125   announceDoSearch();
00126 }
00127 
00128 TDEABC::Field::List IncSearchWidget::currentFields() const
00129 {
00130   TDEABC::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 TDEABC::Field::List &fields )
00151 {
00152   mViewFields = fields;
00153 }
00154 
00155 void IncSearchWidget::clear()
00156 {
00157   mSearchText->clear();
00158 }
00159 
00160 void IncSearchWidget::keyPressEvent( TQKeyEvent *event )
00161 {
00162   if ( event->key() == TQt::Key_Up ) {
00163     event->accept();
00164     emit scrollUp();
00165   } else if ( event->key() == TQt::Key_Down ) {
00166     event->accept();
00167     emit scrollDown();
00168   }
00169 }
00170 
00171 #include "incsearchwidget.moc"