1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QApplication>
52#include <QQmlApplicationEngine>
53#include <QtQml>
54#include <QFileSystemModel>
55#include <QDateTime>
56#include <QDesktopServices>
57#include <QUrl>
58
59static inline QString permissionString(const QFileInfo &fi)
60{
61 const QFile::Permissions permissions = fi.permissions();
62 QString result = QLatin1String("----------");
63 if (fi.isSymLink())
64 result[0] = QLatin1Char('l');
65 else if (fi.isDir())
66 result[0] = QLatin1Char('d');
67 if (permissions & QFileDevice::ReadUser)
68 result[1] = QLatin1Char('r');
69 if (permissions & QFileDevice::WriteUser)
70 result[2] = QLatin1Char('w');
71 if (permissions & QFileDevice::ExeUser)
72 result[3] = QLatin1Char('x');
73 if (permissions & QFileDevice::ReadGroup)
74 result[4] = QLatin1Char('r');
75 if (permissions & QFileDevice::WriteGroup)
76 result[5] = QLatin1Char('w');
77 if (permissions & QFileDevice::ExeGroup)
78 result[6] = QLatin1Char('x');
79 if (permissions & QFileDevice::ReadOther)
80 result[7] = QLatin1Char('r');
81 if (permissions & QFileDevice::WriteOther)
82 result[8] = QLatin1Char('w');
83 if (permissions & QFileDevice::ExeOther)
84 result[9] = QLatin1Char('x');
85 return result;
86}
87
88static inline QString sizeString(const QFileInfo &fi)
89{
90 if (!fi.isFile())
91 return QString();
92 const qint64 size = fi.size();
93 if (size > 1024 * 1024 * 10)
94 return QString::number(size / (1024 * 1024)) + QLatin1Char('M');
95 if (size > 1024 * 10)
96 return QString::number(size / 1024) + QLatin1Char('K');
97 return QString::number(size);
98}
99
100class DisplayFileSystemModel : public QFileSystemModel {
101 Q_OBJECT
102public:
103 explicit DisplayFileSystemModel(QObject *parent = nullptr)
104 : QFileSystemModel(parent) {}
105
106 enum Roles {
107 SizeRole = Qt::UserRole + 4,
108 DisplayableFilePermissionsRole = Qt::UserRole + 5,
109 LastModifiedRole = Qt::UserRole + 6,
110 UrlStringRole = Qt::UserRole + 7
111 };
112 Q_ENUM(Roles)
113
114 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
115 {
116 if (index.isValid() && role >= SizeRole) {
117 switch (role) {
118 case SizeRole:
119 return QVariant(sizeString(fi: fileInfo(index)));
120 case DisplayableFilePermissionsRole:
121 return QVariant(permissionString(fi: fileInfo(index)));
122 case LastModifiedRole:
123 return QVariant(QLocale::system().toString(dateTime: fileInfo(index).lastModified(), format: QLocale::ShortFormat));
124 case UrlStringRole:
125 return QVariant(QUrl::fromLocalFile(localfile: filePath(index)).toString());
126 default:
127 break;
128 }
129 }
130 return QFileSystemModel::data(index, role);
131 }
132
133 QHash<int,QByteArray> roleNames() const override
134 {
135 QHash<int, QByteArray> result = QFileSystemModel::roleNames();
136 result.insert(akey: SizeRole, QByteArrayLiteral("size"));
137 result.insert(akey: DisplayableFilePermissionsRole, QByteArrayLiteral("displayableFilePermissions"));
138 result.insert(akey: LastModifiedRole, QByteArrayLiteral("lastModified"));
139 return result;
140 }
141};
142
143int main(int argc, char *argv[])
144{
145 QApplication app(argc, argv);
146
147 QQmlApplicationEngine engine;
148 qmlRegisterUncreatableType<DisplayFileSystemModel>(uri: "io.qt.examples.quick.controls.filesystembrowser", versionMajor: 1, versionMinor: 0,
149 qmlName: "FileSystemModel", reason: "Cannot create a FileSystemModel instance.");
150 QFileSystemModel *fsm = new DisplayFileSystemModel(&engine);
151 fsm->setRootPath(QDir::homePath());
152 fsm->setResolveSymlinks(true);
153 engine.rootContext()->setContextProperty("fileSystemModel", fsm);
154 engine.rootContext()->setContextProperty("rootPathIndex", fsm->index(path: fsm->rootPath()));
155 engine.load(url: QUrl(QStringLiteral("qrc:///main.qml")));
156 if (engine.rootObjects().isEmpty())
157 return -1;
158
159 return app.exec();
160}
161
162#include "main.moc"
163

source code of qtquickcontrols/examples/quickcontrols/controls/filesystembrowser/main.cpp