dust3d/application/sources/bone_manage_widget.cc

156 lines
5.6 KiB
C++
Raw Normal View History

2022-11-11 12:06:06 +00:00
#include "bone_manage_widget.h"
#include "bone_list_model.h"
#include "bone_preview_grid_widget.h"
#include "bone_property_widget.h"
#include "document.h"
#include "theme.h"
#include <QVBoxLayout>
#include <QWidgetAction>
BoneManageWidget::BoneManageWidget(Document* document, QWidget* parent)
: QWidget(parent)
, m_document(document)
{
setContextMenuPolicy(Qt::CustomContextMenu);
QHBoxLayout* toolsLayout = new QHBoxLayout;
toolsLayout->setSpacing(0);
toolsLayout->setMargin(0);
setStyleSheet("QPushButton:disabled {border: 0; color: " + Theme::gray.name() + "}");
auto createButton = [](QChar icon, const QString& title) {
QPushButton* button = new QPushButton(icon);
Theme::initIconButton(button);
button->setToolTip(title);
return button;
};
2022-11-11 15:31:20 +00:00
m_addButton = createButton(QChar(fa::plus), tr("Add new bone"));
2022-11-11 12:06:06 +00:00
m_selectButton = createButton(QChar(fa::objectgroup), tr("Select them on canvas"));
m_propertyButton = createButton(QChar(fa::sliders), tr("Configure properties"));
2022-11-11 15:31:20 +00:00
toolsLayout->addWidget(m_addButton);
2022-11-11 12:06:06 +00:00
toolsLayout->addWidget(m_selectButton);
toolsLayout->addWidget(m_propertyButton);
toolsLayout->addStretch();
QWidget* toolsWidget = new QWidget();
toolsWidget->setObjectName("tools");
toolsWidget->setStyleSheet("QWidget#tools {background: qlineargradient( x1:0 y1:0, x2:1 y2:0, stop:0 transparent, stop:0.5 " + Theme::black.name() + ", stop:1 transparent)};");
toolsWidget->setLayout(toolsLayout);
m_bonePreviewGridWidget = new BonePreviewGridWidget(document);
connect(m_bonePreviewGridWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &BoneManageWidget::updateToolButtons);
connect(m_bonePreviewGridWidget, &BonePreviewGridWidget::unselectAllOnCanvas, this, &BoneManageWidget::unselectAllOnCanvas);
connect(m_bonePreviewGridWidget, &BonePreviewGridWidget::selectNodeOnCanvas, this, &BoneManageWidget::selectNodeOnCanvas);
2022-11-11 15:31:20 +00:00
connect(m_addButton, &QPushButton::clicked, this, [this]() {
dust3d::Uuid boneId = dust3d::Uuid::createUuid();
this->m_document->addBone(boneId);
this->m_document->saveSnapshot();
});
2022-12-05 08:42:22 +00:00
connect(m_selectButton, &QPushButton::clicked, [this]() {
for (const auto& boneId : this->m_bonePreviewGridWidget->getSelectedBoneIds()) {
for (const auto& nodeIt : m_document->nodeMap) {
if (nodeIt.second.boneIds.end() == nodeIt.second.boneIds.find(boneId))
continue;
emit this->selectNodeOnCanvas(nodeIt.first);
}
}
});
2022-11-11 12:06:06 +00:00
connect(m_propertyButton, &QPushButton::clicked, this, &BoneManageWidget::showSelectedBoneProperties);
connect(this, &BoneManageWidget::groupOperationAdded, m_document, &Document::saveSnapshot);
2022-12-10 13:51:17 +00:00
connect(this, &BoneManageWidget::removeBone, m_document, &Document::removeBone);
2022-11-11 12:06:06 +00:00
connect(this, &BoneManageWidget::customContextMenuRequested, this, &BoneManageWidget::showContextMenu);
QVBoxLayout* mainLayout = new QVBoxLayout;
mainLayout->addWidget(toolsWidget);
mainLayout->addWidget(m_bonePreviewGridWidget);
setLayout(mainLayout);
updateToolButtons();
}
void BoneManageWidget::showSelectedBoneProperties()
{
auto boneIds = m_bonePreviewGridWidget->getSelectedBoneIds();
if (boneIds.empty())
return;
auto* propertyWidget = new BonePropertyWidget(m_document, boneIds);
2022-11-16 10:58:13 +00:00
m_propertyMenu = std::make_unique<QMenu>(this->parentWidget());
2022-11-29 10:32:19 +00:00
connect(propertyWidget, &BonePropertyWidget::pickBoneJoints, this, [this](const dust3d::Uuid& boneId, size_t joints) {
2022-11-16 10:58:13 +00:00
if (nullptr == this->m_propertyMenu)
return;
2022-11-29 10:32:19 +00:00
this->m_document->startBoneJointsPicking(boneId, joints);
2022-11-16 10:58:13 +00:00
this->m_propertyMenu->close();
});
QWidgetAction* widgetAction = new QWidgetAction(m_propertyMenu.get());
2022-11-11 12:06:06 +00:00
widgetAction->setDefaultWidget(propertyWidget);
2022-11-16 10:58:13 +00:00
m_propertyMenu->addAction(widgetAction);
2022-11-11 12:06:06 +00:00
auto x = mapToGlobal(QPoint(0, 0)).x();
if (x <= 0)
x = QCursor::pos().x();
2022-11-16 10:58:13 +00:00
m_propertyMenu->exec(QPoint(
2022-11-11 12:06:06 +00:00
x - propertyWidget->width(),
QCursor::pos().y()));
}
void BoneManageWidget::selectBoneByBoneId(const dust3d::Uuid& boneId)
{
QModelIndex index = m_bonePreviewGridWidget->boneListModel()->boneIdToIndex(boneId);
if (index.isValid()) {
m_bonePreviewGridWidget->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
m_bonePreviewGridWidget->scrollTo(index);
return;
}
dust3dDebug << "Unable to select bone:" << boneId.toString();
}
void BoneManageWidget::updateToolButtons()
{
auto selectedBones = m_bonePreviewGridWidget->getSelectedBones();
bool enableSelectButton = false;
bool enablePropertyButton = false;
for (const auto& bone : selectedBones) {
enablePropertyButton = true;
enableSelectButton = true;
}
m_selectButton->setEnabled(enableSelectButton);
m_propertyButton->setEnabled(enablePropertyButton);
}
2022-12-10 13:51:17 +00:00
void BoneManageWidget::removeSelectedBones()
{
auto selectedBoneIds = m_bonePreviewGridWidget->getSelectedBoneIds();
for (const auto& boneId : selectedBoneIds)
emit removeBone(boneId);
emit groupOperationAdded();
}
2022-11-11 12:06:06 +00:00
void BoneManageWidget::showContextMenu(const QPoint& pos)
{
auto selectedBoneIds = m_bonePreviewGridWidget->getSelectedBoneIds();
if (selectedBoneIds.empty())
return;
QMenu contextMenu(this);
2022-12-10 13:51:17 +00:00
QAction deleteAction(tr("Delete"), this);
connect(&deleteAction, &QAction::triggered, this, &BoneManageWidget::removeSelectedBones);
contextMenu.addAction(&deleteAction);
2022-11-11 12:06:06 +00:00
contextMenu.exec(mapToGlobal(pos));
}