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>
|
2019-11-15 14:12:26 +01:00
|
|
|
#include <utils/optional.h>
|
2010-01-28 15:47:45 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2019-11-15 14:12:26 +01:00
|
|
|
#include <utils/variant.h>
|
2010-01-28 15:47:45 +01:00
|
|
|
|
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
|
|
|
/*!
|
2020-03-18 13:32:02 +01:00
|
|
|
\namespace Core::FileIconProvider
|
|
|
|
|
\inmodule QtCreator
|
|
|
|
|
\brief Provides functions for registering custom overlay icons for system
|
|
|
|
|
icons.
|
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
|
|
|
|
2020-03-18 13:32:02 +01:00
|
|
|
\note Registering overlay icons currently completely replaces the system
|
2010-01-28 15:47:45 +01:00
|
|
|
icon and is therefore not recommended on platforms that have their
|
2020-03-18 13:32:02 +01:00
|
|
|
own overlay icon handling (\macOS and 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
|
|
|
*/
|
|
|
|
|
|
2019-11-15 14:12:26 +01:00
|
|
|
using Item = Utils::variant<QIcon, QString>; // icon or filename for the icon
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
namespace Core {
|
|
|
|
|
namespace FileIconProvider {
|
|
|
|
|
|
2010-01-28 15:47:45 +01:00
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
2019-11-15 14:12:26 +01:00
|
|
|
static Utils::optional<QIcon> getIcon(QHash<QString, Item> &cache, const QString &key)
|
|
|
|
|
{
|
|
|
|
|
auto it = cache.constFind(key);
|
|
|
|
|
if (it == cache.constEnd())
|
|
|
|
|
return {};
|
|
|
|
|
if (const QIcon *icon = Utils::get_if<QIcon>(&*it))
|
|
|
|
|
return *icon;
|
|
|
|
|
// need to create icon from file name first
|
|
|
|
|
const QString *fileName = Utils::get_if<QString>(&*it);
|
|
|
|
|
QTC_ASSERT(fileName, return {});
|
|
|
|
|
const QIcon icon = QIcon(
|
|
|
|
|
FileIconProvider::overlayIcon(QStyle::SP_FileIcon, QIcon(*fileName), QSize(16, 16)));
|
|
|
|
|
cache.insert(key, icon);
|
|
|
|
|
return icon;
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
{}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2016-11-29 17:06:05 +01:00
|
|
|
QIcon icon(const QFileInfo &info) const override;
|
2013-09-12 16:06:33 +02:00
|
|
|
using QFileIconProvider::icon;
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2019-11-15 14:12:26 +01:00
|
|
|
void registerIconOverlayForFilename(const QString &iconFilePath, const QString &filename)
|
2016-11-09 15:07:40 +01:00
|
|
|
{
|
2019-11-15 14:12:26 +01:00
|
|
|
m_filenameCache.insert(filename, iconFilePath);
|
2016-11-09 15:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
2019-11-15 14:12:26 +01:00
|
|
|
void registerIconOverlayForSuffix(const QString &iconFilePath, const QString &suffix)
|
2013-09-12 16:06:33 +02:00
|
|
|
{
|
2019-11-15 14:12:26 +01:00
|
|
|
m_suffixCache.insert(suffix, iconFilePath);
|
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)
|
2019-11-15 14:12:26 +01:00
|
|
|
{
|
|
|
|
|
const QStringList suffixes = mimeType.suffixes();
|
|
|
|
|
for (const QString &suffix : suffixes) {
|
|
|
|
|
QTC_ASSERT(!icon.isNull() && !suffix.isEmpty(), return );
|
|
|
|
|
|
|
|
|
|
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 QString &iconFilePath, const Utils::MimeType &mimeType)
|
2013-09-12 16:06:33 +02:00
|
|
|
{
|
|
|
|
|
foreach (const QString &suffix, mimeType.suffixes())
|
2019-11-15 14:12:26 +01:00
|
|
|
registerIconOverlayForSuffix(iconFilePath, suffix);
|
2013-09-12 16:06:33 +02:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
// Mapping of file suffix to icon.
|
2019-11-15 14:12:26 +01:00
|
|
|
mutable QHash<QString, Item> m_suffixCache;
|
|
|
|
|
mutable QHash<QString, Item> m_filenameCache;
|
2013-09-12 16:06:33 +02:00
|
|
|
};
|
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
|
|
|
}
|
|
|
|
|
|
2016-11-29 17:06:05 +01:00
|
|
|
QIcon FileIconProviderImplementation::icon(const QFileInfo &fileInfo) const
|
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()) {
|
2019-11-15 14:12:26 +01:00
|
|
|
const Utils::optional<QIcon> icon = getIcon(m_filenameCache, filename);
|
|
|
|
|
if (icon)
|
|
|
|
|
return *icon;
|
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()) {
|
2019-11-15 14:12:26 +01:00
|
|
|
const Utils::optional<QIcon> icon = getIcon(m_suffixCache, suffix);
|
|
|
|
|
if (icon)
|
|
|
|
|
return *icon;
|
2016-11-09 15:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get icon from OS (and cache it based on suffix!)
|
2013-11-19 13:05:42 +01:00
|
|
|
QIcon icon;
|
2019-01-28 14:18:20 +01:00
|
|
|
if (HostOsInfo::isWindowsHost() || HostOsInfo::isMacHost()) {
|
2013-11-19 13:05:42 +01:00
|
|
|
icon = QFileIconProvider::icon(fileInfo);
|
2019-01-28 14:18:20 +01:00
|
|
|
} else { // File icons are unknown on linux systems.
|
|
|
|
|
static const QIcon unknownFileIcon(QApplication::style()->standardIcon(QStyle::SP_FileIcon));
|
|
|
|
|
icon = isDir ? QFileIconProvider::icon(fileInfo) : unknownFileIcon;
|
|
|
|
|
}
|
2013-11-19 13:05:42 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
2020-03-18 13:32:02 +01:00
|
|
|
Returns the icon associated with the file suffix in \a info. If there is none,
|
2013-09-12 16:06:33 +02:00
|
|
|
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
|
|
|
/*!
|
2020-03-18 13:32:02 +01:00
|
|
|
Creates a pixmap with \a baseIcon and lays \a overlayIcon over it.
|
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
|
|
|
/*!
|
2020-03-18 13:32:02 +01:00
|
|
|
Creates a pixmap with \a baseIcon at \a size and \a overlay.
|
2016-03-01 11:07:10 +02:00
|
|
|
*/
|
|
|
|
|
QPixmap overlayIcon(QStyle::StandardPixmap baseIcon, const QIcon &overlay, const QSize &size)
|
|
|
|
|
{
|
2017-04-24 17:01:10 +02:00
|
|
|
return overlayIcon(QApplication::style()->standardIcon(baseIcon).pixmap(size), overlay);
|
2016-03-01 11:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
/*!
|
2020-03-18 13:32:02 +01:00
|
|
|
Registers an icon at \a path for a given \a suffix, overlaying the system
|
|
|
|
|
file icon.
|
|
|
|
|
*/
|
2016-11-10 15:05:05 +01:00
|
|
|
void registerIconOverlayForSuffix(const QString &path, const QString &suffix)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2019-11-15 14:12:26 +01:00
|
|
|
instance()->registerIconOverlayForSuffix(path, suffix);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2009-07-15 11:20:24 +02:00
|
|
|
/*!
|
2020-03-18 13:32:02 +01:00
|
|
|
Registers \a icon for all the suffixes of a the mime type \a mimeType,
|
|
|
|
|
overlaying the system file icon.
|
2009-07-15 11:20:24 +02:00
|
|
|
*/
|
2016-11-10 15:05:05 +01:00
|
|
|
void registerIconOverlayForMimeType(const QIcon &icon, const QString &mimeType)
|
2009-07-15 11:20:24 +02:00
|
|
|
{
|
2017-03-02 12:07:11 +01:00
|
|
|
instance()->registerIconOverlayForMimeType(icon, Utils::mimeTypeForName(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
|
|
|
|
|
*/
|
2016-11-10 15:05:05 +01:00
|
|
|
void registerIconOverlayForMimeType(const QString &path, const QString &mimeType)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2019-11-15 14:12:26 +01:00
|
|
|
instance()->registerIconOverlayForMimeType(path, Utils::mimeTypeForName(mimeType));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2016-11-10 15:05:05 +01:00
|
|
|
void registerIconOverlayForFilename(const QString &path, const QString &filename)
|
2016-11-09 15:07:40 +01:00
|
|
|
{
|
2019-11-15 14:12:26 +01:00
|
|
|
instance()->registerIconOverlayForFilename(path, filename);
|
2016-11-09 15:07:40 +01:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 11:32:15 +02:00
|
|
|
// Return a standard directory icon with the specified overlay:
|
|
|
|
|
QIcon directoryIcon(const QString &overlay)
|
|
|
|
|
{
|
|
|
|
|
// Overlay the SP_DirIcon with the custom icons
|
|
|
|
|
const QSize desiredSize = QSize(16, 16);
|
|
|
|
|
|
|
|
|
|
const QPixmap dirPixmap = QApplication::style()->standardIcon(QStyle::SP_DirIcon).pixmap(desiredSize);
|
|
|
|
|
const QIcon overlayIcon(overlay);
|
|
|
|
|
QIcon result;
|
|
|
|
|
result.addPixmap(Core::FileIconProvider::overlayIcon(dirPixmap, overlayIcon));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
} // namespace FileIconProvider
|
|
|
|
|
} // namespace Core
|