添加删除逻辑检测 | #9

dev-yyq
karlis 2023-03-19 15:33:55 +08:00
parent 587c09115a
commit 3904ff0b61
5 changed files with 61 additions and 6 deletions

View File

@ -120,6 +120,7 @@ void ElementPoolWidget::popMenu(const QPoint& pos)
this->elementManager->removeElement(currentElement); this->elementManager->removeElement(currentElement);
refresh(); refresh();
}); });
menu->actions().last()->setDisabled(currentElement->referencedCount > 0);
} }
else else
{ {

View File

@ -20,6 +20,7 @@ class ComposedPainterPath;
class GraphicElement class GraphicElement
{ {
public: public:
size_t referencedCount = 0;
Renderer::ElementRenderer *renderer; Renderer::ElementRenderer *renderer;
QString name = ""; QString name = "";
int index; int index;

View File

@ -79,6 +79,14 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager* elementMana
styles(LayerStyleContainer::fromJson(wrappedElement->isClosed(), json.value("styles").toArray())) styles(LayerStyleContainer::fromJson(wrappedElement->isClosed(), json.value("styles").toArray()))
{ {
qDebug() << json.value("name").toString() << " " << this; qDebug() << json.value("name").toString() << " " << this;
if(wrappedElement != nullptr)
wrappedElement->referencedCount++;
}
LeafLayerWrapper::~LeafLayerWrapper()
{
if (wrappedElement != nullptr)
wrappedElement->referencedCount--;
} }
void LayerWrapper::SimpleProperty::apply(PixelPath&cache) void LayerWrapper::SimpleProperty::apply(PixelPath&cache)
@ -368,4 +376,41 @@ void FolderLayerWrapper::refreshTreeItem()
this->qTreeWidgetItem->setText(1, "<< " + ele->name); this->qTreeWidgetItem->setText(1, "<< " + ele->name);
this->qTreeWidgetItem->setTextColor(1, Qt::darkGreen); 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;
} }

View File

@ -71,6 +71,8 @@ class LayerWrapper
virtual void collectUpReachable(std::set<LayerWrapper*>& reachable); virtual void collectUpReachable(std::set<LayerWrapper*>& reachable);
virtual void collectDownReachable(std::set<LayerWrapper*>& reachable); virtual void collectDownReachable(std::set<LayerWrapper*>& reachable);
virtual void refreshTreeItem(); virtual void refreshTreeItem();
virtual size_t referencedCount(bool excludeSelf = false) const;
virtual bool deleteable(bool excludeSubTree = false) const;
}; };
class FolderLayerWrapper : public LayerWrapper class FolderLayerWrapper : public LayerWrapper
@ -96,6 +98,8 @@ class FolderLayerWrapper : public LayerWrapper
void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override; void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override;
void collectDownReachable(std::set<LayerWrapper*>& reachable) override; void collectDownReachable(std::set<LayerWrapper*>& reachable) override;
void refreshTreeItem() override; void refreshTreeItem() override;
size_t referencedCount(bool excludeSelf = false) const override;
bool deleteable(bool excludeSubTree = false) const override;
}; };
class LeafLayerWrapper : public LayerWrapper class LeafLayerWrapper : public LayerWrapper
@ -105,7 +109,7 @@ class LeafLayerWrapper : public LayerWrapper
LayerStyleContainer styles; LayerStyleContainer styles;
public: public:
~LeafLayerWrapper() = default; ~LeafLayerWrapper();
void refresh(LayerWrapper* layer = nullptr) override; void refresh(LayerWrapper* layer = nullptr) override;
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent);
QJsonObject toJson() const override; QJsonObject toJson() const override;

View File

@ -78,8 +78,10 @@ void LayerTreeWidget::popMenu(const QPoint &pos)
emit requireRefreshPreview(); emit requireRefreshPreview();
emit requireRefreshElementWidget(); emit requireRefreshElementWidget();
}); });
menu.actions().last()->setEnabled(layer->deleteable());
menu.addAction(QString::fromLocal8Bit("重命名"), this, &LayerTreeWidget::onRenameEvent); menu.addAction(QString::fromLocal8Bit("重命名"), this, &LayerTreeWidget::onRenameEvent);
if(typeid(*layer) == typeid(FolderLayerWrapper)) if(typeid(*layer) == typeid(FolderLayerWrapper))
{
menu.addAction(QString::fromLocal8Bit("删除(保留子节点)"), this, [this]() { menu.addAction(QString::fromLocal8Bit("删除(保留子节点)"), this, [this]() {
auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>(); auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>();
layer->delSelf(); layer->delSelf();
@ -88,6 +90,8 @@ void LayerTreeWidget::popMenu(const QPoint &pos)
emit requireRefreshPreview(); emit requireRefreshPreview();
emit requireRefreshElementWidget(); emit requireRefreshElementWidget();
}); });
menu.actions().last()->setEnabled(layer->deleteable(true));
}
} }
if (typeid(*layer) == typeid(FolderLayerWrapper) && ((FolderLayerWrapper*)layer)->getReferencedBy() == -1) { if (typeid(*layer) == typeid(FolderLayerWrapper) && ((FolderLayerWrapper*)layer)->getReferencedBy() == -1) {
menu.addAction(QString::fromLocal8Bit("创建组合元素"), this, [this]() { menu.addAction(QString::fromLocal8Bit("创建组合元素"), this, [this]() {