2 * Copyright 2020 Felix Ernst <fe.a.ernst@gmail.com>
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.
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.
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/>.
21 #include "dolphinurlnavigator.h"
23 #include "dolphin_generalsettings.h"
24 #include "dolphinplacesmodelsingleton.h"
27 #include <KToggleAction>
28 #include <KUrlComboBox>
29 #include <KLocalizedString>
34 DolphinUrlNavigator::DolphinUrlNavigator(QWidget
*parent
) :
35 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), QUrl(), parent
)
40 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl
&url
, QWidget
*parent
) :
41 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url
, parent
)
46 void DolphinUrlNavigator::init()
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>"));
69 s_instances
.push_front(this);
71 connect(this, &DolphinUrlNavigator::returnPressed
,
72 this, &DolphinUrlNavigator::slotReturnPressed
);
73 connect(editor(), &KUrlComboBox::completionModeChanged
,
74 this, DolphinUrlNavigator::setCompletionMode
);
77 DolphinUrlNavigator::~DolphinUrlNavigator()
79 s_instances
.remove(this);
82 bool DolphinUrlNavigator::eventFilter(QObject
* watched
, QEvent
* event
)
85 if (event
->type() == QEvent::ChildPolished
) {
86 QChildEvent
*childEvent
= static_cast<QChildEvent
*>(event
);
87 QMenu
*popup
= qobject_cast
<QMenu
*>(childEvent
->child());
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()) {
99 if (separatorCount
> 1) {
100 q_check_ptr(s_ActionForContextMenu
);
101 popup
->addAction(s_ActionForContextMenu
);
108 void DolphinUrlNavigator::slotReadSettings()
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());
122 void DolphinUrlNavigator::slotReturnPressed()
124 if (!GeneralSettings::editableUrl()) {
125 setUrlEditable(false);
129 void DolphinUrlNavigator::addToContextMenu(QAction
* action
)
131 s_ActionForContextMenu
= action
;
135 void DolphinUrlNavigator::slotPlacesPanelVisibilityChanged(bool visible
)
137 // The places-selector from the URL navigator should only be shown
138 // if the places dock is invisible
139 s_placesSelectorVisible
= !visible
;
141 for (DolphinUrlNavigator
*urlNavigator
: s_instances
) {
142 urlNavigator
->setPlacesSelectorVisible(s_placesSelectorVisible
);
146 void DolphinUrlNavigator::setCompletionMode(const KCompletion::CompletionMode completionMode
)
148 if (completionMode
!= GeneralSettings::urlCompletionMode())
150 GeneralSettings::setUrlCompletionMode(completionMode
);
151 for (const DolphinUrlNavigator
*urlNavigator
: s_instances
)
153 urlNavigator
->editor()->setCompletionMode(completionMode
);
158 std::forward_list
<DolphinUrlNavigator
*> DolphinUrlNavigator::s_instances
;
159 bool DolphinUrlNavigator::s_placesSelectorVisible
= true;
160 QAction
*DolphinUrlNavigator::s_ActionForContextMenu
= nullptr;