~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/widgets/itemviews/qfileiconprovider.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 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
 
 
65
QT_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
 
 
86
class QFileIconProviderPrivate
 
87
{
 
88
    Q_DECLARE_PUBLIC(QFileIconProvider)
 
89
 
 
90
public:
 
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
 
 
98
private:
 
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
 
 
115
QFileIconProviderPrivate::QFileIconProviderPrivate() :
 
116
    homePath(QDir::home().absolutePath())
 
117
{
 
118
}
 
119
 
 
120
QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const
 
121
{
 
122
    switch (name) {
 
123
    case QStyle::SP_FileIcon:
 
124
        if (file.isNull())
 
125
            file = QApplication::style()->standardIcon(name);
 
126
        return file;
 
127
    case QStyle::SP_FileLinkIcon:
 
128
        if (fileLink.isNull())
 
129
            fileLink = QApplication::style()->standardIcon(name);
 
130
        return fileLink;
 
131
    case QStyle::SP_DirIcon:
 
132
        if (directory.isNull())
 
133
            directory = QApplication::style()->standardIcon(name);
 
134
        return directory;
 
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);
 
142
        return harddisk;
 
143
    case QStyle::SP_DriveFDIcon:
 
144
        if (floppy.isNull())
 
145
            floppy = QApplication::style()->standardIcon(name);
 
146
        return floppy;
 
147
    case QStyle::SP_DriveCDIcon:
 
148
        if (cdrom.isNull())
 
149
            cdrom = QApplication::style()->standardIcon(name);
 
150
        return cdrom;
 
151
    case QStyle::SP_DriveNetIcon:
 
152
        if (network.isNull())
 
153
            network = QApplication::style()->standardIcon(name);
 
154
        return network;
 
155
    case QStyle::SP_ComputerIcon:
 
156
        if (computer.isNull())
 
157
            computer = QApplication::style()->standardIcon(name);
 
158
        return computer;
 
159
    case QStyle::SP_DesktopIcon:
 
160
        if (desktop.isNull())
 
161
            desktop = QApplication::style()->standardIcon(name);
 
162
        return desktop;
 
163
    case QStyle::SP_TrashIcon:
 
164
        if (trashcan.isNull())
 
165
            trashcan = QApplication::style()->standardIcon(name);
 
166
        return trashcan;
 
167
    case QStyle::SP_DirHomeIcon:
 
168
        if (home.isNull())
 
169
            home = QApplication::style()->standardIcon(name);
 
170
        return home;
 
171
    default:
 
172
        return QIcon();
 
173
    }
 
174
    return QIcon();
 
175
}
 
176
 
 
177
/*!
 
178
  Constructs a file icon provider.
 
179
*/
 
180
 
 
181
QFileIconProvider::QFileIconProvider()
 
182
    : d_ptr(new QFileIconProviderPrivate)
 
183
{
 
184
}
 
185
 
 
186
/*!
 
187
  Destroys the file icon provider.
 
188
 
 
189
*/
 
190
 
 
191
QFileIconProvider::~QFileIconProvider()
 
192
{
 
193
}
 
194
 
 
195
/*!
 
196
  Returns an icon set for the given \a type.
 
197
*/
 
198
 
 
199
QIcon QFileIconProvider::icon(IconType type) const
 
200
{
 
201
    Q_D(const QFileIconProvider);
 
202
    switch (type) {
 
203
    case Computer:
 
204
        return d->getIcon(QStyle::SP_ComputerIcon);
 
205
    case Desktop:
 
206
        return d->getIcon(QStyle::SP_DesktopIcon);
 
207
    case Trashcan:
 
208
        return d->getIcon(QStyle::SP_TrashIcon);
 
209
    case Network:
 
210
        return d->getIcon(QStyle::SP_DriveNetIcon);
 
211
    case Drive:
 
212
        return d->getIcon(QStyle::SP_DriveHDIcon);
 
213
    case Folder:
 
214
        return d->getIcon(QStyle::SP_DirIcon);
 
215
    case File:
 
216
        return d->getIcon(QStyle::SP_FileIcon);
 
217
    default:
 
218
        break;
 
219
    };
 
220
    return QIcon();
 
221
}
 
222
 
 
223
QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const
 
224
{
 
225
    QIcon retIcon;
 
226
    const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
 
227
    if (!theme)
 
228
        return retIcon;
 
229
 
 
230
    QList<int> sizes = theme->themeHint(QPlatformTheme::IconPixmapSizes).value<QList<int> >();
 
231
    if (sizes.isEmpty())
 
232
        return retIcon;
 
233
 
 
234
    const QString fileExtension = fi.suffix().toUpper();
 
235
    const QString keyBase = QLatin1String("qt_.") + fi.suffix().toUpper();
 
236
 
 
237
    bool cacheable = fi.isFile() && !fi.isExecutable() && !fi.isSymLink() && fileExtension != QLatin1String("ICO");
 
238
    if (cacheable) {
 
239
        QPixmap pixmap;
 
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);
 
247
                } else {
 
248
                    iconIsComplete = false;
 
249
                    break;
 
250
                }
 
251
            if (iconIsComplete)
 
252
                return retIcon;
 
253
        }
 
254
    }
 
255
 
 
256
    Q_FOREACH (int size, sizes) {
 
257
        QPixmap pixmap = theme->fileIconPixmap(fi, QSizeF(size, size));
 
258
        if (!pixmap.isNull()) {
 
259
            retIcon.addPixmap(pixmap);
 
260
            if (cacheable)
 
261
                QPixmapCache::insert(keyBase + QString::number(size), pixmap);
 
262
        }
 
263
    }
 
264
 
 
265
    return retIcon;
 
266
}
 
267
 
 
268
 
 
269
/*!
 
270
  Returns an icon for the file described by \a info.
 
271
*/
 
272
 
 
273
QIcon QFileIconProvider::icon(const QFileInfo &info) const
 
274
{
 
275
    Q_D(const QFileIconProvider);
 
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);
 
287
    if (!retIcon.isNull())
 
288
        return retIcon;
 
289
 
 
290
    if (info.isRoot())
 
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);
 
313
#endif
 
314
 
 
315
    if (info.isFile()) {
 
316
        if (info.isSymLink())
 
317
            return d->getIcon(QStyle::SP_FileLinkIcon);
 
318
        else
 
319
            return d->getIcon(QStyle::SP_FileIcon);
 
320
    }
 
321
  if (info.isDir()) {
 
322
    if (info.isSymLink()) {
 
323
      return d->getIcon(QStyle::SP_DirLinkIcon);
 
324
    } else {
 
325
      if (info.absoluteFilePath() == d->homePath) {
 
326
        return d->getIcon(QStyle::SP_DirHomeIcon);
 
327
      } else {
 
328
        return d->getIcon(QStyle::SP_DirIcon);
 
329
      }
 
330
    }
 
331
  }
 
332
  return QIcon();
 
333
}
 
334
 
 
335
/*!
 
336
  Returns the type of the file described by \a info.
 
337
*/
 
338
 
 
339
QString QFileIconProvider::type(const QFileInfo &info) const
 
340
{
 
341
    if (info.isRoot())
 
342
        return QApplication::translate("QFileDialog", "Drive");
 
343
    if (info.isFile()) {
 
344
        if (!info.suffix().isEmpty())
 
345
            return info.suffix() + QLatin1Char(' ') + QApplication::translate("QFileDialog", "File");
 
346
        return QApplication::translate("QFileDialog", "File");
 
347
    }
 
348
 
 
349
    if (info.isDir())
 
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");
 
354
#endif
 
355
    // Windows   - "File Folder"
 
356
    // OS X      - "Folder"
 
357
    // Konqueror - "Folder"
 
358
    // Nautilus  - "folder"
 
359
 
 
360
    if (info.isSymLink())
 
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");
 
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");
 
372
}
 
373
 
 
374
QT_END_NAMESPACE
 
375
 
 
376
#endif