summaryrefslogtreecommitdiffstats
path: root/src/kiconcanvas.cpp
blob: a5d25e5a5c695c7cf59f019fe152c88ed491e3d0 (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
/* vi: ts=8 sts=4 sw=4
 * kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;
 *
 * This file is part of the KDE project, module kfile.
 * Copyright (C) 2006 Luke Sandell <[email protected]>
 *           (C) 2002 Carsten Pfeiffer <[email protected]>
 *           (C) 2000 Geert Jansen <[email protected]>
 *           (C) 2000 Kurt Granroth <[email protected]>
 *           (C) 1997 Christoph Neerfeld <[email protected]>
 *
 * This is free software; it comes under the GNU Library General
 * Public License, version 2. See the file "COPYING.LIB" for the
 * exact licensing terms.
 */

#include "kiconcanvas.h"
#include "kicondialog.h"

#include <config.h>

#include <kiconviewsearchline.h>

#include <kapplication.h>
#include <kdebug.h>
#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kiconloader.h>
#include <kimagefilepreview.h>
#include <kurldrag.h>
#include <kmultipledrag.h>

#include <tqsortedlist.h>
#include <tqimage.h>
#include <tqpixmap.h>
#include <tqtimer.h>
#include <tqfileinfo.h>
#include <tqdragobject.h>
#include <cmath>
#include <math.h>
#include <algorithm>

#ifdef HAVE_LIBART
#include <svgicons/ksvgiconengine.h>
#include <svgicons/ksvgiconpainter.h>
#endif

class KIconCanvasItem : public TQIconViewItem
{
  public:
    KIconCanvasItem ( TQIconView * parent, const TQString & key, const TQPixmap & pixmap )
        : TQIconViewItem(parent)
    {
        setText(TQFileInfo(key).baseName());
        setKey(key);
        setPixmap(pixmap);
        setDragEnabled(true);
        setDropEnabled(false);

    }

    int compare(TQIconViewItem *rhs) const
    {
        return TQString::localeAwareCompare(text().lower(), rhs->text().lower());
    }
};

class KIconCanvas::KIconCanvasPrivate
{
  public:
    KIconCanvasPrivate()
    {
        m_bLoading = false;
        mSize = 0;
    }
    ~KIconCanvasPrivate()
    {
    }
    bool m_bLoading;
    TQString mSetCurrent;
    int mSize;
#ifdef HAVE_LIBART
    KSvgIconEngine mSvgEngine;
#endif
    bool mStrictIconSize;
};

/*
 * KIconCanvas: Iconview for the iconloader dialog.
 */

KIconCanvas::KIconCanvas(TQWidget *parent, const char *name)
    : KIconView(parent, name)
{
    d = new KIconCanvasPrivate;
    mpLoader = KGlobal::iconLoader();
    mpTimer = new TQTimer(this);
    connect(mpTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotLoadFiles()));
    connect(this, TQT_SIGNAL(currentChanged(TQIconViewItem *)),
	    TQT_SLOT(slotCurrentChanged(TQIconViewItem *)));
    setAcceptDrops(false);
    setShowToolTips(true);
    setStrictIconSize(false);
}

KIconCanvas::~KIconCanvas()
{
    delete mpTimer;
    delete d;
}

void KIconCanvas::setIconLoader(KIconLoader *loader)
{
    mpLoader = loader;
}

void KIconCanvas::loadIcon(const TQString &name)
{
    TQImage img;
    TQString path = mpLoader->iconPath(name,-d->mSize);
    // Use the extension as the format. Works for XPM and PNG, but not for SVG
    TQString ext = path.right(3).upper();
    int maxSize = std::min(d->mSize, 60);

    if (ext != "SVG" && ext != "VGZ")
        img.load(path);
#ifdef HAVE_LIBART
   else
       if (d->mSvgEngine.load(maxSize, maxSize, path))
        img = *d->mSvgEngine.painter()->image();
#endif

   if (img.isNull())
       return;

   // For non-KDE icons
   if (d->mStrictIconSize && (img.width() != d->mSize || img.height() != d->mSize))
       return;

   if (img.width() > maxSize || img.height() > maxSize)
   {
       if (img.width() > img.height()) {
           int height = (int) ((float(maxSize) / img.width()) * img.height());
           img = img.smoothScale(maxSize, height);
       } else {
           int width = (int) ((float(maxSize) / img.height()) * img.width());
           img = img.smoothScale(width, maxSize);
       }
   }
   TQPixmap pm;
   pm.convertFromImage(img);

   (void) new KIconCanvasItem(this, name, pm);
}

void KIconCanvas::loadFiles(const TQStringList& files)
{
    clear();
    mFiles = files;
    emit startLoading(mFiles.count());
    mpTimer->start(10, true); // #86680
    d->m_bLoading = false;
}

void KIconCanvas::slotLoadFiles()
{
    setResizeMode(Fixed);
    TQApplication::setOverrideCursor(waitCursor);

    // disable updates to not trigger paint events when adding child items
    setUpdatesEnabled( false );

    d->m_bLoading = true;
    int count;
    TQStringList::ConstIterator it;
    TQStringList::ConstIterator end(mFiles.end());
    for (it=mFiles.begin(), count=0; it!=end; ++it, count++)
    {
        loadIcon(*it);

        // Calling kapp->processEvents() makes the iconview flicker like hell
        // (it's being repainted once for every new item), so we don't do this.
        // Instead, we directly tqrepaint the progress bar without going through
        // the event-loop. We do that just once for every 10th item so that
        // the progress bar doesn't flicker in turn. (pfeiffer)
        // FIXME: TQt4 will have double buffering
        if ( count % 10 == 0) {
            emit progress(count);
        }
        if ( !d->m_bLoading ) // user clicked on a button that will load another set of icons
            break;
    }

    // enable updates since we have to draw the whole view now
    sort();
    d->m_bLoading = false;
    setUpdatesEnabled( true );
    TQApplication::restoreOverrideCursor();
    emit finished();
    setResizeMode(Adjust);
}

TQString KIconCanvas::getCurrent() const
{
   return currentItem() ? currentItem()->key() : TQString();
}

void KIconCanvas::stopLoading()
{
    d->m_bLoading = false;
}

void KIconCanvas::slotCurrentChanged(TQIconViewItem *item)
{
    emit nameChanged((item != 0L) ? item->text() : TQString());
}

void KIconCanvas::setGroupOrSize( int groupOrSize )
{
    d->mSize = ((int)groupOrSize >= 0) ?
            mpLoader->currentSize((KIcon::Group)groupOrSize) :
            -groupOrSize;
}

void KIconCanvas::setStrictIconSize( bool strictIconSize )
{
    d->mStrictIconSize = strictIconSize;
}

TQDragObject *KIconCanvas::dragObject()
{
    // We use TQImageDrag rather than KURLDrag so that the user can't drag an icon out of the theme!
    // TODO: support SVG?
    TQPixmap *pixmap = currentItem()->pixmap();
    TQPoint pos = viewportToContents( viewport()->mapFromGlobal( TQCursor::pos() ) );
    TQPoint hot;
    hot.setX(pos.x() - currentItem()->pos().x() - (currentItem()->width() - pixmap->width()) / 2);
    hot.setY(pos.y() - currentItem()->pos().y() - (currentItem()->height() - pixmap->height()) / 2);
    TQImageDrag *drag = new TQImageDrag( pixmap->convertToImage(), this );
    drag->setPixmap(*pixmap, hot);
    return drag;
}

#include "kiconcanvas.moc"