itemviews/qfileiconprovider.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtGui module 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 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. -
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 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. -
24** -
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. -
28** -
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. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qfileiconprovider.h" -
43 -
44#ifndef QT_NO_FILEICONPROVIDER -
45#include <qstyle.h> -
46#include <qapplication.h> -
47#include <qdir.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> -
54 -
55#if defined(Q_OS_WIN) -
56# include <qt_windows.h> -
57# include <commctrl.h> -
58# include <objbase.h> -
59#endif -
60 -
61#if defined(Q_OS_UNIX) && !defined(QT_NO_STYLE_GTK) -
62# include <private/qgtkstyle_p_p.h> -
63#endif -
64 -
65QT_BEGIN_NAMESPACE -
66 -
67/*! -
68 \class QFileIconProvider -
69 -
70 \inmodule QtWidgets -
71 -
72 \brief The QFileIconProvider class provides file icons for the QDirModel and the QFileSystemModel classes. -
73*/ -
74 -
75/*! -
76 \enum QFileIconProvider::IconType -
77 \value Computer -
78 \value Desktop -
79 \value Trashcan -
80 \value Network -
81 \value Drive -
82 \value Folder -
83 \value File -
84*/ -
85 -
86class QFileIconProviderPrivate -
87{ -
88 Q_DECLARE_PUBLIC(QFileIconProvider) -
89 -
90public: -
91 QFileIconProviderPrivate(); -
92 QIcon getIcon(QStyle::StandardPixmap name) const; -
93 QIcon getIcon(const QFileInfo &fi) const; -
94 -
95 QFileIconProvider *q_ptr; -
96 const QString homePath; -
97 -
98private: -
99 mutable QIcon file; -
100 mutable QIcon fileLink; -
101 mutable QIcon directory; -
102 mutable QIcon directoryLink; -
103 mutable QIcon harddisk; -
104 mutable QIcon floppy; -
105 mutable QIcon cdrom; -
106 mutable QIcon ram; -
107 mutable QIcon network; -
108 mutable QIcon computer; -
109 mutable QIcon desktop; -
110 mutable QIcon trashcan; -
111 mutable QIcon generic; -
112 mutable QIcon home; -
113}; -
114 -
115QFileIconProviderPrivate::QFileIconProviderPrivate() : -
116 homePath(QDir::home().absolutePath()) -
117{ -
118}
executed: }
Execution Count:376
376
119 -
120QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const -
121{ -
122 switch (name) { -
123 case QStyle::SP_FileIcon: -
124 if (file.isNull())
evaluated: file.isNull()
TRUEFALSE
yes
Evaluation Count:89
yes
Evaluation Count:24339
89-24339
125 file = QApplication::style()->standardIcon(name);
executed: file = QApplication::style()->standardIcon(name);
Execution Count:89
89
126 return file;
executed: return file;
Execution Count:24428
24428
127 case QStyle::SP_FileLinkIcon: -
128 if (fileLink.isNull())
evaluated: fileLink.isNull()
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:55
13-55
129 fileLink = QApplication::style()->standardIcon(name);
executed: fileLink = QApplication::style()->standardIcon(name);
Execution Count:13
13
130 return fileLink;
executed: return fileLink;
Execution Count:68
68
131 case QStyle::SP_DirIcon: -
132 if (directory.isNull())
evaluated: directory.isNull()
TRUEFALSE
yes
Evaluation Count:251
yes
Evaluation Count:3054
251-3054
133 directory = QApplication::style()->standardIcon(name);
executed: directory = QApplication::style()->standardIcon(name);
Execution Count:251
251
134 return directory;
executed: return directory;
Execution Count:3305
3305
135 case QStyle::SP_DirLinkIcon: -
136 if (directoryLink.isNull())
partially evaluated: directoryLink.isNull()
TRUEFALSE
yes
Evaluation Count:12
no
Evaluation Count:0
0-12
137 directoryLink = QApplication::style()->standardIcon(name);
executed: directoryLink = QApplication::style()->standardIcon(name);
Execution Count:12
12
138 return directoryLink;
executed: return directoryLink;
Execution Count:12
12
139 case QStyle::SP_DriveHDIcon: -
140 if (harddisk.isNull())
evaluated: harddisk.isNull()
TRUEFALSE
yes
Evaluation Count:29
yes
Evaluation Count:2
2-29
141 harddisk = QApplication::style()->standardIcon(name);
executed: harddisk = QApplication::style()->standardIcon(name);
Execution Count:29
29
142 return harddisk;
executed: return harddisk;
Execution Count:31
31
143 case QStyle::SP_DriveFDIcon: -
144 if (floppy.isNull())
never evaluated: floppy.isNull()
0
145 floppy = QApplication::style()->standardIcon(name);
never executed: floppy = QApplication::style()->standardIcon(name);
0
146 return floppy;
never executed: return floppy;
0
147 case QStyle::SP_DriveCDIcon: -
148 if (cdrom.isNull())
never evaluated: cdrom.isNull()
0
149 cdrom = QApplication::style()->standardIcon(name);
never executed: cdrom = QApplication::style()->standardIcon(name);
0
150 return cdrom;
never executed: return cdrom;
0
151 case QStyle::SP_DriveNetIcon: -
152 if (network.isNull())
partially evaluated: network.isNull()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
153 network = QApplication::style()->standardIcon(name);
executed: network = QApplication::style()->standardIcon(name);
Execution Count:1
1
154 return network;
executed: return network;
Execution Count:1
1
155 case QStyle::SP_ComputerIcon: -
156 if (computer.isNull())
evaluated: computer.isNull()
TRUEFALSE
yes
Evaluation Count:191
yes
Evaluation Count:108
108-191
157 computer = QApplication::style()->standardIcon(name);
executed: computer = QApplication::style()->standardIcon(name);
Execution Count:191
191
158 return computer;
executed: return computer;
Execution Count:299
299
159 case QStyle::SP_DesktopIcon: -
160 if (desktop.isNull())
partially evaluated: desktop.isNull()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
161 desktop = QApplication::style()->standardIcon(name);
executed: desktop = QApplication::style()->standardIcon(name);
Execution Count:1
1
162 return desktop;
executed: return desktop;
Execution Count:1
1
163 case QStyle::SP_TrashIcon: -
164 if (trashcan.isNull())
partially evaluated: trashcan.isNull()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
165 trashcan = QApplication::style()->standardIcon(name);
executed: trashcan = QApplication::style()->standardIcon(name);
Execution Count:1
1
166 return trashcan;
executed: return trashcan;
Execution Count:1
1
167 case QStyle::SP_DirHomeIcon: -
168 if (home.isNull())
evaluated: home.isNull()
TRUEFALSE
yes
Evaluation Count:197
yes
Evaluation Count:399
197-399
169 home = QApplication::style()->standardIcon(name);
executed: home = QApplication::style()->standardIcon(name);
Execution Count:197
197
170 return home;
executed: return home;
Execution Count:596
596
171 default: -
172 return QIcon();
never executed: return QIcon();
0
173 } -
174 return QIcon();
never executed: return QIcon();
0
175} -
176 -
177/*! -
178 Constructs a file icon provider. -
179*/ -
180 -
181QFileIconProvider::QFileIconProvider() -
182 : d_ptr(new QFileIconProviderPrivate) -
183{ -
184}
executed: }
Execution Count:376
376
185 -
186/*! -
187 Destroys the file icon provider. -
188 -
189*/ -
190 -
191QFileIconProvider::~QFileIconProvider() -
192{ -
193} -
194 -
195/*! -
196 Returns an icon set for the given \a type. -
197*/ -
198 -
199QIcon QFileIconProvider::icon(IconType type) const -
200{ -
201 Q_D(const QFileIconProvider);
executed (the execution status of this line is deduced): const QFileIconProviderPrivate * const d = d_func();
-
202 switch (type) { -
203 case Computer: -
204 return d->getIcon(QStyle::SP_ComputerIcon);
executed: return d->getIcon(QStyle::SP_ComputerIcon);
Execution Count:299
299
205 case Desktop: -
206 return d->getIcon(QStyle::SP_DesktopIcon);
executed: return d->getIcon(QStyle::SP_DesktopIcon);
Execution Count:1
1
207 case Trashcan: -
208 return d->getIcon(QStyle::SP_TrashIcon);
executed: return d->getIcon(QStyle::SP_TrashIcon);
Execution Count:1
1
209 case Network: -
210 return d->getIcon(QStyle::SP_DriveNetIcon);
executed: return d->getIcon(QStyle::SP_DriveNetIcon);
Execution Count:1
1
211 case Drive: -
212 return d->getIcon(QStyle::SP_DriveHDIcon);
executed: return d->getIcon(QStyle::SP_DriveHDIcon);
Execution Count:1
1
213 case Folder: -
214 return d->getIcon(QStyle::SP_DirIcon);
executed: return d->getIcon(QStyle::SP_DirIcon);
Execution Count:1
1
215 case File: -
216 return d->getIcon(QStyle::SP_FileIcon);
executed: return d->getIcon(QStyle::SP_FileIcon);
Execution Count:1
1
217 default: -
218 break;
never executed: break;
0
219 }; -
220 return QIcon();
never executed: return QIcon();
0
221} -
222 -
223QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const -
224{ -
225 QIcon retIcon;
executed (the execution status of this line is deduced): QIcon retIcon;
-
226 const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
executed (the execution status of this line is deduced): const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
-
227 if (!theme)
partially evaluated: !theme
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:28437
0-28437
228 return retIcon;
never executed: return retIcon;
0
229 -
230 QList<int> sizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >();
executed (the execution status of this line is deduced): QList<int> sizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >();
-
231 if (sizes.isEmpty())
partially evaluated: sizes.isEmpty()
TRUEFALSE
yes
Evaluation Count:28437
no
Evaluation Count:0
0-28437
232 return retIcon;
executed: return retIcon;
Execution Count:28437
28437
233 -
234 const QString fileExtension = fi.suffix().toUpper();
never executed (the execution status of this line is deduced): const QString fileExtension = fi.suffix().toUpper();
-
235 const QString keyBase = QLatin1String("qt_.") + fi.suffix().toUpper();
never executed (the execution status of this line is deduced): const QString keyBase = QLatin1String("qt_.") + fi.suffix().toUpper();
-
236 -
237 bool cacheable = fi.isFile() && !fi.isExecutable() && !fi.isSymLink() && fileExtension != QLatin1String("ICO");
never evaluated: fi.isFile()
never evaluated: !fi.isExecutable()
never evaluated: !fi.isSymLink()
never evaluated: fileExtension != QLatin1String("ICO")
0
238 if (cacheable) {
never evaluated: cacheable
0
239 QPixmap pixmap;
never executed (the execution status of this line is deduced): QPixmap pixmap;
-
240 QPixmapCache::find(keyBase + QString::number(sizes.at(0)), pixmap);
never executed (the execution status of this line is deduced): QPixmapCache::find(keyBase + QString::number(sizes.at(0)), pixmap);
-
241 if (!pixmap.isNull()) {
never evaluated: !pixmap.isNull()
0
242 bool iconIsComplete = true;
never executed (the execution status of this line is deduced): bool iconIsComplete = true;
-
243 retIcon.addPixmap(pixmap);
never executed (the execution status of this line is deduced): retIcon.addPixmap(pixmap);
-
244 for (int i = 1; i < sizes.count(); i++)
never evaluated: i < sizes.count()
0
245 if (QPixmapCache::find(keyBase + QString::number(sizes.at(i)), pixmap)) {
never evaluated: QPixmapCache::find(keyBase + QString::number(sizes.at(i)), pixmap)
0
246 retIcon.addPixmap(pixmap);
never executed (the execution status of this line is deduced): retIcon.addPixmap(pixmap);
-
247 } else {
never executed: }
0
248 iconIsComplete = false;
never executed (the execution status of this line is deduced): iconIsComplete = false;
-
249 break;
never executed: break;
0
250 } -
251 if (iconIsComplete)
never evaluated: iconIsComplete
0
252 return retIcon;
never executed: return retIcon;
0
253 }
never executed: }
0
254 }
never executed: }
0
255 -
256 Q_FOREACH (int size, sizes) {
never executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(sizes)> _container_(sizes); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (int size = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
257 QPixmap pixmap = theme->fileIconPixmap(fi, QSizeF(size, size));
never executed (the execution status of this line is deduced): QPixmap pixmap = theme->fileIconPixmap(fi, QSizeF(size, size));
-
258 if (!pixmap.isNull()) {
never evaluated: !pixmap.isNull()
0
259 retIcon.addPixmap(pixmap);
never executed (the execution status of this line is deduced): retIcon.addPixmap(pixmap);
-
260 if (cacheable)
never evaluated: cacheable
0
261 QPixmapCache::insert(keyBase + QString::number(size), pixmap);
never executed: QPixmapCache::insert(keyBase + QString::number(size), pixmap);
0
262 }
never executed: }
0
263 }
never executed: }
0
264 -
265 return retIcon;
never executed: return retIcon;
0
266} -
267 -
268 -
269/*! -
270 Returns an icon for the file described by \a info. -
271*/ -
272 -
273QIcon QFileIconProvider::icon(const QFileInfo &info) const -
274{ -
275 Q_D(const QFileIconProvider);
executed (the execution status of this line is deduced): const QFileIconProviderPrivate * const d = d_func();
-
276 -
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()) -
282 return gtkIcon; -
283 } -
284#endif -
285 -
286 QIcon retIcon = d->getIcon(info);
executed (the execution status of this line is deduced): QIcon retIcon = d->getIcon(info);
-
287 if (!retIcon.isNull())
partially evaluated: !retIcon.isNull()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:28437
0-28437
288 return retIcon;
never executed: return retIcon;
0
289 -
290 if (info.isRoot())
evaluated: info.isRoot()
TRUEFALSE
yes
Evaluation Count:30
yes
Evaluation Count:28407
30-28407
291#if defined (Q_OS_WIN) && !defined(Q_OS_WINCE) -
292 { -
293 UINT type = GetDriveType((wchar_t *)info.absoluteFilePath().utf16()); -
294 -
295 switch (type) { -
296 case DRIVE_REMOVABLE: -
297 return d->getIcon(QStyle::SP_DriveFDIcon); -
298 case DRIVE_FIXED: -
299 return d->getIcon(QStyle::SP_DriveHDIcon); -
300 case DRIVE_REMOTE: -
301 return d->getIcon(QStyle::SP_DriveNetIcon); -
302 case DRIVE_CDROM: -
303 return d->getIcon(QStyle::SP_DriveCDIcon); -
304 case DRIVE_RAMDISK: -
305 case DRIVE_UNKNOWN: -
306 case DRIVE_NO_ROOT_DIR: -
307 default: -
308 return d->getIcon(QStyle::SP_DriveHDIcon); -
309 } -
310 } -
311#else -
312 return d->getIcon(QStyle::SP_DriveHDIcon);
executed: return d->getIcon(QStyle::SP_DriveHDIcon);
Execution Count:30
30
313#endif -
314 -
315 if (info.isFile()) {
evaluated: info.isFile()
TRUEFALSE
yes
Evaluation Count:24495
yes
Evaluation Count:3912
3912-24495
316 if (info.isSymLink())
evaluated: info.isSymLink()
TRUEFALSE
yes
Evaluation Count:68
yes
Evaluation Count:24427
68-24427
317 return d->getIcon(QStyle::SP_FileLinkIcon);
executed: return d->getIcon(QStyle::SP_FileLinkIcon);
Execution Count:68
68
318 else -
319 return d->getIcon(QStyle::SP_FileIcon);
executed: return d->getIcon(QStyle::SP_FileIcon);
Execution Count:24427
24427
320 } -
321 if (info.isDir()) {
partially evaluated: info.isDir()
TRUEFALSE
yes
Evaluation Count:3912
no
Evaluation Count:0
0-3912
322 if (info.isSymLink()) {
evaluated: info.isSymLink()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:3900
12-3900
323 return d->getIcon(QStyle::SP_DirLinkIcon);
executed: return d->getIcon(QStyle::SP_DirLinkIcon);
Execution Count:12
12
324 } else { -
325 if (info.absoluteFilePath() == d->homePath) {
evaluated: info.absoluteFilePath() == d->homePath
TRUEFALSE
yes
Evaluation Count:596
yes
Evaluation Count:3304
596-3304
326 return d->getIcon(QStyle::SP_DirHomeIcon);
executed: return d->getIcon(QStyle::SP_DirHomeIcon);
Execution Count:596
596
327 } else { -
328 return d->getIcon(QStyle::SP_DirIcon);
executed: return d->getIcon(QStyle::SP_DirIcon);
Execution Count:3304
3304
329 } -
330 } -
331 } -
332 return QIcon();
never executed: return QIcon();
0
333} -
334 -
335/*! -
336 Returns the type of the file described by \a info. -
337*/ -
338 -
339QString QFileIconProvider::type(const QFileInfo &info) const -
340{ -
341 if (info.isRoot())
evaluated: info.isRoot()
TRUEFALSE
yes
Evaluation Count:29
yes
Evaluation Count:28973
29-28973
342 return QApplication::translate("QFileDialog", "Drive");
executed: return QApplication::translate("QFileDialog", "Drive");
Execution Count:29
29
343 if (info.isFile()) {
evaluated: info.isFile()
TRUEFALSE
yes
Evaluation Count:24904
yes
Evaluation Count:4069
4069-24904
344 if (!info.suffix().isEmpty())
evaluated: !info.suffix().isEmpty()
TRUEFALSE
yes
Evaluation Count:13326
yes
Evaluation Count:11578
11578-13326
345 return info.suffix() + QLatin1Char(' ') + QApplication::translate("QFileDialog", "File");
executed: return info.suffix() + QLatin1Char(' ') + QApplication::translate("QFileDialog", "File");
Execution Count:13326
13326
346 return QApplication::translate("QFileDialog", "File");
executed: return QApplication::translate("QFileDialog", "File");
Execution Count:11578
11578
347 } -
348 -
349 if (info.isDir())
evaluated: info.isDir()
TRUEFALSE
yes
Evaluation Count:4045
yes
Evaluation Count:24
24-4045
350#ifdef Q_OS_WIN -
351 return QApplication::translate("QFileDialog", "File Folder", "Match Windows Explorer"); -
352#else -
353 return QApplication::translate("QFileDialog", "Folder", "All other platforms");
executed: return QApplication::translate("QFileDialog", "Folder", "All other platforms");
Execution Count:4045
4045
354#endif -
355 // Windows - "File Folder" -
356 // OS X - "Folder" -
357 // Konqueror - "Folder" -
358 // Nautilus - "folder" -
359 -
360 if (info.isSymLink())
partially evaluated: info.isSymLink()
TRUEFALSE
yes
Evaluation Count:24
no
Evaluation Count:0
0-24
361#ifdef Q_OS_MAC -
362 return QApplication::translate("QFileDialog", "Alias", "Mac OS X Finder"); -
363#else -
364 return QApplication::translate("QFileDialog", "Shortcut", "All other platforms");
executed: return QApplication::translate("QFileDialog", "Shortcut", "All other platforms");
Execution Count:24
24
365#endif -
366 // OS X - "Alias" -
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 -
370 -
371 return QApplication::translate("QFileDialog", "Unknown");
never executed: return QApplication::translate("QFileDialog", "Unknown");
0
372} -
373 -
374QT_END_NAMESPACE -
375 -
376#endif -
377 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial