2022-10-03 00:06:25 +00:00
|
|
|
#include "part_manage_widget.h"
|
2022-10-18 09:35:04 +00:00
|
|
|
#include "component_list_model.h"
|
2022-10-03 00:06:25 +00:00
|
|
|
#include "component_preview_grid_widget.h"
|
2022-10-08 01:05:53 +00:00
|
|
|
#include "component_property_widget.h"
|
2022-10-04 09:50:39 +00:00
|
|
|
#include "document.h"
|
2022-10-18 09:35:04 +00:00
|
|
|
#include "theme.h"
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QWidgetAction>
|
2022-10-03 00:06:25 +00:00
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
PartManageWidget::PartManageWidget(Document* document, QWidget* parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
, m_document(document)
|
2022-10-03 00:06:25 +00:00
|
|
|
{
|
2022-10-08 05:03:26 +00:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
QHBoxLayout* toolsLayout = new QHBoxLayout;
|
2022-10-03 00:06:25 +00:00
|
|
|
toolsLayout->setSpacing(0);
|
|
|
|
toolsLayout->setMargin(0);
|
|
|
|
|
2022-10-04 09:50:39 +00:00
|
|
|
setStyleSheet("QPushButton:disabled {border: 0; color: " + Theme::gray.name() + "}");
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
auto createButton = [](QChar icon, const QString& title) {
|
|
|
|
QPushButton* button = new QPushButton(icon);
|
2022-10-14 11:11:41 +00:00
|
|
|
Theme::initIconButton(button);
|
2022-10-08 01:05:53 +00:00
|
|
|
button->setToolTip(title);
|
2022-10-03 00:18:13 +00:00
|
|
|
return button;
|
|
|
|
};
|
|
|
|
|
2022-10-08 01:05:53 +00:00
|
|
|
m_levelUpButton = createButton(QChar(fa::levelup), tr("Go level up"));
|
|
|
|
m_selectButton = createButton(QChar(fa::objectgroup), tr("Select them on canvas"));
|
|
|
|
m_lockButton = createButton(QChar(fa::lock), tr("Lock them on canvas"));
|
|
|
|
m_unlockButton = createButton(QChar(fa::unlock), tr("Unlock them on canvas"));
|
|
|
|
m_showButton = createButton(QChar(fa::eye), tr("Show them on canvas"));
|
|
|
|
m_hideButton = createButton(QChar(fa::eyeslash), tr("Hide them on canvas"));
|
|
|
|
m_unlinkButton = createButton(QChar(fa::unlink), tr("Exclude them from result generation"));
|
|
|
|
m_linkButton = createButton(QChar(fa::link), tr("Include them in result generation"));
|
|
|
|
m_propertyButton = createButton(QChar(fa::sliders), tr("Configure properties"));
|
2022-10-03 00:06:25 +00:00
|
|
|
|
2022-10-04 09:50:39 +00:00
|
|
|
toolsLayout->addWidget(m_levelUpButton);
|
2022-10-04 12:17:39 +00:00
|
|
|
toolsLayout->addWidget(m_selectButton);
|
2022-10-04 09:50:39 +00:00
|
|
|
toolsLayout->addWidget(m_hideButton);
|
|
|
|
toolsLayout->addWidget(m_showButton);
|
|
|
|
toolsLayout->addWidget(m_lockButton);
|
|
|
|
toolsLayout->addWidget(m_unlockButton);
|
|
|
|
toolsLayout->addWidget(m_unlinkButton);
|
|
|
|
toolsLayout->addWidget(m_linkButton);
|
2022-10-08 01:05:53 +00:00
|
|
|
toolsLayout->addWidget(m_propertyButton);
|
2022-10-03 00:06:25 +00:00
|
|
|
toolsLayout->addStretch();
|
|
|
|
|
2022-11-11 11:03:23 +00:00
|
|
|
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);
|
|
|
|
|
2022-10-04 09:50:39 +00:00
|
|
|
m_componentPreviewGridWidget = new ComponentPreviewGridWidget(document);
|
|
|
|
|
|
|
|
connect(m_componentPreviewGridWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &PartManageWidget::updateToolButtons);
|
|
|
|
connect(m_componentPreviewGridWidget->componentListModel(), &ComponentListModel::listingComponentChanged, this, &PartManageWidget::updateLevelUpButton);
|
2022-10-07 21:39:38 +00:00
|
|
|
connect(m_componentPreviewGridWidget, &ComponentPreviewGridWidget::unselectAllOnCanvas, this, &PartManageWidget::unselectAllOnCanvas);
|
|
|
|
connect(m_componentPreviewGridWidget, &ComponentPreviewGridWidget::selectPartOnCanvas, this, &PartManageWidget::selectPartOnCanvas);
|
2022-10-04 09:50:39 +00:00
|
|
|
|
2022-10-08 01:05:53 +00:00
|
|
|
//connect(m_componentPreviewGridWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &PartManageWidget::showSelectedComponentProperties);
|
|
|
|
|
2022-10-04 09:50:39 +00:00
|
|
|
connect(m_levelUpButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
const auto& parent = m_document->findComponentParent(this->m_componentPreviewGridWidget->componentListModel()->listingComponentId());
|
2022-10-04 09:50:39 +00:00
|
|
|
if (nullptr == parent)
|
|
|
|
return;
|
|
|
|
this->m_componentPreviewGridWidget->componentListModel()->setListingComponentId(parent->id);
|
|
|
|
});
|
2022-10-03 00:06:25 +00:00
|
|
|
|
2022-10-04 11:45:21 +00:00
|
|
|
connect(m_hideButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : this->m_componentPreviewGridWidget->getSelectedPartIds())
|
2022-10-04 11:45:21 +00:00
|
|
|
this->m_document->setPartVisibleState(partId, false);
|
|
|
|
this->m_document->saveSnapshot();
|
|
|
|
});
|
|
|
|
connect(m_showButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : this->m_componentPreviewGridWidget->getSelectedPartIds())
|
2022-10-04 11:45:21 +00:00
|
|
|
this->m_document->setPartVisibleState(partId, true);
|
|
|
|
this->m_document->saveSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(m_unlockButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : this->m_componentPreviewGridWidget->getSelectedPartIds())
|
2022-10-04 11:45:21 +00:00
|
|
|
this->m_document->setPartLockState(partId, false);
|
|
|
|
this->m_document->saveSnapshot();
|
|
|
|
});
|
|
|
|
connect(m_lockButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : this->m_componentPreviewGridWidget->getSelectedPartIds())
|
2022-10-04 11:45:21 +00:00
|
|
|
this->m_document->setPartLockState(partId, true);
|
|
|
|
this->m_document->saveSnapshot();
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(m_unlinkButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : this->m_componentPreviewGridWidget->getSelectedPartIds())
|
2022-10-04 11:45:21 +00:00
|
|
|
this->m_document->setPartDisableState(partId, true);
|
|
|
|
this->m_document->saveSnapshot();
|
|
|
|
});
|
|
|
|
connect(m_linkButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : this->m_componentPreviewGridWidget->getSelectedPartIds())
|
2022-10-04 11:45:21 +00:00
|
|
|
this->m_document->setPartDisableState(partId, false);
|
|
|
|
this->m_document->saveSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-10-04 12:17:39 +00:00
|
|
|
connect(m_selectButton, &QPushButton::clicked, [this]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& componentId : this->m_componentPreviewGridWidget->getSelectedComponentIds()) {
|
2022-10-04 12:17:39 +00:00
|
|
|
std::vector<dust3d::Uuid> partIds;
|
|
|
|
this->m_document->collectComponentDescendantParts(componentId, partIds);
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& partId : partIds)
|
2022-10-04 12:17:39 +00:00
|
|
|
emit this->selectPartOnCanvas(partId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-08 01:05:53 +00:00
|
|
|
connect(m_propertyButton, &QPushButton::clicked, this, &PartManageWidget::showSelectedComponentProperties);
|
|
|
|
|
2022-10-04 11:45:21 +00:00
|
|
|
connect(m_document, &Document::partLockStateChanged, this, &PartManageWidget::updateToolButtons);
|
|
|
|
connect(m_document, &Document::partVisibleStateChanged, this, &PartManageWidget::updateToolButtons);
|
|
|
|
connect(m_document, &Document::partDisableStateChanged, this, &PartManageWidget::updateToolButtons);
|
|
|
|
connect(m_document, &Document::componentChildrenChanged, this, &PartManageWidget::updateToolButtons);
|
|
|
|
|
2022-10-10 11:05:16 +00:00
|
|
|
connect(this, &PartManageWidget::groupComponents, m_document, &Document::groupComponents);
|
|
|
|
connect(this, &PartManageWidget::ungroupComponent, m_document, &Document::ungroupComponent);
|
2022-10-08 05:03:26 +00:00
|
|
|
connect(this, &PartManageWidget::groupOperationAdded, m_document, &Document::saveSnapshot);
|
|
|
|
|
|
|
|
connect(this, &PartManageWidget::customContextMenuRequested, this, &PartManageWidget::showContextMenu);
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
QVBoxLayout* mainLayout = new QVBoxLayout;
|
2022-11-11 11:03:23 +00:00
|
|
|
mainLayout->addWidget(toolsWidget);
|
2022-10-04 09:50:39 +00:00
|
|
|
mainLayout->addWidget(m_componentPreviewGridWidget);
|
2022-10-03 00:06:25 +00:00
|
|
|
|
|
|
|
setLayout(mainLayout);
|
2022-10-04 09:50:39 +00:00
|
|
|
|
|
|
|
updateToolButtons();
|
|
|
|
updateLevelUpButton();
|
|
|
|
}
|
|
|
|
|
2022-10-08 01:05:53 +00:00
|
|
|
void PartManageWidget::showSelectedComponentProperties()
|
|
|
|
{
|
|
|
|
auto componentIds = m_componentPreviewGridWidget->getSelectedComponentIds();
|
|
|
|
if (componentIds.empty())
|
|
|
|
return;
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
auto* propertyWidget = new ComponentPropertyWidget(m_document, componentIds);
|
2022-10-08 01:05:53 +00:00
|
|
|
|
2022-10-08 03:46:57 +00:00
|
|
|
auto menu = std::make_unique<QMenu>(this->parentWidget());
|
2022-10-18 09:35:04 +00:00
|
|
|
QWidgetAction* widgetAction = new QWidgetAction(menu.get());
|
2022-10-08 01:05:53 +00:00
|
|
|
widgetAction->setDefaultWidget(propertyWidget);
|
|
|
|
menu->addAction(widgetAction);
|
|
|
|
|
|
|
|
auto x = mapToGlobal(QPoint(0, 0)).x();
|
|
|
|
if (x <= 0)
|
|
|
|
x = QCursor::pos().x();
|
|
|
|
menu->exec(QPoint(
|
2022-10-18 09:35:04 +00:00
|
|
|
x - propertyWidget->width(),
|
|
|
|
QCursor::pos().y()));
|
2022-10-08 01:05:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
void PartManageWidget::selectComponentByPartId(const dust3d::Uuid& partId)
|
2022-10-06 11:16:47 +00:00
|
|
|
{
|
2022-10-18 09:35:04 +00:00
|
|
|
const auto& part = m_document->findPart(partId);
|
2022-10-06 11:16:47 +00:00
|
|
|
if (nullptr == part)
|
|
|
|
return;
|
|
|
|
auto componentId = part->componentId;
|
|
|
|
QModelIndex index = m_componentPreviewGridWidget->componentListModel()->componentIdToIndex(componentId);
|
|
|
|
if (index.isValid()) {
|
|
|
|
m_componentPreviewGridWidget->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
|
2022-10-06 12:18:03 +00:00
|
|
|
m_componentPreviewGridWidget->scrollTo(index);
|
2022-10-06 11:16:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-10-18 09:35:04 +00:00
|
|
|
const auto& component = m_document->findComponent(componentId);
|
2022-10-06 11:16:47 +00:00
|
|
|
if (nullptr == component)
|
|
|
|
return;
|
|
|
|
m_componentPreviewGridWidget->componentListModel()->setListingComponentId(component->parentId);
|
|
|
|
index = m_componentPreviewGridWidget->componentListModel()->componentIdToIndex(componentId);
|
|
|
|
if (index.isValid()) {
|
|
|
|
m_componentPreviewGridWidget->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
|
2022-10-06 12:18:03 +00:00
|
|
|
m_componentPreviewGridWidget->scrollTo(index);
|
2022-10-06 11:16:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dust3dDebug << "Unable to select component:" << componentId.toString();
|
|
|
|
}
|
|
|
|
|
2022-10-04 09:50:39 +00:00
|
|
|
void PartManageWidget::updateLevelUpButton()
|
|
|
|
{
|
2022-10-18 09:35:04 +00:00
|
|
|
const auto& parent = m_document->findComponentParent(m_componentPreviewGridWidget->componentListModel()->listingComponentId());
|
2022-10-04 09:50:39 +00:00
|
|
|
m_levelUpButton->setEnabled(nullptr != parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PartManageWidget::updateToolButtons()
|
|
|
|
{
|
|
|
|
auto selectedComponents = m_componentPreviewGridWidget->getSelectedComponents();
|
|
|
|
bool enableHideButton = false;
|
2022-10-04 12:17:39 +00:00
|
|
|
bool enableSelectButton = false;
|
2022-10-04 09:50:39 +00:00
|
|
|
bool enableShowButton = false;
|
|
|
|
bool enableLockButton = false;
|
|
|
|
bool enableUnlockButton = false;
|
|
|
|
bool enableUnlinkButton = false;
|
|
|
|
bool enableLinkButton = false;
|
2022-10-08 01:05:53 +00:00
|
|
|
bool enablePropertyButton = false;
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& component : selectedComponents) {
|
2022-10-08 01:05:53 +00:00
|
|
|
enablePropertyButton = true;
|
2022-10-04 12:17:39 +00:00
|
|
|
enableSelectButton = true;
|
2022-10-04 09:50:39 +00:00
|
|
|
if (component->linkToPartId.isNull()) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-10-18 09:35:04 +00:00
|
|
|
const auto& part = m_document->findPart(component->linkToPartId);
|
2022-10-04 09:50:39 +00:00
|
|
|
if (part->visible) {
|
|
|
|
enableHideButton = true;
|
|
|
|
} else {
|
|
|
|
enableShowButton = true;
|
|
|
|
}
|
|
|
|
if (part->locked) {
|
|
|
|
enableUnlockButton = true;
|
|
|
|
} else {
|
|
|
|
enableLockButton = true;
|
|
|
|
}
|
|
|
|
if (part->disabled) {
|
|
|
|
enableLinkButton = true;
|
|
|
|
} else {
|
|
|
|
enableUnlinkButton = true;
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 12:17:39 +00:00
|
|
|
m_selectButton->setEnabled(enableSelectButton);
|
2022-10-04 09:50:39 +00:00
|
|
|
m_hideButton->setEnabled(enableHideButton);
|
|
|
|
m_showButton->setEnabled(enableShowButton);
|
|
|
|
m_lockButton->setEnabled(enableLockButton);
|
|
|
|
m_unlockButton->setEnabled(enableUnlockButton);
|
|
|
|
m_unlinkButton->setEnabled(enableUnlinkButton);
|
|
|
|
m_linkButton->setEnabled(enableLinkButton);
|
2022-10-08 01:05:53 +00:00
|
|
|
m_propertyButton->setEnabled(enablePropertyButton);
|
2022-10-03 00:06:25 +00:00
|
|
|
}
|
2022-10-08 05:03:26 +00:00
|
|
|
|
2022-10-10 11:05:16 +00:00
|
|
|
bool PartManageWidget::hasSelectedGroupedComponent()
|
|
|
|
{
|
|
|
|
auto selectedComponents = m_componentPreviewGridWidget->getSelectedComponents();
|
2022-10-18 09:35:04 +00:00
|
|
|
for (auto& component : selectedComponents) {
|
2022-10-10 11:05:16 +00:00
|
|
|
if (!component->childrenIds.empty())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:35:04 +00:00
|
|
|
void PartManageWidget::showContextMenu(const QPoint& pos)
|
2022-10-08 05:03:26 +00:00
|
|
|
{
|
|
|
|
auto selectedComponentIds = m_componentPreviewGridWidget->getSelectedComponentIds();
|
|
|
|
if (selectedComponentIds.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMenu contextMenu(this);
|
|
|
|
|
|
|
|
QAction makeGroupAction(tr("Make group"), this);
|
|
|
|
makeGroupAction.setIcon(Theme::awesome()->icon(fa::folder));
|
|
|
|
connect(&makeGroupAction, &QAction::triggered, this, [=]() {
|
|
|
|
emit this->groupComponents(selectedComponentIds);
|
|
|
|
emit this->groupOperationAdded();
|
|
|
|
});
|
|
|
|
contextMenu.addAction(&makeGroupAction);
|
|
|
|
|
2022-10-10 11:05:16 +00:00
|
|
|
QAction ungroupAction(tr("Ungroup"), this);
|
|
|
|
if (hasSelectedGroupedComponent()) {
|
|
|
|
connect(&ungroupAction, &QAction::triggered, this, [=]() {
|
2022-10-18 09:35:04 +00:00
|
|
|
for (const auto& it : selectedComponentIds)
|
2022-10-10 11:05:16 +00:00
|
|
|
emit this->ungroupComponent(it);
|
|
|
|
emit this->groupOperationAdded();
|
|
|
|
});
|
|
|
|
contextMenu.addAction(&ungroupAction);
|
|
|
|
}
|
|
|
|
|
2022-10-08 05:03:26 +00:00
|
|
|
contextMenu.exec(mapToGlobal(pos));
|
|
|
|
}
|