]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinurlnavigator.cpp
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <felixernst@kde.org>
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"
12 #include "dolphinurlnavigatorscontroller.h"
15 #include <KLocalizedString>
16 #include <KUrlComboBox>
18 #include <QAbstractButton>
23 DolphinUrlNavigator::DolphinUrlNavigator(QWidget
*parent
)
24 : DolphinUrlNavigator(QUrl(), parent
)
28 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl
&url
, QWidget
*parent
)
29 : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url
, parent
)
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>"));
50 DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
52 connect(this, &KUrlNavigator::returnPressed
, this, &DolphinUrlNavigator::slotReturnPressed
);
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
);
61 DolphinUrlNavigator::~DolphinUrlNavigator()
63 DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
66 QSize
DolphinUrlNavigator::sizeHint() const
68 if (isUrlEditable()) {
69 return editor()->lineEdit()->sizeHint();
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();
79 if (readOnlyBadgeVisible()) {
80 widthHint
+= badgeWidget()->sizeHint().width();
82 return QSize(widthHint
, KUrlNavigator::sizeHint().height());
85 std::unique_ptr
<DolphinUrlNavigator::VisualState
> DolphinUrlNavigator::visualState() const
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();
98 void DolphinUrlNavigator::setVisualState(const VisualState
&visualState
)
100 setUrlEditable(visualState
.isUrlEditable
);
101 if (!visualState
.isUrlEditable
) {
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
);
114 void DolphinUrlNavigator::clearText() const
116 editor()->lineEdit()->clear();
119 void DolphinUrlNavigator::setPlaceholderText(const QString
&text
)
121 editor()->lineEdit()->setPlaceholderText(text
);
124 void DolphinUrlNavigator::setReadOnlyBadgeVisible(bool visible
)
126 QWidget
*readOnlyBadge
= badgeWidget();
128 readOnlyBadge
->setVisible(visible
);
132 bool DolphinUrlNavigator::readOnlyBadgeVisible() const
134 QWidget
*readOnlyBadge
= badgeWidget();
136 return readOnlyBadge
->isVisible();
141 void DolphinUrlNavigator::slotReturnPressed()
143 if (!GeneralSettings::editableUrl()) {
144 setUrlEditable(false);
148 #include "moc_dolphinurlnavigator.cpp"