]>
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>
24 DolphinUrlNavigator::DolphinUrlNavigator(QWidget
*parent
)
25 : DolphinUrlNavigator(QUrl(), parent
)
29 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl
&url
, QWidget
*parent
)
30 : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url
, parent
)
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>"));
51 DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
53 connect(this, &KUrlNavigator::returnPressed
, this, &DolphinUrlNavigator::slotReturnPressed
);
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
);
62 DolphinUrlNavigator::~DolphinUrlNavigator()
64 DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
67 QSize
DolphinUrlNavigator::sizeHint() const
69 if (isUrlEditable()) {
70 return editor()->lineEdit()->sizeHint();
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();
80 if (readOnlyBadgeVisible()) {
81 widthHint
+= badgeWidget()->sizeHint().width();
83 return QSize(widthHint
, KUrlNavigator::sizeHint().height());
86 std::unique_ptr
<DolphinUrlNavigator::VisualState
> DolphinUrlNavigator::visualState() const
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();
99 void DolphinUrlNavigator::setVisualState(const VisualState
&visualState
)
101 setUrlEditable(visualState
.isUrlEditable
);
102 if (!visualState
.isUrlEditable
) {
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
);
115 void DolphinUrlNavigator::clearText() const
117 editor()->lineEdit()->clear();
120 void DolphinUrlNavigator::setPlaceholderText(const QString
&text
)
122 editor()->lineEdit()->setPlaceholderText(text
);
125 void DolphinUrlNavigator::setReadOnlyBadgeVisible(bool visible
)
127 QWidget
*readOnlyBadge
= badgeWidget();
129 readOnlyBadge
->setVisible(visible
);
133 bool DolphinUrlNavigator::readOnlyBadgeVisible() const
135 QWidget
*readOnlyBadge
= badgeWidget();
137 return readOnlyBadge
->isVisible();
142 void DolphinUrlNavigator::slotReturnPressed()
144 if (!GeneralSettings::editableUrl()) {
145 setUrlEditable(false);
149 void DolphinUrlNavigator::keyPressEvent(QKeyEvent
*keyEvent
)
151 if (keyEvent
->key() == Qt::Key_Escape
&& !isUrlEditable()) {
152 Q_EMIT
requestToLoseFocus();
155 KUrlNavigator::keyPressEvent(keyEvent
);
158 #include "moc_dolphinurlnavigator.cpp"