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
4#include "qfileiconprovider.h"
5#include "qfileiconprovider_p.h"
6
7#include <qapplication.h>
8#include <qdir.h>
9#include <qpixmapcache.h>
10#include <private/qfunctions_p.h>
11#include <private/qguiapplication_p.h>
12#include <private/qicon_p.h>
13#include <private/qfilesystementry_p.h>
14#include <qpa/qplatformintegration.h>
15#include <qpa/qplatformservices.h>
16#include <qpa/qplatformtheme.h>
17
18#if defined(Q_OS_WIN)
19# include <qt_windows.h>
20# include <commctrl.h>
21# include <objbase.h>
22#endif
23
24QT_BEGIN_NAMESPACE
25
26/*!
27 \class QFileIconProvider
28
29 \inmodule QtWidgets
30
31 \brief The QFileIconProvider class provides file icons for the QFileSystemModel class.
32*/
33
34QFileIconProviderPrivate::QFileIconProviderPrivate(QFileIconProvider *q)
35 : QAbstractFileIconProviderPrivate(q), homePath(QDir::home().absolutePath())
36{
37}
38
39QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const
40{
41 switch (name) {
42 case QStyle::SP_FileIcon:
43 if (file.isNull())
44 file = QApplication::style()->standardIcon(standardIcon: name);
45 return file;
46 case QStyle::SP_FileLinkIcon:
47 if (fileLink.isNull())
48 fileLink = QApplication::style()->standardIcon(standardIcon: name);
49 return fileLink;
50 case QStyle::SP_DirIcon:
51 if (directory.isNull())
52 directory = QApplication::style()->standardIcon(standardIcon: name);
53 return directory;
54 case QStyle::SP_DirLinkIcon:
55 if (directoryLink.isNull())
56 directoryLink = QApplication::style()->standardIcon(standardIcon: name);
57 return directoryLink;
58 case QStyle::SP_DriveHDIcon:
59 if (harddisk.isNull())
60 harddisk = QApplication::style()->standardIcon(standardIcon: name);
61 return harddisk;
62 case QStyle::SP_DriveFDIcon:
63 if (floppy.isNull())
64 floppy = QApplication::style()->standardIcon(standardIcon: name);
65 return floppy;
66 case QStyle::SP_DriveCDIcon:
67 if (cdrom.isNull())
68 cdrom = QApplication::style()->standardIcon(standardIcon: name);
69 return cdrom;
70 case QStyle::SP_DriveNetIcon:
71 if (network.isNull())
72 network = QApplication::style()->standardIcon(standardIcon: name);
73 return network;
74 case QStyle::SP_ComputerIcon:
75 if (computer.isNull())
76 computer = QApplication::style()->standardIcon(standardIcon: name);
77 return computer;
78 case QStyle::SP_DesktopIcon:
79 if (desktop.isNull())
80 desktop = QApplication::style()->standardIcon(standardIcon: name);
81 return desktop;
82 case QStyle::SP_TrashIcon:
83 if (trashcan.isNull())
84 trashcan = QApplication::style()->standardIcon(standardIcon: name);
85 return trashcan;
86 case QStyle::SP_DirHomeIcon:
87 if (home.isNull())
88 home = QApplication::style()->standardIcon(standardIcon: name);
89 return home;
90 default:
91 return QIcon();
92 }
93 return QIcon();
94}
95
96/*!
97 Constructs a file icon provider.
98*/
99
100QFileIconProvider::QFileIconProvider()
101 : QAbstractFileIconProvider(*new QFileIconProviderPrivate(this))
102{
103}
104
105/*!
106 Destroys the file icon provider.
107*/
108
109QFileIconProvider::~QFileIconProvider() = default;
110
111/*!
112 \reimp
113*/
114
115QIcon QFileIconProvider::icon(IconType type) const
116{
117 Q_D(const QFileIconProvider);
118 switch (type) {
119 case Computer:
120 return d->getIcon(name: QStyle::SP_ComputerIcon);
121 case Desktop:
122 return d->getIcon(name: QStyle::SP_DesktopIcon);
123 case Trashcan:
124 return d->getIcon(name: QStyle::SP_TrashIcon);
125 case Network:
126 return d->getIcon(name: QStyle::SP_DriveNetIcon);
127 case Drive:
128 return d->getIcon(name: QStyle::SP_DriveHDIcon);
129 case Folder:
130 return d->getIcon(name: QStyle::SP_DirIcon);
131 case File:
132 return d->getIcon(name: QStyle::SP_FileIcon);
133 default:
134 break;
135 };
136 return QIcon();
137}
138
139QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const
140{
141 return getPlatformThemeIcon(info: fi);
142}
143
144/*!
145 \reimp
146*/
147
148QIcon QFileIconProvider::icon(const QFileInfo &info) const
149{
150 Q_D(const QFileIconProvider);
151
152 QIcon retIcon = d->getIcon(fi: info);
153 if (!retIcon.isNull())
154 return retIcon;
155
156 const QString &path = info.absoluteFilePath();
157 if (path.isEmpty() || QFileSystemEntry::isRootPath(path))
158#if defined (Q_OS_WIN)
159 {
160 UINT type = GetDriveType(reinterpret_cast<const wchar_t *>(path.utf16()));
161
162 switch (type) {
163 case DRIVE_REMOVABLE:
164 return d->getIcon(QStyle::SP_DriveFDIcon);
165 case DRIVE_FIXED:
166 return d->getIcon(QStyle::SP_DriveHDIcon);
167 case DRIVE_REMOTE:
168 return d->getIcon(QStyle::SP_DriveNetIcon);
169 case DRIVE_CDROM:
170 return d->getIcon(QStyle::SP_DriveCDIcon);
171 case DRIVE_RAMDISK:
172 case DRIVE_UNKNOWN:
173 case DRIVE_NO_ROOT_DIR:
174 default:
175 return d->getIcon(QStyle::SP_DriveHDIcon);
176 }
177 }
178#else
179 return d->getIcon(name: QStyle::SP_DriveHDIcon);
180#endif
181
182 if (info.isFile()) {
183 if (info.isSymLink())
184 return d->getIcon(name: QStyle::SP_FileLinkIcon);
185 else
186 return d->getIcon(name: QStyle::SP_FileIcon);
187 }
188 if (info.isDir()) {
189 if (info.isSymLink()) {
190 return d->getIcon(name: QStyle::SP_DirLinkIcon);
191 } else {
192 if (info.absoluteFilePath() == d->homePath) {
193 return d->getIcon(name: QStyle::SP_DirHomeIcon);
194 } else {
195 return d->getIcon(name: QStyle::SP_DirIcon);
196 }
197 }
198 }
199 return QIcon();
200}
201
202QT_END_NAMESPACE
203

source code of qtbase/src/widgets/itemviews/qfileiconprovider.cpp