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