]>
cloud.milkyroute.net Git - dolphin.git/blob - 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>) *
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. *
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. *
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 ***************************************************************************/
21 #include "urlnavigatorbutton.h"
25 #include "urlnavigator.h"
28 #include <kio/jobclasses.h>
29 #include <kglobalsettings.h>
33 #include <QPaintEvent>
36 UrlNavigatorButton::UrlNavigatorButton(int index
, UrlNavigator
* parent
) :
43 setMinimumWidth(arrowWidth());
45 connect(this, SIGNAL(clicked()), this, SLOT(updateNavigatorUrl()));
47 m_popupDelay
= new QTimer(this);
48 connect(m_popupDelay
, SIGNAL(timeout()), this, SLOT(startListJob()));
49 connect(this, SIGNAL(pressed()), this, SLOT(startPopupDelay()));
52 UrlNavigatorButton::~UrlNavigatorButton()
56 void UrlNavigatorButton::setIndex(int index
)
64 QString
path(urlNavigator()->url().pathOrUrl());
65 setText(path
.section('/', index
, index
));
67 // Check whether the button indicates the full path of the Url. If
68 // this is the case, the button is marked as 'active'.
70 QFont
adjustedFont(font());
71 if (path
.section('/', index
, index
).isEmpty()) {
72 setDisplayHintEnabled(ActivatedHint
, true);
73 adjustedFont
.setBold(true);
76 setDisplayHintEnabled(ActivatedHint
, false);
77 adjustedFont
.setBold(false);
80 setFont(adjustedFont
);
84 QSize
UrlNavigatorButton::sizeHint() const
86 const int width
= fontMetrics().width(text()) + (arrowWidth() * 4);
87 return QSize(width
, UrlButton::sizeHint().height());
90 void UrlNavigatorButton::paintEvent(QPaintEvent
* event
)
92 QPainter
painter(this);
93 painter
.setClipRect(event
->rect());
94 const int buttonWidth
= width();
95 const int buttonHeight
= height();
97 QColor backgroundColor
;
98 QColor foregroundColor
;
99 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
100 isDisplayHintEnabled(DraggedHint
) ||
101 isDisplayHintEnabled(PopupActiveHint
);
103 backgroundColor
= KGlobalSettings::highlightColor();
104 foregroundColor
= KGlobalSettings::highlightedTextColor();
107 backgroundColor
= palette().brush(QPalette::Background
).color();
108 foregroundColor
= KGlobalSettings::buttonTextColor();
111 // dimm the colors if the parent view does not have the focus
112 const bool isActive
= urlNavigator()->isActive();
114 QColor
dimmColor(palette().brush(QPalette::Background
).color());
115 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
117 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
121 // draw button background
122 painter
.setPen(Qt::NoPen
);
123 painter
.setBrush(backgroundColor
);
124 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
126 int textWidth
= buttonWidth
;
127 if (isDisplayHintEnabled(ActivatedHint
) && isActive
|| isHighlighted
) {
128 painter
.setPen(foregroundColor
);
131 // dimm the foreground color by mixing it with the background
132 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
133 painter
.setPen(foregroundColor
);
136 if (!isDisplayHintEnabled(ActivatedHint
)) {
138 const int border
= 2; // horizontal border
139 const int middleY
= height() / 2;
140 const int width
= arrowWidth();
141 const int startX
= (buttonWidth
- width
) - (2 * border
);
142 const int startTopY
= middleY
- (width
- 1);
143 const int startBottomY
= middleY
+ (width
- 1);
144 for (int i
= 0; i
< width
; ++i
) {
145 painter
.drawLine(startX
, startTopY
+ i
, startX
+ i
, startTopY
+ i
);
146 painter
.drawLine(startX
, startBottomY
- i
, startX
+ i
, startBottomY
- i
);
149 textWidth
= startX
- border
;
152 const bool clipped
= isTextClipped();
153 const int align
= clipped
? Qt::AlignVCenter
: Qt::AlignCenter
;
154 painter
.drawText(QRect(0, 0, textWidth
, buttonHeight
), align
, text());
157 // Blend the right area of the text with the background, as the
159 // TODO: use alpha blending in Qt4 instead of drawing the text that often
160 const int blendSteps
= 16;
162 QColor
blendColor(backgroundColor
);
163 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
164 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
165 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
166 for (int i
= 0; i
< blendSteps
; ++i
) {
167 painter
.setClipRect(QRect(textWidth
- i
, 0, 1, buttonHeight
));
168 painter
.setPen(blendColor
);
169 painter
.drawText(QRect(0, 0, textWidth
, buttonHeight
), align
, text());
171 blendColor
.setRgb(blendColor
.red() + redInc
,
172 blendColor
.green() + greenInc
,
173 blendColor
.blue() + blueInc
);
178 void UrlNavigatorButton::enterEvent(QEvent
* event
)
180 UrlButton::enterEvent(event
);
182 // if the text is clipped due to a small window width, the text should
183 // be shown as tooltip
184 if (isTextClipped()) {
189 void UrlNavigatorButton::leaveEvent(QEvent
* event
)
191 UrlButton::leaveEvent(event
);
192 setToolTip(QString());
195 void UrlNavigatorButton::dropEvent(QDropEvent
* event
)
201 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
202 if (!urls
.isEmpty()) {
203 event
->acceptProposedAction();
205 setDisplayHintEnabled(DraggedHint
, true);
207 QString
path(urlNavigator()->url().prettyUrl());
208 path
= path
.section('/', 0, m_index
+ 2);
210 urlNavigator()->dropUrls(urls
, KUrl(path
));
212 setDisplayHintEnabled(DraggedHint
, false);
217 void UrlNavigatorButton::dragEnterEvent(QDragEnterEvent
* event
)
219 if (event
->mimeData()->hasUrls()) {
220 setDisplayHintEnabled(DraggedHint
, true);
221 event
->acceptProposedAction();
227 void UrlNavigatorButton::dragLeaveEvent(QDragLeaveEvent
* event
)
229 UrlButton::dragLeaveEvent(event
);
231 setDisplayHintEnabled(DraggedHint
, false);
236 void UrlNavigatorButton::updateNavigatorUrl()
242 urlNavigator()->setUrl(urlNavigator()->url(m_index
));
245 void UrlNavigatorButton::startPopupDelay()
247 if (m_popupDelay
->isActive() || m_listJob
|| m_index
< 0) {
251 m_popupDelay
->setSingleShot(true);
252 m_popupDelay
->start(300);
255 void UrlNavigatorButton::stopPopupDelay()
257 m_popupDelay
->stop();
264 void UrlNavigatorButton::startListJob()
270 const KUrl
& url
= urlNavigator()->url(m_index
);
271 m_listJob
= KIO::listDir(url
, false, false);
272 m_subdirs
.clear(); // just to be ++safe
274 connect(m_listJob
, SIGNAL(entries(KIO::Job
*, const KIO::UDSEntryList
&)),
275 this, SLOT(entriesList(KIO::Job
*, const KIO::UDSEntryList
&)));
276 connect(m_listJob
, SIGNAL(result(KJob
*)), this, SLOT(listJobFinished(KJob
*)));
279 void UrlNavigatorButton::entriesList(KIO::Job
* job
, const KIO::UDSEntryList
& entries
)
281 if (job
!= m_listJob
) {
285 KIO::UDSEntryList::const_iterator it
= entries
.constBegin();
286 KIO::UDSEntryList::const_iterator itEnd
= entries
.constEnd();
287 while (it
!= itEnd
) {
289 //bool isDir = false;
290 KIO::UDSEntry entry
= *it
;
293 KIO::UDSEntry::const_iterator atomIt = entry.constBegin();
294 KIO::UDSEntry::const_iterator atomEndIt = entry.constEnd();
296 while (atomIt != atomEndIt) {
297 switch ((*atomIt).m_uds) {
299 name = (*atomIt).m_str;
301 case KIO::UDS_FILE_TYPE:
302 isDir = S_ISDIR((*atomIt).m_long);
310 m_subdirs.append(name);
315 m_subdirs
.append(entry
.stringValue(KIO::UDS_NAME
));
324 void UrlNavigatorButton::listJobFinished(KJob
* job
)
326 if (job
!= m_listJob
) {
330 if (job
->error() || m_subdirs
.isEmpty()) {
335 setDisplayHintEnabled(PopupActiveHint
, true);
336 update(); // ensure the button is drawn highlighted
338 KMenu
* dirsMenu
= new KMenu(this);
339 QStringList::const_iterator it
= m_subdirs
.constBegin();
340 QStringList::const_iterator itEnd
= m_subdirs
.constEnd();
342 while (it
!= itEnd
) {
343 QAction
* action
= new QAction(*it
, this);
345 dirsMenu
->addAction(action
);
350 const QAction
* action
= dirsMenu
->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));
352 const int result
= action
->data().toInt();
353 KUrl url
= urlNavigator()->url(m_index
);
354 url
.addPath(m_subdirs
[result
]);
355 urlNavigator()->setUrl(url
);
363 setDisplayHintEnabled(PopupActiveHint
, false);
366 int UrlNavigatorButton::arrowWidth() const
368 int width
= (height() / 2) - 7;
375 bool UrlNavigatorButton::isTextClipped() const
377 int availableWidth
= width();
378 if (!isDisplayHintEnabled(ActivatedHint
)) {
379 availableWidth
-= arrowWidth() + 1;
382 QFontMetrics
fontMetrics(font());
383 return fontMetrics
.width(text()) >= availableWidth
;
386 #include "urlnavigatorbutton.moc"