Qt 6.x
The Qt SDK
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
23 : q_ptr(q)
24{}
25
27
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
164{
165}
166
171 : d_ptr(&dd)
172{}
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
237{
238 Q_D(const QAbstractFileIconProvider);
240 return QGuiApplication::translate("QAbstractFileIconProvider", "Drive");
241 if (info.isFile()) {
242#if QT_CONFIG(mimetype)
243 const QMimeType mimeType = d->mimeDatabase.mimeTypeForFile(info);
244 return mimeType.comment().isEmpty() ? mimeType.name() : mimeType.comment();
245#else
246 Q_UNUSED(d);
247 return QGuiApplication::translate("QAbstractFileIconProvider", "File");
248#endif
249 }
250
251 if (info.isDir())
252#ifdef Q_OS_WIN
253 return QGuiApplication::translate("QAbstractFileIconProvider", "File Folder", "Match Windows Explorer");
254#else
255 return QGuiApplication::translate("QAbstractFileIconProvider", "Folder", "All other platforms");
256#endif
257 // Windows - "File Folder"
258 // macOS - "Folder"
259 // Konqueror - "Folder"
260 // Nautilus - "folder"
261
262 if (info.isSymLink())
263#ifdef Q_OS_MACOS
264 return QGuiApplication::translate("QAbstractFileIconProvider", "Alias", "macOS Finder");
265#else
266 return QGuiApplication::translate("QAbstractFileIconProvider", "Shortcut", "All other platforms");
267#endif
268 // macOS - "Alias"
269 // Windows - "Shortcut"
270 // Konqueror - "Folder" or "TXT File" i.e. what it is pointing to
271 // Nautilus - "link to folder" or "link to object file", same as Konqueror
272
273 return QGuiApplication::translate("QAbstractFileIconProvider", "Unknown");
274}
275
QIcon getPlatformThemeIcon(QAbstractFileIconProvider::IconType type) const
QAbstractFileIconProviderPrivate(QAbstractFileIconProvider *q)
QIcon getIconThemeIcon(QAbstractFileIconProvider::IconType type) const
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
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
bool isSymLink() const
Returns true if this object points to a symbolic link, shortcut, or alias; otherwise returns false.
bool isRoot() const
Returns true if the object points to a directory or to a symbolic link to a directory,...
QString absoluteFilePath() const
Returns an absolute path including the file name.
bool isFile() const
Returns true if this object points to a file or to a symbolic link to a file.
bool isDir() const
Returns true if this object points to a directory or to a symbolic link to a directory.
static Q_CORE_EXPORT bool isRootPath(const QString &path)
static QPlatformTheme * platformTheme()
\inmodule QtCore
Definition qhash.h:818
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:1014
static QIcon fromTheme(const QString &name)
Definition qicon.cpp:1296
Definition qlist.h:74
void append(parameter_type t)
Definition qlist.h:441
\inmodule QtCore
Definition qmimetype.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
#define this
Definition dialogs.cpp:9
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
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
#define Q_UNUSED(x)
QFileInfo info(fileName)
[8]