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>
35 #include <KIconLoader>
37 #include <KLocalizedString>
40 #include <Plasma/Theme>
42 #include "ui/itemdelegate.h"
44 using namespace Kickoff
;
46 class SearchBar::Private
49 Private() : editWidget(0), timer(0) {}
51 KLineEdit
*editWidget
;
56 SearchBar::SearchBar(QWidget
*parent
)
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()));
68 QHBoxLayout
*layout
= new QHBoxLayout
;
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");
77 searchIcon
->setPixmap(QPixmap(fi
.absoluteFilePath()).scaled(KIconLoader::SizeMedium
, KIconLoader::SizeMedium
, Qt::KeepAspectRatio
));
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
);
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()
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
);
139 void SearchBar::clear()
141 d
->editWidget
->clear();
144 #include "searchbar.moc"