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"
21 #include <kiconviewsearchline.h>
23 #include <kapplication.h>
27 #include <kstandarddirs.h>
28 #include <kiconloader.h>
29 #include <kimagefilepreview.h>
31 #include <kmultipledrag.h>
33 #include <q3sortedlist.h>
37 #include <qfileinfo.h>
38 #include <q3dragobject.h>
44 #include <svgicons/ksvgiconengine.h>
45 #include <svgicons/ksvgiconpainter.h>
48 class KIconCanvasItem
: public Q3IconViewItem
51 KIconCanvasItem ( Q3IconView
* parent
, const QString
& key
, const QPixmap
& pixmap
)
52 : Q3IconViewItem(parent
)
54 setText(QFileInfo(key
).baseName());
58 setDropEnabled(false);
62 int compare(Q3IconViewItem
*rhs
) const
64 return QString::localeAwareCompare(text().lower(), rhs
->text().lower());
68 class KIconCanvas::KIconCanvasPrivate
83 KSvgIconEngine mSvgEngine
;
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()
112 void KIconCanvas::setIconLoader(KIconLoader
*loader
)
117 void KIconCanvas::loadIcon(const QString
&name
)
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")
129 if (d
->mSvgEngine
.load(maxSize
, maxSize
, path
))
130 img
= *d
->mSvgEngine
.painter()->image();
137 if (d
->mStrictIconSize
&& (img
.width() != d
->mSize
|| img
.height() != d
->mSize
))
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
);
146 int width
= (int) ((float(maxSize
) / img
.height()) * img
.width());
147 img
= img
.smoothScale(width
, maxSize
);
151 pm
.convertFromImage(img
);
153 (void) new KIconCanvasItem(this, name
, pm
);
156 void KIconCanvas::loadFiles(const QStringList
& 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;
175 QStringList::ConstIterator it
;
176 QStringList::ConstIterator
end(mFiles
.end());
177 for (it
=mFiles
.begin(), count
=0; it
!=end
; ++it
, count
++)
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
194 // enable updates since we have to draw the whole view now
196 d
->m_bLoading
= false;
197 setUpdatesEnabled( true );
198 QApplication::restoreOverrideCursor();
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
) :
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() ) );
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
);
244 #include "kiconcanvas.moc"