summaryrefslogtreecommitdiffstats
path: root/src/dolphiniconsview.cpp
blob: 7a2489b0bfe633f6fee8ea34e957b4ebc7a11d97 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz                                      *
 *   peter.penz@gmx.at                                                     *
 *                                                                         *
 *   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 "dolphiniconsview.h"
#include <qpainter.h>
#include <kiconeffect.h>
#include <kapplication.h>
#include <qobjectlist.h>
#include <kglobalsettings.h>
#include <kurldrag.h>
#include <qclipboard.h>
#include <assert.h>
#include <kaction.h>
#include <kstdaction.h>
#include <kfileitem.h>

#include "dolphinview.h"
#include "viewproperties.h"
#include "dolphin.h"
#include "dolphinstatusbar.h"
#include "dolphinsettings.h"
#include "dolphiniconsviewsettings.h"

DolphinIconsView::DolphinIconsView(DolphinView* parent, LayoutMode layoutMode) :
    KFileIconView(parent, 0),
    m_previewIconSize(-1),
    m_layoutMode(layoutMode),
    m_dolphinView(parent)
{
    setAcceptDrops(true);
    setMode(KIconView::Execute);
    setSelectionMode(KFile::Extended);
    Dolphin& dolphin = Dolphin::mainWin();

    connect(this, SIGNAL(onItem(QIconViewItem*)),
            this, SLOT(slotOnItem(QIconViewItem*)));
    connect(this, SIGNAL(onViewport()),
            this, SLOT(slotOnViewport()));
    connect(this, SIGNAL(contextMenuRequested(QIconViewItem*, const QPoint&)),
            this, SLOT(slotContextMenuRequested(QIconViewItem*, const QPoint&)));
    connect(this, SIGNAL(selectionChanged()),
            &dolphin, SLOT(slotSelectionChanged()));
    connect(&dolphin, SIGNAL(activeViewChanged()),
            this, SLOT(slotActivationUpdate()));
    connect(this, SIGNAL(itemRenamed(QIconViewItem*, const QString&)),
            this, SLOT(slotItemRenamed(QIconViewItem*, const QString&)));
    connect(this, SIGNAL(dropped(QDropEvent*, const KURL::List&, const KURL&)),
            parent, SLOT(slotURLListDropped(QDropEvent*, const KURL::List&, const KURL&)));

    QClipboard* clipboard = QApplication::clipboard();
    connect(clipboard, SIGNAL(dataChanged()),
            this, SLOT(slotUpdateDisabledItems()));

    // KFileIconView creates two actions for zooming, which are directly connected to the
    // slots KFileIconView::zoomIn() and KFileIconView::zoomOut(). As this behavior is not
    // wanted and the slots are not virtual, the actions are disabled here.
    KAction* zoomInAction = actionCollection()->action("zoomIn");
    assert(zoomInAction != 0);
    zoomInAction->setEnabled(false);

    KAction* zoomOutAction = actionCollection()->action("zoomOut");
    assert(zoomOutAction != 0);
    zoomOutAction->setEnabled(false);

    setItemsMovable(true);
    setWordWrapIconText(true);
    if (m_layoutMode == Previews) {
        showPreviews();
    }
    refreshSettings();
}

DolphinIconsView::~DolphinIconsView()
{
}

void DolphinIconsView::setLayoutMode(LayoutMode mode)
{
    if (m_layoutMode != mode) {
        m_layoutMode = mode;
        refreshSettings();
    }
}

void DolphinIconsView::beginItemUpdates()
{
}

void DolphinIconsView::endItemUpdates()
{
    arrangeItemsInGrid();

    // TODO: KFileIconView does not emit any signal when the preview
    // has been finished. Using a delay of 300 ms is a temporary workaround
    // until the DolphinIconsView will implement the previews by it's own in
    // future releases.
    QTimer::singleShot(300, this, SLOT(slotUpdateDisabledItems()));

    const KFileIconViewItem* item = static_cast<const KFileIconViewItem*>(firstItem());
    if (item != 0) {
        setCurrentItem(item->fileInfo());
    }

    int index = 0;
    const QValueList<URLNavigator::HistoryElem> history = m_dolphinView->urlHistory(index);
    if (!history.isEmpty()) {
        KFileView* fileView = static_cast<KFileView*>(this);
        fileView->setCurrentItem(history[index].currentFileName());
        setContentsPos(history[index].contentsX(), history[index].contentsY());
    }
}

void DolphinIconsView::refreshSettings()
{
    const DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_layoutMode);
    assert(settings != 0);

    setIconSize(settings->iconSize());

    const QIconView::Arrangement arrangement = settings->arrangement();
    const QIconView::ItemTextPos textPos = (arrangement == QIconView::LeftToRight) ?
                                           QIconView::Bottom :
                                           QIconView::Right;
    setArrangement(arrangement);
    setItemTextPos(textPos);

    setGridX(settings->gridWidth());
    setGridY(settings->gridHeight());
    setSpacing(settings->gridSpacing());

    QFont adjustedFont(font());
    adjustedFont.setFamily(settings->fontFamily());
    adjustedFont.setPointSize(settings->fontSize());
    setFont(adjustedFont);
    setIconTextHeight(settings->textlinesCount());

    if (m_layoutMode == Previews) {
        // There is no getter method for the current size in KFileIconView. To
        // prevent a flickering the current size is stored in m_previewIconSize and
        // setPreviewSize is only invoked if the size really has changed.
        showPreviews();

        const int size = settings->previewSize();
        if (size != m_previewIconSize) {
            m_previewIconSize = size;
            setPreviewSize(size);
        }
    }
}

void DolphinIconsView::zoomIn()
{
    if (isZoomInPossible()) {
        DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_layoutMode);
        const int textWidthHint = settings->textWidthHint();

        const int iconSize = increasedIconSize(settings->iconSize());
        settings->setIconSize(iconSize);

        if (m_layoutMode == Previews) {
            const int previewSize = increasedIconSize(settings->previewSize());
            settings->setPreviewSize(previewSize);
        }

        settings->calculateGridSize(textWidthHint);
        ItemEffectsManager::zoomIn();
    }
}

void DolphinIconsView::zoomOut()
{
    if (isZoomOutPossible()) {
        DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_layoutMode);
        const int textWidthHint = settings->textWidthHint();

        const int iconSize = decreasedIconSize(settings->iconSize());
        settings->setIconSize(iconSize);

        if (m_layoutMode == Previews) {
            const int previewSize = decreasedIconSize(settings->previewSize());
            settings->setPreviewSize(previewSize);
        }

        settings->calculateGridSize(textWidthHint);
        ItemEffectsManager::zoomOut();
    }
}

bool DolphinIconsView::isZoomInPossible() const
{
    DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_layoutMode);
    const int size = (m_layoutMode == Icons) ? settings->iconSize() : settings->previewSize();
    return size < KIcon::SizeEnormous;
}

bool DolphinIconsView::isZoomOutPossible() const
{
    DolphinIconsViewSettings* settings = DolphinSettings::instance().iconsView(m_layoutMode);
    return settings->iconSize() > KIcon::SizeSmall;
}

void DolphinIconsView::arrangeItemsInGrid( bool updated )
{

    KFileIconView::arrangeItemsInGrid(updated);

    if (m_layoutMode == Previews) {
        // The class KFileIconView has a bug when the size of the previews differs from the size
        // of the icons: For specific MIME types the y-position and the height is calculated in
        // a wrong manner. The following code bypasses this issue. No bugreport has been submitted
        // as this functionality is not used by any KDE3 application and the core developers are
        // busy enough for KDE4 now :-)

        KFileIconViewItem* item = static_cast<KFileIconViewItem*>(QIconView::firstItem());
        QString mimetype;
        while (item != 0) {
            mimetype = item->fileInfo()->mimetype();
            const bool fixSize = mimetype.contains("text") ||
                                 mimetype.contains("application/x-");
            if (fixSize) {
                item->setPixmapSize(QSize(m_previewIconSize, m_previewIconSize));
            }
            item = static_cast<KFileIconViewItem *>(item->nextItem());
        }
    }
}

void DolphinIconsView::setContextPixmap(void* context,
                                        const QPixmap& pixmap)
{
    reinterpret_cast<KFileIconViewItem*>(context)->setPixmap(pixmap);
}

const QPixmap* DolphinIconsView::contextPixmap(void* context)
{
    return reinterpret_cast<KFileIconViewItem*>(context)->pixmap();
}

void* DolphinIconsView::firstContext()
{
    return reinterpret_cast<void*>(firstItem());
}

void* DolphinIconsView::nextContext(void* context)
{
    KFileIconViewItem* iconViewItem = reinterpret_cast<KFileIconViewItem*>(context);
    return reinterpret_cast<void*>(iconViewItem->nextItem());
}

KFileItem* DolphinIconsView::contextFileInfo(void* context)
{
    return reinterpret_cast<KFileIconViewItem*>(context)->fileInfo();
}

void DolphinIconsView::contentsMousePressEvent(QMouseEvent* event)
{
    KFileIconView::contentsMousePressEvent(event);
    resetActivatedItem();
    emit signalRequestActivation();
    m_dolphinView->statusBar()->clear();
}

void DolphinIconsView::contentsMouseReleaseEvent(QMouseEvent* event)
{
    KFileIconView::contentsMouseReleaseEvent(event);

    // The KFileIconView does not send any selectionChanged signal if
    // a selection is done by using the "select-during-button-pressed" feature.
    // Hence inform Dolphin about the selection change manually:
    Dolphin::mainWin().slotSelectionChanged();
}

void DolphinIconsView::drawBackground(QPainter* painter, const QRect& rect)
{
    if (m_dolphinView->isActive()) {
        KFileIconView::drawBackground(painter, rect);
    }
    else {
        const QBrush brush(colorGroup().background());
        painter->fillRect(0, 0, width(), height(), brush);
    }
}

QDragObject* DolphinIconsView::dragObject()
{
    KURL::List urls;
    KFileItemListIterator it(*KFileView::selectedItems());
    while (it.current() != 0) {
        urls.append((*it)->url());
        ++it;
    }

    QPixmap pixmap;
    if(urls.count() > 1) {
        pixmap = DesktopIcon("kmultiple", iconSize());
    }
    else {
        KFileIconViewItem* item = static_cast<KFileIconViewItem*>(currentItem());
        if ((item != 0) && (item->pixmap() != 0)) {
            pixmap = *(item->pixmap());
        }
    }

    if (pixmap.isNull()) {
        pixmap = currentFileItem()->pixmap(iconSize());
    }

    QDragObject* dragObj = new KURLDrag(urls, widget());
    dragObj->setPixmap(pixmap);
    return dragObj;
}

void DolphinIconsView::contentsDragEnterEvent(QDragEnterEvent* event)
{
    // TODO: The method KFileIconView::contentsDragEnterEvent() does
    // not allow drag and drop inside itself, which prevents the possability
    // to move a file into a directory. As the method KFileIconView::acceptDrag()
    // is not virtual, we must overwrite the method
    // KFileIconView::contentsDragEnterEvent() and do some cut/copy/paste for this
    // usecase. Corresponding to the documentation the method KFileIconView::acceptDrag()
    // will get virtual in KDE 4, which will simplify the code.

    if (event->source() != this) {
        KFileIconView::contentsDragEnterEvent(event);
        return;
    }

    const bool accept = KURLDrag::canDecode(event) &&
                        (event->action() == QDropEvent::Copy ||
                         event->action() == QDropEvent::Move ||
                         event->action() == QDropEvent::Link );
    if (accept) {
        event->acceptAction();
    }
    else {
        event->ignore();
    }
}

void DolphinIconsView::contentsDragMoveEvent(QDragMoveEvent* event)
{
    KFileIconView::contentsDragMoveEvent(event);

    // If a dragging is done above a directory, show the icon as 'active' for
    // a visual feedback
    KFileIconViewItem* item = static_cast<KFileIconViewItem*>(findItem(contentsToViewport(event->pos())));

    bool showActive = false;
    if (item != 0) {
        const KFileItem* fileInfo = item->fileInfo();
        showActive = (fileInfo != 0) && fileInfo->isDir();
    }

    if (showActive) {
        slotOnItem(item);
    }
    else {
        slotOnViewport();
    }
}

void DolphinIconsView::contentsDropEvent(QDropEvent* event)
{
    // TODO: Most of the following code is a copy of
    // KFileIconView::contentsDropEvent. See comment in
    // DolphinIconsView::contentsDragEnterEvent for details.

    if (event->source() != this) {
        KFileIconView::contentsDropEvent(event);
        return;
    }

    KFileIconViewItem* item = static_cast<KFileIconViewItem*>(findItem(contentsToViewport(event->pos())));
    const bool accept = KURLDrag::canDecode(event) &&
                        (event->action() == QDropEvent::Copy ||
                         event->action() == QDropEvent::Move ||
                         event->action() == QDropEvent::Link ) &&
                        (item != 0);
    if (!accept) {
        return;
    }

    KFileItem* fileItem = item->fileInfo();
    if (!fileItem->isDir()) {
        // the file is not a directory, hence don't accept any drop
        return;
    }
    emit dropped(event, fileItem);
    KURL::List urls;
    if (KURLDrag::decode(event, urls) && !urls.isEmpty()) {
        emit dropped(event, urls, fileItem != 0 ? fileItem->url() : KURL());
        sig->dropURLs(fileItem, event, urls);
    }
}

void DolphinIconsView::slotOnItem(QIconViewItem* item)
{
    assert(item != 0);
    activateItem(reinterpret_cast<void*>(item));

    KFileItem* fileItem = static_cast<KFileIconViewItem*>(item)->fileInfo();
    m_dolphinView->requestItemInfo(fileItem->url());
}

void DolphinIconsView::slotOnViewport()
{
    resetActivatedItem();
    m_dolphinView->requestItemInfo(KURL());
}

void DolphinIconsView::slotContextMenuRequested(QIconViewItem* item,
                                                const QPoint& pos)
{
    KFileItem* fileInfo = 0;
    if (item != 0) {
        fileInfo = static_cast<KFileIconViewItem*>(item)->fileInfo();
    }
    m_dolphinView->openContextMenu(fileInfo, pos);
}

void DolphinIconsView::slotItemRenamed(QIconViewItem* item,
                                       const QString& name)
{
    KFileItem* fileInfo = static_cast<KFileIconViewItem*>(item)->fileInfo();
    m_dolphinView->rename(KURL(fileInfo->url()), name);
}

void DolphinIconsView::slotActivationUpdate()
{
    update();

    // TODO: there must be a simpler way to say
    // "update all children"
    const QObjectList* list = children();
    if (list == 0) {
        return;
    }

    QObjectListIterator it(*list);
    QObject* object = 0;
    while ((object = it.current()) != 0) {
        if (object->inherits("QWidget")) {
            QWidget* widget = static_cast<QWidget*>(object);
            widget->update();
        }
        ++it;
    }
}

void DolphinIconsView::slotUpdateDisabledItems()
{
    updateDisabledItems();
}

int DolphinIconsView::increasedIconSize(int size) const
{
    int incSize = 0;
    switch (size) {
        case KIcon::SizeSmall:       incSize = KIcon::SizeSmallMedium; break;
        case KIcon::SizeSmallMedium: incSize = KIcon::SizeMedium; break;
        case KIcon::SizeMedium:      incSize = KIcon::SizeLarge; break;
        case KIcon::SizeLarge:       incSize = KIcon::SizeHuge; break;
        case KIcon::SizeHuge:        incSize = KIcon::SizeEnormous; break;
        default: assert(false); break;
    }
    return incSize;
}

int DolphinIconsView::decreasedIconSize(int size) const
{
    int decSize = 0;
    switch (size) {
        case KIcon::SizeSmallMedium: decSize = KIcon::SizeSmall; break;
        case KIcon::SizeMedium: decSize = KIcon::SizeSmallMedium; break;
        case KIcon::SizeLarge: decSize = KIcon::SizeMedium; break;
        case KIcon::SizeHuge: decSize = KIcon::SizeLarge; break;
        case KIcon::SizeEnormous: decSize = KIcon::SizeHuge; break;
        default: assert(false); break;
    }
    return decSize;
}

#include "dolphiniconsview.moc"