添加了删除功能(请先删除引用后删除实例)

TaoZhang-Branch
karlis 2023-02-28 16:48:41 +08:00
parent 28ea0dd394
commit 9028ecdca0
8 changed files with 213 additions and 49 deletions

View File

@ -14,6 +14,8 @@ EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent)
connect(treeWidget, &LayerTreeWidget::displayLayerChange, this, &EditorWidget::onLayerChange);
connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshPreview, this,
&EditorWidget::triggerRefreshPreview);
connect(treeWidget, &LayerTreeWidget::requireRefreshPreview, this,
&EditorWidget::triggerRefreshPreview);
connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireSelfRefresh, layerInfoDisplayWidget, &InfoDisplayWidget::triggerSelfRefresh);
// &EditorWidget::triggerRefreshPreview);
// test
@ -32,7 +34,9 @@ EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent)
previewWindow->initialize(layerManager,QSize(jsonDoc.object().value("width").toDouble(),jsonDoc.object().value("height").toDouble()));
if (layerManager->getRoot() != nullptr)
{
treeWidget->addTopLevelItem(layerManager->getRoot()->qTreeItem);
treeWidget->root = layerManager->getRoot();
treeWidget->refresh();
treeWidget->addTopLevelItem(treeWidget->root->getQTreeItem());
}
}

View File

@ -48,7 +48,7 @@ bool LayerManager::combine() const
if (layer->getParent() != prevCommonFather)
return false;
auto newCommonFather = new FolderLayerWrapper();
newCommonFather->setParent(prevCommonFather);
newCommonFather->setParent(static_cast<FolderLayerWrapper*>(prevCommonFather));
for (auto &layer : selectedLayers)
layer->setParent(newCommonFather);
return true;

View File

@ -1,11 +1,11 @@
#include "LayerWrapper.h"
using namespace std;
void FolderLayerWrapper::addChild(LayerWrapper *child)
void FolderLayerWrapper::addChild(shared_ptr<LayerWrapper> child)
{
for (auto &chi : children)
if (chi.get() == child)
if (chi == child)
return;
children.push_back(shared_ptr<LayerWrapper>(child));
children.push_back(child);
}
void FolderLayerWrapper::removeChild(LayerWrapper *child)
@ -18,9 +18,9 @@ void FolderLayerWrapper::removeChild(LayerWrapper *child)
}
}
LayerWrapper *LayerWrapper::getParent() const
FolderLayerWrapper*LayerWrapper::getParent() const
{
return this == nullptr ? nullptr : this->parent.get();
return this == nullptr ? nullptr : this->parent;
}
PixelPath LayerWrapper::getCache()
@ -30,9 +30,9 @@ PixelPath LayerWrapper::getCache()
}
// TODO: undone
LayerWrapper::LayerWrapper(QJsonObject json, LayerWrapper *parent)
LayerWrapper::LayerWrapper(QJsonObject json, FolderLayerWrapper*parent)
{
this->parent = shared_ptr<LayerWrapper>(parent);
this->parent = parent;
auto transformJson = json.value("transform").toObject();
property.name = json.value("name").toString();
property.offset = {transformJson.value("offset").toObject().value("x").toDouble(),
@ -40,14 +40,9 @@ LayerWrapper::LayerWrapper(QJsonObject json, LayerWrapper *parent)
property.scale = {transformJson.value("scale").toObject().value("x").toDouble(),
transformJson.value("scale").toObject().value("y").toDouble()};
property.rotation = {transformJson.value("rotation").toDouble()};
qTreeItem = new QTreeWidgetItem();
qTreeItem->setText(0, property.name);
if (parent != nullptr)
parent->qTreeItem->addChild(qTreeItem);
qTreeItem->setData(0, Qt::UserRole, QVariant::fromValue(this));
}
FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent)
FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent)
: LayerWrapper(json, parent)
{
qDebug() << json.value("name").toString() << " " << this;
@ -70,7 +65,7 @@ FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *element
}
}
LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent)
LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent)
: LayerWrapper(json, parent)
{
qDebug() << json.value("name").toString() << " " << this;
@ -99,8 +94,10 @@ void LayerWrapper::refresh()
void FolderLayerWrapper::refresh()
{
cache.clear();
for (auto &child : children)
for (auto& child : children) {
qDebug() << child.get();
cache.addPath(child.get()->getCache());
}
LayerWrapper::refresh();
}
@ -114,21 +111,73 @@ void LeafLayerWrapper::refresh()
LayerWrapper::refresh();
}
void LayerWrapper::setParent(LayerWrapper *newParent)
void LayerWrapper::setParent(FolderLayerWrapper* newParent)
{
this->parent = shared_ptr<LayerWrapper>(newParent);
this->parent = newParent;
}
void FolderLayerWrapper::removeAllChild()
{
children.clear();
qTreeWidgetItem.takeChildren();
}
namespace LayerEvent
{
static void onDoubleClick(QTreeWidgetItem *qItem, LayerWrapper *layerWrapper)
{
void LayerWrapper::del() {
qDebug() << "LayerWrapper::del()";
if (parent != nullptr){
qTreeWidgetItem.takeChildren();
parent->qTreeWidgetItem.removeChild(&qTreeWidgetItem);
}
}
} // namespace LayerEvent
void LayerWrapper::delSelf() {
qDebug() << "LayerWrapper::delSelf()";
if (parent != nullptr) {
qTreeWidgetItem.takeChildren();
parent->qTreeWidgetItem.removeChild(&qTreeWidgetItem);
}
}
void FolderLayerWrapper::del()
{
qDebug() << "FolderLayerWrapper::del()";
for (auto& child : children)
child->del();
LayerWrapper::del();
}
void FolderLayerWrapper::delSelf() {
qDebug() << "FolderLayerWrapper::delSelf() "<<this<<" "<<this->parent;
LayerWrapper::delSelf();
if (parent != nullptr) {
qDebug() << this->children.size();
qDebug() << this;
for (auto& child : this->children) {
this->parent->addChild(child);
this->parent->qTreeWidgetItem.addChild(&child.get()->qTreeWidgetItem);
child->setParent(this->parent);
}
while (!this->children.empty()) {
this->children.pop_back();
}
}
}
QTreeWidgetItem* LayerWrapper::getQTreeItem()
{
this->qTreeWidgetItem.setText(0, this->property.name);
this->qTreeWidgetItem.setData(0, Qt::UserRole, QVariant::fromValue(this));
return &this->qTreeWidgetItem;
}
QTreeWidgetItem* FolderLayerWrapper::getQTreeItem()
{
while (this->qTreeWidgetItem.childCount() > 0) {
this->qTreeWidgetItem.removeChild(this->qTreeWidgetItem.child(0));
}
for (auto& child : this->children) {
this->qTreeWidgetItem.addChild(child->getQTreeItem());
}
return LayerWrapper::getQTreeItem();
}

View File

@ -20,19 +20,20 @@ class GraphicElement;
class SimpleElement;
class GroupElement;
class ElementManager;
class FolderLayerWrapper;
class LayerWrapper
{
protected:
shared_ptr<LayerWrapper> parent;
FolderLayerWrapper* parent;
QPointF referencePoint;
// vector<LayerStyle> styles;
// TODO: 将cache移到子类对Leaf用ComposedPainterPath对Folder用FolderBitmapPath
PixelPath cache;
public:
QTreeWidgetItem *qTreeItem;
QTreeWidgetItem qTreeWidgetItem;
struct SimpleProperty
{
QString name = "";
@ -44,18 +45,22 @@ class LayerWrapper
// TODO: 将QPainterPath改为BitmapPath
void apply(PixelPath&cache) const;
} property;
void setParent(LayerWrapper *newParent);
virtual void setParent(FolderLayerWrapper*newParent);
virtual void refresh();
virtual QTreeWidgetItem* getQTreeItem();
// TODO: 将QPainterPath改为BitmapPath/QImage或者直接将其删除绘制时直接使用BitmapPath的paint方法
virtual PixelPath getCache();
LayerWrapper *getParent() const; // invoke by manager, then invoke parent's applyStyles
LayerWrapper(QJsonObject json, LayerWrapper *parent);
FolderLayerWrapper*getParent() const; // invoke by manager, then invoke parent's applyStyles
LayerWrapper(QJsonObject json, FolderLayerWrapper*parent);
LayerWrapper() = default;
// TODO : export Function
// virtual LayerWrapper *addChild() = 0; // Leaf Child Only
// virtual LayerWrapper *addParent() = 0; // Folder Parent Only
// virtual void deleteSelf() const = 0;
// virtual void deleteAll() const = 0;
virtual void del();
virtual void delSelf();
~LayerWrapper() = default;
};
class FolderLayerWrapper : public LayerWrapper
@ -64,12 +69,17 @@ class FolderLayerWrapper : public LayerWrapper
vector<shared_ptr<LayerWrapper>> children;
public:
~FolderLayerWrapper() = default;
void refresh() override;
FolderLayerWrapper() = default;
FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent);
void addChild(LayerWrapper *child);
FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent);
void addChild(shared_ptr<LayerWrapper> child);
void removeChild(LayerWrapper *child);
void removeAllChild();
void del() override;
void delSelf() override;
QTreeWidgetItem* getQTreeItem() override;
};
class LeafLayerWrapper : public LayerWrapper
@ -79,9 +89,12 @@ class LeafLayerWrapper : public LayerWrapper
const vector<Renderer::ElementStyleStrokeDemo> styles;
public:
~LeafLayerWrapper() = default;
void refresh() override;
LeafLayerWrapper() = default;
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent);
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent);
};
Q_DECLARE_METATYPE(LayerWrapper *)
Q_DECLARE_METATYPE(FolderLayerWrapper *)
Q_DECLARE_METATYPE(LeafLayerWrapper *)

View File

@ -26,7 +26,6 @@ QPixmap PixelPath::getPixmap() const
{
auto k = pixmap;
QPainter pt(&k);
qDebug() << this->boundingRect;
//pt.fillRect(this->boundingRect, Qt::red);
//pt.fillRect(QRectF(1013.35 - 1000, 768.327 - 700,58.0887,41.5352), Qt::blue);
return k;

View File

@ -11,7 +11,10 @@ LayerTreeWidget::LayerTreeWidget(QWidget *parent)
connect(this, &QTreeWidget::customContextMenuRequested, this, &LayerTreeWidget::popMenu);
connect(this, &QTreeWidget::currentItemChanged, [=](QTreeWidgetItem *currentItem) {
this->selectedItem = currentItem;
emit displayLayerChange(this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper *>());
if(this->selectedItem !=nullptr)
emit displayLayerChange(this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper *>());
else
emit displayLayerChange(nullptr);
});
// connect(this, &QTreeWidget::itemDoubleClicked, this, &LayerTreeWidget::onItemDoubleClicked);
}
@ -26,19 +29,33 @@ LayerTreeWidget::LayerTreeWidget(QWidget *parent)
// this->selectedItem = item;
// // TODO
// }
#include <QDebug>
void LayerTreeWidget::popMenu(const QPoint &pos)
{
QMenu menu;
QTreeWidgetItem *item = itemAt(pos);
this->selectedItem = item;
// TODO
menu.addAction("Add Child", this, &LayerTreeWidget::onRenameEvent);
menu.addAction("Rename", this, &LayerTreeWidget::onRenameEvent);
menu.addAction("Copy", this, &LayerTreeWidget::onRenameEvent);
menu.addAction(QString::fromLocal8Bit("创建子节点"), this, &LayerTreeWidget::onRenameEvent);
menu.addAction(QString::fromLocal8Bit("重命名"), this, &LayerTreeWidget::onRenameEvent);
// menu.addAction("Copy", this, &LayerTreeWidget::onRenameEvent);
if (item != nullptr && item->childCount() > 0)
menu.addAction("Delete (Self Only)", this, &LayerTreeWidget::onRenameEvent);
menu.addAction("Delete", this, &LayerTreeWidget::onRenameEvent);
menu.addAction(QString::fromLocal8Bit("删除(保留子节点)"), this, [this]() {
auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>();
layer->delSelf();
layer->getParent()->removeChild(layer);
this->refresh();
emit requireRefreshPreview();
});
menu.addAction(QString::fromLocal8Bit("删除"), this, [this]() {
if (this->selectedItem == nullptr)
return;
auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>();
layer->del();
layer->getParent()->removeChild(layer);
this->refresh();
emit requireRefreshPreview();
});
menu.exec(mapToGlobal(pos));
}
@ -57,3 +74,11 @@ void LayerTreeWidget::onRenameEvent()
}
emit displayLayerChange(this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper *>());
}
void LayerTreeWidget::refresh() {
// if(this->root!=nullptr)
//{
// this->clear();
// this->addTopLevelItem(this->root->getQTreeItem());
//}
}

View File

@ -10,12 +10,15 @@ class LayerTreeWidget : public QTreeWidget
LayerWrapper *copiedItem;
public:
LayerWrapper* root;
LayerTreeWidget(QWidget *parent = nullptr);
void onRenameEvent();
void popMenu(const QPoint &pos);
void refresh();
// void mouseDoubleClickEvent(QMouseEvent *event) override;
// void onItemDoubleClicked(QTreeWidgetItem *item, int column = 0);
signals:
void displayLayerChange(LayerWrapper *);
void requireRefreshPreview();
};

View File

@ -1,12 +1,12 @@
{
"height": 1080,
"width": 1920,
"width": 1080,
"elements": [
{
"name": "ababa",
"type": "svg-file",
"data": {
"include": "./svg/12(1).svg"
"include": "./svg/2.svg"
}
},
{
@ -25,14 +25,85 @@
"y": 0
},
"scale": {
"x": 0.5,
"y": 0.5
"x": 1.0,
"y": 1.0
},
"rotation": 0.0
},
"effects": [],
"is-folder": false,
"element": 0
"is-folder": true,
"referenced-by": null,
"children": [
{
"name": "GroupFolderExample",
"transform": {
"offset": {
"x": 50,
"y": 50
},
"scale": {
"x": 1.0,
"y": 1.0
},
"rotation": 0.0
},
"effects": [],
"is-folder": true,
"referenced-by": 1,
"children": [
{
"name": "Leaf1",
"transform": {
"offset": {
"x": 0,
"y": 0
},
"scale": {
"x": 1.0,
"y": 1.0
},
"rotation": 0.0
},
"effects": [],
"is-folder": false,
"element": 0
},
{
"name": "Leaf2",
"transform": {
"offset": {
"x": 150,
"y": 0
},
"scale": {
"x": 1.5,
"y": 1.5
},
"rotation": 0.0
},
"effects": [],
"is-folder": false,
"element": 0
}
]
},
{
"name": "ReferencingGroupLayer",
"transform": {
"offset": {
"x": 100,
"y": 0
},
"scale": {
"x": 1,
"y": 1
},
"rotation": 45
},
"effects": [],
"is-folder": false,
"element": 1
}
]
}
}
}