]> cloud.milkyroute.net Git - dolphin.git/blob - src/filterbar/filterbar.cpp
Merge remote-tracking branch 'origin/master' into frameworks
[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, &QToolButton::clicked, this, &FilterBar::closeRequest);
42
43 // Create button to lock text when changing folders
44 m_lockButton = new QToolButton(this);
45 m_lockButton->setAutoRaise(true);
46 m_lockButton->setCheckable(true);
47 m_lockButton->setIcon(KIcon("object-unlocked"));
48 m_lockButton->setToolTip(i18nc("@info:tooltip", "Keep Filter When Changing Folders"));
49 connect(m_lockButton, &QToolButton::toggled, this, &FilterBar::slotToggleLockButton);
50
51 // Create label
52 QLabel* filterLabel = new QLabel(i18nc("@label:textbox", "Filter:"), this);
53
54 // Create filter editor
55 m_filterInput = new KLineEdit(this);
56 m_filterInput->setLayoutDirection(Qt::LeftToRight);
57 m_filterInput->setClearButtonShown(true);
58 connect(m_filterInput, &KLineEdit::textChanged,
59 this, &FilterBar::filterChanged);
60 setFocusProxy(m_filterInput);
61
62 // Apply layout
63 QHBoxLayout* hLayout = new QHBoxLayout(this);
64 hLayout->setMargin(0);
65 hLayout->addWidget(closeButton);
66 hLayout->addWidget(filterLabel);
67 hLayout->addWidget(m_filterInput);
68 hLayout->addWidget(m_lockButton);
69
70 filterLabel->setBuddy(m_filterInput);
71 }
72
73 FilterBar::~FilterBar()
74 {
75 }
76
77 void FilterBar::closeFilterBar()
78 {
79 hide();
80 clear();
81 if (m_lockButton) {
82 m_lockButton->setChecked(false);
83 }
84 }
85
86 void FilterBar::selectAll()
87 {
88 m_filterInput->selectAll();
89 }
90
91 void FilterBar::clear()
92 {
93 m_filterInput->clear();
94 }
95
96 void FilterBar::slotUrlChanged()
97 {
98 if (!m_lockButton || !(m_lockButton->isChecked())) {
99 clear();
100 }
101 }
102
103 void FilterBar::slotToggleLockButton(bool checked)
104 {
105 if (checked) {
106 m_lockButton->setIcon(KIcon("object-locked"));
107 } else {
108 m_lockButton->setIcon(KIcon("object-unlocked"));
109 clear();
110 }
111 }
112
113 void FilterBar::showEvent(QShowEvent* event)
114 {
115 if (!event->spontaneous()) {
116 m_filterInput->setFocus();
117 }
118 }
119
120 void FilterBar::keyReleaseEvent(QKeyEvent* event)
121 {
122 QWidget::keyReleaseEvent(event);
123
124 switch (event->key()) {
125 case Qt::Key_Escape:
126 if (m_filterInput->text().isEmpty()) {
127 emit closeRequest();
128 } else {
129 m_filterInput->clear();
130 }
131 break;
132
133 case Qt::Key_Enter:
134 case Qt::Key_Return:
135 emit focusViewRequest();
136 break;
137
138 default:
139 break;
140 }
141 }
142
143 #include "filterbar.moc"