]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinurlnavigator.cpp
Merge remote-tracking branch 'upstream/master' into work/zakharafoniam/useful-groups
[dolphin.git] / src / dolphinurlnavigator.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <felixernst@kde.org>
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 <KLocalizedString>
16 #include <KUrlComboBox>
17
18 #include <QAbstractButton>
19 #include <QLabel>
20 #include <QLayout>
21 #include <QLineEdit>
22
23 DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent)
24 : DolphinUrlNavigator(QUrl(), parent)
25 {
26 }
27
28 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent)
29 : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
30 {
31 const GeneralSettings *settings = GeneralSettings::self();
32 setUrlEditable(settings->editableUrl());
33 setShowFullPath(settings->showFullPath());
34 setHomeUrl(Dolphin::homeUrl());
35 setPlacesSelectorVisible(DolphinUrlNavigatorsController::placesSelectorVisible());
36 editor()->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode()));
37 setWhatsThis(xi18nc("@info:whatsthis location bar",
38 "<para>This describes the location of the files and folders "
39 "displayed below.</para><para>The name of the currently viewed "
40 "folder can be read at the very right. To the left of it is the "
41 "name of the folder that contains it. The whole line is called "
42 "the <emphasis>path</emphasis> to the current location because "
43 "following these folders from left to right leads here.</para>"
44 "<para>This interactive path "
45 "is more powerful than one would expect. To learn more "
46 "about the basic and advanced features of the location bar "
47 "<link url='help:/dolphin/location-bar.html'>click here</link>. "
48 "This will open the dedicated page in the Handbook.</para>"));
49
50 DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
51
52 connect(this, &KUrlNavigator::returnPressed, this, &DolphinUrlNavigator::slotReturnPressed);
53
54 auto readOnlyBadge = new QLabel();
55 readOnlyBadge->setPixmap(QIcon::fromTheme(QStringLiteral("emblem-readonly")).pixmap(12, 12));
56 readOnlyBadge->setToolTip(i18nc("@info:tooltip of a 'locked' symbol in url navigator", "This folder is not writable for you."));
57 readOnlyBadge->hide();
58 setBadgeWidget(readOnlyBadge);
59 }
60
61 DolphinUrlNavigator::~DolphinUrlNavigator()
62 {
63 DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
64 }
65
66 QSize DolphinUrlNavigator::sizeHint() const
67 {
68 if (isUrlEditable()) {
69 return editor()->lineEdit()->sizeHint();
70 }
71 int widthHint = 0;
72 for (int i = 0; i < layout()->count(); ++i) {
73 QWidget *widget = layout()->itemAt(i)->widget();
74 const QAbstractButton *button = qobject_cast<QAbstractButton *>(widget);
75 if (button && button->icon().isNull()) {
76 widthHint += widget->minimumSizeHint().width();
77 }
78 }
79 if (readOnlyBadgeVisible()) {
80 widthHint += badgeWidget()->sizeHint().width();
81 }
82 return QSize(widthHint, KUrlNavigator::sizeHint().height());
83 }
84
85 std::unique_ptr<DolphinUrlNavigator::VisualState> DolphinUrlNavigator::visualState() const
86 {
87 std::unique_ptr<VisualState> visualState{new VisualState};
88 visualState->isUrlEditable = (isUrlEditable());
89 const QLineEdit *lineEdit = editor()->lineEdit();
90 visualState->hasFocus = lineEdit->hasFocus();
91 visualState->text = lineEdit->text();
92 visualState->cursorPosition = lineEdit->cursorPosition();
93 visualState->selectionStart = lineEdit->selectionStart();
94 visualState->selectionLength = lineEdit->selectionLength();
95 return visualState;
96 }
97
98 void DolphinUrlNavigator::setVisualState(const VisualState &visualState)
99 {
100 setUrlEditable(visualState.isUrlEditable);
101 if (!visualState.isUrlEditable) {
102 return;
103 }
104 editor()->lineEdit()->setText(visualState.text);
105 if (visualState.hasFocus) {
106 editor()->lineEdit()->setFocus();
107 editor()->lineEdit()->setCursorPosition(visualState.cursorPosition);
108 if (visualState.selectionStart != -1) {
109 editor()->lineEdit()->setSelection(visualState.selectionStart, visualState.selectionLength);
110 }
111 }
112 }
113
114 void DolphinUrlNavigator::clearText() const
115 {
116 editor()->lineEdit()->clear();
117 }
118
119 void DolphinUrlNavigator::setPlaceholderText(const QString &text)
120 {
121 editor()->lineEdit()->setPlaceholderText(text);
122 }
123
124 void DolphinUrlNavigator::setReadOnlyBadgeVisible(bool visible)
125 {
126 QWidget *readOnlyBadge = badgeWidget();
127 if (readOnlyBadge) {
128 readOnlyBadge->setVisible(visible);
129 }
130 }
131
132 bool DolphinUrlNavigator::readOnlyBadgeVisible() const
133 {
134 QWidget *readOnlyBadge = badgeWidget();
135 if (readOnlyBadge) {
136 return readOnlyBadge->isVisible();
137 }
138 return false;
139 }
140
141 void DolphinUrlNavigator::slotReturnPressed()
142 {
143 if (!GeneralSettings::editableUrl()) {
144 setUrlEditable(false);
145 }
146 }
147
148 #include "moc_dolphinurlnavigator.cpp"