• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

searchbar.cpp

Go to the documentation of this file.
00001 /*  
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 // Own
00021 #include "ui/searchbar.h"
00022 
00023 // Qt
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 // KDE
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     // timer for buffered updates
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     // setup UI
00064     QHBoxLayout *layout = new QHBoxLayout;
00065     layout->setMargin(3);
00066     layout->setSpacing(0); // we do the spacing manually to line up with the views below
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     //add arbitrary spacing
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     // left and right arrow key presses in the search edit when the
00109     // edit is empty are propagated up to the parent widget
00110     // this allows views in the Launcher to use left and right arrows for
00111     // navigation whilst the search bar still has the focus
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"

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal