~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/desktop/applets/kickoff/ui/searchbar.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2007 Robert Knight <robertknight@gmail.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
// Own
 
21
#include "ui/searchbar.h"
 
22
 
 
23
// Qt
 
24
#include <QCoreApplication>
 
25
#include <QDir>
 
26
#include <QFileInfo>
 
27
#include <QHBoxLayout>
 
28
#include <QKeyEvent>
 
29
#include <QLabel>
 
30
#include <QTimer>
 
31
 
 
32
// KDE
 
33
#include <KIcon>
 
34
#include <KIconLoader>
 
35
#include <KLineEdit>
 
36
 
 
37
//Plasma
 
38
#include <Plasma/Theme>
 
39
 
 
40
#include "ui/itemdelegate.h"
 
41
 
 
42
using namespace Kickoff;
 
43
 
 
44
class SearchBar::Private
 
45
{
 
46
public:
 
47
    Private() : editWidget(0), timer(0) {}
 
48
 
 
49
    KLineEdit *editWidget;
 
50
    QLabel *searchLabel;
 
51
    QTimer *timer;
 
52
};
 
53
 
 
54
SearchBar::SearchBar(QWidget *parent)
 
55
        : QWidget(parent)
 
56
        , d(new Private)
 
57
{
 
58
    // timer for buffered updates
 
59
    d->timer = new QTimer(this);
 
60
    d->timer->setInterval(300);
 
61
    d->timer->setSingleShot(true);
 
62
    connect(d->timer, SIGNAL(timeout()), this, SLOT(updateTimerExpired()));
 
63
    connect(this, SIGNAL(startUpdateTimer()), d->timer, SLOT(start()));
 
64
 
 
65
    // setup UI
 
66
    QHBoxLayout *layout = new QHBoxLayout;
 
67
    layout->setMargin(3);
 
68
    layout->setSpacing(0); // we do the spacing manually to line up with the views below
 
69
 
 
70
    d->searchLabel = new QLabel(i18nc("Label of the search bar textedit", "Search:"), this);
 
71
    QLabel *searchIcon = new QLabel(this);
 
72
 
 
73
    const QFileInfo fi(QDir(QDir::homePath()), ".face.icon");
 
74
    if (fi.exists()) {
 
75
        searchIcon->setPixmap(QPixmap(fi.absoluteFilePath()).scaled(KIconLoader::SizeMedium, KIconLoader::SizeMedium, Qt::KeepAspectRatio));
 
76
    } else {
 
77
        searchIcon->setPixmap(KIcon("system-search").pixmap(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
 
78
    }
 
79
 
 
80
    d->editWidget = new KLineEdit(this);
 
81
    d->editWidget->installEventFilter(this);
 
82
    d->editWidget->setClearButtonShown(true);
 
83
    connect(d->editWidget, SIGNAL(textChanged(QString)), this, SIGNAL(startUpdateTimer()));
 
84
 
 
85
    //add arbitrary spacing
 
86
    layout->addSpacing(2);
 
87
    layout->addWidget(searchIcon);
 
88
    layout->addSpacing(5);
 
89
    layout->addWidget(d->searchLabel);
 
90
    layout->addSpacing(5);
 
91
    layout->addWidget(d->editWidget);
 
92
    setLayout(layout);
 
93
 
 
94
    setFocusProxy(d->editWidget);
 
95
 
 
96
    updateThemedPalette();
 
97
    connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
 
98
            this, SLOT(updateThemedPalette()));
 
99
}
 
100
 
 
101
void SearchBar::updateThemedPalette()
 
102
{
 
103
    QColor color = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
 
104
    QPalette p = d->searchLabel->palette();
 
105
    p.setColor(QPalette::Normal, QPalette::WindowText, color);
 
106
    p.setColor(QPalette::Inactive, QPalette::WindowText, color);
 
107
    d->searchLabel->setPalette(p);
 
108
}
 
109
 
 
110
void SearchBar::updateTimerExpired()
 
111
{
 
112
    emit queryChanged(d->editWidget->text());
 
113
}
 
114
 
 
115
SearchBar::~SearchBar()
 
116
{
 
117
    delete d;
 
118
}
 
119
 
 
120
bool SearchBar::eventFilter(QObject *watched, QEvent *event)
 
121
{
 
122
    // left and right arrow key presses in the search edit when the
 
123
    // edit is empty are propagated up to the parent widget
 
124
    // this allows views in the Launcher to use left and right arrows for
 
125
    // navigation whilst the search bar still has the focus
 
126
    if (watched == d->editWidget && event->type() == QEvent::KeyPress) {
 
127
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
 
128
        if ((keyEvent->key() == Qt::Key_Left || keyEvent->key() == Qt::Key_Right) &&
 
129
                d->editWidget->text().isEmpty()) {
 
130
            QCoreApplication::sendEvent(this, event);
 
131
            return true;
 
132
        }
 
133
    }
 
134
    return false;
 
135
}
 
136
 
 
137
void SearchBar::clear()
 
138
{
 
139
    d->editWidget->clear();
 
140
}
 
141
 
 
142
#include "searchbar.moc"