Files
qt-creator/src/plugins/coreplugin/fileiconprovider.cpp

227 lines
7.2 KiB
C++
Raw Normal View History

/****************************************************************************
2008-12-02 12:01:29 +01:00
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01: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
** 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.
**
** 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
**
****************************************************************************/
2008-12-02 14:09:21 +01:00
2008-12-02 12:01:29 +01:00
#include "fileiconprovider.h"
#include <utils/hostosinfo.h>
#include <utils/mimetypes/mimedatabase.h>
#include <utils/qtcassert.h>
#include <QApplication>
#include <QStyle>
#include <QPainter>
#include <QFileInfo>
#include <QHash>
#include <QDebug>
2008-12-02 12:01:29 +01:00
#include <QFileIconProvider>
#include <QIcon>
2008-12-02 12:01:29 +01: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
Provides icons based on file suffixes with the ability to overwrite system
icons for specific subtypes. The underlying QFileIconProvider
can 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
Plugins can register custom overlay icons via registerIconOverlayForSuffix(), and
retrieve icons via the icon() function.
2008-12-02 12:01:29 +01:00
*/
// Cache icons in a list of pairs suffix/icon which should be faster than
// hashes for small lists.
namespace Core {
namespace FileIconProvider {
enum { debug = 0 };
class FileIconProviderImplementation : public QFileIconProvider
{
public:
FileIconProviderImplementation()
: m_unknownFileIcon(qApp->style()->standardIcon(QStyle::SP_FileIcon))
{}
QIcon icon(const QFileInfo &info);
using QFileIconProvider::icon;
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);
}
void registerIconOverlayForSuffix(const QIcon &icon, const QString &suffix)
{
if (debug)
qDebug() << "FileIconProvider::registerIconOverlayForSuffix" << suffix;
QTC_ASSERT(!icon.isNull() && !suffix.isEmpty(), return);
2008-12-02 12:01:29 +01:00
const QPixmap fileIconPixmap = FileIconProvider::overlayIcon(QStyle::SP_FileIcon, icon, QSize(16, 16));
// replace old icon, if it exists
m_suffixCache.insert(suffix, fileIconPixmap);
}
void registerIconOverlayForMimeType(const QIcon &icon, const Utils::MimeType &mimeType)
{
foreach (const QString &suffix, mimeType.suffixes())
registerIconOverlayForSuffix(icon, suffix);
}
// Mapping of file suffix to icon.
QHash<QString, QIcon> m_suffixCache;
QHash<QString, QIcon> m_filenameCache;
QIcon m_unknownFileIcon;
};
FileIconProviderImplementation *instance()
2008-12-02 12:01:29 +01:00
{
static FileIconProviderImplementation theInstance;
return &theInstance;
2008-12-02 12:01:29 +01:00
}
QFileIconProvider *iconProvider()
2008-12-02 12:01:29 +01:00
{
return instance();
2008-12-02 12:01:29 +01:00
}
QIcon FileIconProviderImplementation::icon(const QFileInfo &fileInfo)
{
if (debug)
qDebug() << "FileIconProvider::icon" << fileInfo.absoluteFilePath();
// Check for cached overlay icons by file suffix.
bool isDir = fileInfo.isDir();
const QString filename = !isDir ? fileInfo.fileName() : QString();
if (!filename.isEmpty()) {
auto it = m_filenameCache.constFind(filename);
if (it != m_filenameCache.constEnd())
return it.value();
}
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!)
QIcon icon;
if (HostOsInfo::isWindowsHost() || HostOsInfo::isMacHost())
icon = QFileIconProvider::icon(fileInfo);
else // File icons are unknown on linux systems.
icon = isDir ? QFileIconProvider::icon(fileInfo) : m_unknownFileIcon;
if (!isDir && !suffix.isEmpty())
m_suffixCache.insert(suffix, icon);
return icon;
}
/*!
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);
}
/*!
Creates a pixmap with baseicon and overlays overlayIcon over it.
See platform note in class documentation about recommended usage.
*/
QPixmap overlayIcon(const QPixmap &baseIcon, const QIcon &overlayIcon)
{
QPixmap result = baseIcon;
QPainter painter(&result);
overlayIcon.paint(&painter, QRect(QPoint(), result.size() / result.devicePixelRatio()));
return result;
}
/*!
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
/*!
Registers an icon for a given suffix, overlaying the system file icon.
See platform note in class documentation about recommended usage.
2008-12-02 12:01:29 +01:00
*/
void registerIconOverlayForSuffix(const char *path, const char *suffix)
2008-12-02 12:01:29 +01:00
{
instance()->registerIconOverlayForSuffix(QIcon(QLatin1String(path)), QLatin1String(suffix));
2008-12-02 12:01:29 +01:00
}
/*!
Registers an icon for all the suffixes of a given mime type, overlaying the system file icon.
*/
void registerIconOverlayForMimeType(const QIcon &icon, const char *mimeType)
{
Utils::MimeDatabase mdb;
instance()->registerIconOverlayForMimeType(icon,
mdb.mimeTypeForName(QString::fromLatin1(mimeType)));
}
2008-12-02 12:01:29 +01:00
/*!
* \overload
*/
void registerIconOverlayForMimeType(const char *path, const char *mimeType)
2008-12-02 12:01:29 +01:00
{
Utils::MimeDatabase mdb;
instance()->registerIconOverlayForMimeType(QIcon(QLatin1String(path)),
mdb.mimeTypeForName(QString::fromLatin1(mimeType)));
2008-12-02 12:01:29 +01:00
}
void registerIconOverlayForFilename(const char *path, const char *filename)
{
instance()->registerIconOverlayForFilename(QIcon(QString::fromLatin1(path)),
QString::fromLatin1(filename));
}
} // namespace FileIconProvider
} // namespace Core