]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinurlnavigator.cpp
Add the UrlNavigator to the toolbar automatically if needed
[dolphin.git] / src / dolphinurlnavigator.cpp
1 /*
2 * Copyright 2020 Felix Ernst <fe.a.ernst@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) version 3, or any
8 * later version accepted by the membership of KDE e.V. (or its
9 * successor approved by the membership of KDE e.V.), which shall
10 * act as a proxy defined in Section 6 of version 3 of the license.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "dolphinurlnavigator.h"
22
23 #include "dolphin_generalsettings.h"
24 #include "dolphinplacesmodelsingleton.h"
25 #include "global.h"
26
27 #include <KToggleAction>
28 #include <KUrlComboBox>
29 #include <KLocalizedString>
30
31 #include <QLineEdit>
32 #include <QMenu>
33
34 DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent) :
35 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), QUrl(), parent)
36 {
37 init();
38 }
39
40 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent) :
41 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
42 {
43 init();
44 }
45
46 void DolphinUrlNavigator::init()
47 {
48 const GeneralSettings* settings = GeneralSettings::self();
49 setUrlEditable(settings->editableUrl());
50 setShowFullPath(settings->showFullPath());
51 setHomeUrl(Dolphin::homeUrl());
52 setPlacesSelectorVisible(s_placesSelectorVisible);
53 editor()->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode()));
54 editor()->lineEdit()->installEventFilter(this);
55 installEventFilter(this);
56 setWhatsThis(xi18nc("@info:whatsthis location bar",
57 "<para>This line describes the location of the files and folders "
58 "displayed below.</para><para>The name of the currently viewed "
59 "folder can be read at the very right. To the left of it is the "
60 "name of the folder that contains it. The whole line is called "
61 "the <emphasis>path</emphasis> to the current location because "
62 "following these folders from left to right leads here.</para>"
63 "<para>This interactive path "
64 "is more powerful than one would expect. To learn more "
65 "about the basic and advanced features of the location bar "
66 "<link url='help:/dolphin/location-bar.html'>click here</link>. "
67 "This will open the dedicated page in the Handbook.</para>"));
68
69 s_instances.push_front(this);
70
71 connect(this, &DolphinUrlNavigator::returnPressed,
72 this, &DolphinUrlNavigator::slotReturnPressed);
73 connect(editor(), &KUrlComboBox::completionModeChanged,
74 this, DolphinUrlNavigator::setCompletionMode);
75 }
76
77 DolphinUrlNavigator::~DolphinUrlNavigator()
78 {
79 s_instances.remove(this);
80 }
81
82 bool DolphinUrlNavigator::eventFilter(QObject* watched, QEvent* event)
83 {
84 Q_UNUSED(watched)
85 if (event->type() == QEvent::ChildPolished) {
86 QChildEvent *childEvent = static_cast<QChildEvent *>(event);
87 QMenu *popup = qobject_cast<QMenu *>(childEvent->child());
88 if (popup) {
89 // The popups of the "breadcrumb mode" navigation buttons
90 // should not get the action added. They can currently be
91 // identified by their number of separators: 0 or 1
92 // The popups we are interested in have 2 or more separators.
93 int separatorCount = 0;
94 for (QAction *action : popup->actions()) {
95 if (action->isSeparator()) {
96 separatorCount++;
97 }
98 }
99 if (separatorCount > 1) {
100 q_check_ptr(s_ActionForContextMenu);
101 popup->addAction(s_ActionForContextMenu);
102 }
103 }
104 }
105 return false;
106 }
107
108 void DolphinUrlNavigator::slotReadSettings()
109 {
110 // The startup settings should (only) get applied if they have been
111 // modified by the user. Otherwise keep the (possibly) different current
112 // settings of the URL navigators and split view.
113 if (GeneralSettings::modifiedStartupSettings()) {
114 for (DolphinUrlNavigator *urlNavigator : s_instances) {
115 urlNavigator->setUrlEditable(GeneralSettings::editableUrl());
116 urlNavigator->setShowFullPath(GeneralSettings::showFullPath());
117 urlNavigator->setHomeUrl(Dolphin::homeUrl());
118 }
119 }
120 }
121
122 void DolphinUrlNavigator::slotReturnPressed()
123 {
124 if (!GeneralSettings::editableUrl()) {
125 setUrlEditable(false);
126 }
127 }
128
129 void DolphinUrlNavigator::addToContextMenu(QAction* action)
130 {
131 s_ActionForContextMenu = action;
132 }
133
134
135 void DolphinUrlNavigator::slotPlacesPanelVisibilityChanged(bool visible)
136 {
137 // The places-selector from the URL navigator should only be shown
138 // if the places dock is invisible
139 s_placesSelectorVisible = !visible;
140
141 for (DolphinUrlNavigator *urlNavigator : s_instances) {
142 urlNavigator->setPlacesSelectorVisible(s_placesSelectorVisible);
143 }
144 }
145
146 void DolphinUrlNavigator::setCompletionMode(const KCompletion::CompletionMode completionMode)
147 {
148 if (completionMode != GeneralSettings::urlCompletionMode())
149 {
150 GeneralSettings::setUrlCompletionMode(completionMode);
151 for (const DolphinUrlNavigator *urlNavigator : s_instances)
152 {
153 urlNavigator->editor()->setCompletionMode(completionMode);
154 }
155 }
156 }
157
158 std::forward_list<DolphinUrlNavigator *> DolphinUrlNavigator::s_instances;
159 bool DolphinUrlNavigator::s_placesSelectorVisible = true;
160 QAction *DolphinUrlNavigator::s_ActionForContextMenu = nullptr;