]> cloud.milkyroute.net Git - dolphin.git/blob - src/filterbar/filterbar.cpp
Fix file preview for desktop files with absolute icon paths
[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
22 #include "filterbar.h"
23
24 #include <KLocalizedString>
25
26 #include <QHBoxLayout>
27 #include <QKeyEvent>
28 #include <QLabel>
29 #include <QLineEdit>
30 #include <QToolButton>
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(QStringLiteral("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(QStringLiteral("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
51 // Create filter editor
52 m_filterInput = new QLineEdit(this);
53 m_filterInput->setLayoutDirection(Qt::LeftToRight);
54 m_filterInput->setClearButtonEnabled(true);
55 m_filterInput->setPlaceholderText(i18n("Filter..."));
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->setContentsMargins(0, 0, 0, 0);
63 hLayout->addWidget(closeButton);
64 hLayout->addWidget(m_lockButton);
65 hLayout->addWidget(m_filterInput);
66 }
67
68 FilterBar::~FilterBar()
69 {
70 }
71
72 void FilterBar::closeFilterBar()
73 {
74 hide();
75 clear();
76 if (m_lockButton) {
77 m_lockButton->setChecked(false);
78 }
79 }
80
81 void FilterBar::selectAll()
82 {
83 m_filterInput->selectAll();
84 }
85
86 void FilterBar::clear()
87 {
88 m_filterInput->clear();
89 }
90
91 void FilterBar::slotUrlChanged()
92 {
93 if (!m_lockButton || !(m_lockButton->isChecked())) {
94 clear();
95 }
96 }
97
98 void FilterBar::slotToggleLockButton(bool checked)
99 {
100 if (checked) {
101 m_lockButton->setIcon(QIcon::fromTheme(QStringLiteral("object-locked")));
102 } else {
103 m_lockButton->setIcon(QIcon::fromTheme(QStringLiteral("object-unlocked")));
104 clear();
105 }
106 }
107
108 void FilterBar::showEvent(QShowEvent* event)
109 {
110 if (!event->spontaneous()) {
111 m_filterInput->setFocus();
112 }
113 }
114
115 void FilterBar::keyReleaseEvent(QKeyEvent* event)
116 {
117 QWidget::keyReleaseEvent(event);
118
119 switch (event->key()) {
120 case Qt::Key_Escape:
121 if (m_filterInput->text().isEmpty()) {
122 emit closeRequest();
123 } else {
124 m_filterInput->clear();
125 }
126 break;
127
128 case Qt::Key_Enter:
129 case Qt::Key_Return:
130 emit focusViewRequest();
131 break;
132
133 default:
134 break;
135 }
136 }
137