add more spacing
[personal-kdebase.git] / workspace / plasma / applets / kickoff / ui / searchbar.cpp
blob5a7bc933d55d18e008333360449de2965b1c48b6
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 <KIconLoader>
36 #include <KLineEdit>
37 #include <KLocalizedString>
39 //Plasma
40 #include <Plasma/Theme>
42 #include "ui/itemdelegate.h"
44 using namespace Kickoff;
46 class SearchBar::Private
48 public:
49 Private() : editWidget(0), timer(0) {}
51 KLineEdit *editWidget;
52 QLabel *searchLabel;
53 QTimer *timer;
56 SearchBar::SearchBar(QWidget *parent)
57 : QWidget(parent)
58 , d(new Private)
60 // timer for buffered updates
61 d->timer = new QTimer(this);
62 d->timer->setInterval(300);
63 d->timer->setSingleShot(true);
64 connect(d->timer, SIGNAL(timeout()), this, SLOT(updateTimerExpired()));
65 connect(this, SIGNAL(startUpdateTimer()), d->timer, SLOT(start()));
67 // setup UI
68 QHBoxLayout *layout = new QHBoxLayout;
69 layout->setMargin(3);
70 layout->setSpacing(0); // we do the spacing manually to line up with the views below
72 d->searchLabel = new QLabel(i18n("Search:"), this);
73 QLabel *searchIcon = new QLabel(this);
75 QFileInfo fi(QDir(QDir::homePath()), ".face.icon");
76 if (fi.exists()) {
77 searchIcon->setPixmap(QPixmap(fi.absoluteFilePath()).scaled(KIconLoader::SizeMedium, KIconLoader::SizeMedium, Qt::KeepAspectRatio));
78 } else {
79 searchIcon->setPixmap(KIcon("system-search").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
82 d->editWidget = new KLineEdit(this);
83 d->editWidget->installEventFilter(this);
84 d->editWidget->setClearButtonShown(true);
85 connect(d->editWidget, SIGNAL(textChanged(QString)), this, SIGNAL(startUpdateTimer()));
87 //add arbitrary spacing
88 layout->addSpacing(2);
89 layout->addWidget(searchIcon);
90 layout->addSpacing(5);
91 layout->addWidget(d->searchLabel);
92 layout->addSpacing(5);
93 layout->addWidget(d->editWidget);
94 setLayout(layout);
96 setFocusProxy(d->editWidget);
98 updateThemedPalette();
99 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
100 this, SLOT(updateThemedPalette()));
103 void SearchBar::updateThemedPalette()
105 QColor color = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
106 QPalette p = d->searchLabel->palette();
107 p.setColor(QPalette::Normal, QPalette::WindowText, color);
108 p.setColor(QPalette::Inactive, QPalette::WindowText, color);
109 d->searchLabel->setPalette(p);
112 void SearchBar::updateTimerExpired()
114 emit queryChanged(d->editWidget->text());
117 SearchBar::~SearchBar()
119 delete d;
122 bool SearchBar::eventFilter(QObject *watched, QEvent *event)
124 // left and right arrow key presses in the search edit when the
125 // edit is empty are propagated up to the parent widget
126 // this allows views in the Launcher to use left and right arrows for
127 // navigation whilst the search bar still has the focus
128 if (watched == d->editWidget && event->type() == QEvent::KeyPress) {
129 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
130 if ((keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) &&
131 d->editWidget->text().isEmpty()) {
132 QCoreApplication::sendEvent(this, event);
133 return true;
136 return false;
139 void SearchBar::clear()
141 d->editWidget->clear();
144 #include "searchbar.moc"