]> cloud.milkyroute.net Git - dolphin.git/blob - src/filterbar/filterbar.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / filterbar / filterbar.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20 #include "filterbar.h"
21
22 #include <QBoxLayout>
23 #include <QKeyEvent>
24 #include <QLabel>
25 #include <QToolButton>
26
27 #include <KIcon>
28 #include <KLocale>
29 #include <KLineEdit>
30 #include <KIconLoader>
31
32 FilterBar::FilterBar(QWidget* parent) :
33 QWidget(parent)
34 {
35 // Create close button
36 QToolButton *closeButton = new QToolButton(this);
37 closeButton->setAutoRaise(true);
38 closeButton->setIcon(KIcon("dialog-close"));
39 closeButton->setToolTip(i18nc("@info:tooltip", "Hide Filter Bar"));
40 connect(closeButton, SIGNAL(clicked()), this, SIGNAL(closeRequest()));
41
42 // Create label
43 QLabel* filterLabel = new QLabel(i18nc("@label:textbox", "Filter:"), this);
44
45 // Create filter editor
46 m_filterInput = new KLineEdit(this);
47 m_filterInput->setLayoutDirection(Qt::LeftToRight);
48 m_filterInput->setClearButtonShown(true);
49 connect(m_filterInput, SIGNAL(textChanged(QString)),
50 this, SIGNAL(filterChanged(QString)));
51 setFocusProxy(m_filterInput);
52
53 // Apply layout
54 QHBoxLayout* hLayout = new QHBoxLayout(this);
55 hLayout->setMargin(0);
56 hLayout->addWidget(closeButton);
57 hLayout->addWidget(filterLabel);
58 hLayout->addWidget(m_filterInput);
59
60 filterLabel->setBuddy(m_filterInput);
61 }
62
63 FilterBar::~FilterBar()
64 {
65 }
66
67 void FilterBar::selectAll()
68 {
69 m_filterInput->selectAll();
70 }
71
72 void FilterBar::clear()
73 {
74 m_filterInput->clear();
75 }
76
77 void FilterBar::showEvent(QShowEvent* event)
78 {
79 if (!event->spontaneous()) {
80 m_filterInput->setFocus();
81 }
82 }
83
84 void FilterBar::keyReleaseEvent(QKeyEvent* event)
85 {
86 QWidget::keyReleaseEvent(event);
87
88 switch (event->key()) {
89 case Qt::Key_Escape:
90 if (m_filterInput->text().isEmpty()) {
91 emit closeRequest();
92 } else {
93 m_filterInput->clear();
94 }
95 break;
96
97 case Qt::Key_Enter:
98 case Qt::Key_Return:
99 emit focusViewRequest();
100 break;
101
102 default:
103 break;
104 }
105 }
106
107 #include "filterbar.moc"