1 /*
2     SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at>
3     SPDX-FileCopyrightText: 2007 Kevin Ottens <ervin@kde.org>
4 
5     SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #ifndef KURLNAVIGATORPLACESSELECTOR_P_H
9 #define KURLNAVIGATORPLACESSELECTOR_P_H
10 
11 #include "kurlnavigatorbuttonbase_p.h"
12 #include <QUrl>
13 
14 #include <QPersistentModelIndex>
15 
16 class KFilePlacesModel;
17 class QMenu;
18 
19 namespace KDEPrivate
20 {
21 /**
22  * @brief Allows to select a bookmark from a popup menu.
23  *
24  * The icon from the current selected bookmark is shown
25  * inside the bookmark selector.
26  *
27  * @see KUrlNavigator
28  * @internal
29  */
30 class KUrlNavigatorPlacesSelector : public KUrlNavigatorButtonBase
31 {
32     Q_OBJECT
33 
34 public:
35     /**
36      * @param parent Parent widget where the bookmark selector
37      *               is embedded into.
38      */
39     KUrlNavigatorPlacesSelector(KUrlNavigator *parent, KFilePlacesModel *placesModel);
40 
41     ~KUrlNavigatorPlacesSelector() override;
42 
43     /**
44      * Updates the selection dependent from the given URL \a url. The
45      * URL must not match exactly to one of the available bookmarks:
46      * The bookmark which is equal to the URL or at least is a parent URL
47      * is selected. If there are more than one possible parent URL candidates,
48      * the bookmark which covers the bigger range of the URL is selected.
49      */
50     void updateSelection(const QUrl &url);
51 
52     /** Returns the selected bookmark. */
53     QUrl selectedPlaceUrl() const;
54     /** Returns the selected bookmark. */
55     QString selectedPlaceText() const;
56 
57     /** @see QWidget::sizeHint() */
58     QSize sizeHint() const override;
59 
60 Q_SIGNALS:
61     /**
62      * Is send when a bookmark has been activated by the user.
63      * @param url URL of the selected place.
64      */
65     void placeActivated(const QUrl &url);
66 
67     /**
68      * Is sent when a bookmark was middle clicked by the user
69      * and thus should be opened in a new tab.
70      */
71     void tabRequested(const QUrl &url);
72 
73 protected:
74     /**
75      * Draws the icon of the selected Url as content of the Url
76      * selector.
77      */
78     void paintEvent(QPaintEvent *event) override;
79 
80     void dragEnterEvent(QDragEnterEvent *event) override;
81     void dragLeaveEvent(QDragLeaveEvent *event) override;
82     void dropEvent(QDropEvent *event) override;
83     void mouseReleaseEvent(QMouseEvent *event) override;
84 
85     bool eventFilter(QObject *watched, QEvent *event) override;
86 
87 private Q_SLOTS:
88     /**
89      * Updates the selected index and the icon to the bookmark
90      * which is indicated by the triggered action \a action.
91      */
92     void activatePlace(QAction *action);
93 
94     void updateMenu();
95     void updateTeardownAction();
96 
97     void onStorageSetupDone(const QModelIndex &index, bool success);
98 
99 private:
100     int m_selectedItem;
101     QPersistentModelIndex m_lastClickedIndex;
102     QMenu *m_placesMenu;
103     KFilePlacesModel *m_placesModel;
104     QUrl m_selectedUrl;
105 };
106 
107 } // namespace KDEPrivate
108 
109 #endif
110