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 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | using 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 | */ |
29 | QPixmap QAbstractFileIconEngine::pixmap(const QSize &size, QIcon::Mode mode, |
30 | QIcon::State state) |
31 | { |
32 | return scaledPixmap(size, mode, state, scale: 1.0); |
33 | } |
34 | |
35 | QPixmap QAbstractFileIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) |
36 | { |
37 | Q_UNUSED(mode); |
38 | Q_UNUSED(state); |
39 | |
40 | if (!size.isValid()) |
41 | return QPixmap(); |
42 | |
43 | QString key = cacheKey(); |
44 | if (key.isEmpty()) |
45 | return filePixmap(size: size * scale, mode, state); |
46 | |
47 | key += u'_' + QString::number(size.width()); |
48 | |
49 | QPixmap result; |
50 | if (!QPixmapCache::find(key, pixmap: &result)) { |
51 | result = filePixmap(size: size * scale, mode, state); |
52 | if (!result.isNull()) |
53 | QPixmapCache::insert(key, pixmap: result); |
54 | } |
55 | |
56 | return result; |
57 | } |
58 | |
59 | QSize QAbstractFileIconEngine::actualSize(const QSize &size, QIcon::Mode mode, |
60 | QIcon::State state) |
61 | { |
62 | const QList<QSize> &sizes = availableSizes(mode, state); |
63 | const int numberSizes = sizes.size(); |
64 | if (numberSizes == 0) |
65 | return QSize(); |
66 | |
67 | // Find the smallest available size whose area is still larger than the input |
68 | // size. Otherwise, use the largest area available size. (We don't assume the |
69 | // platform theme sizes are sorted, hence the extra logic.) |
70 | const int sizeArea = size.width() * size.height(); |
71 | QSize actualSize = sizes.first(); |
72 | int actualArea = actualSize.width() * actualSize.height(); |
73 | for (int i = 1; i < numberSizes; ++i) { |
74 | const QSize &s = sizes.at(i); |
75 | const int a = s.width() * s.height(); |
76 | if ((sizeArea <= a && a < actualArea) || (actualArea < sizeArea && actualArea < a)) { |
77 | actualSize = s; |
78 | actualArea = a; |
79 | } |
80 | } |
81 | |
82 | if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) |
83 | actualSize.scale(s: size, mode: Qt::KeepAspectRatio); |
84 | |
85 | return actualSize; |
86 | } |
87 | |
88 | /* Reimplement to return a cache key for the entry. An empty result indicates |
89 | * the icon should not be cached (for example, directory icons having custom icons). */ |
90 | QString QAbstractFileIconEngine::cacheKey() const |
91 | { |
92 | if (!m_fileInfo.isFile() || m_fileInfo.isSymLink() || m_fileInfo.isExecutable()) |
93 | return QString(); |
94 | |
95 | const QString &suffix = m_fileInfo.suffix(); |
96 | return "qt_."_L1 |
97 | + (suffix.isEmpty() ? m_fileInfo.fileName() : suffix); // handle "Makefile" ;) |
98 | } |
99 | |
100 | QT_END_NAMESPACE |
101 | |