Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qabstractfileiconprovider.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 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
5
6#include <qguiapplication.h>
7#include <private/qguiapplication_p.h>
8#include <qpa/qplatformtheme.h>
9#include <qicon.h>
10#if QT_CONFIG(mimetype)
11#include <qmimedatabase.h>
12#endif
13
14
15#include <private/qabstractfileiconprovider_p.h>
16#include <private/qfilesystementry_p.h>
17
19
20using namespace Qt::StringLiterals;
21
25
27
28using IconTypeCache = QHash<QAbstractFileIconProvider::IconType, QIcon>;
29Q_GLOBAL_STATIC(IconTypeCache, iconTypeCache)
30
32{
33 iconTypeCache()->clear();
34}
35
37{
39 if (theme == nullptr)
40 return {};
41
42 auto &cache = *iconTypeCache();
43 auto it = cache.find(type);
44 if (it == cache.end()) {
45 const auto sp = [&]() -> QPlatformTheme::StandardPixmap {
46 switch (type) {
60 break;
61 // no default on purpose; we want warnings when the type enum is extended
62 }
64 }();
65
66 const auto sizesHint = theme->themeHint(QPlatformTheme::IconPixmapSizes);
67 auto sizes = sizesHint.value<QList<QSize>>();
68 if (sizes.isEmpty())
69 sizes.append({64, 64});
70
71 QIcon icon;
72 for (const auto &size : sizes)
73 icon.addPixmap(theme->standardPixmap(sp, size));
74 it = cache.insert(type, icon);
75 }
76 return it.value();
77}
78
80{
81 switch (type) {
83 return QIcon::fromTheme("computer"_L1);
85 return QIcon::fromTheme("user-desktop"_L1);
87 return QIcon::fromTheme("user-trash"_L1);
89 return QIcon::fromTheme("network-workgroup"_L1);
91 return QIcon::fromTheme("drive-harddisk"_L1);
93 return QIcon::fromTheme("folder"_L1);
95 return QIcon::fromTheme("text-x-generic"_L1);
96 // no default on purpose; we want warnings when the type enum is extended
97 }
98 return QIcon::fromTheme("text-x-generic"_L1);
99}
100
101static inline QPlatformTheme::IconOptions toThemeIconOptions(QAbstractFileIconProvider::Options options)
102{
103 QPlatformTheme::IconOptions result;
106 return result;
107}
108
110{
111 if (auto theme = QGuiApplicationPrivate::platformTheme())
112 return theme->fileIcon(info, toThemeIconOptions(options));
113 return {};
114}
115
117{
118 if (info.isRoot())
120 if (info.isDir())
122#if QT_CONFIG(mimetype)
123 return QIcon::fromTheme(mimeDatabase.mimeTypeForFile(info).iconName());
124#else
125 return QIcon::fromTheme("text-x-generic"_L1);
126#endif
127}
128
166
173
179
180
186void QAbstractFileIconProvider::setOptions(QAbstractFileIconProvider::Options options)
187{
189 d->options = options;
190}
191
198QAbstractFileIconProvider::Options QAbstractFileIconProvider::options() const
199{
200 Q_D(const QAbstractFileIconProvider);
201 return d->options;
202}
203
212{
213 Q_D(const QAbstractFileIconProvider);
214 const QIcon result = d->getIconThemeIcon(type);
215 return result.isNull() ? d->getPlatformThemeIcon(type) : result;
216}
217
226{
227 Q_D(const QAbstractFileIconProvider);
228 const QIcon result = d->getIconThemeIcon(info);
229 return result.isNull() ? d->getPlatformThemeIcon(info) : result;
230}
231
232
234{
235 if (QFileSystemEntry::isRootPath(info.absoluteFilePath()))
236 return QGuiApplication::translate("QAbstractFileIconProvider", "Drive");
237 if (info.isFile()) {
238#if QT_CONFIG(mimetype)
240 return mimeType.comment().isEmpty() ? mimeType.name() : mimeType.comment();
241#else
242 return QGuiApplication::translate("QAbstractFileIconProvider", "File");
243#endif
244 }
245
246 if (info.isDir())
247#ifdef Q_OS_WIN
248 return QGuiApplication::translate("QAbstractFileIconProvider", "File Folder", "Match Windows Explorer");
249#else
250 return QGuiApplication::translate("QAbstractFileIconProvider", "Folder", "All other platforms");
251#endif
252 // Windows - "File Folder"
253 // macOS - "Folder"
254 // Konqueror - "Folder"
255 // Nautilus - "folder"
256
257 if (info.isSymLink())
258#ifdef Q_OS_MACOS
259 return QGuiApplication::translate("QAbstractFileIconProvider", "Alias", "macOS Finder");
260#else
261 return QGuiApplication::translate("QAbstractFileIconProvider", "Shortcut", "All other platforms");
262#endif
263 // macOS - "Alias"
264 // Windows - "Shortcut"
265 // Konqueror - "Folder" or "TXT File" i.e. what it is pointing to
266 // Nautilus - "link to folder" or "link to object file", same as Konqueror
267
268 return QGuiApplication::translate("QAbstractFileIconProvider", "Unknown");
269}
270
279
QIcon getPlatformThemeIcon(QAbstractFileIconProvider::IconType type) const
QAbstractFileIconProviderPrivate(QAbstractFileIconProvider *q)
QIcon getIconThemeIcon(QAbstractFileIconProvider::IconType type) const
static QString getFileType(const QFileInfo &info)
QAbstractFileIconProvider::Options options
virtual QIcon icon(IconType) const
Returns an icon set for the given type, using the current icon theme.
virtual ~QAbstractFileIconProvider()
Destroys the file icon provider.
QAbstractFileIconProvider()
Constructs a file icon provider.
virtual void setOptions(Options)
Sets options that affect the icon provider.
virtual Options options() const
Returns all the options that affect the icon provider.
virtual QString type(const QFileInfo &) const
Returns the type of the file described by info.
IconType
\value Computer The icon used for the computing device as a whole \value Desktop The icon for the spe...
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
static Q_CORE_EXPORT bool isRootPath(const QString &path)
static QPlatformTheme * platformTheme()
The QIcon class provides scalable icons in different modes and states.
Definition qicon.h:20
void addPixmap(const QPixmap &pixmap, Mode mode=Normal, State state=Off)
Adds pixmap to the icon, as a specialization for mode and state.
Definition qicon.cpp:1060
bool isNull() const
Returns true if the icon is empty; otherwise returns false.
Definition qicon.cpp:1019
static QIcon fromTheme(const QString &name)
Definition qicon.cpp:1344
\inmodule QtCore
QMimeType mimeTypeForFile(const QString &fileName, MatchMode mode=MatchDefault) const
Returns a MIME type for the file named fileName using mode.
\inmodule QtCore
Definition qmimetype.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
#define this
Definition dialogs.cpp:9
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QHash< QAbstractFileIconProvider::IconType, QIcon > IconTypeCache
static QPlatformTheme::IconOptions toThemeIconOptions(QAbstractFileIconProvider::Options options)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
const char * mimeType
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum type
GLuint GLsizei const GLuint const GLintptr const GLsizeiptr * sizes
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
#define sp
QHostInfo info
[0]