summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2016-07-27 14:00:14 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2016-08-05 08:06:21 +0000
commit2cd2cba0868efb7e8dd2d00805f1a5ceed0a1349 (patch)
tree7fff626ad15f65aeb09508559d82b94f97337714 /src/widgets/itemviews
parent955b2bdfc0cb1d707f8914be230e5e00c548b6ab (diff)
QtPlatformSupport: Add QAbstractFileIconEngine
Move the code from QtWidgets/QFileIconEngine into a new class with virtuals in QtPlatformSupport so that platforms can reuse it. Prototypically use the class in the Windows and macOS QPA plugins. Remove QPlatformTheme::fileIconPixmap() and change the type of the hint QPlatformTheme::IconPixmapSizes from QList<int> to QList<QSize> so that it fits better with the icon code. Change-Id: I580e936f3507218757565ca099272cd575b3a779 Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src/widgets/itemviews')
-rw-r--r--src/widgets/itemviews/qfileiconprovider.cpp134
1 files changed, 6 insertions, 128 deletions
diff --git a/src/widgets/itemviews/qfileiconprovider.cpp b/src/widgets/itemviews/qfileiconprovider.cpp
index 5638e438d5..8c3cf3859e 100644
--- a/src/widgets/itemviews/qfileiconprovider.cpp
+++ b/src/widgets/itemviews/qfileiconprovider.cpp
@@ -60,110 +60,6 @@
QT_BEGIN_NAMESPACE
-static bool isCacheable(const QFileInfo &fi);
-
-static QPlatformTheme::IconOptions toThemeIconOptions(QFileIconProvider::Options options)
-{
- QPlatformTheme::IconOptions result;
- if (options & QFileIconProvider::DontUseCustomDirectoryIcons)
- result |= QPlatformTheme::DontUseCustomDirectoryIcons;
- return result;
-}
-
-class QFileIconEngine : public QPixmapIconEngine
-{
-public:
- QFileIconEngine(const QFileInfo &info, QFileIconProvider::Options opts)
- : QPixmapIconEngine(), m_fileInfo(info), m_fipOpts(opts)
- { }
-
- QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) Q_DECL_OVERRIDE
- {
- Q_UNUSED(mode);
- Q_UNUSED(state);
- QPixmap pixmap;
-
- if (!size.isValid())
- return pixmap;
-
- const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
- if (!theme)
- return pixmap;
-
- const QString &keyBase = QLatin1String("qt_.") + m_fileInfo.suffix().toUpper();
-
- bool cacheable = isCacheable(m_fileInfo);
- if (cacheable) {
- QPixmapCache::find(keyBase + QString::number(size.width()), pixmap);
- if (!pixmap.isNull())
- return pixmap;
- }
-
- pixmap = theme->fileIconPixmap(m_fileInfo, size, toThemeIconOptions(m_fipOpts));
- if (!pixmap.isNull()) {
- if (cacheable)
- QPixmapCache::insert(keyBase + QString::number(size.width()), pixmap);
- }
-
- return pixmap;
- }
-
- QList<QSize> availableSizes(QIcon::Mode mode = QIcon::Normal, QIcon::State state = QIcon::Off) const Q_DECL_OVERRIDE
- {
- Q_UNUSED(mode);
- Q_UNUSED(state);
- static QList<QSize> sizes;
- static QPlatformTheme *theme = 0;
- if (!theme) {
- theme = QGuiApplicationPrivate::platformTheme();
- if (!theme)
- return sizes;
-
- QList<int> themeSizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >();
- if (themeSizes.isEmpty())
- return sizes;
-
- sizes.reserve(themeSizes.count());
- foreach (int size, themeSizes)
- sizes << QSize(size, size);
- }
- return sizes;
- }
-
- QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) Q_DECL_OVERRIDE
- {
- const QList<QSize> &sizes = availableSizes(mode, state);
- const int numberSizes = sizes.length();
- if (numberSizes == 0)
- return QSize();
-
- // Find the smallest available size whose area is still larger than the input
- // size. Otherwise, use the largest area available size. (We don't assume the
- // platform theme sizes are sorted, hence the extra logic.)
- const int sizeArea = size.width() * size.height();
- QSize actualSize = sizes.first();
- int actualArea = actualSize.width() * actualSize.height();
- for (int i = 1; i < numberSizes; ++i) {
- const QSize &s = sizes.at(i);
- const int a = s.width() * s.height();
- if ((sizeArea <= a && a < actualArea) || (actualArea < sizeArea && actualArea < a)) {
- actualSize = s;
- actualArea = a;
- }
- }
-
- if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
- actualSize.scale(size, Qt::KeepAspectRatio);
-
- return actualSize;
- }
-
-private:
- QFileInfo m_fileInfo;
- QFileIconProvider::Options m_fipOpts;
-};
-
-
/*!
\class QFileIconProvider
@@ -326,35 +222,17 @@ QIcon QFileIconProvider::icon(IconType type) const
return QIcon();
}
-static bool isCacheable(const QFileInfo &fi)
+static inline QPlatformTheme::IconOptions toThemeIconOptions(QFileIconProvider::Options options)
{
- if (!fi.isFile())
- return false;
-
-#ifdef Q_OS_WIN
- // On windows it's faster to just look at the file extensions. QTBUG-13182
- const QString fileExtension = fi.suffix();
- // Will return false for .exe, .lnk and .ico extensions
- return fileExtension.compare(QLatin1String("exe"), Qt::CaseInsensitive) &&
- fileExtension.compare(QLatin1String("lnk"), Qt::CaseInsensitive) &&
- fileExtension.compare(QLatin1String("ico"), Qt::CaseInsensitive);
-#else
- return !fi.isExecutable() && !fi.isSymLink();
-#endif
+ QPlatformTheme::IconOptions result;
+ if (options & QFileIconProvider::DontUseCustomDirectoryIcons)
+ result |= QPlatformTheme::DontUseCustomDirectoryIcons;
+ return result;
}
QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const
{
- const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
- if (!theme)
- return QIcon();
-
- QIcon themeFileIcon = theme->fileIcon(fi, toThemeIconOptions(options));
- if (!themeFileIcon.isNull())
- return themeFileIcon;
-
- QList<int> sizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >();
- return sizes.isEmpty() ? QIcon() : QIcon(new QFileIconEngine(fi, options));
+ return QGuiApplicationPrivate::platformTheme()->fileIcon(fi, toThemeIconOptions(options));
}
/*!