From 3904ff0b61ad2d86ab2dab5198a3dfafc460b719 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Sun, 19 Mar 2023 15:33:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=A0=E9=99=A4=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E6=A3=80=E6=B5=8B=20|=20#9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/ElementPoolWidget.cpp | 1 + .../src/Editor/GraphicElement.h | 1 + .../src/Editor/LayerWrapper.cpp | 45 +++++++++++++++++++ .../src/Editor/LayerWrapper.h | 6 ++- .../src/Editor/RightBar/LayerTreeWidget.cpp | 14 +++--- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/ElementPoolWidget.cpp b/ArchitectureColoredPainting/src/Editor/ElementPoolWidget.cpp index 17ec55a..ff166f0 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementPoolWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/ElementPoolWidget.cpp @@ -120,6 +120,7 @@ void ElementPoolWidget::popMenu(const QPoint& pos) this->elementManager->removeElement(currentElement); refresh(); }); + menu->actions().last()->setDisabled(currentElement->referencedCount > 0); } else { diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index 377b9e5..8ce9bf2 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -20,6 +20,7 @@ class ComposedPainterPath; class GraphicElement { public: + size_t referencedCount = 0; Renderer::ElementRenderer *renderer; QString name = ""; int index; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index f131f27..770c45d 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -79,6 +79,14 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager* elementMana styles(LayerStyleContainer::fromJson(wrappedElement->isClosed(), json.value("styles").toArray())) { qDebug() << json.value("name").toString() << " " << this; + if(wrappedElement != nullptr) + wrappedElement->referencedCount++; +} + +LeafLayerWrapper::~LeafLayerWrapper() +{ + if (wrappedElement != nullptr) + wrappedElement->referencedCount--; } void LayerWrapper::SimpleProperty::apply(PixelPath&cache) @@ -368,4 +376,41 @@ void FolderLayerWrapper::refreshTreeItem() this->qTreeWidgetItem->setText(1, "<< " + ele->name); this->qTreeWidgetItem->setTextColor(1, Qt::darkGreen); } + if (this->referencedCount() > 0) + { + this->qTreeWidgetItem->setToolTip(0,QString::fromLocal8Bit("子树被引用计数:") + QString::number(this->referencedCount(true)) + "\n" + + QString::fromLocal8Bit("当前节点被引用计数:") + QString::number(this->getReferencedBy() != -1)); + } + else + { + this->qTreeWidgetItem->setToolTip(0, ""); + } } + +size_t LayerWrapper::referencedCount(bool excludeSelf) const +{ + return 0; +} + +size_t FolderLayerWrapper::referencedCount(bool excludeSelf) const +{ + size_t count = 0; + for (auto& child : children) + count += child->referencedCount(); + if (!excludeSelf && this->getReferencedBy() != -1) + count++; + return count; +} + +bool LayerWrapper::deleteable(bool excludeSubTree) const +{ + return true; +} + +bool FolderLayerWrapper::deleteable(bool excludeSubTree) const +{ + if (excludeSubTree) + return this->getReferencedBy() == -1; + else + return this->referencedCount() == 0; +} \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 3b82b86..b62d685 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -71,6 +71,8 @@ class LayerWrapper virtual void collectUpReachable(std::set& reachable); virtual void collectDownReachable(std::set& reachable); virtual void refreshTreeItem(); + virtual size_t referencedCount(bool excludeSelf = false) const; + virtual bool deleteable(bool excludeSubTree = false) const; }; class FolderLayerWrapper : public LayerWrapper @@ -96,6 +98,8 @@ class FolderLayerWrapper : public LayerWrapper void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override; void collectDownReachable(std::set& reachable) override; void refreshTreeItem() override; + size_t referencedCount(bool excludeSelf = false) const override; + bool deleteable(bool excludeSubTree = false) const override; }; class LeafLayerWrapper : public LayerWrapper @@ -105,7 +109,7 @@ class LeafLayerWrapper : public LayerWrapper LayerStyleContainer styles; public: - ~LeafLayerWrapper() = default; + ~LeafLayerWrapper(); void refresh(LayerWrapper* layer = nullptr) override; LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index 69a67b9..dba5943 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -78,16 +78,20 @@ void LayerTreeWidget::popMenu(const QPoint &pos) emit requireRefreshPreview(); emit requireRefreshElementWidget(); }); + menu.actions().last()->setEnabled(layer->deleteable()); menu.addAction(QString::fromLocal8Bit("重命名"), this, &LayerTreeWidget::onRenameEvent); if(typeid(*layer) == typeid(FolderLayerWrapper)) + { menu.addAction(QString::fromLocal8Bit("删除(保留子节点)"), this, [this]() { auto layer = this->selectedItem->data(0, Qt::UserRole).value(); - layer->delSelf(); - layer->getParent()->removeChild(layer); - this->refresh(); - emit requireRefreshPreview(); - emit requireRefreshElementWidget(); + layer->delSelf(); + layer->getParent()->removeChild(layer); + this->refresh(); + emit requireRefreshPreview(); + emit requireRefreshElementWidget(); }); + menu.actions().last()->setEnabled(layer->deleteable(true)); + } } if (typeid(*layer) == typeid(FolderLayerWrapper) && ((FolderLayerWrapper*)layer)->getReferencedBy() == -1) { menu.addAction(QString::fromLocal8Bit("创建组合元素"), this, [this]() {