1 | /* |
2 | SPDX-FileCopyrightText: 2006 Peter Penz <peter.penz@gmx.at> |
3 | SPDX-FileCopyrightText: 2006 Aaron J. Seigo <aseigo@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef KURLNAVIGATORBUTTON_P_H |
9 | #define KURLNAVIGATORBUTTON_P_H |
10 | |
11 | #include "kurlnavigatorbuttonbase_p.h" |
12 | #include "kurlnavigatormenu_p.h" |
13 | |
14 | #include <kio/global.h> |
15 | #include <kio/udsentry.h> |
16 | |
17 | #include <QPointer> |
18 | #include <QUrl> |
19 | |
20 | class KJob; |
21 | class QDropEvent; |
22 | class QPaintEvent; |
23 | |
24 | namespace KIO |
25 | { |
26 | class ListJob; |
27 | class Job; |
28 | } |
29 | |
30 | namespace KDEPrivate |
31 | { |
32 | /** |
33 | * @brief Button of the URL navigator which contains one part of an URL. |
34 | * |
35 | * It is possible to drop a various number of items to an UrlNavigatorButton. In this case |
36 | * a context menu is opened where the user must select whether he wants |
37 | * to copy, move or link the dropped items to the URL part indicated by |
38 | * the button. |
39 | */ |
40 | class KUrlNavigatorButton : public KUrlNavigatorButtonBase |
41 | { |
42 | Q_OBJECT |
43 | Q_PROPERTY(QString plainText READ plainText) // for the unittest |
44 | |
45 | public: |
46 | explicit KUrlNavigatorButton(const QUrl &url, KUrlNavigator *parent); |
47 | ~KUrlNavigatorButton() override; |
48 | |
49 | void setUrl(const QUrl &url); |
50 | QUrl url() const; |
51 | |
52 | /* Implementation note: QAbstractButton::setText() is not virtual, |
53 | * but KUrlNavigatorButton needs to adjust the minimum size when |
54 | * the text has been changed. KUrlNavigatorButton::setText() hides |
55 | * QAbstractButton::setText() which is not nice, but sufficient for |
56 | * the usage in KUrlNavigator. |
57 | */ |
58 | void setText(const QString &text); |
59 | |
60 | /** |
61 | * Sets the name of the sub directory that should be marked when |
62 | * opening the sub directories popup. |
63 | */ |
64 | void setActiveSubDirectory(const QString &subDir); |
65 | QString activeSubDirectory() const; |
66 | |
67 | /** @see QWidget::sizeHint() */ |
68 | QSize sizeHint() const override; |
69 | |
70 | void setShowMnemonic(bool show); |
71 | bool showMnemonic() const; |
72 | |
73 | struct SubDirInfo { |
74 | QString name; |
75 | QString displayName; |
76 | }; |
77 | |
78 | Q_SIGNALS: |
79 | /** |
80 | * Emitted when URLs are dropped on the KUrlNavigatorButton associated with |
81 | * the URL @p destination. |
82 | */ |
83 | void urlsDroppedOnNavButton(const QUrl &destination, QDropEvent *event); |
84 | |
85 | void navigatorButtonActivated(const QUrl &url, Qt::MouseButton button, Qt::KeyboardModifiers modifiers); |
86 | |
87 | /** |
88 | * Is emitted, if KUrlNavigatorButton::setUrl() cannot resolve |
89 | * the text synchronously and KUrlNavigator::text() will return |
90 | * an empty string in this case. The signal finishedTextResolving() is |
91 | * emitted, as soon as the text has been resolved. |
92 | */ |
93 | void startedTextResolving(); |
94 | |
95 | /** |
96 | * Is emitted, if the asynchronous resolving of the text has |
97 | * been finished (see startTextResolving()). |
98 | * KUrlNavigatorButton::text() contains the resolved text. |
99 | */ |
100 | void finishedTextResolving(); |
101 | |
102 | protected: |
103 | void enterEvent(QEnterEvent *event) override; |
104 | |
105 | void paintEvent(QPaintEvent *event) override; |
106 | void leaveEvent(QEvent *event) override; |
107 | void keyPressEvent(QKeyEvent *event) override; |
108 | void dropEvent(QDropEvent *event) override; |
109 | void dragEnterEvent(QDragEnterEvent *event) override; |
110 | void dragMoveEvent(QDragMoveEvent *event) override; |
111 | void dragLeaveEvent(QDragLeaveEvent *event) override; |
112 | void mousePressEvent(QMouseEvent *event) override; |
113 | void mouseReleaseEvent(QMouseEvent *event) override; |
114 | void mouseMoveEvent(QMouseEvent *event) override; |
115 | void wheelEvent(QWheelEvent *event) override; |
116 | |
117 | private Q_SLOTS: |
118 | /** |
119 | * Requests to load the sub-directories after a short delay. |
120 | * startSubDirsJob() is invoked if the delay is exceeded. |
121 | */ |
122 | void requestSubDirs(); |
123 | |
124 | /** |
125 | * Starts to load the sub directories asynchronously. The directories |
126 | * are stored in m_subDirs by addEntriesToSubDirs(). |
127 | */ |
128 | void startSubDirsJob(); |
129 | |
130 | /** |
131 | * Adds the entries from the sub-directories job to m_subDirs. The entries |
132 | * will be shown if the job has been finished in openSubDirsMenu() or |
133 | * replaceButton(). |
134 | */ |
135 | void addEntriesToSubDirs(KIO::Job *job, const KIO::UDSEntryList &entries); |
136 | |
137 | /** |
138 | * Is called after the sub-directories job has been finished and opens a menu |
139 | * showing all sub directories. |
140 | */ |
141 | void (KJob *job); |
142 | |
143 | /** |
144 | * Is called after the sub-directories job has been finished and replaces |
145 | * the button content by the current sub directory (triggered by |
146 | * the scroll wheel). |
147 | */ |
148 | void replaceButton(KJob *job); |
149 | |
150 | void slotUrlsDropped(QAction *action, QDropEvent *event); |
151 | |
152 | /** |
153 | * Is called, if an action of a sub-menu has been triggered by |
154 | * a click. |
155 | */ |
156 | void (QAction *action, Qt::MouseButton button); |
157 | |
158 | void statFinished(KJob *); |
159 | |
160 | private: |
161 | /** |
162 | * Cancels any request done by requestSubDirs(). |
163 | */ |
164 | void cancelSubDirsRequest(); |
165 | |
166 | /** |
167 | * @return Text without mnemonic characters. |
168 | */ |
169 | QString plainText() const; |
170 | |
171 | int arrowWidth() const; |
172 | bool isAboveArrow(int x) const; |
173 | bool isTextClipped() const; |
174 | void updateMinimumWidth(); |
175 | void (KUrlNavigatorMenu *, int startIndex); |
176 | |
177 | private: |
178 | bool m_hoverArrow; |
179 | bool m_pendingTextChange; |
180 | bool m_replaceButton; |
181 | bool m_showMnemonic; |
182 | int m_wheelSteps; |
183 | QUrl m_url; |
184 | |
185 | QString m_subDir; |
186 | QTimer *m_openSubDirsTimer; |
187 | static QPointer<KUrlNavigatorMenu> ; |
188 | KIO::ListJob *m_subDirsJob; |
189 | std::vector<SubDirInfo> m_subDirs; |
190 | }; |
191 | |
192 | } // namespace KDEPrivate |
193 | |
194 | #endif |
195 | |