1 | // Copyright (C) 2022 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "qquick3drenderstatstexturesmodel_p.h" |
5 | |
6 | #include <QtCore/QRegularExpression> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | QHash<int, QByteArray> QQuick3DRenderStatsTexturesModel::roleNames() const |
11 | { |
12 | return { {Qt::DisplayRole, "display"} }; |
13 | } |
14 | |
15 | int QQuick3DRenderStatsTexturesModel::rowCount(const QModelIndex &parent) const |
16 | { |
17 | Q_UNUSED(parent); |
18 | return m_data.count(); |
19 | } |
20 | |
21 | int QQuick3DRenderStatsTexturesModel::columnCount(const QModelIndex &parent) const |
22 | { |
23 | Q_UNUSED(parent); |
24 | return 5; |
25 | } |
26 | |
27 | QVariant QQuick3DRenderStatsTexturesModel::data(const QModelIndex &index, int role) const |
28 | { |
29 | if (!index.isValid()) |
30 | return QVariant(); |
31 | |
32 | const uint row = index.row(); |
33 | const uint column = index.column(); |
34 | |
35 | if (role == Qt::DisplayRole) { |
36 | // Name 0 |
37 | if (column == 0) |
38 | return m_data[row].name; |
39 | // Size 1 |
40 | if (column == 1) |
41 | return m_data[row].size; |
42 | // Format 2 |
43 | if (column == 2) |
44 | return m_data[row].format; |
45 | // Mip Levels 3 |
46 | if (column == 3) |
47 | return m_data[row].mipLevels; |
48 | // Flags 4 |
49 | if (column == 4) |
50 | return m_data[row].flags; |
51 | } |
52 | |
53 | return QVariant(); |
54 | } |
55 | |
56 | QVariant QQuick3DRenderStatsTexturesModel::headerData(int section, Qt::Orientation orientation, int role) const |
57 | { |
58 | if (role != Qt::DisplayRole || orientation != Qt::Horizontal || section > 5) |
59 | return QVariant(); |
60 | |
61 | switch (section) { |
62 | case 0: |
63 | return QStringLiteral("Name"); |
64 | case 1: |
65 | return QStringLiteral("Size"); |
66 | case 2: |
67 | return QStringLiteral("Format"); |
68 | case 3: |
69 | return QStringLiteral("Mip Levels"); |
70 | case 4: |
71 | return QStringLiteral("Flags"); |
72 | default: |
73 | Q_UNREACHABLE(); |
74 | return QVariant(); |
75 | } |
76 | } |
77 | |
78 | const QString &QQuick3DRenderStatsTexturesModel::textureData() const |
79 | { |
80 | return m_textureData; |
81 | } |
82 | |
83 | void QQuick3DRenderStatsTexturesModel::setTextureData(const QString &newTextureData) |
84 | { |
85 | if (m_textureData == newTextureData) |
86 | return; |
87 | |
88 | m_textureData = newTextureData; |
89 | emit textureDataChanged(); |
90 | |
91 | // newTextureData is just a markdown table... |
92 | QVector<Data> newData; |
93 | if (!m_textureData.isEmpty()) { |
94 | auto lines = m_textureData.split(sep: QRegularExpression(QStringLiteral("[\r\n]")), behavior: Qt::SkipEmptyParts); |
95 | if (lines.size() > 2) { |
96 | for (qsizetype i = 2; i < lines.size(); ++i) { |
97 | const auto &line = lines.at(i); |
98 | auto fields = line.split(sep: QLatin1Char('|'), behavior: Qt::SkipEmptyParts); |
99 | if (fields.size() < 4) // flags field can be empty |
100 | continue; |
101 | Data data; |
102 | bool isUInt32 = false; |
103 | data.name = fields[0]; |
104 | data.size = fields[1]; |
105 | data.format = fields[2]; |
106 | data.mipLevels = fields[3].toULong(ok: &isUInt32); |
107 | if (!isUInt32) |
108 | continue; |
109 | if (fields.size() == 5) |
110 | data.flags = fields[4]; |
111 | newData.append(t: data); |
112 | } |
113 | } |
114 | } |
115 | |
116 | // update the model |
117 | beginResetModel(); |
118 | m_data = newData; |
119 | endResetModel(); |
120 | } |
121 | |
122 | QT_END_NAMESPACE |
123 |