summaryrefslogtreecommitdiffstats
path: root/src/itemeffectsmanager.cpp
blob: f402f20f08eb1dc1cd31f3f26172b0a589d365a7 (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
/***************************************************************************
 *   Copyright (C) 2006 by Peter Penz                                      *
 *   [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 "itemeffectsmanager.h"
#include <kiconeffect.h>
#include <kapplication.h>
#include <tqobjectlist.h>
#include <kglobalsettings.h>
#include <tqclipboard.h>
#include <kurldrag.h>
#include <klocale.h>

#include "dolphin.h"
#include "dolphinstatusbar.h"

ItemEffectsManager::ItemEffectsManager()
{
    m_pixmapCopy = new TQPixmap();
}

ItemEffectsManager::~ItemEffectsManager()
{
    delete m_pixmapCopy;
    m_pixmapCopy = 0;

    m_highlightedURL = 0;
}

void ItemEffectsManager::zoomIn()
{
    Dolphin::mainWin().refreshViews();
}

void ItemEffectsManager::zoomOut()
{
    Dolphin::mainWin().refreshViews();
}

void ItemEffectsManager::activateItem(void* context)
{
    KFileItem* fileInfo = contextFileInfo(context);
    const KURL itemURL(fileInfo->url());
    if (m_highlightedURL == itemURL) {
        // the item is already highlighted
        return;
    }

    resetActivatedItem();

    const TQPixmap* itemPixmap = contextPixmap(context);
    if (itemPixmap != 0) {
        // remember the pixmap and item to be able to
        // restore it to the old state later
        *m_pixmapCopy = *itemPixmap;
        m_highlightedURL = itemURL;

        // apply an icon effect to the item below the mouse pointer
        KIconEffect iconEffect;
        TQPixmap pixmap = iconEffect.apply(*itemPixmap,
                                          KIcon::Desktop,
                                          KIcon::ActiveState);
        setContextPixmap(context, pixmap);
    }

    if (!Dolphin::mainWin().activeView()->hasSelection()) {
        DolphinStatusBar* statusBar = Dolphin::mainWin().activeView()->statusBar();
        statusBar->setMessage(statusBarText(fileInfo), DolphinStatusBar::Default);
    }
}

void ItemEffectsManager::resetActivatedItem()
{
    if (m_highlightedURL.isEmpty()) {
        return;
    }

    for (void* context = firstContext(); context != 0; context = nextContext(context)) {
        KURL itemURL(contextFileInfo(context)->url());
        if (itemURL == m_highlightedURL) {
            // the highlighted item has been found and is restored to the default state
            KIconEffect iconEffect;
            TQPixmap pixmap = iconEffect.apply(*m_pixmapCopy,
                                              KIcon::Desktop,
                                              KIcon::DefaultState);

            // TODO: KFileIconView does not emit any signal when the preview has been finished.
            // Hence check the size to prevent that a preview is hidden by restoring a
            // non-preview pixmap.
            const TQPixmap* highlightedPixmap = contextPixmap(context);
            const bool restore = (pixmap.width() == highlightedPixmap->width()) &&
                                 (pixmap.height() == highlightedPixmap->height());
            if (restore) {
                setContextPixmap(context, pixmap);
            }
            break;
        }
    }

    m_highlightedURL = 0;

    DolphinStatusBar* statusBar = Dolphin::mainWin().activeView()->statusBar();
    statusBar->clear();
}

void ItemEffectsManager::updateDisabledItems()
{
    if (!m_disabledItems.isEmpty()) {
        // restore all disabled items with their original pixmap
        for (void* context = firstContext(); context != 0; context = nextContext(context)) {
            const KFileItem* fileInfo = contextFileInfo(context);
            const KURL& fileURL = fileInfo->url();
            TQValueListIterator<DisabledItem> it = m_disabledItems.begin();
            while (it != m_disabledItems.end()) {
                if (fileURL == (*it).url) {
                    setContextPixmap(context, (*it).pixmap);
                }
                ++it;
            }
        }
        m_disabledItems.clear();
    }

    if (!Dolphin::mainWin().clipboardContainsCutData()) {
        return;
    }

    TQClipboard* clipboard = TQApplication::tqclipboard();
    TQMimeSource* data = clipboard->data();
    if (!KURLDrag::canDecode(data)) {
        return;
    }

    // The clipboard contains items, which have been cutted. Change the pixmaps of all those
    // items to the disabled state.
    KURL::List urls;
    KURLDrag::decode(data, urls);
    for (void* context = firstContext(); context != 0; context = nextContext(context)) {
        const KFileItem* fileInfo = contextFileInfo(context);
        const KURL& fileURL = fileInfo->url();
        for(KURL::List::ConstIterator it = urls.begin(); it != urls.end(); ++it) {
            if (fileURL == (*it)) {
                const TQPixmap* itemPixmap = contextPixmap(context);
                if (itemPixmap != 0) {
                    // remember old pixmap
                    DisabledItem disabledItem;
                    disabledItem.url = fileURL;
                    disabledItem.pixmap = *itemPixmap;
                    m_disabledItems.append(disabledItem);

                    KIconEffect iconEffect;
                    TQPixmap disabledPixmap = iconEffect.apply(*itemPixmap,
                                                              KIcon::Desktop,
                                                              KIcon::DisabledState);
                    setContextPixmap(context, disabledPixmap);
                }
                break;
            }
        }
    }
}

TQString ItemEffectsManager::statusBarText(KFileItem* fileInfo) const
{
    if (fileInfo->isDir()) {
        // KFileItem::getStatusBar() returns "MyDocuments/ Folder" as
        // status bar text for a folder 'MyDocuments'. This is adjusted
        // to "MyDocuments (Folder)" in Dolphin.
        return i18n("%1 (Folder)").tqarg(fileInfo->name());
    }

    return fileInfo->getStatusBarInfo();
}