2022-06-23 21:36:00 -04:00
|
|
|
#include "chunkmodel.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
#define super QAbstractItemModel
|
2022-06-23 21:36:00 -04:00
|
|
|
|
2022-06-27 12:44:50 -04:00
|
|
|
using namespace si;
|
|
|
|
|
2022-06-23 21:36:00 -04:00
|
|
|
ChunkModel::ChunkModel(QObject *parent) :
|
2022-07-10 23:51:49 -04:00
|
|
|
super{parent},
|
|
|
|
chunk_(nullptr)
|
2022-06-23 21:36:00 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int ChunkModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return kColCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex ChunkModel::index(int row, int column, const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Chunk *c = GetChunkFromIndex(parent);
|
|
|
|
if (!c) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
return createIndex(row, column, c->GetChildAt(row));
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex ChunkModel::parent(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
Chunk *child = GetChunkFromIndex(index);
|
|
|
|
if (!child) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
Core *parent = child->GetParent();
|
2022-06-23 21:36:00 -04:00
|
|
|
if (!parent) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2022-07-10 23:51:49 -04:00
|
|
|
Core *grandparent = parent->GetParent();
|
2022-06-23 21:36:00 -04:00
|
|
|
if (!grandparent) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t row = grandparent->IndexOfChild(parent);
|
2022-07-03 23:45:57 -04:00
|
|
|
return createIndex(int(row), 0, parent);
|
2022-06-23 21:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int ChunkModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Chunk *c = GetChunkFromIndex(parent);
|
|
|
|
if (!c) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-03 23:45:57 -04:00
|
|
|
return int(c->GetChildCount());
|
2022-06-23 21:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ChunkModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
Chunk *c = GetChunkFromIndex(index);
|
|
|
|
if (!c) {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
|
|
|
|
switch (index.column()) {
|
|
|
|
case kColType:
|
|
|
|
// Convert 4-byte ID to QString
|
2022-07-11 00:09:39 -04:00
|
|
|
return QString::fromLatin1(reinterpret_cast<const char *>(&c->id()), sizeof(uint32_t));
|
2022-06-27 12:44:50 -04:00
|
|
|
case kColOffset:
|
|
|
|
return QStringLiteral("0x%1").arg(QString::number(c->offset(), 16).toUpper());
|
2022-06-23 21:36:00 -04:00
|
|
|
case kColDesc:
|
|
|
|
return QString::fromUtf8(c->GetTypeDescription());
|
2022-07-04 02:33:56 -04:00
|
|
|
case kColObjectID:
|
|
|
|
if (c->type() == Chunk::TYPE_MxOb) {
|
|
|
|
return c->data("ID").toU32();
|
|
|
|
} else if (c->type() == Chunk::TYPE_MxCh) {
|
|
|
|
return c->data("Object").toU32();
|
|
|
|
}
|
|
|
|
break;
|
2022-06-23 21:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ChunkModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch (section) {
|
|
|
|
case kColType:
|
|
|
|
return tr("Type");
|
2022-06-27 12:44:50 -04:00
|
|
|
case kColOffset:
|
|
|
|
return tr("Offset");
|
2022-06-23 21:36:00 -04:00
|
|
|
case kColDesc:
|
|
|
|
return tr("Description");
|
2022-07-04 02:33:56 -04:00
|
|
|
case kColObjectID:
|
|
|
|
return tr("Object ID");
|
2022-06-23 21:36:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return super::headerData(section, orientation, role);
|
|
|
|
}
|
2022-07-10 23:51:49 -04:00
|
|
|
|
|
|
|
void ChunkModel::SetChunk(si::Chunk *c)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
chunk_ = c;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
si::Chunk *ChunkModel::GetChunkFromIndex(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return chunk_;
|
|
|
|
} else {
|
|
|
|
return static_cast<Chunk*>(index.internalPointer());
|
|
|
|
}
|
|
|
|
}
|