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>
36 #include <KLocalizedString>
38 #include "ui/itemdelegate.h"
40 using namespace Kickoff
;
42 class SearchBar::Private
45 Private() : editWidget(0),timer(0) {}
47 KLineEdit
*editWidget
;
51 SearchBar::SearchBar(QWidget
*parent
)
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()));
63 QHBoxLayout
*layout
= new QHBoxLayout
;
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");
72 searchIcon
->setPixmap(QPixmap(fi
.absoluteFilePath()).scaled(ItemDelegate::ICON_SIZE
, ItemDelegate::ICON_SIZE
));
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
);
90 setFocusProxy(d
->editWidget
);
93 void SearchBar::updateTimerExpired()
95 emit
queryChanged(d
->editWidget
->text());
98 SearchBar::~SearchBar()
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
);
120 void SearchBar::paintEvent(QPaintEvent
*event
)
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"