~ubuntu-branches/ubuntu/precise/kalzium/precise

« back to all changes in this revision

Viewing changes to src/searchwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-03 12:28:58 UTC
  • Revision ID: james.westby@ubuntu.com-20110703122858-q1yyxncs89e4w0hs
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 ***************************************************************************/
 
19
 
 
20
#include "searchwidget.h"
 
21
 
 
22
#include <QLabel>
 
23
#include <KLineEdit>
 
24
#include <QTimer>
 
25
#include <QHBoxLayout>
 
26
#include <KLocale>
 
27
 
 
28
#include "kalziumdataobject.h"
 
29
#include "search.h"
 
30
 
 
31
SearchWidget::SearchWidget( QWidget *parent )
 
32
        : QWidget( parent ), m_timer( 0 )
 
33
{
 
34
    QHBoxLayout *mainlay = new QHBoxLayout( this );
 
35
    mainlay->setMargin( 2 );
 
36
    mainlay->setSpacing( 5 );
 
37
 
 
38
    mainlay->addWidget( new QLabel( i18n( "Search:" ), this ) );
 
39
 
 
40
    m_searchLine = new KLineEdit( this );
 
41
    m_searchLine->setClearButtonShown(true);
 
42
    m_searchLine->setTrapReturnKey(true);
 
43
    connect( m_searchLine, SIGNAL( textChanged( const QString& ) ),
 
44
             this, SLOT( searchTextChanged( const QString& ) ) );
 
45
    connect( m_searchLine, SIGNAL( returnPressed() ),
 
46
             this, SLOT( slotReturnPressed() ) );
 
47
    mainlay->addWidget( m_searchLine );
 
48
}
 
49
 
 
50
SearchWidget::~SearchWidget()
 
51
{
 
52
  delete m_searchLine;
 
53
  delete m_timer;
 
54
}
 
55
 
 
56
void SearchWidget::giveFocus()
 
57
{
 
58
    m_searchLine->setFocus( Qt::MouseFocusReason );
 
59
    m_searchLine->setCursorPosition( m_searchLine->text().length() );
 
60
}
 
61
 
 
62
void SearchWidget::searchTextChanged( const QString& )
 
63
{
 
64
    if ( m_timer )
 
65
    {
 
66
        m_timer->stop();
 
67
    }
 
68
    else
 
69
    {
 
70
        m_timer = new QTimer( this );
 
71
        m_timer->setSingleShot( true );
 
72
        connect( m_timer, SIGNAL( timeout() ), this, SLOT( doSearch() ) );
 
73
    }
 
74
    // 1/3 of second should be ok
 
75
    m_timer->start( 333 );
 
76
}
 
77
 
 
78
void SearchWidget::slotReturnPressed()
 
79
{
 
80
    if ( m_timer )
 
81
    {
 
82
        m_timer->stop();
 
83
    }
 
84
    doSearch();
 
85
}
 
86
 
 
87
void SearchWidget::doSearch()
 
88
{
 
89
    Search *s = KalziumDataObject::instance()->search();
 
90
    if ( !s ) return;
 
91
 
 
92
    QString txt = m_searchLine->text();
 
93
    if ( txt.length() > 0 )
 
94
        s->doSearch( txt, Search::SearchAll );
 
95
    else
 
96
        s->resetSearch();
 
97
}
 
98
 
 
99
#include "searchwidget.moc"