1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qabstractfileiconengine_p.h"
41
42#include <qpixmapcache.h>
43
44QT_BEGIN_NAMESPACE
45
46/*!
47 \class QAbstractFileIconEngine
48 \brief Helper base class for retrieving icons for files for usage by QFileIconProvider and related.
49
50 Reimplement availableSizes() and new virtual filePixmap() and return icons created
51 with this engine from QPlatformTheme::fileIcon().
52
53 Note: The class internally caches pixmaps for files by suffix (with the exception
54 of some files on Windows), but not for directories (since directory icons may have
55 overlay icons on Windows). You might want to cache pixmaps for directories
56 in your implementation.
57
58 \since 5.8
59 \internal
60 \sa QFileIconProvider::DontUseCustomDirectoryIcons, QPlatformTheme
61 \ingroup qpa
62*/
63QPixmap QAbstractFileIconEngine::pixmap(const QSize &size, QIcon::Mode mode,
64 QIcon::State state)
65{
66 Q_UNUSED(mode);
67 Q_UNUSED(state);
68
69 if (!size.isValid())
70 return QPixmap();
71
72 QString key = cacheKey();
73 if (key.isEmpty())
74 return filePixmap(size, mode, state);
75
76 key += QLatin1Char('_') + QString::number(size.width());
77
78 QPixmap result;
79 if (!QPixmapCache::find(key, pixmap: &result)) {
80 result = filePixmap(size, mode, state);
81 if (!result.isNull())
82 QPixmapCache::insert(key, pixmap: result);
83 }
84
85 return result;
86}
87
88QSize QAbstractFileIconEngine::actualSize(const QSize &size, QIcon::Mode mode,
89 QIcon::State state)
90{
91 const QList<QSize> &sizes = availableSizes(mode, state);
92 const int numberSizes = sizes.length();
93 if (numberSizes == 0)
94 return QSize();
95
96 // Find the smallest available size whose area is still larger than the input
97 // size. Otherwise, use the largest area available size. (We don't assume the
98 // platform theme sizes are sorted, hence the extra logic.)
99 const int sizeArea = size.width() * size.height();
100 QSize actualSize = sizes.first();
101 int actualArea = actualSize.width() * actualSize.height();
102 for (int i = 1; i < numberSizes; ++i) {
103 const QSize &s = sizes.at(i);
104 const int a = s.width() * s.height();
105 if ((sizeArea <= a && a < actualArea) || (actualArea < sizeArea && actualArea < a)) {
106 actualSize = s;
107 actualArea = a;
108 }
109 }
110
111 if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
112 actualSize.scale(s: size, mode: Qt::KeepAspectRatio);
113
114 return actualSize;
115}
116
117/* Reimplement to return a cache key for the entry. An empty result indicates
118 * the icon should not be cached (for example, directory icons having custom icons). */
119QString QAbstractFileIconEngine::cacheKey() const
120{
121 if (!m_fileInfo.isFile() || m_fileInfo.isSymLink() || m_fileInfo.isExecutable())
122 return QString();
123
124 const QString &suffix = m_fileInfo.suffix();
125 return QLatin1String("qt_.")
126 + (suffix.isEmpty() ? m_fileInfo.fileName() : suffix); // handle "Makefile" ;)
127}
128
129QT_END_NAMESPACE
130

source code of qtbase/src/platformsupport/themes/qabstractfileiconengine.cpp