File: sggeometrymodel.cpp

package info (click to toggle)
gammaray 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,620 kB
  • sloc: cpp: 94,712; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 32
file content (302 lines) | stat: -rw-r--r-- 9,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
  sggeometrymodel.cpp

  This file is part of GammaRay, the Qt application inspection and manipulation tool.

  SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
  Author: Volker Krause <volker.krause@kdab.com>

  SPDX-License-Identifier: GPL-2.0-or-later

  Contact KDAB at <info@kdab.com> for commercial licensing options.
*/

#include "sggeometrymodel.h"

#include <QMetaEnum>
#include <QMetaObject>
#include <QSGGeometry>
#include <QSGMaterial>
#include <QSGNode>
#include <QtGui/qopengl.h>

using namespace GammaRay;

Q_DECLARE_METATYPE(QVector<quint16>)
Q_DECLARE_METATYPE(QVector<qint16>)
Q_DECLARE_METATYPE(QVector<int>)
Q_DECLARE_METATYPE(QVector<uint>)
Q_DECLARE_METATYPE(QVector<float>)
Q_DECLARE_METATYPE(QVector<double>)

template<typename T>
static QStringList toStringList(void *data, int size)
{
    QStringList list;
    T *typedData = static_cast<T *>(data);
    for (int i = 0; i < size; i++) {
        list << QString::number(*typedData);
        ++typedData;
    }
    return list;
}

template<typename T>
static QVariantList toVariantList(void *data, int size)
{
    QVariantList list;
    T *typedData = static_cast<T *>(data);
    for (int i = 0; i < size; i++) {
        list << QVariant::fromValue<T>(*typedData);
        ++typedData;
    }
    return list;
}

GammaRay::SGVertexModel::SGVertexModel(QObject *parent)
    : QAbstractTableModel(parent)
    , m_geometry(nullptr)
    , m_node(nullptr)
{
}

int SGVertexModel::rowCount(const QModelIndex &parent) const
{
    if (!m_geometry || parent.isValid())
        return 0;

    return m_geometry->vertexCount();
}

int GammaRay::SGVertexModel::columnCount(const QModelIndex &parent) const
{
    if (!m_geometry || parent.isValid())
        return 0;

    return m_geometry->attributeCount();
}

QVariant SGVertexModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()
        || !m_geometry
        || !index.internalPointer()
        || index.row() >= m_geometry->vertexCount()
        || index.column() >= m_geometry->attributeCount())
        return QVariant();

    if (role == Qt::DisplayRole) {
        const QSGGeometry::Attribute *attrInfo = m_geometry->attributes();
        attrInfo += index.column();
        switch (attrInfo->type) {
        case GL_BYTE:
            return toStringList<char>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
        case GL_UNSIGNED_BYTE:
            return toStringList<unsigned char>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
        case GL_UNSIGNED_SHORT:
            return toStringList<quint16>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
        case GL_SHORT:
            return toStringList<qint16>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
        case GL_INT:
            return toStringList<int>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
        case GL_UNSIGNED_INT:
            return toStringList<uint>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
        case GL_FLOAT:
            return toStringList<float>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
#if defined(GL_DOUBLE) && GL_DOUBLE != GL_FLOAT
        case GL_DOUBLE:
            return toStringList<double>(index.internalPointer(), attrInfo->tupleSize).join(QStringLiteral(", "));
#endif
#if !QT_CONFIG(opengles2)
        case GL_2_BYTES:
            return "2Bytes";
        case GL_3_BYTES:
            return "3Bytes";
        case GL_4_BYTES:
            return "4Bytes";
#endif
        default:
            return QStringLiteral("Unknown %1 byte data: 0x").arg(attrInfo->tupleSize).append(QByteArray(( char * )index.internalPointer(), attrInfo->tupleSize).toHex());
        }
    } else if (role == IsCoordinateRole) {
        const QSGGeometry::Attribute *attrInfo = m_geometry->attributes();
        attrInfo += index.column();
        return ( bool )attrInfo->isVertexCoordinate;
    } else if (role == RenderRole) {
        const QSGGeometry::Attribute *attrInfo = m_geometry->attributes();
        attrInfo += index.column();
        switch (attrInfo->type) {
        case GL_BYTE:
            return toVariantList<char>(index.internalPointer(), attrInfo->tupleSize);
        case GL_UNSIGNED_BYTE:
            return toVariantList<unsigned char>(index.internalPointer(), attrInfo->tupleSize);
        case GL_UNSIGNED_SHORT:
            return toVariantList<quint16>(index.internalPointer(), attrInfo->tupleSize);
        case GL_SHORT:
            return toVariantList<qint16>(index.internalPointer(), attrInfo->tupleSize);
        case GL_INT:
            return toVariantList<int>(index.internalPointer(), attrInfo->tupleSize);
        case GL_UNSIGNED_INT:
            return toVariantList<uint>(index.internalPointer(), attrInfo->tupleSize);
        case GL_FLOAT:
            return toVariantList<float>(index.internalPointer(), attrInfo->tupleSize);
#if defined(GL_DOUBLE) && GL_DOUBLE != GL_FLOAT
        case GL_DOUBLE:
            return toVariantList<double>(index.internalPointer(), attrInfo->tupleSize);
#endif
        default:
            return QVariant();
        }
    }

    return QVariant();
}

QMap<int, QVariant> SGVertexModel::itemData(const QModelIndex &index) const
{
    QMap<int, QVariant> map = QAbstractItemModel::itemData(index);
    map.insert(IsCoordinateRole, data(index, IsCoordinateRole));
    map.insert(RenderRole, data(index, RenderRole));
    return map;
}

QVariant SGVertexModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal && m_geometry) {
        if (section < m_geometry->attributeCount()) {
            switch (m_geometry->attributes()[section].attributeType) {
            case QSGGeometry::UnknownAttribute:
                return QStringLiteral("UnknownAttribute");
            case QSGGeometry::PositionAttribute:
                return QStringLiteral("PositionAttribute");
            case QSGGeometry::ColorAttribute:
                return QStringLiteral("ColorAttribute");
            case QSGGeometry::TexCoordAttribute:
                return QStringLiteral("TexCoordAttribute");
            case QSGGeometry::TexCoord1Attribute:
                return QStringLiteral("TexCoord1Attribute");
            case QSGGeometry::TexCoord2Attribute:
                return QStringLiteral("TexCoord2Attribute");
                break;
            }
        }
    }
    return QAbstractItemModel::headerData(section, orientation, role);
}

void SGVertexModel::setNode(QSGGeometryNode *node)
{
    beginResetModel();
    m_node = node;
    m_geometry = node->geometry();
    endResetModel();
}

QModelIndex GammaRay::SGVertexModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!m_geometry
        || row >= m_geometry->vertexCount()
        || column >= m_geometry->attributeCount()
        || parent.isValid())
        return {};

    char *attr = static_cast<char *>(m_geometry->vertexData());
    attr += m_geometry->sizeOfVertex() * row;
    const QSGGeometry::Attribute *attrInfo = m_geometry->attributes();
    int tupleItemSize = 0;

    switch (attrInfo->type) {
    case GL_BYTE:
        tupleItemSize = sizeof(char);
        break;
    case GL_UNSIGNED_BYTE:
        tupleItemSize = sizeof(unsigned char);
        break;
    case GL_UNSIGNED_SHORT:
        tupleItemSize = sizeof(unsigned short);
        break;
    case GL_SHORT:
        tupleItemSize = sizeof(short);
        break;
    case GL_INT:
        tupleItemSize = sizeof(int);
        break;
    case GL_UNSIGNED_INT:
        tupleItemSize = sizeof(unsigned int);
        break;
    case GL_FLOAT:
        tupleItemSize = sizeof(float);
        break;
#if defined(GL_DOUBLE) && GL_DOUBLE != GL_FLOAT
    case GL_DOUBLE:
        tupleItemSize = sizeof(double);
        break;
#endif
    default:
        return createIndex(row, column);
    }

    for (int i = 0; i < column; i++) {
        attr += tupleItemSize * attrInfo->tupleSize;
        attrInfo++;
    }

    return createIndex(row, column, attr);
}


GammaRay::SGAdjacencyModel::SGAdjacencyModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_geometry(nullptr)
    , m_node(nullptr)
{
}

int SGAdjacencyModel::rowCount(const QModelIndex &parent) const
{
    if (!m_geometry || parent.isValid())
        return 0;

    return m_geometry->indexCount();
}

QVariant SGAdjacencyModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()
        || !m_geometry
        || index.row() >= m_geometry->indexCount()
        || index.column() != 0)
        return QVariant();

    if (role == DrawingModeRole) {
        return m_geometry->drawingMode();
    }
    if (role == RenderRole) {
        int indexType = m_geometry->indexType();

        if (indexType == GL_UNSIGNED_INT)
            return *(static_cast<const quint32 *>(m_geometry->indexData()) + index.row());
        else if (indexType == GL_UNSIGNED_SHORT)
            return *(static_cast<const quint16 *>(m_geometry->indexData()) + index.row());
        else if (indexType == GL_UNSIGNED_BYTE)
            return *(static_cast<const quint8 *>(m_geometry->indexData()) + index.row());
    }

    return QVariant();
}

void SGAdjacencyModel::setNode(QSGGeometryNode *node)
{
    beginResetModel();
    m_node = node;
    m_geometry = node->geometry();
    endResetModel();
}

QMap<int, QVariant> SGAdjacencyModel::itemData(const QModelIndex &index) const
{
    QMap<int, QVariant> map = QAbstractItemModel::itemData(index);
    map.insert(DrawingModeRole, data(index, DrawingModeRole));
    map.insert(RenderRole, data(index, RenderRole));
    return map;
}