From 4471bb3f7983a7f5f2eb53c89f7cb174455ca9ec Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Mon, 27 Mar 2023 14:49:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E3=80=90=E6=8F=90=E5=8D=87/?= =?UTF-8?q?=E4=B8=8B=E6=94=BE=E3=80=91=E9=99=90=E5=88=B6=EF=BC=8C=E6=9A=82?= =?UTF-8?q?=E5=AE=9A=E4=B8=BApre-release=20|=20#22?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/LayerWrapper.cpp | 50 +++++++++++++++++++ .../src/Editor/LayerWrapper.h | 5 ++ .../src/Editor/RightBar/LayerTreeWidget.cpp | 12 +++-- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 22dc6e1..0f203f8 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -485,4 +485,54 @@ void FolderLayerWrapper::markNeedRefresh() LayerWrapper::markNeedRefresh(); for (auto& child : children) child->markNeedRefresh(); +} + +void LayerWrapper::collectDownUsedElements(std::set& elements, bool deep) +{ + +} + +void LeafLayerWrapper::collectDownUsedElements(std::set& elements, bool deep) +{ + elements.insert(wrappedElement); + if (deep && typeid(*wrappedElement) == typeid(GroupElement)) + { + auto ele = dynamic_cast(wrappedElement); + ele->sourceLayer->collectDownUsedElements(elements, deep); + } +} + +void FolderLayerWrapper::collectDownUsedElements(std::set& elements, bool deep) +{ + LayerWrapper::collectDownUsedElements(elements, deep); + for (auto& child : children) + child->collectDownUsedElements(elements, deep); +} + +void LayerWrapper::collectUpProvidedElements(std::set& elements) +{ + auto cPos = this; + while (cPos != nullptr) + { + if (typeid(*cPos) == typeid(FolderLayerWrapper)) + { + auto ele = elementManager->getElementById(dynamic_cast(cPos)->getReferencedBy()); + if (ele != nullptr) + elements.insert(ele); + } + cPos = cPos->parent; + } +} + +bool LayerWrapper::isLegalParent(FolderLayerWrapper* parent) +{ + std::set downUsed, upProvided; + this->collectDownUsedElements(downUsed); + parent->collectUpProvidedElements(upProvided); + for (auto& ele : downUsed) + { + if (typeid(*ele) == typeid(GroupElement) && upProvided.find(ele) != upProvided.end()) + return false; + } + return true; } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index fe3dacc..6d4bfaa 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -74,6 +74,8 @@ class LayerWrapper ~LayerWrapper() = default; virtual void collectUpReachable(std::set& reachable); virtual void collectDownReachable(std::set& reachable); + virtual void collectDownUsedElements(std::set& elements, bool deep = true); + virtual void collectUpProvidedElements(std::set& elements); virtual void refreshTreeItem(); virtual size_t referencedCount(bool excludeSelf = false) const; virtual bool deleteable(bool excludeSubTree = false) const; @@ -81,6 +83,7 @@ class LayerWrapper virtual void paintVisualBounding(QPainter* painter) const; bool canApplyStyles() const; virtual void markNeedRefresh(); + bool isLegalParent(FolderLayerWrapper* parent); }; class FolderLayerWrapper : public LayerWrapper @@ -105,6 +108,7 @@ class FolderLayerWrapper : public LayerWrapper int getReferencedBy()const; void paint(QPainter* painter, QTransform transform = QTransform(), bool force = false, LayerStyleContainer styles = LayerStyleContainer(ElementType::TYPE_ALL)) override; void collectDownReachable(std::set& reachable) override; + void collectDownUsedElements(std::set& elements, bool deep = true) override; void refreshTreeItem() override; size_t referencedCount(bool excludeSelf = false) const override; bool deleteable(bool excludeSubTree = false) const override; @@ -123,6 +127,7 @@ class LeafLayerWrapper : public LayerWrapper QJsonObject toJson() const override; void paint(QPainter* painter, QTransform transform = QTransform(), bool force = false, LayerStyleContainer styles = LayerStyleContainer(ElementType::TYPE_ALL)) override; void collectDownReachable(std::set& reachable) override; + void collectDownUsedElements(std::set& elements, bool deep = true) override; QTreeWidgetItem* getQTreeItem() override; void refreshTreeItem() override; bool referencingGroupElement() const override; diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index cc1b052..b364f02 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -214,11 +214,12 @@ void LayerTreeWidget::pushDownLayer() QAction* LayerTreeWidget::getPromoteUpAction() { - QAction* action = new QAction(QString::fromLocal8Bit("提升 (experimental)"), this); + QAction* action = new QAction(QString::fromLocal8Bit("提升"), this); QMenu* optionMenu = new QMenu(); QList optionList; std::vector layers; auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + auto currentLayer = layer; layer = layer->getParent(); while (layer != nullptr) { @@ -238,7 +239,8 @@ QAction* LayerTreeWidget::getPromoteUpAction() if (layer == nullptr) continue; QAction* option = new QAction(QString::fromLocal8Bit("提升至 ") + layer->property.name + QString::fromLocal8Bit(" 下"), this); - option->setData(QVariant::fromValue(std::pair({this->selectedItem->data(0, Qt::UserRole).value(), layer}))); + option->setEnabled(currentLayer->isLegalParent(layer)); + option->setData(QVariant::fromValue(std::pair({currentLayer, layer}))); connect(option, &QAction::triggered, this, [this, option]() { auto pair = option->data().value>(); auto layer = pair.first; @@ -255,11 +257,12 @@ QAction* LayerTreeWidget::getPromoteUpAction() QAction* LayerTreeWidget::getPromoteDownAction() { - QAction* action = new QAction(QString::fromLocal8Bit("下放 (experimental)"), this); + QAction* action = new QAction(QString::fromLocal8Bit("下放"), this); QMenu* optionMenu = new QMenu(); QList optionList; std::vector layers; auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + auto currentLayer = layer; auto parent = layer->getParent(); if (parent == nullptr) { @@ -283,7 +286,8 @@ QAction* LayerTreeWidget::getPromoteDownAction() if (layer == nullptr) continue; QAction* option = new QAction(QString::fromLocal8Bit("下放至 ") + layer->property.name + QString::fromLocal8Bit(" 中"), this); - option->setData(QVariant::fromValue(std::pair({ this->selectedItem->data(0, Qt::UserRole).value(), layer }))); + option->setEnabled(currentLayer->isLegalParent(layer)); + option->setData(QVariant::fromValue(std::pair({ currentLayer, layer }))); connect(option, &QAction::triggered, this, [this, option]() { auto pair = option->data().value>(); auto layer = pair.first;