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, &result)) {
80 result = filePixmap(size, mode, state);
81 if (!result.isNull())
82 QPixmapCache::insert(key, result);
83 }
84
85 return result;
86}
87
88QPixmap QAbstractFileIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
89{
90 Q_UNUSED(scale); // (size is pre-multiplied by scale)
91 return pixmap(size, mode, state);
92}
93
94QSize QAbstractFileIconEngine::actualSize(const QSize &size, QIcon::Mode mode,
95 QIcon::State state)
96{
97 const QList<QSize> &sizes = availableSizes(mode, state);
98 const int numberSizes = sizes.length();
99 if (numberSizes == 0)
100 return QSize();
101
102 // Find the smallest available size whose area is still larger than the input
103 // size. Otherwise, use the largest area available size. (We don't assume the
104 // platform theme sizes are sorted, hence the extra logic.)
105 const int sizeArea = size.width() * size.height();
106 QSize actualSize = sizes.first();
107 int actualArea = actualSize.width() * actualSize.height();
108 for (int i = 1; i < numberSizes; ++i) {
109 const QSize &s = sizes.at(i);
110 const int a = s.width() * s.height();
111 if ((sizeArea <= a && a < actualArea) || (actualArea < sizeArea && actualArea < a)) {
112 actualSize = s;
113 actualArea = a;
114 }
115 }
116
117 if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height()))
118 actualSize.scale(size, Qt::KeepAspectRatio);
119
120 return actualSize;
121}
122
123/* Reimplement to return a cache key for the entry. An empty result indicates
124 * the icon should not be cached (for example, directory icons having custom icons). */
125QString QAbstractFileIconEngine::cacheKey() const
126{
127 if (!m_fileInfo.isFile() || m_fileInfo.isSymLink() || m_fileInfo.isExecutable())
128 return QString();
129
130 const QString &suffix = m_fileInfo.suffix();
131 return QLatin1String("qt_.")
132 + (suffix.isEmpty() ? m_fileInfo.fileName() : suffix); // handle "Makefile" ;)
133}
134
135QT_END_NAMESPACE
136