2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 #include "dolphinurlnavigator.h"
10 #include "dolphin_generalsettings.h"
11 #include "dolphinplacesmodelsingleton.h"
14 #include <KUrlComboBox>
15 #include <KLocalizedString>
17 #include <QAbstractButton>
21 DolphinUrlNavigator::DolphinUrlNavigator(QWidget
*parent
) :
22 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), QUrl(), parent
)
27 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl
&url
, QWidget
*parent
) :
28 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url
, parent
)
33 void DolphinUrlNavigator::init()
35 const GeneralSettings
* settings
= GeneralSettings::self();
36 setUrlEditable(settings
->editableUrl());
37 setShowFullPath(settings
->showFullPath());
38 setHomeUrl(Dolphin::homeUrl());
39 setPlacesSelectorVisible(s_placesSelectorVisible
);
40 editor()->setCompletionMode(KCompletion::CompletionMode(settings
->urlCompletionMode()));
41 setWhatsThis(xi18nc("@info:whatsthis location bar",
42 "<para>This describes the location of the files and folders "
43 "displayed below.</para><para>The name of the currently viewed "
44 "folder can be read at the very right. To the left of it is the "
45 "name of the folder that contains it. The whole line is called "
46 "the <emphasis>path</emphasis> to the current location because "
47 "following these folders from left to right leads here.</para>"
48 "<para>This interactive path "
49 "is more powerful than one would expect. To learn more "
50 "about the basic and advanced features of the location bar "
51 "<link url='help:/dolphin/location-bar.html'>click here</link>. "
52 "This will open the dedicated page in the Handbook.</para>"));
54 s_instances
.push_front(this);
56 connect(this, &DolphinUrlNavigator::returnPressed
,
57 this, &DolphinUrlNavigator::slotReturnPressed
);
58 connect(editor(), &KUrlComboBox::completionModeChanged
,
59 this, DolphinUrlNavigator::setCompletionMode
);
62 DolphinUrlNavigator::~DolphinUrlNavigator()
64 s_instances
.remove(this);
67 QSize
DolphinUrlNavigator::sizeHint() const
69 // Change sizeHint() in KUrlNavigator instead.
70 if (isUrlEditable()) {
71 return editor()->lineEdit()->sizeHint();
74 for (int i
= 0; i
< layout()->count(); ++i
) {
75 QWidget
*widget
= layout()->itemAt(i
)->widget();
76 const QAbstractButton
*button
= qobject_cast
<QAbstractButton
*>(widget
);
77 if (button
&& button
->icon().isNull()) {
78 widthHint
+= widget
->minimumSizeHint().width();
81 return QSize(widthHint
, KUrlNavigator::sizeHint().height());
84 std::unique_ptr
<DolphinUrlNavigator::VisualState
> DolphinUrlNavigator::visualState() const
86 std::unique_ptr
<VisualState
> visualState
{new VisualState
};
87 visualState
->isUrlEditable
= (isUrlEditable());
88 const QLineEdit
*lineEdit
= editor()->lineEdit();
89 visualState
->hasFocus
= lineEdit
->hasFocus();
90 visualState
->text
= lineEdit
->text();
91 visualState
->cursorPosition
= lineEdit
->cursorPosition();
92 visualState
->selectionStart
= lineEdit
->selectionStart();
93 visualState
->selectionLength
= lineEdit
->selectionLength();
97 void DolphinUrlNavigator::setVisualState(const VisualState
& visualState
)
99 setUrlEditable(visualState
.isUrlEditable
);
100 if (!visualState
.isUrlEditable
) {
103 editor()->lineEdit()->setText(visualState
.text
);
104 if (visualState
.hasFocus
) {
105 editor()->lineEdit()->setFocus();
106 editor()->lineEdit()->setCursorPosition(visualState
.cursorPosition
);
107 if (visualState
.selectionStart
!= -1) {
108 editor()->lineEdit()->setSelection(visualState
.selectionStart
, visualState
.selectionLength
);
113 void DolphinUrlNavigator::slotReadSettings()
115 // The startup settings should (only) get applied if they have been
116 // modified by the user. Otherwise keep the (possibly) different current
117 // settings of the URL navigators and split view.
118 if (GeneralSettings::modifiedStartupSettings()) {
119 for (DolphinUrlNavigator
*urlNavigator
: s_instances
) {
120 urlNavigator
->setUrlEditable(GeneralSettings::editableUrl());
121 urlNavigator
->setShowFullPath(GeneralSettings::showFullPath());
122 urlNavigator
->setHomeUrl(Dolphin::homeUrl());
127 void DolphinUrlNavigator::slotReturnPressed()
129 if (!GeneralSettings::editableUrl()) {
130 setUrlEditable(false);
134 void DolphinUrlNavigator::slotPlacesPanelVisibilityChanged(bool visible
)
136 // The places-selector from the URL navigator should only be shown
137 // if the places dock is invisible
138 s_placesSelectorVisible
= !visible
;
140 for (DolphinUrlNavigator
*urlNavigator
: s_instances
) {
141 urlNavigator
->setPlacesSelectorVisible(s_placesSelectorVisible
);
145 void DolphinUrlNavigator::setCompletionMode(const KCompletion::CompletionMode completionMode
)
147 if (completionMode
!= GeneralSettings::urlCompletionMode())
149 GeneralSettings::setUrlCompletionMode(completionMode
);
150 for (const DolphinUrlNavigator
*urlNavigator
: s_instances
)
152 urlNavigator
->editor()->setCompletionMode(completionMode
);
157 std::forward_list
<DolphinUrlNavigator
*> DolphinUrlNavigator::s_instances
;
158 bool DolphinUrlNavigator::s_placesSelectorVisible
= true;