File indexing completed on 2024-04-14 05:38:44

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2020 Felix Ernst <felixernst@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "dolphinurlnavigator.h"
0009 
0010 #include "dolphin_generalsettings.h"
0011 #include "dolphinplacesmodelsingleton.h"
0012 #include "dolphinurlnavigatorscontroller.h"
0013 #include "global.h"
0014 
0015 #include <KLocalizedString>
0016 #include <KUrlComboBox>
0017 
0018 #include <QAbstractButton>
0019 #include <QLayout>
0020 #include <QLineEdit>
0021 
0022 DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent)
0023     : DolphinUrlNavigator(QUrl(), parent)
0024 {
0025 }
0026 
0027 DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent)
0028     : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
0029 {
0030     const GeneralSettings *settings = GeneralSettings::self();
0031     setUrlEditable(settings->editableUrl());
0032     setShowFullPath(settings->showFullPath());
0033     setHomeUrl(Dolphin::homeUrl());
0034     setPlacesSelectorVisible(DolphinUrlNavigatorsController::placesSelectorVisible());
0035     editor()->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode()));
0036     setWhatsThis(xi18nc("@info:whatsthis location bar",
0037                         "<para>This describes the location of the files and folders "
0038                         "displayed below.</para><para>The name of the currently viewed "
0039                         "folder can be read at the very right. To the left of it is the "
0040                         "name of the folder that contains it. The whole line is called "
0041                         "the <emphasis>path</emphasis> to the current location because "
0042                         "following these folders from left to right leads here.</para>"
0043                         "<para>This interactive path "
0044                         "is more powerful than one would expect. To learn more "
0045                         "about the basic and advanced features of the location bar "
0046                         "<link url='help:/dolphin/location-bar.html'>click here</link>. "
0047                         "This will open the dedicated page in the Handbook.</para>"));
0048 
0049     DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
0050 
0051     connect(this, &KUrlNavigator::returnPressed, this, &DolphinUrlNavigator::slotReturnPressed);
0052 }
0053 
0054 DolphinUrlNavigator::~DolphinUrlNavigator()
0055 {
0056     DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
0057 }
0058 
0059 QSize DolphinUrlNavigator::sizeHint() const
0060 {
0061     if (isUrlEditable()) {
0062         return editor()->lineEdit()->sizeHint();
0063     }
0064     int widthHint = 0;
0065     for (int i = 0; i < layout()->count(); ++i) {
0066         QWidget *widget = layout()->itemAt(i)->widget();
0067         const QAbstractButton *button = qobject_cast<QAbstractButton *>(widget);
0068         if (button && button->icon().isNull()) {
0069             widthHint += widget->minimumSizeHint().width();
0070         }
0071     }
0072     return QSize(widthHint, KUrlNavigator::sizeHint().height());
0073 }
0074 
0075 std::unique_ptr<DolphinUrlNavigator::VisualState> DolphinUrlNavigator::visualState() const
0076 {
0077     std::unique_ptr<VisualState> visualState{new VisualState};
0078     visualState->isUrlEditable = (isUrlEditable());
0079     const QLineEdit *lineEdit = editor()->lineEdit();
0080     visualState->hasFocus = lineEdit->hasFocus();
0081     visualState->text = lineEdit->text();
0082     visualState->cursorPosition = lineEdit->cursorPosition();
0083     visualState->selectionStart = lineEdit->selectionStart();
0084     visualState->selectionLength = lineEdit->selectionLength();
0085     return visualState;
0086 }
0087 
0088 void DolphinUrlNavigator::setVisualState(const VisualState &visualState)
0089 {
0090     setUrlEditable(visualState.isUrlEditable);
0091     if (!visualState.isUrlEditable) {
0092         return;
0093     }
0094     editor()->lineEdit()->setText(visualState.text);
0095     if (visualState.hasFocus) {
0096         editor()->lineEdit()->setFocus();
0097         editor()->lineEdit()->setCursorPosition(visualState.cursorPosition);
0098         if (visualState.selectionStart != -1) {
0099             editor()->lineEdit()->setSelection(visualState.selectionStart, visualState.selectionLength);
0100         }
0101     }
0102 }
0103 
0104 void DolphinUrlNavigator::clearText() const
0105 {
0106     editor()->lineEdit()->clear();
0107 }
0108 
0109 void DolphinUrlNavigator::setPlaceholderText(const QString &text)
0110 {
0111     editor()->lineEdit()->setPlaceholderText(text);
0112 }
0113 
0114 void DolphinUrlNavigator::slotReturnPressed()
0115 {
0116     if (!GeneralSettings::editableUrl()) {
0117         setUrlEditable(false);
0118     }
0119 }
0120 
0121 #include "moc_dolphinurlnavigator.cpp"