2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02: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
|
|
|
|
2012-08-23 15:53:58 +02:00
|
|
|
#include <utils/hostosinfo.h>
|
2015-02-04 09:32:46 +01:00
|
|
|
#include <utils/mimetypes/mimedatabase.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>
|
2013-11-19 12:49:13 +01:00
|
|
|
#include <QHash>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDebug>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileIconProvider>
|
|
|
|
|
#include <QIcon>
|
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
|
2013-09-12 16:06:33 +02:00
|
|
|
icons for specific subtypes. The underlying QFileIconProvider
|
|
|
|
|
can be used for QFileSystemModel.
|
2010-01-28 15:47:45 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-10-25 09:56:17 +02:00
|
|
|
Plugins can register custom overlay icons via registerIconOverlayForSuffix(), and
|
2013-10-07 13:34:40 +02:00
|
|
|
retrieve icons via the icon() function.
|
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.
|
|
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
namespace Core {
|
|
|
|
|
namespace FileIconProvider {
|
|
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
class FileIconProviderImplementation : public QFileIconProvider
|
2010-01-28 15:47:45 +01:00
|
|
|
{
|
2013-09-12 16:06:33 +02:00
|
|
|
public:
|
|
|
|
|
FileIconProviderImplementation()
|
|
|
|
|
: m_unknownFileIcon(qApp->style()->standardIcon(QStyle::SP_FileIcon))
|
|
|
|
|
{}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-11-19 13:05:42 +01:00
|
|
|
QIcon icon(const QFileInfo &info);
|
2013-09-12 16:06:33 +02:00
|
|
|
using QFileIconProvider::icon;
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2016-11-09 15:07:40 +01:00
|
|
|
void registerIconOverlayForFilename(const QIcon &icon, const QString &filename)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!icon.isNull() && !filename.isEmpty(), return);
|
|
|
|
|
|
|
|
|
|
const QPixmap fileIconPixmap = FileIconProvider::overlayIcon(QStyle::SP_FileIcon, icon, QSize(16, 16));
|
|
|
|
|
m_filenameCache.insert(filename, fileIconPixmap);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
void registerIconOverlayForSuffix(const QIcon &icon, const QString &suffix)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "FileIconProvider::registerIconOverlayForSuffix" << suffix;
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
QTC_ASSERT(!icon.isNull() && !suffix.isEmpty(), return);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
const QPixmap fileIconPixmap = FileIconProvider::overlayIcon(QStyle::SP_FileIcon, icon, QSize(16, 16));
|
|
|
|
|
// replace old icon, if it exists
|
2016-11-09 15:07:40 +01:00
|
|
|
m_suffixCache.insert(suffix, fileIconPixmap);
|
2013-09-12 16:06:33 +02:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2015-02-04 09:32:46 +01:00
|
|
|
void registerIconOverlayForMimeType(const QIcon &icon, const Utils::MimeType &mimeType)
|
2013-09-12 16:06:33 +02:00
|
|
|
{
|
|
|
|
|
foreach (const QString &suffix, mimeType.suffixes())
|
|
|
|
|
registerIconOverlayForSuffix(icon, suffix);
|
|
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
// Mapping of file suffix to icon.
|
2016-11-09 15:07:40 +01:00
|
|
|
QHash<QString, QIcon> m_suffixCache;
|
|
|
|
|
QHash<QString, QIcon> m_filenameCache;
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
QIcon m_unknownFileIcon;
|
|
|
|
|
};
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
FileIconProviderImplementation *instance()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-09-12 16:06:33 +02:00
|
|
|
static FileIconProviderImplementation theInstance;
|
|
|
|
|
return &theInstance;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
QFileIconProvider *iconProvider()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-09-12 16:06:33 +02:00
|
|
|
return instance();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:05:42 +01:00
|
|
|
QIcon FileIconProviderImplementation::icon(const QFileInfo &fileInfo)
|
2010-01-28 15:47:45 +01:00
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "FileIconProvider::icon" << fileInfo.absoluteFilePath();
|
|
|
|
|
// Check for cached overlay icons by file suffix.
|
2013-11-19 13:05:42 +01:00
|
|
|
bool isDir = fileInfo.isDir();
|
2016-11-09 15:07:40 +01:00
|
|
|
const QString filename = !isDir ? fileInfo.fileName() : QString();
|
|
|
|
|
if (!filename.isEmpty()) {
|
|
|
|
|
auto it = m_filenameCache.constFind(filename);
|
|
|
|
|
if (it != m_filenameCache.constEnd())
|
|
|
|
|
return it.value();
|
2010-01-28 15:47:45 +01:00
|
|
|
}
|
2016-11-09 15:07:40 +01:00
|
|
|
const QString suffix = !isDir ? fileInfo.suffix() : QString();
|
|
|
|
|
if (!suffix.isEmpty()) {
|
|
|
|
|
auto it = m_suffixCache.constFind(suffix);
|
|
|
|
|
if (it != m_suffixCache.constEnd())
|
|
|
|
|
return it.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get icon from OS (and cache it based on suffix!)
|
2013-11-19 13:05:42 +01:00
|
|
|
QIcon icon;
|
2012-08-23 15:53:58 +02:00
|
|
|
if (HostOsInfo::isWindowsHost() || HostOsInfo::isMacHost())
|
2013-11-19 13:05:42 +01:00
|
|
|
icon = QFileIconProvider::icon(fileInfo);
|
|
|
|
|
else // File icons are unknown on linux systems.
|
|
|
|
|
icon = isDir ? QFileIconProvider::icon(fileInfo) : m_unknownFileIcon;
|
|
|
|
|
if (!isDir && !suffix.isEmpty())
|
2016-11-09 15:07:40 +01:00
|
|
|
m_suffixCache.insert(suffix, icon);
|
2013-11-19 13:05:42 +01:00
|
|
|
return icon;
|
2013-09-12 16:06:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns the icon associated with the file suffix in fileInfo. If there is none,
|
|
|
|
|
the default icon of the operating system is returned.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
QIcon icon(const QFileInfo &info)
|
|
|
|
|
{
|
|
|
|
|
return instance()->icon(info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \overload
|
|
|
|
|
*/
|
|
|
|
|
QIcon icon(QFileIconProvider::IconType type)
|
|
|
|
|
{
|
|
|
|
|
return instance()->icon(type);
|
2010-01-28 15:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 11:20:24 +02:00
|
|
|
/*!
|
2016-03-01 11:07:10 +02:00
|
|
|
Creates a pixmap with baseicon 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
|
|
|
*/
|
2016-03-01 11:07:10 +02:00
|
|
|
QPixmap overlayIcon(const QPixmap &baseIcon, const QIcon &overlayIcon)
|
2009-01-27 12:40:49 +01:00
|
|
|
{
|
2016-04-22 15:42:30 +02:00
|
|
|
QPixmap result = baseIcon;
|
|
|
|
|
QPainter painter(&result);
|
|
|
|
|
overlayIcon.paint(&painter, QRect(QPoint(), result.size() / result.devicePixelRatio()));
|
|
|
|
|
return result;
|
2009-01-27 12:40:49 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-01 11:07:10 +02:00
|
|
|
/*!
|
|
|
|
|
Creates a pixmap with baseicon at size and overlays overlayIcon over it.
|
|
|
|
|
See platform note in class documentation about recommended usage.
|
|
|
|
|
*/
|
|
|
|
|
QPixmap overlayIcon(QStyle::StandardPixmap baseIcon, const QIcon &overlay, const QSize &size)
|
|
|
|
|
{
|
|
|
|
|
return overlayIcon(qApp->style()->standardIcon(baseIcon).pixmap(size), overlay);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2013-09-12 16:06:33 +02:00
|
|
|
void registerIconOverlayForSuffix(const char *path, const char *suffix)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-09-12 16:06:33 +02:00
|
|
|
instance()->registerIconOverlayForSuffix(QIcon(QLatin1String(path)), QLatin1String(suffix));
|
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.
|
|
|
|
|
*/
|
2013-09-12 16:06:33 +02:00
|
|
|
void registerIconOverlayForMimeType(const QIcon &icon, const char *mimeType)
|
2009-07-15 11:20:24 +02:00
|
|
|
{
|
2015-02-04 09:32:46 +01:00
|
|
|
Utils::MimeDatabase mdb;
|
|
|
|
|
instance()->registerIconOverlayForMimeType(icon,
|
|
|
|
|
mdb.mimeTypeForName(QString::fromLatin1(mimeType)));
|
2009-07-15 11:20:24 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*!
|
2013-09-12 16:06:33 +02:00
|
|
|
* \overload
|
|
|
|
|
*/
|
|
|
|
|
void registerIconOverlayForMimeType(const char *path, const char *mimeType)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-02-04 09:32:46 +01:00
|
|
|
Utils::MimeDatabase mdb;
|
|
|
|
|
instance()->registerIconOverlayForMimeType(QIcon(QLatin1String(path)),
|
|
|
|
|
mdb.mimeTypeForName(QString::fromLatin1(mimeType)));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2016-11-09 15:07:40 +01:00
|
|
|
void registerIconOverlayForFilename(const char *path, const char *filename)
|
|
|
|
|
{
|
|
|
|
|
instance()->registerIconOverlayForFilename(QIcon(QString::fromLatin1(path)),
|
|
|
|
|
QString::fromLatin1(filename));
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
} // namespace FileIconProvider
|
|
|
|
|
} // namespace Core
|