]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinurlnavigator.cpp
f24cf2e067ab700f93fa597f4fa242663d29d5aa
[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 "dolphinurlnavigatorscontroller.h"
13 #include "global.h"
14
15 #include <KUrlComboBox>
16 #include <KLocalizedString>
17
18 #include <QAbstractButton>
19 #include <QLayout>
20 #include <QLineEdit>
21
22 DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent) :
23 DolphinUrlNavigator(QUrl(), parent)
24 {}
25
26 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent) :
27 KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
28 {
29 const GeneralSettings* settings = GeneralSettings::self();
30 setUrlEditable(settings->editableUrl());
31 setShowFullPath(settings->showFullPath());
32 setHomeUrl(Dolphin::homeUrl());
33 setPlacesSelectorVisible(DolphinUrlNavigatorsController::placesSelectorVisible());
34 editor()->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode()));
35 setWhatsThis(xi18nc("@info:whatsthis location bar",
36 "<para>This describes the location of the files and folders "
37 "displayed below.</para><para>The name of the currently viewed "
38 "folder can be read at the very right. To the left of it is the "
39 "name of the folder that contains it. The whole line is called "
40 "the <emphasis>path</emphasis> to the current location because "
41 "following these folders from left to right leads here.</para>"
42 "<para>This interactive path "
43 "is more powerful than one would expect. To learn more "
44 "about the basic and advanced features of the location bar "
45 "<link url='help:/dolphin/location-bar.html'>click here</link>. "
46 "This will open the dedicated page in the Handbook.</para>"));
47
48 DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
49
50 connect(this, &DolphinUrlNavigator::returnPressed,
51 this, &DolphinUrlNavigator::slotReturnPressed);
52 connect(editor(), &KUrlComboBox::completionModeChanged,
53 DolphinUrlNavigatorsController::setCompletionMode);
54 }
55
56 DolphinUrlNavigator::~DolphinUrlNavigator()
57 {
58 DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
59 }
60
61 QSize DolphinUrlNavigator::sizeHint() const
62 {
63 if (isUrlEditable()) {
64 return editor()->lineEdit()->sizeHint();
65 }
66 int widthHint = 0;
67 for (int i = 0; i < layout()->count(); ++i) {
68 QWidget *widget = layout()->itemAt(i)->widget();
69 const QAbstractButton *button = qobject_cast<QAbstractButton *>(widget);
70 if (button && button->icon().isNull()) {
71 widthHint += widget->minimumSizeHint().width();
72 }
73 }
74 return QSize(widthHint, KUrlNavigator::sizeHint().height());
75 }
76
77 std::unique_ptr<DolphinUrlNavigator::VisualState> DolphinUrlNavigator::visualState() const
78 {
79 std::unique_ptr<VisualState> visualState{new VisualState};
80 visualState->isUrlEditable = (isUrlEditable());
81 const QLineEdit *lineEdit = editor()->lineEdit();
82 visualState->hasFocus = lineEdit->hasFocus();
83 visualState->text = lineEdit->text();
84 visualState->cursorPosition = lineEdit->cursorPosition();
85 visualState->selectionStart = lineEdit->selectionStart();
86 visualState->selectionLength = lineEdit->selectionLength();
87 return visualState;
88 }
89
90 void DolphinUrlNavigator::setVisualState(const VisualState& visualState)
91 {
92 setUrlEditable(visualState.isUrlEditable);
93 if (!visualState.isUrlEditable) {
94 return;
95 }
96 editor()->lineEdit()->setText(visualState.text);
97 if (visualState.hasFocus) {
98 editor()->lineEdit()->setFocus();
99 editor()->lineEdit()->setCursorPosition(visualState.cursorPosition);
100 if (visualState.selectionStart != -1) {
101 editor()->lineEdit()->setSelection(visualState.selectionStart, visualState.selectionLength);
102 }
103 }
104 }
105
106 void DolphinUrlNavigator::slotReturnPressed()
107 {
108 if (!GeneralSettings::editableUrl()) {
109 setUrlEditable(false);
110 }
111 }