]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinurlnavigator.cpp
DolphinView: Remove -1 interval, add setAutoActivationEnabled
[dolphin.git] / src / dolphinurlnavigator.cpp
index 2b7f3d4eb4d55d90c23c33821ded1b1260f29199..5c32538b06b27dbbdf12d7556d24166d90ce5926 100644 (file)
@@ -1,6 +1,6 @@
 /*
     This file is part of the KDE project
-    SPDX-FileCopyrightText: 2020 Felix Ernst <fe.a.ernst@gmail.com>
+    SPDX-FileCopyrightText: 2020 Felix Ernst <felixernst@kde.org>
 
     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */
@@ -9,64 +9,63 @@
 
 #include "dolphin_generalsettings.h"
 #include "dolphinplacesmodelsingleton.h"
+#include "dolphinurlnavigatorscontroller.h"
 #include "global.h"
 
-#include <KUrlComboBox>
 #include <KLocalizedString>
+#include <KUrlComboBox>
 
 #include <QAbstractButton>
+#include <QKeyEvent>
+#include <QLabel>
 #include <QLayout>
 #include <QLineEdit>
 
-DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent) :
-    KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), QUrl(), parent)
-{
-    init();
-}
-
-DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent) :
-    KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
+DolphinUrlNavigator::DolphinUrlNavigator(QWidget *parent)
+    : DolphinUrlNavigator(QUrl(), parent)
 {
-    init();
 }
 
-void DolphinUrlNavigator::init()
+DolphinUrlNavigator::DolphinUrlNavigator(const QUrl &url, QWidget *parent)
+    : KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, parent)
 {
-    const GeneralSettingssettings = GeneralSettings::self();
+    const GeneralSettings *settings = GeneralSettings::self();
     setUrlEditable(settings->editableUrl());
     setShowFullPath(settings->showFullPath());
     setHomeUrl(Dolphin::homeUrl());
-    setPlacesSelectorVisible(s_placesSelectorVisible);
+    setPlacesSelectorVisible(DolphinUrlNavigatorsController::placesSelectorVisible());
     editor()->setCompletionMode(KCompletion::CompletionMode(settings->urlCompletionMode()));
     setWhatsThis(xi18nc("@info:whatsthis location bar",
-        "<para>This describes the location of the files and folders "
-        "displayed below.</para><para>The name of the currently viewed "
-        "folder can be read at the very right. To the left of it is the "
-        "name of the folder that contains it. The whole line is called "
-        "the <emphasis>path</emphasis> to the current location because "
-        "following these folders from left to right leads here.</para>"
-        "<para>This interactive path "
-        "is more powerful than one would expect. To learn more "
-        "about the basic and advanced features of the location bar "
-        "<link url='help:/dolphin/location-bar.html'>click here</link>. "
-        "This will open the dedicated page in the Handbook.</para>"));
-
-    s_instances.push_front(this);
-
-    connect(this, &DolphinUrlNavigator::returnPressed,
-            this, &DolphinUrlNavigator::slotReturnPressed);
-    connect(editor(), &KUrlComboBox::completionModeChanged,
-            this, DolphinUrlNavigator::setCompletionMode);
+                        "<para>This describes the location of the files and folders "
+                        "displayed below.</para><para>The name of the currently viewed "
+                        "folder can be read at the very right. To the left of it is the "
+                        "name of the folder that contains it. The whole line is called "
+                        "the <emphasis>path</emphasis> to the current location because "
+                        "following these folders from left to right leads here.</para>"
+                        "<para>This interactive path "
+                        "is more powerful than one would expect. To learn more "
+                        "about the basic and advanced features of the location bar "
+                        "<link url='help:/dolphin/location-bar.html'>click here</link>. "
+                        "This will open the dedicated page in the Handbook.</para>"));
+
+    DolphinUrlNavigatorsController::registerDolphinUrlNavigator(this);
+
+    connect(this, &KUrlNavigator::returnPressed, this, &DolphinUrlNavigator::slotReturnPressed);
+
+    auto readOnlyBadge = new QLabel();
+    readOnlyBadge->setPixmap(QIcon::fromTheme(QStringLiteral("emblem-readonly")).pixmap(12, 12));
+    readOnlyBadge->setToolTip(i18nc("@info:tooltip of a 'locked' symbol in url navigator", "This folder is not writable for you."));
+    readOnlyBadge->hide();
+    setBadgeWidget(readOnlyBadge);
 }
 
 DolphinUrlNavigator::~DolphinUrlNavigator()
 {
-    s_instances.remove(this);
+    DolphinUrlNavigatorsController::unregisterDolphinUrlNavigator(this);
 }
 
 QSize DolphinUrlNavigator::sizeHint() const
 {
-    // Change sizeHint() in KUrlNavigator instead.
     if (isUrlEditable()) {
         return editor()->lineEdit()->sizeHint();
     }
@@ -78,6 +77,9 @@ QSize DolphinUrlNavigator::sizeHint() const
             widthHint += widget->minimumSizeHint().width();
         }
     }
+    if (readOnlyBadgeVisible()) {
+        widthHint += badgeWidget()->sizeHint().width();
+    }
     return QSize(widthHint, KUrlNavigator::sizeHint().height());
 }
 
@@ -94,7 +96,7 @@ std::unique_ptr<DolphinUrlNavigator::VisualState> DolphinUrlNavigator::visualSta
     return visualState;
 }
 
-void DolphinUrlNavigator::setVisualState(const VisualStatevisualState)
+void DolphinUrlNavigator::setVisualState(const VisualState &visualState)
 {
     setUrlEditable(visualState.isUrlEditable);
     if (!visualState.isUrlEditable) {
@@ -110,49 +112,47 @@ void DolphinUrlNavigator::setVisualState(const VisualState& visualState)
     }
 }
 
-void DolphinUrlNavigator::slotReadSettings()
+void DolphinUrlNavigator::clearText() const
 {
-    // The startup settings should (only) get applied if they have been
-    // modified by the user. Otherwise keep the (possibly) different current
-    // settings of the URL navigators and split view.
-    if (GeneralSettings::modifiedStartupSettings()) {
-        for (DolphinUrlNavigator *urlNavigator : s_instances) {
-            urlNavigator->setUrlEditable(GeneralSettings::editableUrl());
-            urlNavigator->setShowFullPath(GeneralSettings::showFullPath());
-            urlNavigator->setHomeUrl(Dolphin::homeUrl());
-        }
-    }
+    editor()->lineEdit()->clear();
 }
 
-void DolphinUrlNavigator::slotReturnPressed()
+void DolphinUrlNavigator::setPlaceholderText(const QString &text)
 {
-    if (!GeneralSettings::editableUrl()) {
-        setUrlEditable(false);
+    editor()->lineEdit()->setPlaceholderText(text);
+}
+
+void DolphinUrlNavigator::setReadOnlyBadgeVisible(bool visible)
+{
+    QWidget *readOnlyBadge = badgeWidget();
+    if (readOnlyBadge) {
+        readOnlyBadge->setVisible(visible);
     }
 }
 
-void DolphinUrlNavigator::slotPlacesPanelVisibilityChanged(bool visible)
+bool DolphinUrlNavigator::readOnlyBadgeVisible() const
 {
-    // The places-selector from the URL navigator should only be shown
-    // if the places dock is invisible
-    s_placesSelectorVisible = !visible;
+    QWidget *readOnlyBadge = badgeWidget();
+    if (readOnlyBadge) {
+        return readOnlyBadge->isVisible();
+    }
+    return false;
+}
 
-    for (DolphinUrlNavigator *urlNavigator : s_instances) {
-        urlNavigator->setPlacesSelectorVisible(s_placesSelectorVisible);
+void DolphinUrlNavigator::slotReturnPressed()
+{
+    if (!GeneralSettings::editableUrl()) {
+        setUrlEditable(false);
     }
 }
 
-void DolphinUrlNavigator::setCompletionMode(const KCompletion::CompletionMode completionMode)
+void DolphinUrlNavigator::keyPressEvent(QKeyEvent *keyEvent)
 {
-    if (completionMode != GeneralSettings::urlCompletionMode())
-    {
-        GeneralSettings::setUrlCompletionMode(completionMode);
-        for (const DolphinUrlNavigator *urlNavigator : s_instances)
-        {
-            urlNavigator->editor()->setCompletionMode(completionMode);
-        }
+    if (keyEvent->key() == Qt::Key_Escape && !isUrlEditable()) {
+        Q_EMIT requestToLoseFocus();
+        return;
     }
+    KUrlNavigator::keyPressEvent(keyEvent);
 }
 
-std::forward_list<DolphinUrlNavigator *> DolphinUrlNavigator::s_instances;
-bool DolphinUrlNavigator::s_placesSelectorVisible = true;
+#include "moc_dolphinurlnavigator.cpp"