Applets
searchbar.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "ui/searchbar.h"
00022
00023
00024 #include <QCoreApplication>
00025 #include <QDir>
00026 #include <QFileInfo>
00027 #include <QHBoxLayout>
00028 #include <QKeyEvent>
00029 #include <QLabel>
00030 #include <QPainter>
00031 #include <QTimer>
00032
00033
00034 #include <KIcon>
00035 #include <KIconLoader>
00036 #include <KLineEdit>
00037 #include <KLocalizedString>
00038
00039 #include "ui/itemdelegate.h"
00040
00041 using namespace Kickoff;
00042
00043 class SearchBar::Private
00044 {
00045 public:
00046 Private() : editWidget(0),timer(0) {}
00047
00048 KLineEdit *editWidget;
00049 QTimer *timer;
00050 };
00051
00052 SearchBar::SearchBar(QWidget *parent)
00053 : QWidget(parent)
00054 , d(new Private)
00055 {
00056
00057 d->timer = new QTimer(this);
00058 d->timer->setInterval(300);
00059 d->timer->setSingleShot(true);
00060 connect(d->timer,SIGNAL(timeout()),this,SLOT(updateTimerExpired()));
00061 connect(this,SIGNAL(startUpdateTimer()),d->timer,SLOT(start()));
00062
00063
00064 QHBoxLayout *layout = new QHBoxLayout;
00065 layout->setMargin(3);
00066 layout->setSpacing(0);
00067
00068 QLabel *searchLabel = new QLabel(i18n("Search:"),this);
00069 QLabel *searchIcon = new QLabel(this);
00070
00071 QFileInfo fi(QDir(QDir::homePath()), ".face.icon");
00072 if (fi.exists()) {
00073 searchIcon->setPixmap(QPixmap(fi.absoluteFilePath()).scaled(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
00074 }
00075 else {
00076 searchIcon->setPixmap(KIcon("system-search").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
00077 }
00078
00079 d->editWidget = new KLineEdit(this);
00080 d->editWidget->installEventFilter(this);
00081 d->editWidget->setClearButtonShown(true);
00082 connect(d->editWidget,SIGNAL(textChanged(QString)),this,SIGNAL(startUpdateTimer()));
00083
00084
00085 layout->addSpacing(2);
00086 layout->addWidget(searchIcon);
00087 layout->addSpacing(5);
00088 layout->addWidget(searchLabel);
00089 layout->addSpacing(5);
00090 layout->addWidget(d->editWidget);
00091 setLayout(layout);
00092
00093 setFocusProxy(d->editWidget);
00094 }
00095
00096 void SearchBar::updateTimerExpired()
00097 {
00098 emit queryChanged(d->editWidget->text());
00099 }
00100
00101 SearchBar::~SearchBar()
00102 {
00103 delete d;
00104 }
00105
00106 bool SearchBar::eventFilter(QObject *watched,QEvent *event)
00107 {
00108
00109
00110
00111
00112 if (watched == d->editWidget && event->type() == QEvent::KeyPress) {
00113 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
00114 if ((keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) &&
00115 d->editWidget->text().isEmpty()) {
00116 QCoreApplication::sendEvent(this,event);
00117 return true;
00118 }
00119 }
00120 return false;
00121 }
00122
00123 void SearchBar::paintEvent(QPaintEvent *event)
00124 {
00125 Q_UNUSED(event);
00126 QPainter p(this);
00127 p.setPen(QPen(palette().mid(), 1));
00128 p.drawLine(0, height() - 1, width() - 1, height() - 1);
00129 }
00130
00131 void SearchBar::clear()
00132 {
00133 d->editWidget->clear();
00134 }
00135
00136 #include "searchbar.moc"