File indexing completed on 2024-04-28 11:41:29

0001 /*
0002     SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz@gmx.at>
0003     SPDX-FileCopyrightText: 2006 Aaron J. Seigo <aseigo@kde.org>
0004     SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
0005     SPDX-FileCopyrightText: 2007 Urs Wolfer <uwolfer @ kde.org>
0006     SPDX-FileCopyrightText: 2022 Carson Black <uhhadd@gmail.com>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #ifndef KCOREURLNAVIGATOR_H
0012 #define KCOREURLNAVIGATOR_H
0013 
0014 #include "kiogui_export.h"
0015 
0016 #include <QByteArray>
0017 #include <QObject>
0018 #include <QUrl>
0019 
0020 #include <memory>
0021 
0022 class QMouseEvent;
0023 
0024 class KFilePlacesModel;
0025 class KUrlComboBox;
0026 
0027 class KCoreUrlNavigatorPrivate;
0028 
0029 /**
0030  * @class KCoreUrlNavigator kcoreurlnavigator.h <KCoreUrlNavigator>
0031  *
0032  * @brief Object that helps with keeping track of URLs in file-manager like interfaces.
0033  *
0034  * @since 5.93
0035  */
0036 class KIOGUI_EXPORT KCoreUrlNavigator : public QObject
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041     KCoreUrlNavigator(const QUrl &url = QUrl(), QObject *parent = nullptr);
0042     ~KCoreUrlNavigator() override;
0043 
0044     Q_PROPERTY(QUrl currentLocationUrl READ currentLocationUrl WRITE setCurrentLocationUrl NOTIFY currentLocationUrlChanged)
0045 
0046     QUrl currentLocationUrl() const;
0047     void setCurrentLocationUrl(const QUrl &url);
0048     Q_SIGNAL void currentLocationUrlChanged();
0049 
0050     /**
0051      * Is emitted, before the location URL is going to be changed to \a newUrl.
0052      * The signal KCoreUrlNavigator::urlChanged() will be emitted after the change
0053      * has been done. Connecting to this signal is useful to save the state
0054      * of a view with KCoreUrlNavigator::saveLocationState().
0055      */
0056     Q_SIGNAL void currentUrlAboutToChange(const QUrl &newUrl);
0057 
0058     /**
0059      * The amount of locations in the history. The data for each
0060      * location can be retrieved by KCoreUrlNavigator::locationUrl() and
0061      * KCoreUrlNavigator::locationState().
0062      */
0063     Q_PROPERTY(int historySize READ historySize NOTIFY historySizeChanged)
0064     int historySize() const;
0065     Q_SIGNAL void historySizeChanged();
0066 
0067     /**
0068      * When the URL is changed and the new URL (e.g.\ /home/user1/)
0069      * is a parent of the previous URL (e.g.\ /home/user1/data/stuff),
0070      * then this signal is emitted and \p url is set to the child
0071      * directory of the new URL which is an ancestor of the old URL
0072      * (in the example paths this would be /home/user1/data/).
0073      * This signal allows file managers to pre-select the directory
0074      * that the user is navigating up from.
0075      * @since 5.95
0076      */
0077     Q_SIGNAL void urlSelectionRequested(const QUrl &url);
0078 
0079     /**
0080      * The history index of the current location, where
0081      * 0 <= history index < KCoreUrlNavigator::historySize(). 0 is the most
0082      * recent history entry.
0083      */
0084     Q_PROPERTY(int historyIndex READ historyIndex NOTIFY historyIndexChanged)
0085     int historyIndex() const;
0086     Q_SIGNAL void historyIndexChanged();
0087 
0088     /**
0089      * Is emitted, if the history has been changed. Usually
0090      * the history is changed if a new URL has been selected.
0091      */
0092     Q_SIGNAL void historyChanged();
0093 
0094     /**
0095      * @return URL of the location given by the \a historyIndex. If \a historyIndex
0096      *         is smaller than 0, the URL of the current location is returned.
0097      */
0098     Q_INVOKABLE QUrl locationUrl(int historyIndex = -1) const;
0099 
0100     /**
0101      * Saves the location state described by \a state for the current location. It is recommended
0102      * that at least the scroll position of a view is remembered and restored when traversing
0103      * through the history. Saving the location state should be done when the signal
0104      * KCoreUrlNavigator::urlAboutToBeChanged() has been emitted. Restoring the location state (see
0105      * KCoreUrlNavigator::locationState()) should be done when the signal KCoreUrlNavigator::urlChanged()
0106      * has been emitted.
0107      *
0108      * Example:
0109      * \code
0110      * urlNavigator->saveLocationState(QPoint(x, y));
0111      * \endcode
0112      *
0113      */
0114     Q_INVOKABLE void saveLocationState(const QVariant &state);
0115 
0116     /**
0117      * @return Location state given by \a historyIndex. If \a historyIndex
0118      *         is smaller than 0, the state of the current location is returned.
0119      * @see    KCoreUrlNavigator::saveLocationState()
0120      */
0121     Q_INVOKABLE QVariant locationState(int historyIndex = -1) const;
0122 
0123     /**
0124      * Goes back one step in the URL history. The signals
0125      * KCoreUrlNavigator::urlAboutToBeChanged(), KCoreUrlNavigator::urlChanged() and
0126      * KCoreUrlNavigator::historyChanged() are emitted if true is returned. False is returned
0127      * if the beginning of the history has already been reached and hence going back was
0128      * not possible. The history index (see KCoreUrlNavigator::historyIndex()) is
0129      * increased by one if the operation was successful.
0130      */
0131     Q_INVOKABLE bool goBack();
0132 
0133     /**
0134      * Goes forward one step in the URL history. The signals
0135      * KCoreUrlNavigator::urlAboutToBeChanged(), KCoreUrlNavigator::urlChanged() and
0136      * KCoreUrlNavigator::historyChanged() are emitted if true is returned. False is returned
0137      * if the end of the history has already been reached and hence going forward
0138      * was not possible. The history index (see KCoreUrlNavigator::historyIndex()) is
0139      * decreased by one if the operation was successful.
0140      */
0141     Q_INVOKABLE bool goForward();
0142 
0143     /**
0144      * Goes up one step of the URL path and remembers the old path
0145      * in the history. The signals KCoreUrlNavigator::urlAboutToBeChanged(),
0146      * KCoreUrlNavigator::urlChanged() and KCoreUrlNavigator::historyChanged() are
0147      * emitted if true is returned. False is returned if going up was not
0148      * possible as the root has been reached.
0149      */
0150     Q_INVOKABLE bool goUp();
0151 
0152 private:
0153     friend class KCoreUrlNavigatorPrivate;
0154     std::unique_ptr<KCoreUrlNavigatorPrivate> const d;
0155 };
0156 
0157 #endif