Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qabstractfileiconengine.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
7#include <qpixmapcache.h>
8
9QT_BEGIN_NAMESPACE
10
11using namespace Qt::StringLiterals;
12
13/*!
14 \class QAbstractFileIconEngine
15 \brief Helper base class for retrieving icons for files for usage by QFileIconProvider and related.
16
17 Reimplement availableSizes() and new virtual filePixmap() and return icons created
18 with this engine from QPlatformTheme::fileIcon().
19
20 Note: The class internally caches pixmaps for files by suffix (with the exception
21 of some files on Windows), but not for directories (since directory icons may have
22 overlay icons on Windows). You might want to cache pixmaps for directories
23 in your implementation.
24
25 \since 5.8
26 \internal
27 \sa QFileIconProvider::DontUseCustomDirectoryIcons, QPlatformTheme
28 \ingroup qpa
29*/
30QPixmap QAbstractFileIconEngine::pixmap(const QSize &size, QIcon::Mode mode,
31 QIcon::State state)
32{
33 return scaledPixmap(size, mode, state, 1.0);
34}
35
36QPixmap QAbstractFileIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
37{
38 Q_UNUSED(mode);
39 Q_UNUSED(state);
40
41 if (!size.isValid())
42 return QPixmap();
43
44 QString key = cacheKey();
45 if (key.isEmpty())
46 return filePixmap(size * scale, mode, state);
47
48 key += u'_' + QString::number(size.width() * scale);
49
50 QPixmap result;
51 if (!QPixmapCache::find(key, &result)) {
52 result = filePixmap(size * scale, mode, state);
53 if (!result.isNull())
54 QPixmapCache::insert(key, result);
55 }
56
57 return result;
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(size, 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