Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/itemviews/qfileiconprovider.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | - | |||||||
2 | - | |||||||
3 | - | |||||||
4 | static bool isCacheable(const QFileInfo &fi); | - | ||||||
5 | - | |||||||
6 | class QFileIconEngine : public QPixmapIconEngine | - | ||||||
7 | { | - | ||||||
8 | public: | - | ||||||
9 | QFileIconEngine(const QFileInfo &info, QFileIconProvider::Options opts) | - | ||||||
10 | : QPixmapIconEngine(), m_fileInfo(info), m_fipOpts(opts) | - | ||||||
11 | { } | - | ||||||
12 | - | |||||||
13 | QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override | - | ||||||
14 | { | - | ||||||
15 | (void)mode;; | - | ||||||
16 | (void)state;; | - | ||||||
17 | QPixmap pixmap; | - | ||||||
18 | - | |||||||
19 | if (!size.isValid()) | - | ||||||
20 | return pixmap; | - | ||||||
21 | - | |||||||
22 | const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme(); | - | ||||||
23 | if (!theme) | - | ||||||
24 | return pixmap; | - | ||||||
25 | - | |||||||
26 | const QString &keyBase = QLatin1String("qt_.") + m_fileInfo.suffix().toUpper(); | - | ||||||
27 | - | |||||||
28 | bool cacheable = isCacheable(m_fileInfo); | - | ||||||
29 | if (cacheable) { | - | ||||||
30 | QPixmapCache::find(keyBase + QString::number(size.width()), pixmap); | - | ||||||
31 | if (!pixmap.isNull()) | - | ||||||
32 | return pixmap; | - | ||||||
33 | } | - | ||||||
34 | - | |||||||
35 | QPlatformTheme::IconOptions iconOptions; | - | ||||||
36 | if (m_fipOpts & QFileIconProvider::DontUseCustomDirectoryIcons) | - | ||||||
37 | iconOptions |= QPlatformTheme::DontUseCustomDirectoryIcons; | - | ||||||
38 | - | |||||||
39 | pixmap = theme->fileIconPixmap(m_fileInfo, size, iconOptions); | - | ||||||
40 | if (!pixmap.isNull()) { | - | ||||||
41 | if (cacheable) | - | ||||||
42 | QPixmapCache::insert(keyBase + QString::number(size.width()), pixmap); | - | ||||||
43 | } | - | ||||||
44 | - | |||||||
45 | return pixmap; | - | ||||||
46 | } | - | ||||||
47 | - | |||||||
48 | QList<QSize> availableSizes(QIcon::Mode mode = QIcon::Normal, QIcon::State state = QIcon::Off) const override | - | ||||||
49 | { | - | ||||||
50 | (void)mode;; | - | ||||||
51 | (void)state;; | - | ||||||
52 | static QList<QSize> sizes; | - | ||||||
53 | static QPlatformTheme *theme = 0; | - | ||||||
54 | if (!theme) { | - | ||||||
55 | theme = QGuiApplicationPrivate::platformTheme(); | - | ||||||
56 | if (!theme) | - | ||||||
57 | return sizes; | - | ||||||
58 | - | |||||||
59 | QList<int> themeSizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >(); | - | ||||||
60 | if (themeSizes.isEmpty()) | - | ||||||
61 | return sizes; | - | ||||||
62 | - | |||||||
63 | sizes.reserve(themeSizes.count()); | - | ||||||
64 | for (QForeachContainer<typename QtPrivate::remove_reference<decltype(themeSizes)>::type> _container_((themeSizes)); _container_.control && _container_.i != _container_.e; ++_container_.i, _container_.control ^= 1) for (int size = *_container_.i; _container_.control; _container_.control = 0) | - | ||||||
65 | sizes << QSize(size, size); | - | ||||||
66 | } | - | ||||||
67 | return sizes; | - | ||||||
68 | } | - | ||||||
69 | - | |||||||
70 | QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override | - | ||||||
71 | { | - | ||||||
72 | const QList<QSize> &sizes = availableSizes(mode, state); | - | ||||||
73 | const int numberSizes = sizes.length(); | - | ||||||
74 | if (numberSizes == 0) | - | ||||||
75 | return QSize(); | - | ||||||
76 | - | |||||||
77 | - | |||||||
78 | - | |||||||
79 | - | |||||||
80 | const int sizeArea = size.width() * size.height(); | - | ||||||
81 | QSize actualSize = sizes.first(); | - | ||||||
82 | int actualArea = actualSize.width() * actualSize.height(); | - | ||||||
83 | for (int i = 1; i < numberSizes; ++i) { | - | ||||||
84 | const QSize &s = sizes.at(i); | - | ||||||
85 | const int a = s.width() * s.height(); | - | ||||||
86 | if ((sizeArea <= a && a < actualArea) || (actualArea < sizeArea && actualArea < a)) { | - | ||||||
87 | actualSize = s; | - | ||||||
88 | actualArea = a; | - | ||||||
89 | } | - | ||||||
90 | } | - | ||||||
91 | - | |||||||
92 | if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) | - | ||||||
93 | actualSize.scale(size, Qt::KeepAspectRatio); | - | ||||||
94 | - | |||||||
95 | return actualSize; | - | ||||||
96 | } | - | ||||||
97 | - | |||||||
98 | private: | - | ||||||
99 | QFileInfo m_fileInfo; | - | ||||||
100 | QFileIconProvider::Options m_fipOpts; | - | ||||||
101 | }; | - | ||||||
102 | QFileIconProviderPrivate::QFileIconProviderPrivate(QFileIconProvider *q) : | - | ||||||
103 | q_ptr(q), homePath(QDir::home().absolutePath()) | - | ||||||
104 | { | - | ||||||
105 | } | - | ||||||
106 | - | |||||||
107 | QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const | - | ||||||
108 | { | - | ||||||
109 | switch (name) { | - | ||||||
110 | case QStyle::SP_FileIcon: | - | ||||||
111 | if (file.isNull()) | - | ||||||
112 | file = QApplication::style()->standardIcon(name); | - | ||||||
113 | return file; | - | ||||||
114 | case QStyle::SP_FileLinkIcon: | - | ||||||
115 | if (fileLink.isNull()) | - | ||||||
116 | fileLink = QApplication::style()->standardIcon(name); | - | ||||||
117 | return fileLink; | - | ||||||
118 | case QStyle::SP_DirIcon: | - | ||||||
119 | if (directory.isNull()) | - | ||||||
120 | directory = QApplication::style()->standardIcon(name); | - | ||||||
121 | return directory; | - | ||||||
122 | case QStyle::SP_DirLinkIcon: | - | ||||||
123 | if (directoryLink.isNull()) | - | ||||||
124 | directoryLink = QApplication::style()->standardIcon(name); | - | ||||||
125 | return directoryLink; | - | ||||||
126 | case QStyle::SP_DriveHDIcon: | - | ||||||
127 | if (harddisk.isNull()) | - | ||||||
128 | harddisk = QApplication::style()->standardIcon(name); | - | ||||||
129 | return harddisk; | - | ||||||
130 | case QStyle::SP_DriveFDIcon: | - | ||||||
131 | if (floppy.isNull()) | - | ||||||
132 | floppy = QApplication::style()->standardIcon(name); | - | ||||||
133 | return floppy; | - | ||||||
134 | case QStyle::SP_DriveCDIcon: | - | ||||||
135 | if (cdrom.isNull()) | - | ||||||
136 | cdrom = QApplication::style()->standardIcon(name); | - | ||||||
137 | return cdrom; | - | ||||||
138 | case QStyle::SP_DriveNetIcon: | - | ||||||
139 | if (network.isNull()) | - | ||||||
140 | network = QApplication::style()->standardIcon(name); | - | ||||||
141 | return network; | - | ||||||
142 | case QStyle::SP_ComputerIcon: | - | ||||||
143 | if (computer.isNull()) | - | ||||||
144 | computer = QApplication::style()->standardIcon(name); | - | ||||||
145 | return computer; | - | ||||||
146 | case QStyle::SP_DesktopIcon: | - | ||||||
147 | if (desktop.isNull()) | - | ||||||
148 | desktop = QApplication::style()->standardIcon(name); | - | ||||||
149 | return desktop; | - | ||||||
150 | case QStyle::SP_TrashIcon: | - | ||||||
151 | if (trashcan.isNull()) | - | ||||||
152 | trashcan = QApplication::style()->standardIcon(name); | - | ||||||
153 | return trashcan; | - | ||||||
154 | case QStyle::SP_DirHomeIcon: | - | ||||||
155 | if (home.isNull()) | - | ||||||
156 | home = QApplication::style()->standardIcon(name); | - | ||||||
157 | return home; | - | ||||||
158 | default: | - | ||||||
159 | return QIcon(); | - | ||||||
160 | } | - | ||||||
161 | return dead code: QIcon();return QIcon(); dead code: return QIcon(); | - | ||||||
162 | } | - | ||||||
163 | - | |||||||
164 | - | |||||||
165 | - | |||||||
166 | - | |||||||
167 | - | |||||||
168 | QFileIconProvider::QFileIconProvider() | - | ||||||
169 | : d_ptr(new QFileIconProviderPrivate(this)) | - | ||||||
170 | { | - | ||||||
171 | } | - | ||||||
172 | - | |||||||
173 | - | |||||||
174 | - | |||||||
175 | - | |||||||
176 | - | |||||||
177 | - | |||||||
178 | QFileIconProvider::~QFileIconProvider() | - | ||||||
179 | { | - | ||||||
180 | } | - | ||||||
181 | - | |||||||
182 | - | |||||||
183 | - | |||||||
184 | - | |||||||
185 | - | |||||||
186 | - | |||||||
187 | - | |||||||
188 | void QFileIconProvider::setOptions(QFileIconProvider::Options options) | - | ||||||
189 | { | - | ||||||
190 | QFileIconProviderPrivate * const d = d_func(); | - | ||||||
191 | d->options = options; | - | ||||||
192 | } | - | ||||||
193 | QFileIconProvider::Options QFileIconProvider::options() const | - | ||||||
194 | { | - | ||||||
195 | const QFileIconProviderPrivate * const d = d_func(); | - | ||||||
196 | return d->options; | - | ||||||
197 | } | - | ||||||
198 | - | |||||||
199 | - | |||||||
200 | - | |||||||
201 | - | |||||||
202 | - | |||||||
203 | QIcon QFileIconProvider::icon(IconType type) const | - | ||||||
204 | { | - | ||||||
205 | const QFileIconProviderPrivate * const d = d_func(); | - | ||||||
206 | switch (type) { | - | ||||||
207 | case Computer: | - | ||||||
208 | return d->getIcon(QStyle::SP_ComputerIcon); | - | ||||||
209 | case Desktop: | - | ||||||
210 | return d->getIcon(QStyle::SP_DesktopIcon); | - | ||||||
211 | case Trashcan: | - | ||||||
212 | return d->getIcon(QStyle::SP_TrashIcon); | - | ||||||
213 | case Network: | - | ||||||
214 | return d->getIcon(QStyle::SP_DriveNetIcon); | - | ||||||
215 | case Drive: | - | ||||||
216 | return d->getIcon(QStyle::SP_DriveHDIcon); | - | ||||||
217 | case Folder: | - | ||||||
218 | return d->getIcon(QStyle::SP_DirIcon); | - | ||||||
219 | case File: | - | ||||||
220 | return d->getIcon(QStyle::SP_FileIcon); | - | ||||||
221 | default: | - | ||||||
222 | break; | - | ||||||
223 | }; | - | ||||||
224 | return QIcon(); | - | ||||||
225 | } | - | ||||||
226 | - | |||||||
227 | static bool isCacheable(const QFileInfo &fi) | - | ||||||
228 | { | - | ||||||
229 | if (!fi.isFile()) | - | ||||||
230 | return false; | - | ||||||
231 | return !fi.isExecutable() && !fi.isSymLink(); | - | ||||||
232 | - | |||||||
233 | } | - | ||||||
234 | - | |||||||
235 | QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const | - | ||||||
236 | { | - | ||||||
237 | const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme(); | - | ||||||
238 | if (!theme) | - | ||||||
239 | return QIcon(); | - | ||||||
240 | - | |||||||
241 | QList<int> sizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >(); | - | ||||||
242 | if (sizes.isEmpty()) | - | ||||||
243 | return QIcon(); | - | ||||||
244 | - | |||||||
245 | return QIcon(new QFileIconEngine(fi, options)); | - | ||||||
246 | } | - | ||||||
247 | - | |||||||
248 | - | |||||||
249 | - | |||||||
250 | - | |||||||
251 | - | |||||||
252 | QIcon QFileIconProvider::icon(const QFileInfo &info) const | - | ||||||
253 | { | - | ||||||
254 | const QFileIconProviderPrivate * const d = d_func(); | - | ||||||
const QByteArray desktopEnvironment = QGuiApplicationPrivate::platformIntegration()->services()->desktopEnvironment(); | ||||||||
255 | - | |||||||
256 | if (desktopEnvironment != ([]() -> QByteArray { enum { Size = sizeof("KDE") - 1 }; static const QStaticByteArrayData<Size> qbytearray_literal = { { { { -1 } }, Size, 0, 0, sizeof(QByteArrayData) }, "KDE" }; QByteArrayDataPtr holder = { qbytearray_literal.data_ptr() }; const QByteArray ba(holder); return ba; }())) { | - | ||||||
QIcon gtkIcon = QGtkStylePrivate::getFilesystemIcon(info); | ||||||||
if (!gtkIcon.isNull()) | ||||||||
return gtkIcon; | ||||||||
}QIcon retIcon = d->getIcon(info); | ||||||||
257 | if (!retIcon.isNull()
| 0 | ||||||
258 | return never executed: retIcon;return retIcon; never executed: return retIcon; | 0 | ||||||
259 | - | |||||||
260 | if (info.isRoot()
| 0 | ||||||
261 | return never executed: d->getIcon(QStyle::SP_DriveHDIcon);return d->getIcon(QStyle::SP_DriveHDIcon); never executed: return d->getIcon(QStyle::SP_DriveHDIcon); | 0 | ||||||
262 | - | |||||||
263 | - | |||||||
264 | if (info.isFile()
| 0 | ||||||
265 | if (info.isSymLink()
| 0 | ||||||
266 | return never executed: d->getIcon(QStyle::SP_FileLinkIcon);return d->getIcon(QStyle::SP_FileLinkIcon); never executed: return d->getIcon(QStyle::SP_FileLinkIcon); | 0 | ||||||
267 | else | - | ||||||
268 | return never executed: d->getIcon(QStyle::SP_FileIcon);return d->getIcon(QStyle::SP_FileIcon); never executed: return d->getIcon(QStyle::SP_FileIcon); | 0 | ||||||
269 | } | - | ||||||
270 | if (info.isDir()
| 0 | ||||||
271 | if (info.isSymLink()
| 0 | ||||||
272 | return never executed: d->getIcon(QStyle::SP_DirLinkIcon);return d->getIcon(QStyle::SP_DirLinkIcon); never executed: return d->getIcon(QStyle::SP_DirLinkIcon); | 0 | ||||||
273 | } else { | - | ||||||
274 | if (info.absoluteFilePath() == d->homePath
| 0 | ||||||
275 | return never executed: d->getIcon(QStyle::SP_DirHomeIcon);return d->getIcon(QStyle::SP_DirHomeIcon); never executed: return d->getIcon(QStyle::SP_DirHomeIcon); | 0 | ||||||
276 | } else { | - | ||||||
277 | return never executed: d->getIcon(QStyle::SP_DirIcon);return d->getIcon(QStyle::SP_DirIcon); never executed: return d->getIcon(QStyle::SP_DirIcon); | 0 | ||||||
278 | } | - | ||||||
279 | } | - | ||||||
280 | } | - | ||||||
281 | return never executed: QIcon();return QIcon(); never executed: return QIcon(); | 0 | ||||||
282 | } | - | ||||||
283 | - | |||||||
284 | - | |||||||
285 | - | |||||||
286 | - | |||||||
287 | - | |||||||
288 | QString QFileIconProvider::type(const QFileInfo &info) const | - | ||||||
289 | { | - | ||||||
290 | if (info.isRoot()) | - | ||||||
291 | return QApplication::translate("QFileDialog", "Drive"); | - | ||||||
292 | if (info.isFile()) { | - | ||||||
293 | if (!info.suffix().isEmpty()) { | - | ||||||
294 | - | |||||||
295 | return QApplication::translate("QFileDialog", "%1 File").arg(info.suffix()); | - | ||||||
296 | } | - | ||||||
297 | return QApplication::translate("QFileDialog", "File"); | - | ||||||
298 | } | - | ||||||
299 | - | |||||||
300 | if (info.isDir()) | - | ||||||
301 | - | |||||||
302 | - | |||||||
303 | - | |||||||
304 | return QApplication::translate("QFileDialog", "Folder", "All other platforms"); | - | ||||||
305 | - | |||||||
306 | - | |||||||
307 | - | |||||||
308 | - | |||||||
309 | - | |||||||
310 | - | |||||||
311 | if (info.isSymLink()) | - | ||||||
312 | - | |||||||
313 | - | |||||||
314 | - | |||||||
315 | return QApplication::translate("QFileDialog", "Shortcut", "All other platforms"); | - | ||||||
316 | - | |||||||
317 | - | |||||||
318 | - | |||||||
319 | - | |||||||
320 | - | |||||||
321 | - | |||||||
322 | return QApplication::translate("QFileDialog", "Unknown"); | - | ||||||
323 | } | - | ||||||
324 | - | |||||||
325 | - | |||||||
Switch to Source code | Preprocessed file |