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