]>
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "urlnavigatorbutton.h"
23 #include <qfontmetrics.h>
29 #include <QDragLeaveEvent>
30 #include <Q3PopupMenu>
32 #include <QDragEnterEvent>
34 #include <kglobalsettings.h>
35 #include <kiconloader.h>
36 #include <kio/jobclasses.h>
41 #include "urlnavigator.h"
42 #include "dolphinview.h"
45 URLNavigatorButton::URLNavigatorButton(int index
, URLNavigator
* parent
) :
51 setMinimumWidth(arrowWidth());
53 connect(this, SIGNAL(clicked()), this, SLOT(updateNavigatorURL()));
55 m_popupDelay
= new QTimer(this);
56 connect(m_popupDelay
, SIGNAL(timeout()), this, SLOT(startListJob()));
57 connect(this, SIGNAL(pressed()), this, SLOT(startPopupDelay()));
60 URLNavigatorButton::~URLNavigatorButton()
64 void URLNavigatorButton::setIndex(int index
)
72 QString
path(urlNavigator()->url().pathOrUrl());
73 setText(path
.section('/', index
, index
));
75 // Check whether the button indicates the full path of the URL. If
76 // this is the case, the button is marked as 'active'.
78 QFont
adjustedFont(font());
79 if (path
.section('/', index
, index
).isEmpty()) {
80 setDisplayHintEnabled(ActivatedHint
, true);
81 adjustedFont
.setBold(true);
84 setDisplayHintEnabled(ActivatedHint
, false);
85 adjustedFont
.setBold(false);
88 setFont(adjustedFont
);
92 int URLNavigatorButton::index() const
97 void URLNavigatorButton::drawButton(QPainter
* painter
)
99 const int buttonWidth
= width();
100 const int buttonHeight
= height();
102 QColor backgroundColor
;
103 QColor foregroundColor
;
104 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
105 isDisplayHintEnabled(DraggedHint
) ||
106 isDisplayHintEnabled(PopupActiveHint
);
108 backgroundColor
= KGlobalSettings::highlightColor();
109 foregroundColor
= KGlobalSettings::highlightedTextColor();
112 backgroundColor
= colorGroup().background();
113 foregroundColor
= KGlobalSettings::buttonTextColor();
116 // dimm the colors if the parent view does not have the focus
117 const DolphinView
* parentView
= urlNavigator()->dolphinView();
118 const Dolphin
& dolphin
= Dolphin::mainWin();
120 const bool isActive
= (dolphin
.activeView() == parentView
);
122 QColor
dimmColor(colorGroup().background());
123 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
125 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
129 // draw button background
130 painter
->setPen(Qt::NoPen
);
131 painter
->setBrush(backgroundColor
);
132 painter
->drawRect(0, 0, buttonWidth
, buttonHeight
);
134 int textWidth
= buttonWidth
;
135 if (isDisplayHintEnabled(ActivatedHint
) && isActive
|| isHighlighted
) {
136 painter
->setPen(foregroundColor
);
139 // dimm the foreground color by mixing it with the background
140 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
141 painter
->setPen(foregroundColor
);
144 if (!isDisplayHintEnabled(ActivatedHint
)) {
146 const int border
= 2; // horizontal border
147 const int middleY
= height() / 2;
148 const int width
= arrowWidth();
149 const int startX
= (buttonWidth
- width
) - (2 * border
);
150 const int startTopY
= middleY
- (width
- 1);
151 const int startBottomY
= middleY
+ (width
- 1);
152 for (int i
= 0; i
< width
; ++i
) {
153 painter
->drawLine(startX
, startTopY
+ i
, startX
+ i
, startTopY
+ i
);
154 painter
->drawLine(startX
, startBottomY
- i
, startX
+ i
, startBottomY
- i
);
157 textWidth
= startX
- border
;
160 const bool clipped
= isTextClipped();
161 const int align
= clipped
? Qt::AlignVCenter
: Qt::AlignCenter
;
162 painter
->drawText(QRect(0, 0, textWidth
, buttonHeight
), align
, text());
165 // Blend the right area of the text with the background, as the
167 // TODO: use alpha blending in Qt4 instead of drawing the text that often
168 const int blendSteps
= 16;
170 QColor
blendColor(backgroundColor
);
171 const int redInc
= (foregroundColor
.red() - backgroundColor
.red()) / blendSteps
;
172 const int greenInc
= (foregroundColor
.green() - backgroundColor
.green()) / blendSteps
;
173 const int blueInc
= (foregroundColor
.blue() - backgroundColor
.blue()) / blendSteps
;
174 for (int i
= 0; i
< blendSteps
; ++i
) {
175 painter
->setClipRect(QRect(textWidth
- i
, 0, 1, buttonHeight
));
176 painter
->setPen(blendColor
);
177 painter
->drawText(QRect(0, 0, textWidth
, buttonHeight
), align
, text());
179 blendColor
.setRgb(blendColor
.red() + redInc
,
180 blendColor
.green() + greenInc
,
181 blendColor
.blue() + blueInc
);
186 void URLNavigatorButton::enterEvent(QEvent
* event
)
188 URLButton::enterEvent(event
);
190 // if the text is clipped due to a small window width, the text should
191 // be shown as tooltip
192 if (isTextClipped()) {
193 QToolTip::add(this, text());
197 void URLNavigatorButton::leaveEvent(QEvent
* event
)
199 URLButton::leaveEvent(event
);
200 QToolTip::remove(this);
203 void URLNavigatorButton::dropEvent(QDropEvent
* event
)
211 if (KUrlDrag::decode(event, urls) && !urls.isEmpty()) {
212 setDisplayHintEnabled(DraggedHint, true);
214 QString path(urlNavigator()->url().prettyURL());
215 path = path.section('/', 0, m_index);
217 Dolphin::mainWin().dropURLs(urls, KUrl(path));
219 setDisplayHintEnabled(DraggedHint, false);
224 void URLNavigatorButton::dragEnterEvent(QDragEnterEvent
* event
)
227 event->accept(KUrlDrag::canDecode(event));
229 setDisplayHintEnabled(DraggedHint, true);*/
233 void URLNavigatorButton::dragLeaveEvent(QDragLeaveEvent
* event
)
235 URLButton::dragLeaveEvent(event
);
237 setDisplayHintEnabled(DraggedHint
, false);
242 void URLNavigatorButton::updateNavigatorURL()
248 URLNavigator
* navigator
= urlNavigator();
249 assert(navigator
!= 0);
250 navigator
->setURL(navigator
->url(m_index
));
253 void URLNavigatorButton::startPopupDelay()
255 if (m_popupDelay
->isActive() || m_listJob
|| m_index
< 0) {
259 m_popupDelay
->start(300, true);
262 void URLNavigatorButton::stopPopupDelay()
264 m_popupDelay
->stop();
271 void URLNavigatorButton::startListJob()
277 KUrl url
= urlNavigator()->url(m_index
);
278 m_listJob
= KIO::listDir(url
, false, false);
279 m_subdirs
.clear(); // just to be ++safe
281 connect(m_listJob
, SIGNAL(entries(KIO::Job
*, const KIO::UDSEntryList
&)),
282 this, SLOT(entriesList(KIO::Job
*, const KIO::UDSEntryList
&)));
283 connect(m_listJob
, SIGNAL(result(KIO::Job
*)), this, SLOT(listJobFinished(KIO::Job
*)));
286 void URLNavigatorButton::entriesList(KIO::Job
* job
, const KIO::UDSEntryList
& entries
)
288 if (job
!= m_listJob
) {
292 KIO::UDSEntryList::const_iterator it
= entries
.constBegin();
293 KIO::UDSEntryList::const_iterator itEnd
= entries
.constEnd();
294 while (it
!= itEnd
) {
297 KIO::UDSEntry entry
= *it
;
300 KIO::UDSEntry::const_iterator atomIt = entry.constBegin();
301 KIO::UDSEntry::const_iterator atomEndIt = entry.constEnd();
303 while (atomIt != atomEndIt) {
304 switch ((*atomIt).m_uds) {
306 name = (*atomIt).m_str;
308 case KIO::UDS_FILE_TYPE:
309 isDir = S_ISDIR((*atomIt).m_long);
317 m_subdirs.append(name);
322 m_subdirs
.append(entry
.stringValue(KIO::UDS_NAME
));
331 void URLNavigatorButton::listJobFinished(KIO::Job
* job
)
333 if (job
!= m_listJob
) {
337 if (job
->error() || m_subdirs
.isEmpty()) {
342 setDisplayHintEnabled(PopupActiveHint
, true);
343 update(); // ensure the button is drawn highlighted
344 Q3PopupMenu
* dirsMenu
= new Q3PopupMenu(this);
345 //setPopup(dirsMenu);
346 QStringList::const_iterator it
= m_subdirs
.constBegin();
347 QStringList::const_iterator itEnd
= m_subdirs
.constEnd();
349 while (it
!= itEnd
) {
350 dirsMenu
->insertItem(*it
, i
);
355 int result
= dirsMenu
->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));
358 KUrl url
= urlNavigator()->url(m_index
);
359 url
.addPath(m_subdirs
[result
]);
360 urlNavigator()->setURL(url
);
366 setDisplayHintEnabled(PopupActiveHint
, false);
369 int URLNavigatorButton::arrowWidth() const
371 int width
= (height() / 2) - 7;
378 bool URLNavigatorButton::isTextClipped() const
380 int availableWidth
= width();
381 if (!isDisplayHintEnabled(ActivatedHint
)) {
382 availableWidth
-= arrowWidth() + 1;
385 QFontMetrics
fontMetrics(font());
386 return fontMetrics
.width(text()) >= availableWidth
;