]> cloud.milkyroute.net Git - dolphin.git/blob - src/filterbar/filterbar.cpp
Filter bar: add a button that prevents clearing if the URL changes
[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 * Copyright (C) 2012 by Stuart Citrin <ctrn3e8@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21 #include "filterbar.h"
22
23 #include <QBoxLayout>
24 #include <QKeyEvent>
25 #include <QLabel>
26 #include <QToolButton>
27
28 #include <KIcon>
29 #include <KLocale>
30 #include <KLineEdit>
31 #include <KIconLoader>
32
33 FilterBar::FilterBar(QWidget* parent) :
34 QWidget(parent)
35 {
36 // Create close button
37 QToolButton *closeButton = new QToolButton(this);
38 closeButton->setAutoRaise(true);
39 closeButton->setIcon(KIcon("dialog-close"));
40 closeButton->setToolTip(i18nc("@info:tooltip", "Hide Filter Bar"));
41 connect(closeButton, SIGNAL(clicked()), this, SIGNAL(closeRequest()));
42
43 // Create button to lock text when changing folders
44 m_lockButton = new QToolButton(this);
45 m_lockButton->setCheckable(true);
46 m_lockButton->setIcon(KIcon("system-lock-screen.png"));
47 m_lockButton->setToolTip(i18nc("@info:tooltip", "Keep Filter When Changing Folders"));
48 connect(m_lockButton, SIGNAL(toggled(bool)), this, SLOT(slotToggleLockButton(bool)));
49
50 // Create label
51 QLabel* filterLabel = new QLabel(i18nc("@label:textbox", "Filter:"), this);
52
53 // Create filter editor
54 m_filterInput = new KLineEdit(this);
55 m_filterInput->setLayoutDirection(Qt::LeftToRight);
56 m_filterInput->setClearButtonShown(true);
57 connect(m_filterInput, SIGNAL(textChanged(QString)),
58 this, SIGNAL(filterChanged(QString)));
59 setFocusProxy(m_filterInput);
60
61 // Apply layout
62 QHBoxLayout* hLayout = new QHBoxLayout(this);
63 hLayout->setMargin(0);
64 hLayout->addWidget(closeButton);
65 hLayout->addWidget(filterLabel);
66 hLayout->addWidget(m_filterInput);
67 hLayout->addWidget(m_lockButton);
68
69 filterLabel->setBuddy(m_filterInput);
70 }
71
72 FilterBar::~FilterBar()
73 {
74 }
75
76 void FilterBar::closeFilterBar()
77 {
78 hide();
79 clear();
80 if (m_lockButton) {
81 m_lockButton->setChecked(false);
82 }
83 }
84
85 void FilterBar::selectAll()
86 {
87 m_filterInput->selectAll();
88 }
89
90 void FilterBar::clear()
91 {
92 m_filterInput->clear();
93 }
94
95 void FilterBar::slotUrlChanged()
96 {
97 if (!m_lockButton || !(m_lockButton->isChecked())) {
98 clear();
99 }
100 }
101
102 void FilterBar::slotToggleLockButton(bool checked)
103 {
104 if (!checked) {
105 clear();
106 }
107 }
108
109 void FilterBar::showEvent(QShowEvent* event)
110 {
111 if (!event->spontaneous()) {
112 m_filterInput->setFocus();
113 }
114 }
115
116 void FilterBar::keyReleaseEvent(QKeyEvent* event)
117 {
118 QWidget::keyReleaseEvent(event);
119
120 switch (event->key()) {
121 case Qt::Key_Escape:
122 if (m_filterInput->text().isEmpty()) {
123 emit closeRequest();
124 } else {
125 m_filterInput->clear();
126 }
127 break;
128
129 case Qt::Key_Enter:
130 case Qt::Key_Return:
131 emit focusViewRequest();
132 break;
133
134 default:
135 break;
136 }
137 }
138
139 #include "filterbar.moc"