]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinurlnavigator.cpp
Add clang-format and format code as in Frameworks
[dolphin.git] / src / dolphinurlnavigator.cpp
1 /*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>
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 <QLayout>
20 #include <QLineEdit>
21
22 DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent)
23 : DolphinUrlNavigator(QUrl(), parent)
24 {
25 }
26
27 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent)
28 : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
29 {
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>"));
48
49 DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
50
51 connect(this, &KUrlNavigator::returnPressed, this, &DolphinUrlNavigator::slotReturnPressed);
52 }
53
54 DolphinUrlNavigator::~DolphinUrlNavigator()
55 {
56 DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
57 }
58
59 QSize DolphinUrlNavigator::sizeHint() const
60 {
61 if (isUrlEditable()) {
62 return editor()->lineEdit()->sizeHint();
63 }
64 int widthHint = 0;
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();
70 }
71 }
72 return QSize(widthHint, KUrlNavigator::sizeHint().height());
73 }
74
75 std::unique_ptr<DolphinUrlNavigator::VisualState> DolphinUrlNavigator::visualState() const
76 {
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();
85 return visualState;
86 }
87
88 void DolphinUrlNavigator::setVisualState(const VisualState &visualState)
89 {
90 setUrlEditable(visualState.isUrlEditable);
91 if (!visualState.isUrlEditable) {
92 return;
93 }
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);
100 }
101 }
102 }
103
104 void DolphinUrlNavigator::clearText() const
105 {
106 editor()->lineEdit()->clear();
107 }
108
109 void DolphinUrlNavigator::setPlaceholderText(const QString &text)
110 {
111 editor()->lineEdit()->setPlaceholderText(text);
112 }
113
114 void DolphinUrlNavigator::slotReturnPressed()
115 {
116 if (!GeneralSettings::editableUrl()) {
117 setUrlEditable(false);
118 }
119 }