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 test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef FAKEDIRMODEL_H |
30 | #define FAKEDIRMODEL_H |
31 | |
32 | #include <QtGui/QStandardItemModel> |
33 | #include <QtGui/QStandardItem> |
34 | #include <QtGui/QIcon> |
35 | #include <QtGui/QPixmap> |
36 | #include <QtGui/QImage> |
37 | #include <QtCore/QStringList> |
38 | |
39 | typedef QList<QStandardItem *> StandardItemList; |
40 | |
41 | static inline QIcon coloredIcon(Qt::GlobalColor color) |
42 | { |
43 | QImage image(22, 22, QImage::Format_ARGB32); |
44 | image.fill(color); |
45 | return QPixmap::fromImage(image); |
46 | } |
47 | |
48 | static void addFileEntry(const StandardItemList &directory, const QString &name, const QString &size) |
49 | { |
50 | static const QIcon fileIcon = coloredIcon(color: Qt::blue); |
51 | directory.front()->appendRow(aitems: StandardItemList() << new QStandardItem(fileIcon, name) << new QStandardItem(size)); |
52 | } |
53 | |
54 | static StandardItemList createDirEntry(const QString &name) |
55 | { |
56 | static const QIcon dirIcon = coloredIcon(color: Qt::red); |
57 | StandardItemList result; |
58 | result << new QStandardItem(dirIcon, name) << new QStandardItem; |
59 | return result; |
60 | } |
61 | |
62 | static inline StandardItemList addDirEntry(const StandardItemList &directory, const QString &name) |
63 | { |
64 | const StandardItemList entry = createDirEntry(name); |
65 | directory.front()->appendRow(aitems: entry); |
66 | return entry; |
67 | } |
68 | |
69 | static QStandardItem *populateFakeDirModel(QStandardItemModel *model) |
70 | { |
71 | enum Columns { NameColumn, SizeColumn, ColumnCount }; |
72 | |
73 | model->setColumnCount(ColumnCount); |
74 | model->setHorizontalHeaderLabels(QStringList() << QStringLiteral("Name" ) << QStringLiteral("Size" )); |
75 | |
76 | const StandardItemList root = createDirEntry(QStringLiteral("/" )); |
77 | model->appendRow(items: root); |
78 | |
79 | const StandardItemList binDir = addDirEntry(directory: root, QStringLiteral("bin" )); |
80 | addFileEntry(directory: binDir, QStringLiteral("ls" ), QStringLiteral("100 KB" )); |
81 | addFileEntry(directory: binDir, QStringLiteral("bash" ), QStringLiteral("200 KB" )); |
82 | |
83 | const StandardItemList devDir = addDirEntry(directory: root, QStringLiteral("dev" )); |
84 | addFileEntry(directory: devDir, QStringLiteral("tty1" ), QStringLiteral("0 B" )); |
85 | addDirEntry(directory: devDir, QStringLiteral("proc" )); |
86 | |
87 | const StandardItemList etcDir = addDirEntry(directory: root, QStringLiteral("etc" )); |
88 | addFileEntry(directory: etcDir, QStringLiteral("foo1.config" ), QStringLiteral("1 KB" )); |
89 | addFileEntry(directory: etcDir, QStringLiteral("foo2.conf" ), QStringLiteral("654 B" )); |
90 | |
91 | const StandardItemList homeDir = addDirEntry(directory: root, QStringLiteral("home" )); |
92 | addFileEntry(directory: homeDir, QStringLiteral("file1" ), QStringLiteral("1 KB" )); |
93 | |
94 | const StandardItemList documentsDir = addDirEntry(directory: homeDir, QStringLiteral("Documents" )); |
95 | addFileEntry(directory: documentsDir, QStringLiteral("txt1.odt" ), QStringLiteral("2 MB" )); |
96 | addFileEntry(directory: documentsDir, QStringLiteral("sheet1.xls" ), QStringLiteral("32 KB" )); |
97 | addFileEntry(directory: documentsDir, QStringLiteral("foo.doc" ), QStringLiteral("214 KB" )); |
98 | |
99 | const StandardItemList downloadsDir = addDirEntry(directory: homeDir, QStringLiteral("Downloads" )); |
100 | addFileEntry(directory: downloadsDir, QStringLiteral("package1.zip" ), QStringLiteral("34 MB" )); |
101 | addFileEntry(directory: downloadsDir, QStringLiteral("package2.zip" ), QStringLiteral("623 KB" )); |
102 | |
103 | const StandardItemList picturesDir = addDirEntry(directory: homeDir, QStringLiteral("Pictures" )); |
104 | addFileEntry(directory: picturesDir, QStringLiteral("img0001.jpg" ), QStringLiteral("4 MB" )); |
105 | addFileEntry(directory: picturesDir, QStringLiteral("img0002.png" ), QStringLiteral("10 MB" )); |
106 | |
107 | // qcolumnview::moveCursor() requires an empty directory followed by another one. |
108 | addDirEntry(directory: root, QStringLiteral("lost+found" )); |
109 | |
110 | const StandardItemList tmpDir = addDirEntry(directory: root, QStringLiteral("tmp" )); |
111 | addFileEntry(directory: tmpDir, name: "asdujhsdjys" , size: "435 B" ); |
112 | addFileEntry(directory: tmpDir, name: "krtbldfhd" , size: "5557 B" ); |
113 | |
114 | return homeDir.front(); |
115 | } |
116 | |
117 | #endif // FAKEDIRMODEL_H |
118 | |