2
Copyright 2007 Robert Knight <robertknight@gmail.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library 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 GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
21
#include "ui/searchbar.h"
24
#include <QCoreApplication>
27
#include <QHBoxLayout>
34
#include <KIconLoader>
38
#include <Plasma/Theme>
40
#include "ui/itemdelegate.h"
42
using namespace Kickoff;
44
class SearchBar::Private
47
Private() : editWidget(0), timer(0) {}
49
KLineEdit *editWidget;
54
SearchBar::SearchBar(QWidget *parent)
58
// timer for buffered updates
59
d->timer = new QTimer(this);
60
d->timer->setInterval(300);
61
d->timer->setSingleShot(true);
62
connect(d->timer, SIGNAL(timeout()), this, SLOT(updateTimerExpired()));
63
connect(this, SIGNAL(startUpdateTimer()), d->timer, SLOT(start()));
66
QHBoxLayout *layout = new QHBoxLayout;
68
layout->setSpacing(0); // we do the spacing manually to line up with the views below
70
d->searchLabel = new QLabel(i18nc("Label of the search bar textedit", "Search:"), this);
71
QLabel *searchIcon = new QLabel(this);
73
const QFileInfo fi(QDir(QDir::homePath()), ".face.icon");
75
searchIcon->setPixmap(QPixmap(fi.absoluteFilePath()).scaled(KIconLoader::SizeMedium, KIconLoader::SizeMedium, Qt::KeepAspectRatio));
77
searchIcon->setPixmap(KIcon("system-search").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
80
d->editWidget = new KLineEdit(this);
81
d->editWidget->installEventFilter(this);
82
d->editWidget->setClearButtonShown(true);
83
connect(d->editWidget, SIGNAL(textChanged(QString)), this, SIGNAL(startUpdateTimer()));
85
//add arbitrary spacing
86
layout->addSpacing(2);
87
layout->addWidget(searchIcon);
88
layout->addSpacing(5);
89
layout->addWidget(d->searchLabel);
90
layout->addSpacing(5);
91
layout->addWidget(d->editWidget);
94
setFocusProxy(d->editWidget);
96
updateThemedPalette();
97
connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
98
this, SLOT(updateThemedPalette()));
101
void SearchBar::updateThemedPalette()
103
QColor color = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
104
QPalette p = d->searchLabel->palette();
105
p.setColor(QPalette::Normal, QPalette::WindowText, color);
106
p.setColor(QPalette::Inactive, QPalette::WindowText, color);
107
d->searchLabel->setPalette(p);
110
void SearchBar::updateTimerExpired()
112
emit queryChanged(d->editWidget->text());
115
SearchBar::~SearchBar()
120
bool SearchBar::eventFilter(QObject *watched, QEvent *event)
122
// left and right arrow key presses in the search edit when the
123
// edit is empty are propagated up to the parent widget
124
// this allows views in the Launcher to use left and right arrows for
125
// navigation whilst the search bar still has the focus
126
if (watched == d->editWidget && event->type() == QEvent::KeyPress) {
127
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
128
if ((keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) &&
129
d->editWidget->text().isEmpty()) {
130
QCoreApplication::sendEvent(this, event);
137
void SearchBar::clear()
139
d->editWidget->clear();
142
#include "searchbar.moc"