dust3d/application/sources/bone_manage_widget.cc

133 lines
4.7 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-11-11 12:06:06 +00:00
connect(m_propertyButton, &QPushButton::clicked, this, &BoneManageWidget::showSelectedBoneProperties);
connect(this, &BoneManageWidget::groupOperationAdded, m_document, &Document::saveSnapshot);
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());
connect(propertyWidget, &BonePropertyWidget::pickBoneJoints, this, [this]() {
if (nullptr == this->m_propertyMenu)
return;
this->m_document->setEditMode(Document::EditMode::Pick);
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);
}
void BoneManageWidget::showContextMenu(const QPoint& pos)
{
auto selectedBoneIds = m_bonePreviewGridWidget->getSelectedBoneIds();
if (selectedBoneIds.empty())
return;
QMenu contextMenu(this);
contextMenu.exec(mapToGlobal(pos));
}