Ran qt3to4
[basket4.git] / src / kiconcanvas.cpp
blob25ec51e931d566194d2ced61236bb2d9ac6d88cb
1 /* vi: ts=8 sts=4 sw=4
2 * kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;
4 * This file is part of the KDE project, module kfile.
5 * Copyright (C) 2006 Luke Sandell <lasandell@gmail.com>
6 * (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
7 * (C) 2000 Geert Jansen <jansen@kde.org>
8 * (C) 2000 Kurt Granroth <granroth@kde.org>
9 * (C) 1997 Christoph Neerfeld <chris@kde.org>
11 * This is free software; it comes under the GNU Library General
12 * Public License, version 2. See the file "COPYING.LIB" for the
13 * exact licensing terms.
16 #include "kiconcanvas.h"
17 #include "kicondialog.h"
19 #include <config.h>
21 #include <kiconviewsearchline.h>
23 #include <kapplication.h>
24 #include <kdebug.h>
25 #include <klocale.h>
26 #include <kglobal.h>
27 #include <kstandarddirs.h>
28 #include <kiconloader.h>
29 #include <kimagefilepreview.h>
30 #include <kurldrag.h>
31 #include <kmultipledrag.h>
33 #include <q3sortedlist.h>
34 #include <qimage.h>
35 #include <qpixmap.h>
36 #include <qtimer.h>
37 #include <qfileinfo.h>
38 #include <q3dragobject.h>
39 #include <cmath>
40 #include <math.h>
41 #include <algorithm>
43 #ifdef HAVE_LIBART
44 #include <svgicons/ksvgiconengine.h>
45 #include <svgicons/ksvgiconpainter.h>
46 #endif
48 class KIconCanvasItem : public Q3IconViewItem
50 public:
51 KIconCanvasItem ( Q3IconView * parent, const QString & key, const QPixmap & pixmap )
52 : Q3IconViewItem(parent)
54 setText(QFileInfo(key).baseName());
55 setKey(key);
56 setPixmap(pixmap);
57 setDragEnabled(true);
58 setDropEnabled(false);
62 int compare(Q3IconViewItem *rhs) const
64 return QString::localeAwareCompare(text().lower(), rhs->text().lower());
68 class KIconCanvas::KIconCanvasPrivate
70 public:
71 KIconCanvasPrivate()
73 m_bLoading = false;
74 mSize = 0;
76 ~KIconCanvasPrivate()
79 bool m_bLoading;
80 QString mSetCurrent;
81 int mSize;
82 #ifdef HAVE_LIBART
83 KSvgIconEngine mSvgEngine;
84 #endif
85 bool mStrictIconSize;
89 * KIconCanvas: Iconview for the iconloader dialog.
92 KIconCanvas::KIconCanvas(QWidget *parent, const char *name)
93 : KIconView(parent, name)
95 d = new KIconCanvasPrivate;
96 mpLoader = KGlobal::iconLoader();
97 mpTimer = new QTimer(this);
98 connect(mpTimer, SIGNAL(timeout()), SLOT(slotLoadFiles()));
99 connect(this, SIGNAL(currentChanged(Q3IconViewItem *)),
100 SLOT(slotCurrentChanged(Q3IconViewItem *)));
101 setAcceptDrops(false);
102 setShowToolTips(true);
103 setStrictIconSize(false);
106 KIconCanvas::~KIconCanvas()
108 delete mpTimer;
109 delete d;
112 void KIconCanvas::setIconLoader(KIconLoader *loader)
114 mpLoader = loader;
117 void KIconCanvas::loadIcon(const QString &name)
119 QImage img;
120 QString path = mpLoader->iconPath(name,-d->mSize);
121 // Use the extension as the format. Works for XPM and PNG, but not for SVG
122 QString ext = path.right(3).upper();
123 int maxSize = std::min(d->mSize, 60);
125 if (ext != "SVG" && ext != "VGZ")
126 img.load(path);
127 #ifdef HAVE_LIBART
128 else
129 if (d->mSvgEngine.load(maxSize, maxSize, path))
130 img = *d->mSvgEngine.painter()->image();
131 #endif
133 if (img.isNull())
134 return;
136 // For non-KDE icons
137 if (d->mStrictIconSize && (img.width() != d->mSize || img.height() != d->mSize))
138 return;
140 if (img.width() > maxSize || img.height() > maxSize)
142 if (img.width() > img.height()) {
143 int height = (int) ((float(maxSize) / img.width()) * img.height());
144 img = img.smoothScale(maxSize, height);
145 } else {
146 int width = (int) ((float(maxSize) / img.height()) * img.width());
147 img = img.smoothScale(width, maxSize);
150 QPixmap pm;
151 pm.convertFromImage(img);
153 (void) new KIconCanvasItem(this, name, pm);
156 void KIconCanvas::loadFiles(const QStringList& files)
158 clear();
159 mFiles = files;
160 emit startLoading(mFiles.count());
161 mpTimer->start(10, true); // #86680
162 d->m_bLoading = false;
165 void KIconCanvas::slotLoadFiles()
167 setResizeMode(Fixed);
168 QApplication::setOverrideCursor(waitCursor);
170 // disable updates to not trigger paint events when adding child items
171 setUpdatesEnabled( false );
173 d->m_bLoading = true;
174 int count;
175 QStringList::ConstIterator it;
176 QStringList::ConstIterator end(mFiles.end());
177 for (it=mFiles.begin(), count=0; it!=end; ++it, count++)
179 loadIcon(*it);
181 // Calling kapp->processEvents() makes the iconview flicker like hell
182 // (it's being repainted once for every new item), so we don't do this.
183 // Instead, we directly repaint the progress bar without going through
184 // the event-loop. We do that just once for every 10th item so that
185 // the progress bar doesn't flicker in turn. (pfeiffer)
186 // FIXME: Qt4 will have double buffering
187 if ( count % 10 == 0) {
188 emit progress(count);
190 if ( !d->m_bLoading ) // user clicked on a button that will load another set of icons
191 break;
194 // enable updates since we have to draw the whole view now
195 sort();
196 d->m_bLoading = false;
197 setUpdatesEnabled( true );
198 QApplication::restoreOverrideCursor();
199 emit finished();
200 setResizeMode(Adjust);
203 QString KIconCanvas::getCurrent() const
205 return currentItem() ? currentItem()->key() : QString::null;
208 void KIconCanvas::stopLoading()
210 d->m_bLoading = false;
213 void KIconCanvas::slotCurrentChanged(Q3IconViewItem *item)
215 emit nameChanged((item != 0L) ? item->text() : QString::null);
218 void KIconCanvas::setGroupOrSize( int groupOrSize )
220 d->mSize = ((int)groupOrSize >= 0) ?
221 mpLoader->currentSize((KIcon::Group)groupOrSize) :
222 -groupOrSize;
225 void KIconCanvas::setStrictIconSize( bool strictIconSize )
227 d->mStrictIconSize = strictIconSize;
230 Q3DragObject *KIconCanvas::dragObject()
232 // We use QImageDrag rather than KURLDrag so that the user can't drag an icon out of the theme!
233 // TODO: support SVG?
234 QPixmap *pixmap = currentItem()->pixmap();
235 QPoint pos = viewportToContents( viewport()->mapFromGlobal( QCursor::pos() ) );
236 QPoint hot;
237 hot.setX(pos.x() - currentItem()->pos().x() - (currentItem()->width() - pixmap->width()) / 2);
238 hot.setY(pos.y() - currentItem()->pos().y() - (currentItem()->height() - pixmap->height()) / 2);
239 Q3ImageDrag *drag = new Q3ImageDrag( pixmap->convertToImage(), this );
240 drag->setPixmap(*pixmap, hot);
241 return drag;
244 #include "kiconcanvas.moc"