]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinurlnavigator.cpp
Make UrlNavigators in the toolbar the only option
[dolphin.git] / src / dolphinurlnavigator.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8 #include "dolphinurlnavigator.h"
9
10 #include "dolphin_generalsettings.h"
11 #include "dolphinplacesmodelsingleton.h"
12 #include "global.h"
13
14 #include <KUrlComboBox>
15 #include <KLocalizedString>
16
17 #include <QAbstractButton>
18 #include <QLayout>
19 #include <QLineEdit>
20
21 DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent) :
22 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), QUrl(), parent)
23 {
24 init();
25 }
26
27 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent) :
28 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
29 {
30 init();
31 }
32
33 void DolphinUrlNavigator::init()
34 {
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>"));
53
54 s_instances.push_front(this);
55
56 connect(this, &DolphinUrlNavigator::returnPressed,
57 this, &DolphinUrlNavigator::slotReturnPressed);
58 connect(editor(), &KUrlComboBox::completionModeChanged,
59 this, DolphinUrlNavigator::setCompletionMode);
60 }
61
62 DolphinUrlNavigator::~DolphinUrlNavigator()
63 {
64 s_instances.remove(this);
65 }
66
67 QSize DolphinUrlNavigator::sizeHint() const
68 {
69 // Change sizeHint() in KUrlNavigator instead.
70 if (isUrlEditable()) {
71 return editor()->lineEdit()->sizeHint();
72 }
73 int widthHint = 0;
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();
79 }
80 }
81 return QSize(widthHint, KUrlNavigator::sizeHint().height());
82 }
83
84 std::unique_ptr<DolphinUrlNavigator::VisualState> DolphinUrlNavigator::visualState() const
85 {
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();
94 return visualState;
95 }
96
97 void DolphinUrlNavigator::setVisualState(const VisualState& visualState)
98 {
99 setUrlEditable(visualState.isUrlEditable);
100 if (!visualState.isUrlEditable) {
101 return;
102 }
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);
109 }
110 }
111 }
112
113 void DolphinUrlNavigator::slotReadSettings()
114 {
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());
123 }
124 }
125 }
126
127 void DolphinUrlNavigator::slotReturnPressed()
128 {
129 if (!GeneralSettings::editableUrl()) {
130 setUrlEditable(false);
131 }
132 }
133
134 void DolphinUrlNavigator::slotPlacesPanelVisibilityChanged(bool visible)
135 {
136 // The places-selector from the URL navigator should only be shown
137 // if the places dock is invisible
138 s_placesSelectorVisible = !visible;
139
140 for (DolphinUrlNavigator *urlNavigator : s_instances) {
141 urlNavigator->setPlacesSelectorVisible(s_placesSelectorVisible);
142 }
143 }
144
145 void DolphinUrlNavigator::setCompletionMode(const KCompletion::CompletionMode completionMode)
146 {
147 if (completionMode != GeneralSettings::urlCompletionMode())
148 {
149 GeneralSettings::setUrlCompletionMode(completionMode);
150 for (const DolphinUrlNavigator *urlNavigator : s_instances)
151 {
152 urlNavigator->editor()->setCompletionMode(completionMode);
153 }
154 }
155 }
156
157 std::forward_list<DolphinUrlNavigator *> DolphinUrlNavigator::s_instances;
158 bool DolphinUrlNavigator::s_placesSelectorVisible = true;