Implement make group

master
Jeremy HU 2022-10-08 16:03:26 +11:00
parent 36c7729ed7
commit c729099f0d
4 changed files with 60 additions and 18 deletions

View File

@ -12,6 +12,8 @@ PartManageWidget::PartManageWidget(Document *document, QWidget *parent):
QWidget(parent), QWidget(parent),
m_document(document) m_document(document)
{ {
setContextMenuPolicy(Qt::CustomContextMenu);
QHBoxLayout *toolsLayout = new QHBoxLayout; QHBoxLayout *toolsLayout = new QHBoxLayout;
toolsLayout->setSpacing(0); toolsLayout->setSpacing(0);
toolsLayout->setMargin(0); toolsLayout->setMargin(0);
@ -111,6 +113,11 @@ PartManageWidget::PartManageWidget(Document *document, QWidget *parent):
connect(m_document, &Document::partDisableStateChanged, this, &PartManageWidget::updateToolButtons); connect(m_document, &Document::partDisableStateChanged, this, &PartManageWidget::updateToolButtons);
connect(m_document, &Document::componentChildrenChanged, this, &PartManageWidget::updateToolButtons); connect(m_document, &Document::componentChildrenChanged, this, &PartManageWidget::updateToolButtons);
connect(this, &PartManageWidget::groupComponents, m_document, &Document::createNewComponentAndMoveTheseIn);
connect(this, &PartManageWidget::groupOperationAdded, m_document, &Document::saveSnapshot);
connect(this, &PartManageWidget::customContextMenuRequested, this, &PartManageWidget::showContextMenu);
QVBoxLayout *mainLayout = new QVBoxLayout; QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(toolsLayout); mainLayout->addLayout(toolsLayout);
mainLayout->addWidget(m_componentPreviewGridWidget); mainLayout->addWidget(m_componentPreviewGridWidget);
@ -217,3 +224,23 @@ void PartManageWidget::updateToolButtons()
m_linkButton->setEnabled(enableLinkButton); m_linkButton->setEnabled(enableLinkButton);
m_propertyButton->setEnabled(enablePropertyButton); m_propertyButton->setEnabled(enablePropertyButton);
} }
void PartManageWidget::showContextMenu(const QPoint &pos)
{
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);
contextMenu.exec(mapToGlobal(pos));
}

View File

@ -15,9 +15,12 @@ class PartManageWidget: public QWidget
signals: signals:
void unselectAllOnCanvas(); void unselectAllOnCanvas();
void selectPartOnCanvas(const dust3d::Uuid &partId); void selectPartOnCanvas(const dust3d::Uuid &partId);
void groupComponents(const std::vector<dust3d::Uuid> &componentIds);
void groupOperationAdded();
public slots: public slots:
void selectComponentByPartId(const dust3d::Uuid &partId); void selectComponentByPartId(const dust3d::Uuid &partId);
void showSelectedComponentProperties(); void showSelectedComponentProperties();
void showContextMenu(const QPoint &pos);
public: public:
PartManageWidget(Document *document, QWidget *parent=nullptr); PartManageWidget(Document *document, QWidget *parent=nullptr);
private: private:

View File

@ -520,29 +520,41 @@ void SkeletonDocument::setComponentExpandState(dust3d::Uuid componentId, bool ex
emit optionsChanged(); emit optionsChanged();
} }
void SkeletonDocument::createNewComponentAndMoveThisIn(dust3d::Uuid componentId) void SkeletonDocument::createNewComponentAndMoveTheseIn(const std::vector<dust3d::Uuid> &componentIds)
{ {
auto component = componentMap.find(componentId); if (componentIds.empty())
if (component == componentMap.end()) {
return; return;
}
dust3d::Uuid newParentId;
SkeletonComponent *oldParent = (SkeletonComponent *)findComponentParent(componentId);
SkeletonComponent newParent(dust3d::Uuid::createUuid()); SkeletonComponent newParent(dust3d::Uuid::createUuid());
newParentId = newParent.id;
auto it = componentIds.begin();
SkeletonComponent *oldParent = (SkeletonComponent *)findComponentParent(*it);
auto oldParentId = oldParent->id;
oldParent->replaceChild(*it, newParentId);
for (++it; it != componentIds.end(); ++it) {
oldParent->removeChild(*it);
}
for (const auto &componentId: componentIds) {
auto component = componentMap.find(componentId);
if (component == componentMap.end()) {
continue;
}
component->second.parentId = newParentId;
newParent.addChild(componentId);
}
newParent.parentId = oldParentId;
newParent.name = tr("Group") + " " + QString::number(componentMap.size() - partMap.size() + 1); newParent.name = tr("Group") + " " + QString::number(componentMap.size() - partMap.size() + 1);
oldParent->replaceChild(componentId, newParent.id);
newParent.parentId = oldParent->id;
newParent.addChild(componentId);
auto newParentId = newParent.id;
componentMap.emplace(newParentId, std::move(newParent)); componentMap.emplace(newParentId, std::move(newParent));
component->second.parentId = newParentId; emit componentChildrenChanged(oldParentId);
emit componentChildrenChanged(oldParent->id);
emit componentAdded(newParentId); emit componentAdded(newParentId);
emit optionsChanged(); emit skeletonChanged();
} }
void SkeletonDocument::createNewChildComponent(dust3d::Uuid parentComponentId) void SkeletonDocument::createNewChildComponent(dust3d::Uuid parentComponentId)

View File

@ -678,7 +678,7 @@ public slots:
void addComponent(dust3d::Uuid parentId); void addComponent(dust3d::Uuid parentId);
void moveComponent(dust3d::Uuid componentId, dust3d::Uuid toParentId); void moveComponent(dust3d::Uuid componentId, dust3d::Uuid toParentId);
void setCurrentCanvasComponentId(dust3d::Uuid componentId); void setCurrentCanvasComponentId(dust3d::Uuid componentId);
void createNewComponentAndMoveThisIn(dust3d::Uuid componentId); void createNewComponentAndMoveTheseIn(const std::vector<dust3d::Uuid> &componentIds);
void createNewChildComponent(dust3d::Uuid parentComponentId); void createNewChildComponent(dust3d::Uuid parentComponentId);
void setComponentExpandState(dust3d::Uuid componentId, bool expanded); void setComponentExpandState(dust3d::Uuid componentId, bool expanded);
void hideOtherComponents(dust3d::Uuid componentId); void hideOtherComponents(dust3d::Uuid componentId);