2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
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
|
|
|
#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>
|
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
|
|
|
|
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
|
2013-11-19 12:49:13 +01:00
|
|
|
m_cache.insert(suffix, fileIconPixmap);
|
2013-09-12 16:06:33 +02:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
void registerIconOverlayForMimeType(const QIcon &icon, const MimeType &mimeType)
|
|
|
|
|
{
|
|
|
|
|
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.
|
2013-11-19 12:49:13 +01:00
|
|
|
QHash<QString, QIcon> m_cache;
|
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();
|
|
|
|
|
QString suffix = !isDir ? fileInfo.suffix() : QString();
|
|
|
|
|
if (!m_cache.isEmpty() && !isDir && !suffix.isEmpty()) {
|
|
|
|
|
if (m_cache.contains(suffix))
|
2013-11-19 12:49:13 +01:00
|
|
|
return m_cache.value(suffix);
|
2010-01-28 15:47:45 +01:00
|
|
|
}
|
|
|
|
|
// Get icon from OS.
|
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())
|
|
|
|
|
m_cache.insert(suffix, icon);
|
|
|
|
|
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
|
|
|
/*!
|
|
|
|
|
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
|
|
|
*/
|
2013-09-12 16:06:33 +02:00
|
|
|
QPixmap 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
|
|
|
*/
|
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
|
|
|
{
|
2013-09-12 16:06:33 +02:00
|
|
|
instance()->registerIconOverlayForMimeType(icon, MimeDatabase::findByType(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
|
|
|
{
|
2013-09-12 16:06:33 +02:00
|
|
|
instance()->registerIconOverlayForMimeType(QIcon(QLatin1String(path)), MimeDatabase::findByType(QString::fromLatin1(mimeType)));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-01-28 15:47:45 +01:00
|
|
|
|
2013-09-12 16:06:33 +02:00
|
|
|
} // namespace FileIconProvider
|
|
|
|
|
} // namespace Core
|