]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kurlnavigatorbutton.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 "kurlnavigatorbutton_p.h"
25 #include "kurlnavigator.h"
28 #include <kio/jobclasses.h>
29 #include <kglobalsettings.h>
33 #include <QPaintEvent>
36 KUrlNavigatorButton::KUrlNavigatorButton(int index
, KUrlNavigator
* parent
) :
43 setMinimumWidth(arrowWidth());
45 connect(this, SIGNAL(clicked()), this, SLOT(updateNavigatorUrl()));
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()));
53 KUrlNavigatorButton::~KUrlNavigatorButton()
57 void KUrlNavigatorButton::setIndex(int index
)
65 QString
path(urlNavigator()->url().pathOrUrl());
66 setText(path
.section('/', index
, index
));
68 // Check whether the button indicates the full path of the Url. If
69 // this is the case, the button is marked as 'active'.
71 QFont
adjustedFont(font());
72 if (path
.section('/', index
, index
).isEmpty()) {
73 setDisplayHintEnabled(ActivatedHint
, true);
74 adjustedFont
.setBold(true);
77 setDisplayHintEnabled(ActivatedHint
, false);
78 adjustedFont
.setBold(false);
81 setFont(adjustedFont
);
85 QSize
KUrlNavigatorButton::sizeHint() const
87 const int width
= fontMetrics().width(text()) + (arrowWidth() * 4);
88 return QSize(width
, KUrlButton::sizeHint().height());
91 void KUrlNavigatorButton::paintEvent(QPaintEvent
* event
)
93 QPainter
painter(this);
94 painter
.setClipRect(event
->rect());
95 const int buttonWidth
= width();
96 const int buttonHeight
= height();
98 QColor backgroundColor
;
99 QColor foregroundColor
;
100 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
101 isDisplayHintEnabled(DraggedHint
) ||
102 isDisplayHintEnabled(PopupActiveHint
);
104 backgroundColor
= KGlobalSettings::highlightColor();
105 foregroundColor
= KGlobalSettings::highlightedTextColor();
108 backgroundColor
= palette().brush(QPalette::Background
).color();
109 foregroundColor
= KGlobalSettings::buttonTextColor();
112 // dimm the colors if the parent view does not have the focus
113 const bool isActive
= urlNavigator()->isActive();
115 QColor
dimmColor(palette().brush(QPalette::Background
).color());
116 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
118 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
122 // draw button background
123 painter
.setPen(Qt::NoPen
);
124 painter
.setBrush(backgroundColor
);
125 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
127 int textWidth
= buttonWidth
;
128 if (isDisplayHintEnabled(ActivatedHint
) && isActive
|| isHighlighted
) {
129 painter
.setPen(foregroundColor
);
132 // dimm the foreground color by mixing it with the background
133 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
134 painter
.setPen(foregroundColor
);
137 if (!isDisplayHintEnabled(ActivatedHint
)) {
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
);
150 textWidth
= startX
- border
;
153 const bool clipped
= isTextClipped();
154 const int align
= clipped
? Qt::AlignVCenter
: Qt::AlignCenter
;
155 const QRect
textRect(0, 0, textWidth
, buttonHeight
);
157 QLinearGradient
gradient(textRect
.topLeft(), textRect
.topRight());
158 gradient
.setColorAt(0.8, foregroundColor
);
159 gradient
.setColorAt(1.0, backgroundColor
);
162 pen
.setBrush(QBrush(gradient
));
164 painter
.drawText(textRect
, align
, text());
167 painter
.drawText(textRect
, align
, text());
171 void KUrlNavigatorButton::enterEvent(QEvent
* event
)
173 KUrlButton::enterEvent(event
);
175 // if the text is clipped due to a small window width, the text should
176 // be shown as tooltip
177 if (isTextClipped()) {
182 void KUrlNavigatorButton::leaveEvent(QEvent
* event
)
184 KUrlButton::leaveEvent(event
);
185 setToolTip(QString());
188 void KUrlNavigatorButton::dropEvent(QDropEvent
* event
)
194 const KUrl::List urls
= KUrl::List::fromMimeData(event
->mimeData());
195 if (!urls
.isEmpty()) {
196 event
->acceptProposedAction();
198 setDisplayHintEnabled(DraggedHint
, true);
200 QString
path(urlNavigator()->url().prettyUrl());
201 path
= path
.section('/', 0, m_index
+ 2);
203 urlNavigator()->dropUrls(urls
, KUrl(path
));
205 setDisplayHintEnabled(DraggedHint
, false);
210 void KUrlNavigatorButton::dragEnterEvent(QDragEnterEvent
* event
)
212 if (event
->mimeData()->hasUrls()) {
213 setDisplayHintEnabled(DraggedHint
, true);
214 event
->acceptProposedAction();
220 void KUrlNavigatorButton::dragLeaveEvent(QDragLeaveEvent
* event
)
222 KUrlButton::dragLeaveEvent(event
);
224 setDisplayHintEnabled(DraggedHint
, false);
229 void KUrlNavigatorButton::updateNavigatorUrl()
237 urlNavigator()->setUrl(urlNavigator()->url(m_index
));
240 void KUrlNavigatorButton::startPopupDelay()
242 if (m_popupDelay
->isActive() || (m_listJob
!= 0) || (m_index
< 0)) {
246 m_popupDelay
->start(300);
249 void KUrlNavigatorButton::stopPopupDelay()
251 m_popupDelay
->stop();
252 if (m_listJob
!= 0) {
258 void KUrlNavigatorButton::startListJob()
260 if (m_listJob
!= 0) {
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
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
*)));
273 void KUrlNavigatorButton::entriesList(KIO::Job
* job
, const KIO::UDSEntryList
& entries
)
275 if (job
!= m_listJob
) {
279 KIO::UDSEntryList::const_iterator it
= entries
.constBegin();
280 KIO::UDSEntryList::const_iterator itEnd
= entries
.constEnd();
282 bool showHidden
= urlNavigator()->showHiddenFiles();
283 while (it
!= itEnd
) {
284 const KIO::UDSEntry entry
= *it
;
286 QString name
= entry
.stringValue(KIO::UDS_NAME
);
288 if (!showHidden
|| (name
!= "." && name
!= "..")) {
289 m_subdirs
.append(name
);
299 void KUrlNavigatorButton::listJobFinished(KJob
* job
)
301 if (job
!= m_listJob
) {
305 if (job
->error() || m_subdirs
.isEmpty()) {
310 setDisplayHintEnabled(PopupActiveHint
, true);
311 update(); // ensure the button is drawn highlighted
313 KMenu
* dirsMenu
= new KMenu(this);
314 QStringList::const_iterator it
= m_subdirs
.constBegin();
315 QStringList::const_iterator itEnd
= m_subdirs
.constEnd();
317 while (it
!= itEnd
) {
318 QAction
* action
= new QAction(*it
, this);
320 dirsMenu
->addAction(action
);
325 const QAction
* action
= dirsMenu
->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));
327 const int result
= action
->data().toInt();
328 KUrl url
= urlNavigator()->url(m_index
);
329 url
.addPath(m_subdirs
[result
]);
330 urlNavigator()->setUrl(url
);
338 setDisplayHintEnabled(PopupActiveHint
, false);
341 int KUrlNavigatorButton::arrowWidth() const
343 int width
= (height() / 2) - 7;
350 bool KUrlNavigatorButton::isTextClipped() const
352 int availableWidth
= width();
353 if (!isDisplayHintEnabled(ActivatedHint
)) {
354 availableWidth
-= arrowWidth() + 1;
357 QFontMetrics
fontMetrics(font());
358 return fontMetrics
.width(text()) >= availableWidth
;
361 #include "kurlnavigatorbutton_p.moc"