1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qquick3drenderstatsmeshesmodel_p.h"
5
6#include <QtCore/QRegularExpression>
7
8QT_BEGIN_NAMESPACE
9
10QHash<int, QByteArray> QQuick3DRenderStatsMeshesModel::roleNames() const
11{
12 return { {Qt::DisplayRole, "display"} };
13}
14
15int QQuick3DRenderStatsMeshesModel::rowCount(const QModelIndex &parent) const
16{
17 Q_UNUSED(parent);
18 return m_data.count();
19}
20
21int QQuick3DRenderStatsMeshesModel::columnCount(const QModelIndex &parent) const
22{
23 Q_UNUSED(parent);
24 return 5;
25}
26
27QVariant QQuick3DRenderStatsMeshesModel::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 // Submeshes 1
40 if (column == 1)
41 return m_data[row].submeshes;
42 // Vertices 2
43 if (column == 2)
44 return m_data[row].vertices;
45 // Vertex Buffer Size 3
46 if (column == 3)
47 return m_data[row].vertexBufferSize;
48 // Index Buffer Size 4
49 if (column == 4)
50 return m_data[row].indexBufferSize;
51 }
52
53 return QVariant();
54}
55
56QVariant QQuick3DRenderStatsMeshesModel::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("Submeshes");
66 case 2:
67 return QStringLiteral("Vertices");
68 case 3:
69 return QStringLiteral("VBuf Size");
70 case 4:
71 return QStringLiteral("IBuf Size");
72 default:
73 Q_UNREACHABLE();
74 return QVariant();
75 }
76}
77
78const QString &QQuick3DRenderStatsMeshesModel::meshData() const
79{
80 return m_meshData;
81}
82
83void QQuick3DRenderStatsMeshesModel::setMeshData(const QString &newMeshData)
84{
85 if (m_meshData == newMeshData)
86 return;
87
88 m_meshData = newMeshData;
89 emit meshDataChanged();
90
91 // newMeshData is just a markdown table...
92 QVector<Data> newData;
93 if (!m_meshData.isEmpty()) {
94 auto lines = m_meshData.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() != 5)
100 continue;
101 Data data;
102 bool isUInt64 = false;
103 data.name = fields[0];
104 data.submeshes = fields[1].toULongLong(ok: &isUInt64);
105 if (!isUInt64)
106 continue;
107 data.vertices = fields[2].toULongLong(ok: &isUInt64);
108 if (!isUInt64)
109 continue;
110 data.vertexBufferSize = fields[3].toULongLong(ok: &isUInt64);
111 if (!isUInt64)
112 continue;
113 data.indexBufferSize = fields[4].toULongLong(ok: &isUInt64);
114 if (!isUInt64)
115 continue;
116 newData.append(t: data);
117 }
118 }
119 }
120
121 // update the model
122 beginResetModel();
123 m_data = newData;
124 endResetModel();
125}
126
127QT_END_NAMESPACE
128

source code of qtquick3d/src/helpers/impl/qquick3drenderstatsmeshesmodel.cpp