Test change - can I push OK?
[kdeedu-porting.git] / kalzium / src / searchwidget.cpp
blob7163019c02efcc3cf16a9da4548d801902b680b5
1 /***************************************************************************
2 * Copyright (C) 2006 by Pino Toscano, toscano.pino@tiscali.it *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #include "searchwidget.h"
22 #include <QLabel>
23 #include <QLayout>
24 #include <KLineEdit>
25 #include <QTimer>
26 #include <QToolButton>
28 #include <KIcon>
29 #include <KLocale>
31 #include "kalziumdataobject.h"
32 #include "search.h"
34 SearchWidget::SearchWidget( QWidget *parent )
35 : QWidget( parent ), m_timer( 0 )
37 QHBoxLayout *mainlay = new QHBoxLayout( this );
38 mainlay->setMargin( 2 );
39 mainlay->setSpacing( 5 );
41 mainlay->addWidget( new QLabel( i18n( "Search:" ), this ) );
43 m_searchLine = new KLineEdit( this );
44 m_searchLine->setClearButtonShown(true);
45 m_searchLine->setTrapReturnKey(true);
46 connect( m_searchLine, SIGNAL( textChanged( const QString& ) ),
47 this, SLOT( searchTextChanged( const QString& ) ) );
48 connect( m_searchLine, SIGNAL( returnPressed() ),
49 this, SLOT( slotReturnPressed() ) );
50 mainlay->addWidget( m_searchLine );
53 SearchWidget::~SearchWidget()
57 void SearchWidget::giveFocus()
59 m_searchLine->setFocus( Qt::MouseFocusReason );
60 m_searchLine->setCursorPosition( m_searchLine->text().length() );
63 void SearchWidget::appendSearchText( const QString& text )
65 m_searchLine->setText( m_searchLine->text() + text );
68 void SearchWidget::searchTextChanged( const QString& )
70 if ( m_timer )
72 m_timer->stop();
74 else
76 m_timer = new QTimer( this );
77 m_timer->setSingleShot( true );
78 connect( m_timer, SIGNAL( timeout() ), this, SLOT( doSearch() ) );
80 // 1/3 of second should be ok
81 m_timer->start( 333 );
84 void SearchWidget::slotReturnPressed()
86 if ( m_timer )
88 m_timer->stop();
90 doSearch();
93 void SearchWidget::doSearch()
95 Search *s = KalziumDataObject::instance()->search();
96 if ( !s ) return;
98 QString txt = m_searchLine->text();
99 if ( txt.length() > 0 )
100 s->doSearch( txt, Search::SearchAll );
101 else
102 s->resetSearch();
105 #include "searchwidget.moc"