File indexing completed on 2024-03-03 04:03:36

0001 /*
0002     SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
0003     SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KURLNAVIGATORPLACESSELECTOR_P_H
0009 #define KURLNAVIGATORPLACESSELECTOR_P_H
0010 
0011 #include "kurlnavigatorbuttonbase_p.h"
0012 #include <QUrl>
0013 
0014 #include <QPersistentModelIndex>
0015 
0016 class KFilePlacesModel;
0017 class QMenu;
0018 
0019 namespace KDEPrivate
0020 {
0021 /**
0022  * @brief Allows to select a bookmark from a popup menu.
0023  *
0024  * The icon from the current selected bookmark is shown
0025  * inside the bookmark selector.
0026  *
0027  * @see KUrlNavigator
0028  * @internal
0029  */
0030 class KUrlNavigatorPlacesSelector : public KUrlNavigatorButtonBase
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     /**
0036      * @param parent Parent widget where the bookmark selector
0037      *               is embedded into.
0038      */
0039     KUrlNavigatorPlacesSelector(KUrlNavigator *parent, KFilePlacesModel *placesModel);
0040 
0041     ~KUrlNavigatorPlacesSelector() override;
0042 
0043     using ActivationSignal = void (KUrlNavigatorPlacesSelector::*)(const QUrl &);
0044 
0045     /**
0046      * Updates the selection dependent from the given URL \a url. The
0047      * URL must not match exactly to one of the available bookmarks:
0048      * The bookmark which is equal to the URL or at least is a parent URL
0049      * is selected. If there are more than one possible parent URL candidates,
0050      * the bookmark which covers the bigger range of the URL is selected.
0051      */
0052     void updateSelection(const QUrl &url);
0053 
0054     /** Returns the selected bookmark. */
0055     QUrl selectedPlaceUrl() const;
0056     /** Returns the selected bookmark. */
0057     QString selectedPlaceText() const;
0058 
0059     /** @see QWidget::sizeHint() */
0060     QSize sizeHint() const override;
0061 
0062 Q_SIGNALS:
0063     /**
0064      * Is send when a bookmark has been activated by the user.
0065      * @param url URL of the selected place.
0066      */
0067     void placeActivated(const QUrl &url);
0068 
0069     /**
0070      * Is sent when a bookmark was middle clicked by the user
0071      * and thus should be opened in a new tab.
0072      */
0073     void tabRequested(const QUrl &url);
0074 
0075 protected:
0076     /**
0077      * Draws the icon of the selected Url as content of the Url
0078      * selector.
0079      */
0080     void paintEvent(QPaintEvent *event) override;
0081 
0082     void dragEnterEvent(QDragEnterEvent *event) override;
0083     void dragLeaveEvent(QDragLeaveEvent *event) override;
0084     void dropEvent(QDropEvent *event) override;
0085     void mouseReleaseEvent(QMouseEvent *event) override;
0086 
0087     bool eventFilter(QObject *watched, QEvent *event) override;
0088 
0089 private Q_SLOTS:
0090     /**
0091      * Updates the selected index and the icon to the bookmark
0092      * which is indicated by the triggered action \a action.
0093      */
0094     void activatePlace(QAction *action, ActivationSignal activationSignal);
0095 
0096     void updateMenu();
0097 
0098     void onStorageSetupDone(const QModelIndex &index, bool success);
0099 
0100 private:
0101     int m_selectedItem;
0102     QPersistentModelIndex m_lastClickedIndex;
0103     ActivationSignal m_lastActivationSignal = nullptr;
0104     QMenu *m_placesMenu;
0105     KFilePlacesModel *m_placesModel;
0106     QUrl m_selectedUrl;
0107 };
0108 
0109 } // namespace KDEPrivate
0110 
0111 #endif