2018-10-09 02:19:12 +00:00
|
|
|
#include <QVBoxLayout>
|
2021-11-18 14:58:01 +00:00
|
|
|
#include "material_widget.h"
|
2020-12-19 02:29:24 +00:00
|
|
|
#include "document.h"
|
2018-10-09 02:19:12 +00:00
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
MaterialWidget::MaterialWidget(const Document *document, dust3d::Uuid materialId) :
|
2018-10-09 02:19:12 +00:00
|
|
|
m_materialId(materialId),
|
|
|
|
m_document(document)
|
|
|
|
{
|
|
|
|
setObjectName("MaterialFrame");
|
|
|
|
|
|
|
|
m_previewWidget = new ModelWidget(this);
|
2018-10-25 15:28:10 +00:00
|
|
|
m_previewWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
|
2018-10-09 02:19:12 +00:00
|
|
|
m_previewWidget->setFixedSize(Theme::materialPreviewImageSize, Theme::materialPreviewImageSize);
|
|
|
|
m_previewWidget->enableMove(false);
|
|
|
|
m_previewWidget->enableZoom(false);
|
|
|
|
|
|
|
|
m_nameLabel = new QLabel;
|
|
|
|
m_nameLabel->setAlignment(Qt::AlignCenter);
|
|
|
|
m_nameLabel->setStyleSheet("background: qlineargradient(x1:0.5 y1:-15.5, x2:0.5 y2:1, stop:0 " + Theme::white.name() + ", stop:1 #252525);");
|
|
|
|
|
|
|
|
QFont nameFont;
|
|
|
|
nameFont.setWeight(QFont::Light);
|
|
|
|
nameFont.setBold(false);
|
|
|
|
m_nameLabel->setFont(nameFont);
|
|
|
|
|
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
|
|
|
mainLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
mainLayout->addStretch();
|
|
|
|
mainLayout->addWidget(m_nameLabel);
|
|
|
|
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
|
|
|
setFixedSize(Theme::materialPreviewImageSize, MaterialWidget::preferredHeight());
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
connect(document, &Document::materialNameChanged, this, &MaterialWidget::updateName);
|
|
|
|
connect(document, &Document::materialPreviewChanged, this, &MaterialWidget::updatePreview);
|
2018-10-09 02:19:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialWidget::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
m_previewWidget->move((width() - Theme::materialPreviewImageSize) / 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int MaterialWidget::preferredHeight()
|
|
|
|
{
|
|
|
|
return Theme::materialPreviewImageSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialWidget::reload()
|
|
|
|
{
|
2019-05-19 03:21:38 +00:00
|
|
|
updatePreview(m_materialId);
|
|
|
|
updateName(m_materialId);
|
2018-10-09 02:19:12 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
void MaterialWidget::updatePreview(dust3d::Uuid materialId)
|
2018-10-09 02:19:12 +00:00
|
|
|
{
|
2019-05-19 03:21:38 +00:00
|
|
|
if (materialId != m_materialId)
|
|
|
|
return;
|
2018-10-25 00:19:38 +00:00
|
|
|
const Material *material = m_document->findMaterial(m_materialId);
|
2018-10-09 02:19:12 +00:00
|
|
|
if (!material) {
|
|
|
|
qDebug() << "Material not found:" << m_materialId;
|
|
|
|
return;
|
|
|
|
}
|
2022-09-23 15:54:49 +00:00
|
|
|
ModelMesh *previewMesh = material->takePreviewMesh();
|
2018-10-09 02:19:12 +00:00
|
|
|
m_previewWidget->updateMesh(previewMesh);
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:58:01 +00:00
|
|
|
void MaterialWidget::updateName(dust3d::Uuid materialId)
|
2018-10-09 02:19:12 +00:00
|
|
|
{
|
2019-05-19 03:21:38 +00:00
|
|
|
if (materialId != m_materialId)
|
|
|
|
return;
|
2018-10-25 00:19:38 +00:00
|
|
|
const Material *material = m_document->findMaterial(m_materialId);
|
2018-10-09 02:19:12 +00:00
|
|
|
if (!material) {
|
|
|
|
qDebug() << "Material not found:" << m_materialId;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_nameLabel->setText(material->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialWidget::updateCheckedState(bool checked)
|
|
|
|
{
|
|
|
|
if (checked)
|
|
|
|
setStyleSheet("#MaterialFrame {border: 1px solid " + Theme::red.name() + ";}");
|
|
|
|
else
|
|
|
|
setStyleSheet("#MaterialFrame {border: 1px solid transparent;}");
|
|
|
|
}
|
|
|
|
|
|
|
|
ModelWidget *MaterialWidget::previewWidget()
|
|
|
|
{
|
|
|
|
return m_previewWidget;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MaterialWidget::mouseDoubleClickEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QFrame::mouseDoubleClickEvent(event);
|
|
|
|
emit modifyMaterial(m_materialId);
|
|
|
|
}
|