kaddressbook

incsearchwidget.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tqapplication.h>
25 #include <tqcombobox.h>
26 #include <tqlabel.h>
27 #include <tqlayout.h>
28 #include <tqtimer.h>
29 #include <tqtoolbutton.h>
30 #include <tqtooltip.h>
31 #include <tqwhatsthis.h>
32 
33 #include <kdialog.h>
34 #include <kiconloader.h>
35 #include <klineedit.h>
36 #include <tdelocale.h>
37 
38 #include "incsearchwidget.h"
39 
40 IncSearchWidget::IncSearchWidget( TQWidget *parent, const char *name )
41  : TQWidget( parent, name )
42 {
43  TQHBoxLayout *layout = new TQHBoxLayout( this, 2, KDialog::spacingHint() );
44 
45  TQToolButton *button = new TQToolButton( this );
46  button->setSizePolicy( TQSizePolicy::Minimum, TQSizePolicy::Minimum );
47  button->setPixmap( SmallIcon( TQApplication::reverseLayout() ? "clear_left" : "locationbar_erase" ) );
48  button->setAccel( TQKeySequence( CTRL+ALT+Key_S ) );
49  button->setAutoRaise( true );
50  TQToolTip::add( button, i18n( "Reset" ) );
51  layout->addWidget( button );
52 
53  TQLabel *label = new TQLabel( i18n( "Search:" ), this, "tde toolbar widget" );
54  label->setAlignment( TQLabel::AlignVCenter | TQLabel::AlignRight );
55  layout->addWidget( label );
56 
57  mSearchText = new KLineEdit( this );
58  mSearchText->setSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::Preferred );
59  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." ) );
60  label->setBuddy( mSearchText );
61  layout->addWidget( mSearchText );
62 
63  label = new TQLabel( i18n( "as in 'Search in:'", "&in:" ), this, "tde toolbar widget" );
64  label->setAlignment( TQLabel::AlignVCenter | TQLabel::AlignRight );
65  layout->addWidget( label );
66 
67  mFieldCombo = new TQComboBox( false, this );
68  layout->addWidget( mFieldCombo );
69  label->setBuddy(mFieldCombo);
70 
71  TQToolTip::add( mFieldCombo, i18n( "Select incremental search field" ) );
72  TQWhatsThis::add( mFieldCombo, i18n( "Here you can choose the field, which shall be used for incremental search." ) );
73 
74  mInputTimer = new TQTimer( this );
75 
76  connect( mInputTimer, TQ_SIGNAL( timeout() ),
77  TQ_SLOT( timeout() ) );
78  connect( mSearchText, TQ_SIGNAL( textChanged( const TQString& ) ),
79  TQ_SLOT( announceDoSearch() ) );
80  connect( mSearchText, TQ_SIGNAL( returnPressed() ),
81  TQ_SLOT( announceDoSearch() ) );
82  connect( mFieldCombo, TQ_SIGNAL( activated( const TQString& ) ),
83  TQ_SLOT( announceDoSearch() ) );
84  connect( button, TQ_SIGNAL( clicked() ),
85  mSearchText, TQ_SLOT( clear() ) );
86  connect( button, TQ_SIGNAL( clicked() ),
87  TQ_SLOT( announceDoSearch() ) );
88 
89  initFields();
90 
91  mSearchText->installEventFilter( this );
92 
93  setFocusProxy( mSearchText );
94 }
95 
96 IncSearchWidget::~IncSearchWidget()
97 {
98 }
99 
100 void IncSearchWidget::announceDoSearch()
101 {
102  if ( mInputTimer->isActive() )
103  mInputTimer->stop();
104 
105  mInputTimer->start( 0, true );
106 }
107 
108 void IncSearchWidget::timeout()
109 {
110  emit doSearch( mSearchText->text() );
111 }
112 
113 void IncSearchWidget::initFields()
114 {
115  mFieldList = TDEABC::Field::allFields();
116 
117  mFieldCombo->clear();
118  mFieldCombo->insertItem( i18n( "Visible Fields" ) );
119  mFieldCombo->insertItem( i18n( "All Fields" ) );
120 
121  TDEABC::Field::List::ConstIterator it;
122  for ( it = mFieldList.begin(); it != mFieldList.end(); ++it )
123  mFieldCombo->insertItem( (*it)->label() );
124 
125  announceDoSearch();
126 }
127 
128 TDEABC::Field::List IncSearchWidget::currentFields() const
129 {
130  TDEABC::Field::List fieldList;
131 
132  if ( mFieldCombo->currentItem() == 0 )
133  fieldList = mViewFields;
134  else if ( mFieldCombo->currentItem() > 1 )
135  fieldList.append( mFieldList[ mFieldCombo->currentItem() - 2 ] );
136 
137  return fieldList;
138 }
139 
140 void IncSearchWidget::setCurrentItem( int pos )
141 {
142  mFieldCombo->setCurrentItem( pos );
143 }
144 
145 int IncSearchWidget::currentItem() const
146 {
147  return mFieldCombo->currentItem();
148 }
149 
150 void IncSearchWidget::setViewFields( const TDEABC::Field::List &fields )
151 {
152  mViewFields = fields;
153 }
154 
155 void IncSearchWidget::clear()
156 {
157  mSearchText->clear();
158 }
159 
160 void IncSearchWidget::keyPressEvent( TQKeyEvent *event )
161 {
162  if ( event->key() == TQt::Key_Up ) {
163  event->accept();
164  emit scrollUp();
165  } else if ( event->key() == TQt::Key_Down ) {
166  event->accept();
167  emit scrollDown();
168  }
169 }
170 
171 #include "incsearchwidget.moc"