]>
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 <fe.a.ernst@gmail.com>
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>
22 DolphinUrlNavigator::DolphinUrlNavigator(QWidget
*parent
)
23 : DolphinUrlNavigator(QUrl(), parent
)
27 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl
&url
, QWidget
*parent
)
28 : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url
, parent
)
30 const GeneralSettings
*settings
= GeneralSettings::self();
31 setUrlEditable(settings
->editableUrl());
32 setShowFullPath(settings
->showFullPath());
33 setHomeUrl(Dolphin::homeUrl());
34 setPlacesSelectorVisible(DolphinUrlNavigatorsController::placesSelectorVisible());
35 editor()->setCompletionMode(KCompletion::CompletionMode(settings
->urlCompletionMode()));
36 setWhatsThis(xi18nc("@info:whatsthis location bar",
37 "<para>This describes the location of the files and folders "
38 "displayed below.</para><para>The name of the currently viewed "
39 "folder can be read at the very right. To the left of it is the "
40 "name of the folder that contains it. The whole line is called "
41 "the <emphasis>path</emphasis> to the current location because "
42 "following these folders from left to right leads here.</para>"
43 "<para>This interactive path "
44 "is more powerful than one would expect. To learn more "
45 "about the basic and advanced features of the location bar "
46 "<link url='help:/dolphin/location-bar.html'>click here</link>. "
47 "This will open the dedicated page in the Handbook.</para>"));
49 DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
51 connect(this, &KUrlNavigator::returnPressed
, this, &DolphinUrlNavigator::slotReturnPressed
);
54 DolphinUrlNavigator::~DolphinUrlNavigator()
56 DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
59 QSize
DolphinUrlNavigator::sizeHint() const
61 if (isUrlEditable()) {
62 return editor()->lineEdit()->sizeHint();
65 for (int i
= 0; i
< layout()->count(); ++i
) {
66 QWidget
*widget
= layout()->itemAt(i
)->widget();
67 const QAbstractButton
*button
= qobject_cast
<QAbstractButton
*>(widget
);
68 if (button
&& button
->icon().isNull()) {
69 widthHint
+= widget
->minimumSizeHint().width();
72 return QSize(widthHint
, KUrlNavigator::sizeHint().height());
75 std::unique_ptr
<DolphinUrlNavigator::VisualState
> DolphinUrlNavigator::visualState() const
77 std::unique_ptr
<VisualState
> visualState
{new VisualState
};
78 visualState
->isUrlEditable
= (isUrlEditable());
79 const QLineEdit
*lineEdit
= editor()->lineEdit();
80 visualState
->hasFocus
= lineEdit
->hasFocus();
81 visualState
->text
= lineEdit
->text();
82 visualState
->cursorPosition
= lineEdit
->cursorPosition();
83 visualState
->selectionStart
= lineEdit
->selectionStart();
84 visualState
->selectionLength
= lineEdit
->selectionLength();
88 void DolphinUrlNavigator::setVisualState(const VisualState
&visualState
)
90 setUrlEditable(visualState
.isUrlEditable
);
91 if (!visualState
.isUrlEditable
) {
94 editor()->lineEdit()->setText(visualState
.text
);
95 if (visualState
.hasFocus
) {
96 editor()->lineEdit()->setFocus();
97 editor()->lineEdit()->setCursorPosition(visualState
.cursorPosition
);
98 if (visualState
.selectionStart
!= -1) {
99 editor()->lineEdit()->setSelection(visualState
.selectionStart
, visualState
.selectionLength
);
104 void DolphinUrlNavigator::clearText() const
106 editor()->lineEdit()->clear();
109 void DolphinUrlNavigator::setPlaceholderText(const QString
&text
)
111 editor()->lineEdit()->setPlaceholderText(text
);
114 void DolphinUrlNavigator::slotReturnPressed()
116 if (!GeneralSettings::editableUrl()) {
117 setUrlEditable(false);