• Skip to content
  • Skip to link menu
KDE 4.2 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 //Plasma
00040 #include <Plasma/Theme>
00041 
00042 #include "ui/itemdelegate.h"
00043 
00044 using namespace Kickoff;
00045 
00046 class SearchBar::Private
00047 {
00048 public:
00049     Private() : editWidget(0), timer(0) {}
00050 
00051     KLineEdit *editWidget;
00052     QLabel *searchLabel;
00053     QTimer *timer;
00054 };
00055 
00056 SearchBar::SearchBar(QWidget *parent)
00057         : QWidget(parent)
00058         , d(new Private)
00059 {
00060     // timer for buffered updates
00061     d->timer = new QTimer(this);
00062     d->timer->setInterval(300);
00063     d->timer->setSingleShot(true);
00064     connect(d->timer, SIGNAL(timeout()), this, SLOT(updateTimerExpired()));
00065     connect(this, SIGNAL(startUpdateTimer()), d->timer, SLOT(start()));
00066 
00067     // setup UI
00068     QHBoxLayout *layout = new QHBoxLayout;
00069     layout->setMargin(3);
00070     layout->setSpacing(0); // we do the spacing manually to line up with the views below
00071 
00072     d->searchLabel = new QLabel(i18n("Search:"), this);
00073     QLabel *searchIcon = new QLabel(this);
00074 
00075     QFileInfo fi(QDir(QDir::homePath()), ".face.icon");
00076     if (fi.exists()) {
00077         searchIcon->setPixmap(QPixmap(fi.absoluteFilePath()).scaled(KIconLoader::SizeMedium, KIconLoader::SizeMedium, Qt::KeepAspectRatio));
00078     } else {
00079         searchIcon->setPixmap(KIcon("system-search").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
00080     }
00081 
00082     d->editWidget = new KLineEdit(this);
00083     d->editWidget->installEventFilter(this);
00084     d->editWidget->setClearButtonShown(true);
00085     connect(d->editWidget, SIGNAL(textChanged(QString)), this, SIGNAL(startUpdateTimer()));
00086 
00087     //add arbitrary spacing
00088     layout->addSpacing(2);
00089     layout->addWidget(searchIcon);
00090     layout->addSpacing(5);
00091     layout->addWidget(d->searchLabel);
00092     layout->addSpacing(5);
00093     layout->addWidget(d->editWidget);
00094     setLayout(layout);
00095 
00096     setFocusProxy(d->editWidget);
00097 
00098     updateThemedPalette();
00099     connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
00100             this, SLOT(updateThemedPalette()));
00101 }
00102 
00103 void SearchBar::updateThemedPalette()
00104 {
00105     QColor color = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
00106     QPalette p = d->searchLabel->palette();
00107     p.setColor(QPalette::Normal, QPalette::WindowText, color);
00108     p.setColor(QPalette::Inactive, QPalette::WindowText, color);
00109     d->searchLabel->setPalette(p);
00110 }
00111 
00112 void SearchBar::updateTimerExpired()
00113 {
00114     emit queryChanged(d->editWidget->text());
00115 }
00116 
00117 SearchBar::~SearchBar()
00118 {
00119     delete d;
00120 }
00121 
00122 bool SearchBar::eventFilter(QObject *watched, QEvent *event)
00123 {
00124     // left and right arrow key presses in the search edit when the
00125     // edit is empty are propagated up to the parent widget
00126     // this allows views in the Launcher to use left and right arrows for
00127     // navigation whilst the search bar still has the focus
00128     if (watched == d->editWidget && event->type() == QEvent::KeyPress) {
00129         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
00130         if ((keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) &&
00131                 d->editWidget->text().isEmpty()) {
00132             QCoreApplication::sendEvent(this, event);
00133             return true;
00134         }
00135     }
00136     return false;
00137 }
00138 
00139 void SearchBar::clear()
00140 {
00141     d->editWidget->clear();
00142 }
00143 
00144 #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
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
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