1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qfileiconprovider.h"
44
#ifndef QT_NO_FILEICONPROVIDER
46
#include <qapplication.h>
48
#include <qpixmapcache.h>
49
#include <private/qfunctions_p.h>
50
#include <private/qguiapplication_p.h>
51
#include <qpa/qplatformintegration.h>
52
#include <qpa/qplatformservices.h>
53
#include <qpa/qplatformtheme.h>
56
# include <qt_windows.h>
57
# include <commctrl.h>
61
#if defined(Q_OS_UNIX) && !defined(QT_NO_STYLE_GTK)
62
# include <private/qgtkstyle_p_p.h>
68
\class QFileIconProvider
72
\brief The QFileIconProvider class provides file icons for the QDirModel and the QFileSystemModel classes.
76
\enum QFileIconProvider::IconType
86
class QFileIconProviderPrivate
88
Q_DECLARE_PUBLIC(QFileIconProvider)
91
QFileIconProviderPrivate();
92
QIcon getIcon(QStyle::StandardPixmap name) const;
93
QIcon getIcon(const QFileInfo &fi) const;
95
QFileIconProvider *q_ptr;
96
const QString homePath;
100
mutable QIcon fileLink;
101
mutable QIcon directory;
102
mutable QIcon directoryLink;
103
mutable QIcon harddisk;
104
mutable QIcon floppy;
107
mutable QIcon network;
108
mutable QIcon computer;
109
mutable QIcon desktop;
110
mutable QIcon trashcan;
111
mutable QIcon generic;
115
QFileIconProviderPrivate::QFileIconProviderPrivate() :
116
homePath(QDir::home().absolutePath())
120
QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const
123
case QStyle::SP_FileIcon:
125
file = QApplication::style()->standardIcon(name);
127
case QStyle::SP_FileLinkIcon:
128
if (fileLink.isNull())
129
fileLink = QApplication::style()->standardIcon(name);
131
case QStyle::SP_DirIcon:
132
if (directory.isNull())
133
directory = QApplication::style()->standardIcon(name);
135
case QStyle::SP_DirLinkIcon:
136
if (directoryLink.isNull())
137
directoryLink = QApplication::style()->standardIcon(name);
138
return directoryLink;
139
case QStyle::SP_DriveHDIcon:
140
if (harddisk.isNull())
141
harddisk = QApplication::style()->standardIcon(name);
143
case QStyle::SP_DriveFDIcon:
145
floppy = QApplication::style()->standardIcon(name);
147
case QStyle::SP_DriveCDIcon:
149
cdrom = QApplication::style()->standardIcon(name);
151
case QStyle::SP_DriveNetIcon:
152
if (network.isNull())
153
network = QApplication::style()->standardIcon(name);
155
case QStyle::SP_ComputerIcon:
156
if (computer.isNull())
157
computer = QApplication::style()->standardIcon(name);
159
case QStyle::SP_DesktopIcon:
160
if (desktop.isNull())
161
desktop = QApplication::style()->standardIcon(name);
163
case QStyle::SP_TrashIcon:
164
if (trashcan.isNull())
165
trashcan = QApplication::style()->standardIcon(name);
167
case QStyle::SP_DirHomeIcon:
169
home = QApplication::style()->standardIcon(name);
178
Constructs a file icon provider.
181
QFileIconProvider::QFileIconProvider()
182
: d_ptr(new QFileIconProviderPrivate)
187
Destroys the file icon provider.
191
QFileIconProvider::~QFileIconProvider()
196
Returns an icon set for the given \a type.
199
QIcon QFileIconProvider::icon(IconType type) const
201
Q_D(const QFileIconProvider);
204
return d->getIcon(QStyle::SP_ComputerIcon);
206
return d->getIcon(QStyle::SP_DesktopIcon);
208
return d->getIcon(QStyle::SP_TrashIcon);
210
return d->getIcon(QStyle::SP_DriveNetIcon);
212
return d->getIcon(QStyle::SP_DriveHDIcon);
214
return d->getIcon(QStyle::SP_DirIcon);
216
return d->getIcon(QStyle::SP_FileIcon);
223
QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const
226
const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
230
QList<int> sizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >();
234
const QString fileExtension = fi.suffix().toUpper();
235
const QString keyBase = QLatin1String("qt_.") + fi.suffix().toUpper();
237
bool cacheable = fi.isFile() && !fi.isExecutable() && !fi.isSymLink() && fileExtension != QLatin1String("ICO");
240
QPixmapCache::find(keyBase + QString::number(sizes.at(0)), pixmap);
241
if (!pixmap.isNull()) {
242
bool iconIsComplete = true;
243
retIcon.addPixmap(pixmap);
244
for (int i = 1; i < sizes.count(); i++)
245
if (QPixmapCache::find(keyBase + QString::number(sizes.at(i)), pixmap)) {
246
retIcon.addPixmap(pixmap);
248
iconIsComplete = false;
256
Q_FOREACH (int size, sizes) {
257
QPixmap pixmap = theme->fileIconPixmap(fi, QSizeF(size, size));
258
if (!pixmap.isNull()) {
259
retIcon.addPixmap(pixmap);
261
QPixmapCache::insert(keyBase + QString::number(size), pixmap);
270
Returns an icon for the file described by \a info.
273
QIcon QFileIconProvider::icon(const QFileInfo &info) const
275
Q_D(const QFileIconProvider);
277
#if defined(Q_OS_UNIX) && !defined(QT_NO_STYLE_GTK)
278
const QByteArray desktopEnvironment = QGuiApplicationPrivate::platformIntegration()->services()->desktopEnvironment();
279
if (desktopEnvironment != QByteArrayLiteral("KDE")) {
280
QIcon gtkIcon = QGtkStylePrivate::getFilesystemIcon(info);
281
if (!gtkIcon.isNull())
286
QIcon retIcon = d->getIcon(info);
287
if (!retIcon.isNull())
291
#if defined (Q_OS_WIN) && !defined(Q_OS_WINCE)
293
UINT type = GetDriveType((wchar_t *)info.absoluteFilePath().utf16());
296
case DRIVE_REMOVABLE:
297
return d->getIcon(QStyle::SP_DriveFDIcon);
299
return d->getIcon(QStyle::SP_DriveHDIcon);
301
return d->getIcon(QStyle::SP_DriveNetIcon);
303
return d->getIcon(QStyle::SP_DriveCDIcon);
306
case DRIVE_NO_ROOT_DIR:
308
return d->getIcon(QStyle::SP_DriveHDIcon);
312
return d->getIcon(QStyle::SP_DriveHDIcon);
316
if (info.isSymLink())
317
return d->getIcon(QStyle::SP_FileLinkIcon);
319
return d->getIcon(QStyle::SP_FileIcon);
322
if (info.isSymLink()) {
323
return d->getIcon(QStyle::SP_DirLinkIcon);
325
if (info.absoluteFilePath() == d->homePath) {
326
return d->getIcon(QStyle::SP_DirHomeIcon);
328
return d->getIcon(QStyle::SP_DirIcon);
336
Returns the type of the file described by \a info.
339
QString QFileIconProvider::type(const QFileInfo &info) const
342
return QApplication::translate("QFileDialog", "Drive");
344
if (!info.suffix().isEmpty())
345
return info.suffix() + QLatin1Char(' ') + QApplication::translate("QFileDialog", "File");
346
return QApplication::translate("QFileDialog", "File");
351
return QApplication::translate("QFileDialog", "File Folder", "Match Windows Explorer");
353
return QApplication::translate("QFileDialog", "Folder", "All other platforms");
355
// Windows - "File Folder"
357
// Konqueror - "Folder"
358
// Nautilus - "folder"
360
if (info.isSymLink())
362
return QApplication::translate("QFileDialog", "Alias", "Mac OS X Finder");
364
return QApplication::translate("QFileDialog", "Shortcut", "All other platforms");
367
// Windows - "Shortcut"
368
// Konqueror - "Folder" or "TXT File" i.e. what it is pointing to
369
// Nautilus - "link to folder" or "link to object file", same as Konqueror
371
return QApplication::translate("QFileDialog", "Unknown");