Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / applets / kickoff / ui / searchbar.cpp
blob37ae4246c57ad15fa87683faef5b35eb07daa6a1
1 /*
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.
20 // Own
21 #include "ui/searchbar.h"
23 // Qt
24 #include <QCoreApplication>
25 #include <QDir>
26 #include <QFileInfo>
27 #include <QHBoxLayout>
28 #include <QKeyEvent>
29 #include <QLabel>
30 #include <QPainter>
31 #include <QTimer>
33 // KDE
34 #include <KIcon>
35 #include <KLineEdit>
36 #include <KLocalizedString>
38 #include "ui/itemdelegate.h"
40 using namespace Kickoff;
42 class SearchBar::Private
44 public:
45 Private() : editWidget(0),timer(0) {}
47 KLineEdit *editWidget;
48 QTimer *timer;
51 SearchBar::SearchBar(QWidget *parent)
52 : QWidget(parent)
53 , d(new Private)
55 // timer for buffered updates
56 d->timer = new QTimer(this);
57 d->timer->setInterval(300);
58 d->timer->setSingleShot(true);
59 connect(d->timer,SIGNAL(timeout()),this,SLOT(updateTimerExpired()));
60 connect(this,SIGNAL(startUpdateTimer()),d->timer,SLOT(start()));
62 // setup UI
63 QHBoxLayout *layout = new QHBoxLayout;
64 layout->setMargin(3);
65 layout->setSpacing(0); // we do the spacing manually to line up with the views below
67 QLabel *searchLabel = new QLabel(i18n("Search:"),this);
68 QLabel *searchIcon = new QLabel(this);
70 QFileInfo fi(QDir(QDir::homePath()), ".face.icon");
71 if (fi.exists()) {
72 searchIcon->setPixmap(QPixmap(fi.absoluteFilePath()).scaled(ItemDelegate::ICON_SIZE, ItemDelegate::ICON_SIZE));
74 else {
75 searchIcon->setPixmap(KIcon("system-search").pixmap(ItemDelegate::ICON_SIZE, ItemDelegate::ICON_SIZE));
78 d->editWidget = new KLineEdit(this);
79 d->editWidget->installEventFilter(this);
80 d->editWidget->setClearButtonShown(true);
81 connect(d->editWidget,SIGNAL(textChanged(QString)),this,SIGNAL(startUpdateTimer()));
83 layout->addSpacing(ItemDelegate::ITEM_LEFT_MARGIN - 3);
84 layout->addWidget(searchIcon);
85 layout->addSpacing(ItemDelegate::ICON_TEXT_MARGIN);
86 layout->addWidget(searchLabel);
87 layout->addWidget(d->editWidget);
88 setLayout(layout);
90 setFocusProxy(d->editWidget);
93 void SearchBar::updateTimerExpired()
95 emit queryChanged(d->editWidget->text());
98 SearchBar::~SearchBar()
100 delete d;
103 bool SearchBar::eventFilter(QObject *watched,QEvent *event)
105 // left and right arrow key presses in the search edit when the
106 // edit is empty are propagated up to the parent widget
107 // this allows views in the Launcher to use left and right arrows for
108 // navigation whilst the search bar still has the focus
109 if (watched == d->editWidget && event->type() == QEvent::KeyPress) {
110 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
111 if ((keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) &&
112 d->editWidget->text().isEmpty()) {
113 QCoreApplication::sendEvent(this,event);
114 return true;
117 return false;
120 void SearchBar::paintEvent(QPaintEvent *event)
122 Q_UNUSED(event);
123 QPainter p(this);
124 p.setPen(QPen(palette().mid(), 1));
125 p.drawLine(0, height() - 1, width() - 1, height() - 1);
128 void SearchBar::clear()
130 d->editWidget->clear();
133 #include "searchbar.moc"