Krazy/EBN: qMin is better than QMIN
[kphotoalbum.git] / MainWindow / SearchBar.cpp
blobe3a83e5ce082ca9a66403b1f056a811c20894754
1 /* Copyright (C) 2003-2006 Jesper K. Pedersen <blackie@kde.org>
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
18 #include "SearchBar.h"
19 #include <klineedit.h>
20 #include <kmainwindow.h>
21 #include <qlabel.h>
22 #include <QKeyEvent>
23 #include <QEvent>
24 #include <klocale.h>
25 #include <kaction.h>
26 #include <qapplication.h>
27 #include <kactioncollection.h>
29 MainWindow::SearchBar::SearchBar( KMainWindow* parent )
30 : KToolBar( parent )
32 QLabel* label = new QLabel( i18n("Search:") + QString::fromLatin1(" ") );
33 addWidget( label );
35 _edit = new KLineEdit( this );
36 _edit->setClearButtonShown(true);
37 label->setBuddy( _edit );
39 addWidget( _edit );
40 connect( _edit, SIGNAL( textChanged( const QString& ) ), this, SIGNAL( textChanged( const QString& ) ) );
41 connect( _edit, SIGNAL( returnPressed() ), this, SIGNAL( returnPressed() ) );
43 _edit->installEventFilter( this );
46 bool MainWindow::SearchBar::eventFilter( QObject* , QEvent* e )
48 if ( e->type() == QEvent::KeyPress ) {
49 QKeyEvent* ke = static_cast<QKeyEvent*>( e );
50 if ( ke->key() == Qt::Key_Up ||
51 ke->key() == Qt::Key_Down ||
52 ke->key() == Qt::Key_Left ||
53 ke->key() == Qt::Key_Right ||
54 ke->key() == Qt::Key_PageDown ||
55 ke->key() == Qt::Key_PageUp ||
56 ke->key() == Qt::Key_Home ||
57 ke->key() == Qt::Key_End ) {
58 emit keyPressed( ke );
59 return true;
61 else if ( ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Return ) {
62 // If I don't interpret return and enter here, but simply rely
63 // on QLineEdit itself to emit the signal, then it will
64 // propagate to the main window, and from there be delivered to
65 // the central widget.
66 emit returnPressed();
67 return true;
69 else if ( ke->key() == Qt::Key_Escape )
70 reset();
72 return false;
75 void MainWindow::SearchBar::reset()
77 _edit->clear();
80 /**
81 * This was originally just a call to setEnabled() on the SearchBar itself,
82 * but due to a bug in either KDE or Qt, this resulted in the bar never
83 * being enabled again after a disable.
85 void MainWindow::SearchBar::setLineEditEnabled(bool b)
87 _edit->setEnabled(b);
88 _edit->setFocus();
91 #include "SearchBar.moc"