summaryrefslogtreecommitdiffstats
path: root/src/urlnavigatorbutton.cpp
blob: 1cc5bcc5b9630806fbcfbe59b436a6b898ac17f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz ([email protected])                  *
 *   Copyright (C) 2006 by Aaron J. Seigo (<[email protected]>)               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "urlnavigatorbutton.h"
#include <qcursor.h>
#include <qfontmetrics.h>
#include <qpainter.h>
#include <qtimer.h>
#include <qtooltip.h>

#include <kglobalsettings.h>
#include <kiconloader.h>
#include <kio/jobclasses.h>
#include <klocale.h>
#include <kpopupmenu.h>
#include <kurl.h>
#include <kurldrag.h>
#include <assert.h>

#include "urlnavigator.h"
#include "dolphinview.h"
#include "dolphin.h"

URLNavigatorButton::URLNavigatorButton(int index, URLNavigator* parent) :
    URLButton(parent),
    m_index(-1),
    m_listJob(0)
{
    setAcceptDrops(true);
    setMinimumWidth(arrowWidth());
    setIndex(index);
    connect(this, SIGNAL(clicked()), this, SLOT(updateNavigatorURL()));

    m_popupDelay = new QTimer(this);
    connect(m_popupDelay, SIGNAL(timeout()), this, SLOT(startListJob()));
    connect(this, SIGNAL(pressed()), this, SLOT(startPopupDelay()));
}

URLNavigatorButton::~URLNavigatorButton()
{
}

void URLNavigatorButton::setIndex(int index)
{
    if (index < 0) {
        index = 0;
    }

    m_index = index;
    QString path(urlNavigator()->url().prettyURL());
    setText(path.section('/', index, index));

    // Check whether the button indicates the full path of the URL. If
    // this is the case, the button is marked as 'active'.
    ++index;
    QFont adjustedFont(font());
    if (path.section('/', index, index).isEmpty()) {
        setDisplayHintEnabled(ActivatedHint, true);
        adjustedFont.setBold(true);
    }
    else {
        setDisplayHintEnabled(ActivatedHint, false);
        adjustedFont.setBold(false);
    }

    setFont(adjustedFont);
    update();
}

int URLNavigatorButton::index() const
{
    return m_index;
}

void URLNavigatorButton::drawButton(QPainter* painter)
{
    const int buttonWidth  = width();
    const int buttonHeight = height();

    QColor backgroundColor;
    QColor foregroundColor;
    const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
                               isDisplayHintEnabled(DraggedHint) ||
                               isDisplayHintEnabled(PopupActiveHint);
    if (isHighlighted) {
        backgroundColor = KGlobalSettings::highlightColor();
        foregroundColor = KGlobalSettings::highlightedTextColor();
    }
    else {
        backgroundColor = colorGroup().background();
        foregroundColor = KGlobalSettings::buttonTextColor();
    }

    // dimm the colors if the parent view does not have the focus
    const DolphinView* parentView = urlNavigator()->dolphinView();
    const Dolphin& dolphin = Dolphin::mainWin();

    const bool isActive = (dolphin.activeView() == parentView);
    if (!isActive) {
        QColor dimmColor(colorGroup().background());
        foregroundColor = mixColors(foregroundColor, dimmColor);
        if (isHighlighted) {
            backgroundColor = mixColors(backgroundColor, dimmColor);
        }
    }

    // draw button background
    painter->setPen(NoPen);
    painter->setBrush(backgroundColor);
    painter->drawRect(0, 0, buttonWidth, buttonHeight);

    int textWidth = buttonWidth;
    if (isDisplayHintEnabled(ActivatedHint) && isActive || isHighlighted) {
        painter->setPen(foregroundColor);
    }
    else {
        // dimm the foreground color by mixing it with the background
        foregroundColor = mixColors(foregroundColor, backgroundColor);
        painter->setPen(foregroundColor);
    }

    if (!isDisplayHintEnabled(ActivatedHint)) {
        // draw arrow
        const int border = 2;  // horizontal border
        const int middleY = height() / 2;
        const int width = arrowWidth();
        const int startX = (buttonWidth - width) - (2 * border);
        const int startTopY = middleY - (width - 1);
        const int startBottomY = middleY + (width - 1);
        for (int i = 0; i < width; ++i) {
            painter->drawLine(startX, startTopY + i, startX + i, startTopY + i);
            painter->drawLine(startX, startBottomY - i, startX + i, startBottomY - i);
        }

        textWidth = startX - border;
    }

    const bool clipped = isTextClipped();
    const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
    painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text());

    if (clipped) {
        // Blend the right area of the text with the background, as the
        // text is clipped.
        // TODO: use alpha blending in Qt4 instead of drawing the text that often
        const int blendSteps = 16;

        QColor blendColor(backgroundColor);
        const int redInc   = (foregroundColor.red()   - backgroundColor.red())   / blendSteps;
        const int greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps;
        const int blueInc  = (foregroundColor.blue()  - backgroundColor.blue())  / blendSteps;
        for (int i = 0; i < blendSteps; ++i) {
            painter->setClipRect(QRect(textWidth - i, 0, 1, buttonHeight));
            painter->setPen(blendColor);
            painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text());

            blendColor.setRgb(blendColor.red()   + redInc,
                              blendColor.green() + greenInc,
                              blendColor.blue()  + blueInc);
        }
    }
}

void URLNavigatorButton::enterEvent(QEvent* event)
{
    URLButton::enterEvent(event);

    // if the text is clipped due to a small window width, the text should
    // be shown as tooltip
    if (isTextClipped()) {
        QToolTip::add(this, text());
    }
}

void URLNavigatorButton::leaveEvent(QEvent* event)
{
    URLButton::leaveEvent(event);
    QToolTip::remove(this);
}

void URLNavigatorButton::dropEvent(QDropEvent* event)
{
    KURL::List urls;
    if (KURLDrag::decode(event, urls) && !urls.isEmpty()) {
        setDisplayHintEnabled(DraggedHint, true);

        QString path(urlNavigator()->url().prettyURL());
        path = path.section('/', 0, m_index);

        Dolphin::mainWin().dropURLs(urls, KURL(path));

        setDisplayHintEnabled(DraggedHint, false);
        update();
    }
}

void URLNavigatorButton::dragEnterEvent(QDragEnterEvent* event)
{
    event->accept(KURLDrag::canDecode(event));

    setDisplayHintEnabled(DraggedHint, true);
    update();
}

void URLNavigatorButton::dragLeaveEvent(QDragLeaveEvent* event)
{
    URLButton::dragLeaveEvent(event);

    setDisplayHintEnabled(DraggedHint, false);
    update();
}


void URLNavigatorButton::updateNavigatorURL()
{
    URLNavigator* navigator = urlNavigator();
    assert(navigator != 0);
    navigator->setURL(navigator->url(m_index));
}

void URLNavigatorButton::startPopupDelay()
{
    if (m_popupDelay->isActive() || m_listJob) {
        return;
    }

    m_popupDelay->start(300, true);
}

void URLNavigatorButton::stopPopupDelay()
{
    m_popupDelay->stop();
    if (m_listJob) {
        m_listJob->kill();
        m_listJob = 0;
    }
}

void URLNavigatorButton::startListJob()
{
    if (m_listJob) {
        return;
    }

    KURL url = urlNavigator()->url(m_index);
    m_listJob = KIO::listDir(url, false, false);
    m_subdirs.clear(); // just to be ++safe

    connect(m_listJob, SIGNAL(entries(KIO::Job*, const KIO::UDSEntryList &)),
            this, SLOT(entriesList(KIO::Job*, const KIO::UDSEntryList&)));
    connect(m_listJob, SIGNAL(result(KIO::Job*)), this, SLOT(listJobFinished(KIO::Job*)));
}

void URLNavigatorButton::entriesList(KIO::Job* job, const KIO::UDSEntryList& entries)
{
    if (job != m_listJob) {
        return;
    }

    KIO::UDSEntryList::const_iterator it = entries.constBegin();
    KIO::UDSEntryList::const_iterator itEnd = entries.constEnd();
    while (it != itEnd) {
        QString name;
        bool isDir = false;
        KIO::UDSEntry entry = *it;
        KIO::UDSEntry::const_iterator atomIt = entry.constBegin();
        KIO::UDSEntry::const_iterator atomEndIt = entry.constEnd();

        while (atomIt != atomEndIt) {
            switch ((*atomIt).m_uds) {
                case KIO::UDS_NAME:
                    name = (*atomIt).m_str;
                    break;
                case KIO::UDS_FILE_TYPE:
                    isDir = S_ISDIR((*atomIt).m_long);
                    break;
                default:
                    break;
            }
            ++atomIt;
         }

        if (isDir) {
            m_subdirs.append(name);
        }

        ++it;
    }

    m_subdirs.sort();
}

void URLNavigatorButton::listJobFinished(KIO::Job* job)
{
    if (job != m_listJob) {
        return;
    }

    if (job->error() || m_subdirs.isEmpty()) {
        // clear listing
        return;
    }

    setDisplayHintEnabled(PopupActiveHint, true);
    update(); // ensure the button is drawn highlighted
    QPopupMenu* dirsMenu = new QPopupMenu(this);
    //setPopup(dirsMenu);
    QStringList::const_iterator it = m_subdirs.constBegin();
    QStringList::const_iterator itEnd = m_subdirs.constEnd();
    int i = 0;
    while (it != itEnd) {
        dirsMenu->insertItem(*it, i);
        ++i;
        ++it;
    }

    int result = dirsMenu->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));

    if (result >= 0) {
        KURL url = urlNavigator()->url(m_index);
        url.addPath(*m_subdirs.at(result));
        urlNavigator()->setURL(url);
    }

    m_listJob = 0;
    m_subdirs.clear();
    delete dirsMenu;
    setDisplayHintEnabled(PopupActiveHint, false);
}

int URLNavigatorButton::arrowWidth() const
{
    int width = (height() / 2) - 7;
    if (width < 4) {
        width = 4;
    }
    return width;
}

bool URLNavigatorButton::isTextClipped() const
{
    int availableWidth = width();
    if (!isDisplayHintEnabled(ActivatedHint)) {
        availableWidth -= arrowWidth() + 1;
    }

    QFontMetrics fontMetrics(font());
    return fontMetrics.width(text()) >= availableWidth;
}


void URLNavigatorButton::mousePressEvent(QMouseEvent * event)
{
    if (event->button() == LeftButton)
        dragPos = event->pos();
    URLButton::mousePressEvent(event);
}

void URLNavigatorButton::mouseMoveEvent(QMouseEvent * event)
{
    if (event->state() & LeftButton) {
        int distance = (event->pos() - dragPos).manhattanLength();
        if (distance > QApplication::startDragDistance()*2)//don't start on small move (for submenu usability)
            startDrag();
    }
    URLButton::mouseMoveEvent(event);
}

void URLNavigatorButton::startDrag()
{
    KURL url = urlNavigator()->url(m_index);
    KURL::List lst;
    lst.append( url );
    KURLDrag *drag = new KURLDrag(lst, this);
    drag->drag();
}