]> cloud.milkyroute.net Git - dolphin.git/blob - src/filterbar/filterbar.cpp
Merge branch '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 <QKeyEvent>
24 #include <QLabel>
25 #include <QToolButton>
26 #include <QHBoxLayout>
27
28 #include <QIcon>
29 #include <KLocalizedString>
30 #include <QLineEdit>
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(QIcon::fromTheme("dialog-close"));
39 closeButton->setToolTip(i18nc("@info:tooltip", "Hide Filter Bar"));
40 connect(closeButton, &QToolButton::clicked, this, &FilterBar::closeRequest);
41
42 // Create button to lock text when changing folders
43 m_lockButton = new QToolButton(this);
44 m_lockButton->setAutoRaise(true);
45 m_lockButton->setCheckable(true);
46 m_lockButton->setIcon(QIcon::fromTheme("object-unlocked"));
47 m_lockButton->setToolTip(i18nc("@info:tooltip", "Keep Filter When Changing Folders"));
48 connect(m_lockButton, &QToolButton::toggled, this, &FilterBar::slotToggleLockButton);
49
50 // Create label
51 QLabel* filterLabel = new QLabel(i18nc("@label:textbox", "Filter:"), this);
52
53 // Create filter editor
54 m_filterInput = new QLineEdit(this);
55 m_filterInput->setLayoutDirection(Qt::LeftToRight);
56 m_filterInput->setClearButtonEnabled(true);
57 connect(m_filterInput, &QLineEdit::textChanged,
58 this, &FilterBar::filterChanged);
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 m_lockButton->setIcon(QIcon::fromTheme("object-locked"));
106 } else {
107 m_lockButton->setIcon(QIcon::fromTheme("object-unlocked"));
108 clear();
109 }
110 }
111
112 void FilterBar::showEvent(QShowEvent* event)
113 {
114 if (!event->spontaneous()) {
115 m_filterInput->setFocus();
116 }
117 }
118
119 void FilterBar::keyReleaseEvent(QKeyEvent* event)
120 {
121 QWidget::keyReleaseEvent(event);
122
123 switch (event->key()) {
124 case Qt::Key_Escape:
125 if (m_filterInput->text().isEmpty()) {
126 emit closeRequest();
127 } else {
128 m_filterInput->clear();
129 }
130 break;
131
132 case Qt::Key_Enter:
133 case Qt::Key_Return:
134 emit focusViewRequest();
135 break;
136
137 default:
138 break;
139 }
140 }
141