2 ******************************************************************************
4 * @file fileiconprovider.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @addtogroup GCSPlugins GCS Plugins
9 * @addtogroup CorePlugin Core Plugin
11 * @brief The Core GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "fileiconprovider.h"
30 #include "mimedatabase.h"
32 #include <QtGui/QApplication>
33 #include <QtGui/QStyle>
34 #include <QtGui/QPainter>
39 \class FileIconProvider
41 Provides icons based on file suffixes.
43 The class is a singleton: It's instance can be accessed via the static instance() method.
44 Plugins can register custom icons via registerIconSuffix(), and retrieve icons via the icon()
48 FileIconProvider
*FileIconProvider::m_instance
= 0;
50 FileIconProvider::FileIconProvider()
51 : m_unknownFileIcon(qApp
->style()->standardIcon(QStyle::SP_FileIcon
))
54 FileIconProvider::~FileIconProvider()
60 Returns the icon associated with the file suffix in fileInfo. If there is none,
61 the default icon of the operating system is returned.
63 QIcon
FileIconProvider::icon(const QFileInfo
&fileInfo
)
65 const QString suffix
= fileInfo
.suffix();
66 QIcon icon
= iconForSuffix(suffix
);
69 // Get icon from OS and store it in the cache
71 // Disabled since for now we'll make sure that all icons fit with our
72 // own custom icons by returning an empty one if we don't know it.
73 #if defined(Q_WS_WIN) || defined(Q_WS_MAC)
74 // This is incorrect if the OS does not always return the same icon for the
75 // same suffix (Mac OS X), but should speed up the retrieval a lot ...
76 icon
= m_systemIconProvider
.icon(fileInfo
);
77 if (!suffix
.isEmpty()) {
78 registerIconOverlayForSuffix(icon
, suffix
);
81 if (fileInfo
.isDir()) {
82 icon
= m_systemIconProvider
.icon(fileInfo
);
84 icon
= m_unknownFileIcon
;
93 Creates a pixmap with baseicon at size and overlays overlayIcon over it.
95 QPixmap
FileIconProvider::overlayIcon(QStyle::StandardPixmap baseIcon
, const QIcon
&overlayIcon
, const QSize
&size
) const
97 QPixmap iconPixmap
= qApp
->style()->standardIcon(baseIcon
).pixmap(size
);
98 QPainter
painter(&iconPixmap
);
100 painter
.drawPixmap(0, 0, overlayIcon
.pixmap(size
));
106 Registers an icon for a given suffix, overlaying the system file icon.
108 void FileIconProvider::registerIconOverlayForSuffix(const QIcon
&icon
, const QString
&suffix
)
110 QPixmap fileIconPixmap
= overlayIcon(QStyle::SP_FileIcon
, icon
, QSize(16, 16));
112 // delete old icon, if it exists
113 QList
<QPair
<QString
, QIcon
> >::iterator iter
= m_cache
.begin();
114 for (; iter
!= m_cache
.end(); ++iter
) {
115 if ((*iter
).first
== suffix
) {
116 iter
= m_cache
.erase(iter
);
121 QPair
<QString
, QIcon
> newEntry(suffix
, fileIconPixmap
);
122 m_cache
.append(newEntry
);
126 Registers an icon for all the suffixes of a given mime type, overlaying the system file icon.
128 void FileIconProvider::registerIconOverlayForMimeType(const QIcon
&icon
, const MimeType
&mimeType
)
130 foreach(const QString
&suffix
, mimeType
.suffixes())
131 registerIconOverlayForSuffix(icon
, suffix
);
135 Returns an icon for the given suffix, or an empty one if none registered.
137 QIcon
FileIconProvider::iconForSuffix(const QString
&suffix
) const
141 #if defined(Q_WS_WIN) || defined(Q_WS_MAC) // On Windows and Mac we use the file system icons
144 if (suffix
.isEmpty()) {
148 QList
<QPair
<QString
, QIcon
> >::const_iterator iter
= m_cache
.constBegin();
149 for (; iter
!= m_cache
.constEnd(); ++iter
) {
150 if ((*iter
).first
== suffix
) {
151 icon
= (*iter
).second
;
160 Returns the sole instance of FileIconProvider.
162 FileIconProvider
*FileIconProvider::instance()
165 m_instance
= new FileIconProvider
;