2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "fileiconprovider.h"
|
2009-07-15 11:20:24 +02:00
|
|
|
#include "mimedatabase.h"
|
|
|
|
|
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2010-01-28 15:47:45 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QStyle>
|
|
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QPair>
|
|
|
|
|
#include <QDebug>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileIconProvider>
|
|
|
|
|
#include <QIcon>
|
|
|
|
|
#include <QStyle>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-08-23 15:53:58 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*!
|
2010-07-12 16:53:07 +02:00
|
|
|
\class Core::FileIconProvider
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
Provides icons based on file suffixes with the ability to overwrite system
|
|
|
|
|
icons for specific subtypes. Implements the QFileIconProvider interface
|
|
|
|
|
and can therefore be used for QFileSystemModel.
|
|
|
|
|
|
|
|
|
|
Note: Registering overlay icons currently completely replaces the system
|
|
|
|
|
icon and is therefore not recommended on platforms that have their
|
|
|
|
|
own overlay icon handling (Mac/Windows).
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
The class is a singleton: It's instance can be accessed via the static instance() method.
|
2011-10-25 09:56:17 +02:00
|
|
|
Plugins can register custom overlay icons via registerIconOverlayForSuffix(), and
|
|
|
|
|
retrieve icons via the icon() method.
|
2010-01-28 15:47:45 +01:00
|
|
|
The instance is explicitly deleted by the core plugin for destruction order reasons.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
|
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
// Cache icons in a list of pairs suffix/icon which should be faster than
|
|
|
|
|
// hashes for small lists.
|
|
|
|
|
|
|
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
|
|
|
|
typedef QPair<QString, QIcon> StringIconPair;
|
|
|
|
|
typedef QList<StringIconPair> StringIconPairList;
|
|
|
|
|
|
|
|
|
|
// Helper to find an icon by suffix in a list of pairs for const/non-const-iterators.
|
|
|
|
|
|
|
|
|
|
template <class StringIconPairListIterator>
|
|
|
|
|
inline StringIconPairListIterator
|
|
|
|
|
findBySuffix(const QString &suffix,
|
|
|
|
|
StringIconPairListIterator iter,
|
|
|
|
|
const StringIconPairListIterator &end)
|
|
|
|
|
{
|
|
|
|
|
for (; iter != end; ++iter)
|
|
|
|
|
if ((*iter).first == suffix)
|
|
|
|
|
return iter;
|
|
|
|
|
return end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
struct FileIconProviderPrivate {
|
|
|
|
|
FileIconProviderPrivate();
|
|
|
|
|
|
|
|
|
|
// Mapping of file suffix to icon.
|
|
|
|
|
StringIconPairList m_cache;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
QIcon m_unknownFileIcon;
|
|
|
|
|
|
|
|
|
|
// singleton pattern
|
|
|
|
|
static FileIconProvider *m_instance;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FileIconProviderPrivate::FileIconProviderPrivate() :
|
|
|
|
|
m_unknownFileIcon(qApp->style()->standardIcon(QStyle::SP_FileIcon))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileIconProvider *FileIconProviderPrivate::m_instance = 0;
|
|
|
|
|
|
|
|
|
|
// FileIconProvider
|
|
|
|
|
|
|
|
|
|
FileIconProvider::FileIconProvider() :
|
|
|
|
|
d(new FileIconProviderPrivate)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-28 15:47:45 +01:00
|
|
|
FileIconProviderPrivate::m_instance = this;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileIconProvider::~FileIconProvider()
|
|
|
|
|
{
|
2010-01-28 15:47:45 +01:00
|
|
|
FileIconProviderPrivate::m_instance = 0;
|
|
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the icon associated with the file suffix in fileInfo. If there is none,
|
|
|
|
|
the default icon of the operating system is returned.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
QIcon FileIconProvider::icon(const QFileInfo &fileInfo) const
|
|
|
|
|
{
|
|
|
|
|
typedef StringIconPairList::const_iterator CacheConstIterator;
|
|
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "FileIconProvider::icon" << fileInfo.absoluteFilePath();
|
|
|
|
|
// Check for cached overlay icons by file suffix.
|
|
|
|
|
if (!d->m_cache.isEmpty() && !fileInfo.isDir()) {
|
|
|
|
|
const QString suffix = fileInfo.suffix();
|
|
|
|
|
if (!suffix.isEmpty()) {
|
|
|
|
|
const CacheConstIterator it = findBySuffix(suffix, d->m_cache.constBegin(), d->m_cache.constEnd());
|
|
|
|
|
if (it != d->m_cache.constEnd())
|
|
|
|
|
return (*it).second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Get icon from OS.
|
2012-08-23 15:53:58 +02:00
|
|
|
if (HostOsInfo::isWindowsHost() || HostOsInfo::isMacHost())
|
|
|
|
|
return QFileIconProvider::icon(fileInfo);
|
|
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
// File icons are unknown on linux systems.
|
|
|
|
|
return (fileInfo.isDir()) ?
|
2010-06-09 12:58:16 +02:00
|
|
|
QFileIconProvider::icon(fileInfo) :
|
2010-01-28 15:47:45 +01:00
|
|
|
d->m_unknownFileIcon;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-15 11:20:24 +02:00
|
|
|
/*!
|
|
|
|
|
Creates a pixmap with baseicon at size and overlays overlayIcon over it.
|
2010-01-28 15:47:45 +01:00
|
|
|
See platform note in class documentation about recommended usage.
|
2009-07-15 11:20:24 +02:00
|
|
|
*/
|
2010-02-12 12:40:32 +01:00
|
|
|
QPixmap FileIconProvider::overlayIcon(QStyle::StandardPixmap baseIcon, const QIcon &overlayIcon, const QSize &size)
|
2009-01-27 12:40:49 +01:00
|
|
|
{
|
|
|
|
|
QPixmap iconPixmap = qApp->style()->standardIcon(baseIcon).pixmap(size);
|
|
|
|
|
QPainter painter(&iconPixmap);
|
|
|
|
|
painter.drawPixmap(0, 0, overlayIcon.pixmap(size));
|
|
|
|
|
painter.end();
|
|
|
|
|
return iconPixmap;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*!
|
2009-07-15 11:20:24 +02:00
|
|
|
Registers an icon for a given suffix, overlaying the system file icon.
|
2010-01-28 15:47:45 +01:00
|
|
|
See platform note in class documentation about recommended usage.
|
2008-12-02 12:01:29 +01:00
|
|
|
*/
|
2010-01-28 15:47:45 +01:00
|
|
|
void FileIconProvider::registerIconOverlayForSuffix(const QIcon &icon,
|
|
|
|
|
const QString &suffix)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-01-28 15:47:45 +01:00
|
|
|
typedef StringIconPairList::iterator CacheIterator;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "FileIconProvider::registerIconOverlayForSuffix" << suffix;
|
|
|
|
|
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(!icon.isNull() && !suffix.isEmpty(), return);
|
2010-01-28 15:47:45 +01:00
|
|
|
|
|
|
|
|
const QPixmap fileIconPixmap = overlayIcon(QStyle::SP_FileIcon, icon, QSize(16, 16));
|
|
|
|
|
// replace old icon, if it exists
|
|
|
|
|
const CacheIterator it = findBySuffix(suffix, d->m_cache.begin(), d->m_cache.end());
|
|
|
|
|
if (it == d->m_cache.end()) {
|
|
|
|
|
d->m_cache.append(StringIconPair(suffix, fileIconPixmap));
|
|
|
|
|
} else {
|
|
|
|
|
(*it).second = fileIconPixmap;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 11:20:24 +02:00
|
|
|
/*!
|
|
|
|
|
Registers an icon for all the suffixes of a given mime type, overlaying the system file icon.
|
|
|
|
|
*/
|
|
|
|
|
void FileIconProvider::registerIconOverlayForMimeType(const QIcon &icon, const MimeType &mimeType)
|
|
|
|
|
{
|
|
|
|
|
foreach (const QString &suffix, mimeType.suffixes())
|
|
|
|
|
registerIconOverlayForSuffix(icon, suffix);
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*!
|
|
|
|
|
Returns the sole instance of FileIconProvider.
|
|
|
|
|
*/
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
FileIconProvider *FileIconProvider::instance()
|
|
|
|
|
{
|
2010-01-28 15:47:45 +01:00
|
|
|
if (!FileIconProviderPrivate::m_instance)
|
|
|
|
|
FileIconProviderPrivate::m_instance = new FileIconProvider;
|
|
|
|
|
return FileIconProviderPrivate::m_instance;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
|
|
|
|
} // namespace core
|