]> cloud.milkyroute.net Git - dolphin.git/blob - src/urlnavigatorbutton.cpp
Patch by Luca Gugelmann: get rid of any mainwindow dependency from the sidebars.
[dolphin.git] / src / urlnavigatorbutton.cpp
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 program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program 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 *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "urlnavigatorbutton.h"
22
23 #include <assert.h>
24
25 #include "urlnavigator.h"
26
27 #include <kio/job.h>
28 #include <kio/jobclasses.h>
29 #include <kglobalsettings.h>
30 #include <kmenu.h>
31
32 #include <QPainter>
33 #include <QPaintEvent>
34 #include <QTimer>
35
36 UrlNavigatorButton::UrlNavigatorButton(int index, UrlNavigator* parent) :
37 UrlButton(parent),
38 m_index(-1),
39 m_popupDelay(0),
40 m_listJob(0)
41 {
42 setAcceptDrops(true);
43 setMinimumWidth(arrowWidth());
44 setIndex(index);
45 connect(this, SIGNAL(clicked()), this, SLOT(updateNavigatorUrl()));
46
47 m_popupDelay = new QTimer(this);
48 m_popupDelay->setSingleShot(true);
49 connect(m_popupDelay, SIGNAL(timeout()), this, SLOT(startListJob()));
50 connect(this, SIGNAL(pressed()), this, SLOT(startPopupDelay()));
51 }
52
53 UrlNavigatorButton::~UrlNavigatorButton()
54 {
55 }
56
57 void UrlNavigatorButton::setIndex(int index)
58 {
59 m_index = index;
60
61 if (m_index < 0) {
62 return;
63 }
64
65 QString path(urlNavigator()->url().pathOrUrl());
66 setText(path.section('/', index, index));
67
68 // Check whether the button indicates the full path of the Url. If
69 // this is the case, the button is marked as 'active'.
70 ++index;
71 QFont adjustedFont(font());
72 if (path.section('/', index, index).isEmpty()) {
73 setDisplayHintEnabled(ActivatedHint, true);
74 adjustedFont.setBold(true);
75 }
76 else {
77 setDisplayHintEnabled(ActivatedHint, false);
78 adjustedFont.setBold(false);
79 }
80
81 setFont(adjustedFont);
82 update();
83 }
84
85 QSize UrlNavigatorButton::sizeHint() const
86 {
87 const int width = fontMetrics().width(text()) + (arrowWidth() * 4);
88 return QSize(width, UrlButton::sizeHint().height());
89 }
90
91 void UrlNavigatorButton::paintEvent(QPaintEvent* event)
92 {
93 QPainter painter(this);
94 painter.setClipRect(event->rect());
95 const int buttonWidth = width();
96 const int buttonHeight = height();
97
98 QColor backgroundColor;
99 QColor foregroundColor;
100 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
101 isDisplayHintEnabled(DraggedHint) ||
102 isDisplayHintEnabled(PopupActiveHint);
103 if (isHighlighted) {
104 backgroundColor = KGlobalSettings::highlightColor();
105 foregroundColor = KGlobalSettings::highlightedTextColor();
106 }
107 else {
108 backgroundColor = palette().brush(QPalette::Background).color();
109 foregroundColor = KGlobalSettings::buttonTextColor();
110 }
111
112 // dimm the colors if the parent view does not have the focus
113 const bool isActive = urlNavigator()->isActive();
114 if (!isActive) {
115 QColor dimmColor(palette().brush(QPalette::Background).color());
116 foregroundColor = mixColors(foregroundColor, dimmColor);
117 if (isHighlighted) {
118 backgroundColor = mixColors(backgroundColor, dimmColor);
119 }
120 }
121
122 // draw button background
123 painter.setPen(Qt::NoPen);
124 painter.setBrush(backgroundColor);
125 painter.drawRect(0, 0, buttonWidth, buttonHeight);
126
127 int textWidth = buttonWidth;
128 if (isDisplayHintEnabled(ActivatedHint) && isActive || isHighlighted) {
129 painter.setPen(foregroundColor);
130 }
131 else {
132 // dimm the foreground color by mixing it with the background
133 foregroundColor = mixColors(foregroundColor, backgroundColor);
134 painter.setPen(foregroundColor);
135 }
136
137 if (!isDisplayHintEnabled(ActivatedHint)) {
138 // draw arrow
139 const int border = 2; // horizontal border
140 const int middleY = height() / 2;
141 const int width = arrowWidth();
142 const int startX = (buttonWidth - width) - (2 * border);
143 const int startTopY = middleY - (width - 1);
144 const int startBottomY = middleY + (width - 1);
145 for (int i = 0; i < width; ++i) {
146 painter.drawLine(startX, startTopY + i, startX + i, startTopY + i);
147 painter.drawLine(startX, startBottomY - i, startX + i, startBottomY - i);
148 }
149
150 textWidth = startX - border;
151 }
152
153 const bool clipped = isTextClipped();
154 const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
155 const QRect textRect(0, 0, textWidth, buttonHeight);
156 if (clipped) {
157 QLinearGradient gradient(textRect.topLeft(), textRect.topRight());
158 gradient.setColorAt(0.8, foregroundColor);
159 gradient.setColorAt(1.0, backgroundColor);
160
161 QPen pen;
162 pen.setBrush(QBrush(gradient));
163 painter.setPen(pen);
164 painter.drawText(textRect, align, text());
165 }
166 else {
167 painter.drawText(textRect, align, text());
168 }
169 }
170
171 void UrlNavigatorButton::enterEvent(QEvent* event)
172 {
173 UrlButton::enterEvent(event);
174
175 // if the text is clipped due to a small window width, the text should
176 // be shown as tooltip
177 if (isTextClipped()) {
178 setToolTip(text());
179 }
180 }
181
182 void UrlNavigatorButton::leaveEvent(QEvent* event)
183 {
184 UrlButton::leaveEvent(event);
185 setToolTip(QString());
186 }
187
188 void UrlNavigatorButton::dropEvent(QDropEvent* event)
189 {
190 if (m_index < 0) {
191 return;
192 }
193
194 const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
195 if (!urls.isEmpty()) {
196 event->acceptProposedAction();
197
198 setDisplayHintEnabled(DraggedHint, true);
199
200 QString path(urlNavigator()->url().prettyUrl());
201 path = path.section('/', 0, m_index + 2);
202
203 urlNavigator()->dropUrls(urls, KUrl(path));
204
205 setDisplayHintEnabled(DraggedHint, false);
206 update();
207 }
208 }
209
210 void UrlNavigatorButton::dragEnterEvent(QDragEnterEvent* event)
211 {
212 if (event->mimeData()->hasUrls()) {
213 setDisplayHintEnabled(DraggedHint, true);
214 event->acceptProposedAction();
215
216 update();
217 }
218 }
219
220 void UrlNavigatorButton::dragLeaveEvent(QDragLeaveEvent* event)
221 {
222 UrlButton::dragLeaveEvent(event);
223
224 setDisplayHintEnabled(DraggedHint, false);
225 update();
226 }
227
228
229 void UrlNavigatorButton::updateNavigatorUrl()
230 {
231 stopPopupDelay();
232
233 if (m_index < 0) {
234 return;
235 }
236
237 urlNavigator()->setUrl(urlNavigator()->url(m_index));
238 }
239
240 void UrlNavigatorButton::startPopupDelay()
241 {
242 if (m_popupDelay->isActive() || (m_listJob != 0) || (m_index < 0)) {
243 return;
244 }
245
246 m_popupDelay->start(300);
247 }
248
249 void UrlNavigatorButton::stopPopupDelay()
250 {
251 m_popupDelay->stop();
252 if (m_listJob != 0) {
253 m_listJob->kill();
254 m_listJob = 0;
255 }
256 }
257
258 void UrlNavigatorButton::startListJob()
259 {
260 if (m_listJob != 0) {
261 return;
262 }
263
264 const KUrl& url = urlNavigator()->url(m_index);
265 m_listJob = KIO::listDir(url, false, urlNavigator()->showHiddenFiles());
266 m_subdirs.clear(); // just to be ++safe
267
268 connect(m_listJob, SIGNAL(entries(KIO::Job*, const KIO::UDSEntryList &)),
269 this, SLOT(entriesList(KIO::Job*, const KIO::UDSEntryList&)));
270 connect(m_listJob, SIGNAL(result(KJob*)), this, SLOT(listJobFinished(KJob*)));
271 }
272
273 void UrlNavigatorButton::entriesList(KIO::Job* job, const KIO::UDSEntryList& entries)
274 {
275 if (job != m_listJob) {
276 return;
277 }
278
279 KIO::UDSEntryList::const_iterator it = entries.constBegin();
280 KIO::UDSEntryList::const_iterator itEnd = entries.constEnd();
281
282 bool showHidden = urlNavigator()->showHiddenFiles();
283 while (it != itEnd) {
284 QString name;
285 //bool isDir = false;
286 KIO::UDSEntry entry = *it;
287
288 /* KDE3 reference:
289 KIO::UDSEntry::const_iterator atomIt = entry.constBegin();
290 KIO::UDSEntry::const_iterator atomEndIt = entry.constEnd();
291
292 while (atomIt != atomEndIt) {
293 switch ((*atomIt).m_uds) {
294 case KIO::UDS_NAME:
295 name = (*atomIt).m_str;
296 break;
297 case KIO::UDS_FILE_TYPE:
298 isDir = S_ISDIR((*atomIt).m_long);
299 break;
300 default:
301 break;
302 }
303 ++atomIt;
304 }
305 if (isDir) {
306 m_subdirs.append(name);
307 }
308 */
309
310 if (entry.isDir()) {
311 QString dir = entry.stringValue(KIO::UDS_NAME);
312
313 if (!showHidden || (dir != "." && dir != "..")) {
314 m_subdirs.append(entry.stringValue(KIO::UDS_NAME));
315 }
316 }
317
318 ++it;
319 }
320
321 m_subdirs.sort();
322 }
323
324 void UrlNavigatorButton::listJobFinished(KJob* job)
325 {
326 if (job != m_listJob) {
327 return;
328 }
329
330 if (job->error() || m_subdirs.isEmpty()) {
331 // clear listing
332 return;
333 }
334
335 setDisplayHintEnabled(PopupActiveHint, true);
336 update(); // ensure the button is drawn highlighted
337
338 KMenu* dirsMenu = new KMenu(this);
339 QStringList::const_iterator it = m_subdirs.constBegin();
340 QStringList::const_iterator itEnd = m_subdirs.constEnd();
341 int i = 0;
342 while (it != itEnd) {
343 QAction* action = new QAction(*it, this);
344 action->setData(i);
345 dirsMenu->addAction(action);
346 ++it;
347 ++i;
348 }
349
350 const QAction* action = dirsMenu->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));
351 if (action != 0) {
352 const int result = action->data().toInt();
353 KUrl url = urlNavigator()->url(m_index);
354 url.addPath(m_subdirs[result]);
355 urlNavigator()->setUrl(url);
356 }
357
358 m_listJob = 0;
359 m_subdirs.clear();
360 delete dirsMenu;
361 dirsMenu = 0;
362
363 setDisplayHintEnabled(PopupActiveHint, false);
364 }
365
366 int UrlNavigatorButton::arrowWidth() const
367 {
368 int width = (height() / 2) - 7;
369 if (width < 4) {
370 width = 4;
371 }
372 return width;
373 }
374
375 bool UrlNavigatorButton::isTextClipped() const
376 {
377 int availableWidth = width();
378 if (!isDisplayHintEnabled(ActivatedHint)) {
379 availableWidth -= arrowWidth() + 1;
380 }
381
382 QFontMetrics fontMetrics(font());
383 return fontMetrics.width(text()) >= availableWidth;
384 }
385
386 #include "urlnavigatorbutton.moc"