1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qabstractfileiconengine_p.h"
5
6#include <qpixmapcache.h>
7
8QT_BEGIN_NAMESPACE
9
10using namespace Qt::StringLiterals;
11
12/*!
13 \class QAbstractFileIconEngine
14 \brief Helper base class for retrieving icons for files for usage by QFileIconProvider and related.
15
16 Reimplement availableSizes() and new virtual filePixmap() and return icons created
17 with this engine from QPlatformTheme::fileIcon().
18
19 Note: The class internally caches pixmaps for files by suffix (with the exception
20 of some files on Windows), but not for directories (since directory icons may have
21 overlay icons on Windows). You might want to cache pixmaps for directories
22 in your implementation.
23
24 \since 5.8
25 \internal
26 \sa QFileIconProvider::DontUseCustomDirectoryIcons, QPlatformTheme
27 \ingroup qpa
28*/
29QPixmap QAbstractFileIconEngine::pixmap(const QSize &size, QIcon::Mode mode,
30 QIcon::State state)
31{
32 Q_UNUSED(mode);
33 Q_UNUSED(state);
34
35 if (!size.isValid())
36 return QPixmap();
37
38 QString key = cacheKey();
39 if (key.isEmpty())
40 return filePixmap(size, mode, state);
41
42 key += u'_' + QString::number(size.width());
43
44 QPixmap result;
45 if (!QPixmapCache::find(key, pixmap: &result)) {
46 result = filePixmap(size, mode, state);
47 if (!result.isNull())
48 QPixmapCache::insert(key, pixmap: result);
49 }
50
51 return result;
52}
53
54QPixmap QAbstractFileIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
55{
56 Q_UNUSED(scale); // (size is pre-multiplied by scale)
57 return pixmap(size, mode, state);
58}
59
60QSize QAbstractFileIconEngine::actualSize(const QSize &size, QIcon::Mode mode,
61 QIcon::State state)
62{
63 const QList<QSize> &sizes = availableSizes(mode, state);
64 const int numberSizes = sizes.size();
65 if (numberSizes == 0)
66 return QSize();
67
68 // Find the smallest available size whose area is still larger than the input
69 // size. Otherwise, use the largest area available size. (We don't assume the
70 // platform theme sizes are sorted, hence the extra logic.)
71 const int sizeArea = size.width() * size.height();
72 QSize actualSize = sizes.first();
73 int actualArea = actualSize.width() * actualSize.height();
74 for (int i = 1; i < numberSizes; ++i) {
75 const QSize &s = sizes.at(i);
76 const int a = s.width() * s.height();
77 if ((sizeArea <= a && a < actualArea) || (actualArea < sizeArea && actualArea < a)) {
78 actualSize = s;
79 actualArea = a;
80 }
81 }
82
83 if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
84 actualSize.scale(s: size, mode: Qt::KeepAspectRatio);
85
86 return actualSize;
87}
88
89/* Reimplement to return a cache key for the entry. An empty result indicates
90 * the icon should not be cached (for example, directory icons having custom icons). */
91QString QAbstractFileIconEngine::cacheKey() const
92{
93 if (!m_fileInfo.isFile() || m_fileInfo.isSymLink() || m_fileInfo.isExecutable())
94 return QString();
95
96 const QString &suffix = m_fileInfo.suffix();
97 return "qt_."_L1
98 + (suffix.isEmpty() ? m_fileInfo.fileName() : suffix); // handle "Makefile" ;)
99}
100
101QT_END_NAMESPACE
102

source code of qtbase/src/gui/image/qabstractfileiconengine.cpp