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