2022-10-02 19:45:46 +00:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <dust3d/base/debug.h>
|
|
|
|
#include "component_list_model.h"
|
|
|
|
#include "document.h"
|
|
|
|
|
|
|
|
ComponentListModel::ComponentListModel(const Document *document, QObject *parent):
|
|
|
|
QAbstractListModel(parent),
|
|
|
|
m_document(document)
|
|
|
|
{
|
2022-10-05 11:34:56 +00:00
|
|
|
connect(m_document, &Document::componentPreviewPixmapChanged, [this](const dust3d::Uuid &componentId) {
|
2022-10-06 09:05:09 +00:00
|
|
|
auto findIndex = this->m_componentIdToIndexMap.find(componentId);
|
|
|
|
if (findIndex != this->m_componentIdToIndexMap.end()) {
|
|
|
|
//dust3dDebug << "dataChanged:" << findIndex->second.row();
|
|
|
|
emit this->dataChanged(findIndex->second, findIndex->second);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(m_document, &Document::cleanup, [this]() {
|
|
|
|
this->setListingComponentId(dust3d::Uuid());
|
|
|
|
this->reload();
|
|
|
|
});
|
|
|
|
connect(m_document, &Document::componentChildrenChanged, [this](const dust3d::Uuid &componentId) {
|
|
|
|
if (componentId != this->listingComponentId())
|
|
|
|
return;
|
|
|
|
this->reload();
|
2022-10-02 22:04:46 +00:00
|
|
|
});
|
2022-10-02 19:45:46 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 09:05:09 +00:00
|
|
|
void ComponentListModel::reload()
|
|
|
|
{
|
|
|
|
m_componentIdToIndexMap.clear();
|
|
|
|
const SkeletonComponent *listingComponent = m_document->findComponent(m_listingComponentId);
|
|
|
|
if (nullptr != listingComponent) {
|
|
|
|
for (int i = 0; i < (int)listingComponent->childrenIds.size(); ++i) {
|
|
|
|
m_componentIdToIndexMap[listingComponent->childrenIds[i]] = index(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emit this->layoutChanged();
|
|
|
|
}
|
|
|
|
|
2022-10-02 19:45:46 +00:00
|
|
|
int ComponentListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2022-10-02 22:04:46 +00:00
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
2022-10-02 19:45:46 +00:00
|
|
|
const SkeletonComponent *listingComponent = m_document->findComponent(m_listingComponentId);
|
|
|
|
if (nullptr == listingComponent)
|
|
|
|
return 0;
|
|
|
|
return (int)listingComponent->childrenIds.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ComponentListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2022-10-02 22:04:46 +00:00
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
2022-10-02 19:45:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkeletonComponent *ComponentListModel::modelIndexToComponent(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
const SkeletonComponent *listingComponent = m_document->findComponent(m_listingComponentId);
|
|
|
|
if (nullptr == listingComponent)
|
|
|
|
return nullptr;
|
|
|
|
if (index.row() >= (int)listingComponent->childrenIds.size()) {
|
|
|
|
dust3dDebug << "Component list row is out of range, size:" << listingComponent->childrenIds.size() << "row:" << index.row();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto &componentId = listingComponent->childrenIds[index.row()];
|
|
|
|
const SkeletonComponent *component = m_document->findComponent(componentId);
|
|
|
|
if (nullptr == component) {
|
|
|
|
dust3dDebug << "Component not found:" << componentId.toString();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2022-10-04 11:45:21 +00:00
|
|
|
const dust3d::Uuid ComponentListModel::modelIndexToComponentId(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
const SkeletonComponent *listingComponent = m_document->findComponent(m_listingComponentId);
|
|
|
|
if (nullptr == listingComponent)
|
|
|
|
return dust3d::Uuid();
|
|
|
|
if (index.row() >= (int)listingComponent->childrenIds.size()) {
|
|
|
|
dust3dDebug << "Component list row is out of range, size:" << listingComponent->childrenIds.size() << "row:" << index.row();
|
|
|
|
return dust3d::Uuid();
|
|
|
|
}
|
|
|
|
const auto &componentId = listingComponent->childrenIds[index.row()];
|
|
|
|
return componentId;
|
|
|
|
}
|
|
|
|
|
2022-10-02 19:45:46 +00:00
|
|
|
QVariant ComponentListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
switch (role) {
|
|
|
|
case Qt::ToolTipRole: {
|
|
|
|
const SkeletonComponent *component = modelIndexToComponent(index);
|
|
|
|
if (nullptr != component) {
|
|
|
|
return component->name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DecorationRole: {
|
|
|
|
const SkeletonComponent *component = modelIndexToComponent(index);
|
|
|
|
if (nullptr != component) {
|
2022-10-02 22:04:46 +00:00
|
|
|
return component->previewPixmap;
|
2022-10-02 19:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ComponentListModel::setListingComponentId(const dust3d::Uuid &componentId)
|
|
|
|
{
|
|
|
|
if (m_listingComponentId == componentId)
|
|
|
|
return;
|
|
|
|
m_listingComponentId = componentId;
|
2022-10-06 09:05:09 +00:00
|
|
|
reload();
|
2022-10-04 09:50:39 +00:00
|
|
|
emit listingComponentChanged(m_listingComponentId);
|
2022-10-02 19:45:46 +00:00
|
|
|
}
|
2022-10-04 09:50:39 +00:00
|
|
|
|
|
|
|
const dust3d::Uuid ComponentListModel::listingComponentId() const
|
|
|
|
{
|
|
|
|
return m_listingComponentId;
|
|
|
|
}
|