Commit b72d579e authored by Tobias Hunger's avatar Tobias Hunger
Browse files

Core: Handle matches on complete filename in fileiconprovider



This enables file icons for cmake's CMakeLists.txt files.

Change-Id: I9c0af8f27a64c57251e986508287a05fc1e66073
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
Reviewed-by: Eike Ziller's avatarEike Ziller <eike.ziller@qt.io>
parent a5e11066
......@@ -74,6 +74,14 @@ public:
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)
......@@ -83,7 +91,7 @@ public:
const QPixmap fileIconPixmap = FileIconProvider::overlayIcon(QStyle::SP_FileIcon, icon, QSize(16, 16));
// replace old icon, if it exists
m_cache.insert(suffix, fileIconPixmap);
m_suffixCache.insert(suffix, fileIconPixmap);
}
void registerIconOverlayForMimeType(const QIcon &icon, const Utils::MimeType &mimeType)
......@@ -93,7 +101,8 @@ public:
}
// Mapping of file suffix to icon.
QHash<QString, QIcon> m_cache;
QHash<QString, QIcon> m_suffixCache;
QHash<QString, QIcon> m_filenameCache;
QIcon m_unknownFileIcon;
};
......@@ -115,19 +124,27 @@ QIcon FileIconProviderImplementation::icon(const QFileInfo &fileInfo)
qDebug() << "FileIconProvider::icon" << fileInfo.absoluteFilePath();
// Check for cached overlay icons by file suffix.
bool isDir = fileInfo.isDir();
QString suffix = !isDir ? fileInfo.suffix() : QString();
if (!m_cache.isEmpty() && !isDir && !suffix.isEmpty()) {
if (m_cache.contains(suffix))
return m_cache.value(suffix);
const QString filename = !isDir ? fileInfo.fileName() : QString();
if (!filename.isEmpty()) {
auto it = m_filenameCache.constFind(filename);
if (it != m_filenameCache.constEnd())
return it.value();
}
// Get icon from OS.
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_cache.insert(suffix, icon);
m_suffixCache.insert(suffix, icon);
return icon;
}
......@@ -199,5 +216,11 @@ void registerIconOverlayForMimeType(const char *path, const char *mimeType)
mdb.mimeTypeForName(QString::fromLatin1(mimeType)));
}
void registerIconOverlayForFilename(const char *path, const char *filename)
{
instance()->registerIconOverlayForFilename(QIcon(QString::fromLatin1(path)),
QString::fromLatin1(filename));
}
} // namespace FileIconProvider
} // namespace Core
......@@ -45,6 +45,7 @@ CORE_EXPORT QIcon icon(QFileIconProvider::IconType type);
CORE_EXPORT QPixmap overlayIcon(const QPixmap &baseIcon, const QIcon &overlayIcon);
CORE_EXPORT QPixmap overlayIcon(QStyle::StandardPixmap baseIcon, const QIcon &overlayIcon, const QSize &size);
CORE_EXPORT void registerIconOverlayForSuffix(const char *path, const char *suffix);
CORE_EXPORT void registerIconOverlayForFilename(const char *path, const char *filename);
CORE_EXPORT void registerIconOverlayForMimeType(const char *path, const char *mimeType);
CORE_EXPORT void registerIconOverlayForMimeType(const QIcon &icon, const char *mimeType);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment