From 72f0f78e644a783aa8096a479a25804c8d37e936 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Tue, 14 Mar 2023 14:53:36 +0800 Subject: [PATCH] bug fix --- .../src/Editor/GraphicElement.cpp | 15 ++++++ .../src/Editor/GraphicElement.h | 3 ++ .../src/Editor/LayerWrapper.cpp | 53 ++++++++++--------- .../src/Editor/LayerWrapper.h | 10 ++-- .../src/Editor/RightBar/LayerTreeWidget.cpp | 4 ++ 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 403ae3f..d3dc67b 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -83,4 +83,19 @@ QJsonObject GraphicElement::toJson() const QJsonObject result; result.insert("name", name); return result; +} + +void SimpleElement::paint(QPainter* painter, QTransform transform, vector> styles) +{ + Renderer::ElementStyleStrokeDemo demo(2); + auto [img, mov] = renderer->drawElement(painterPath, demo, 1.0); + painter->save(); + painter->setTransform(transform); + painter->drawImage(mov, img); + painter->restore(); +} + +void GroupElement::paint(QPainter* painter, QTransform transform, vector> styles) +{ + sourceLayer->paint(painter, transform); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index c0b3c09..577f7db 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -28,6 +28,7 @@ public: virtual QJsonObject toJson() const; virtual PixelPath getPaintObject() const = 0; virtual PixelPath getPaintObject(std::vector>*) const = 0; + virtual void paint(QPainter* painter, QTransform transform, std::vector> styles) = 0; }; class SimpleElement : public GraphicElement @@ -44,6 +45,7 @@ public: ~SimpleElement() = default; PixelPath getPaintObject() const override; PixelPath getPaintObject(std::vector>*) const override; + void paint(QPainter* painter, QTransform transform, std::vector> styles) override; }; class GroupElement : public GraphicElement @@ -58,6 +60,7 @@ public: PixelPath getPaintObject() const override; PixelPath getPaintObject(std::vector>*) const override; void setSourceLayer(FolderLayerWrapper* sourceLayer); + void paint(QPainter* painter, QTransform transform, std::vector> styles) override; }; //******************************** BitmapPath ********************************// diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 9e1febe..c914236 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -46,6 +46,7 @@ LayerWrapper::LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementM property.scale = {transformJson.value("scale").toObject().value("x").toDouble(), transformJson.value("scale").toObject().value("y").toDouble()}; property.rotation = {transformJson.value("rotation").toDouble()}; + selected = false; } FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent) @@ -80,19 +81,19 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana wrappedElement = elementManager->getElementById(elementIndex); } -void LayerWrapper::SimpleProperty::apply(PixelPath&cache) const +void LayerWrapper::SimpleProperty::apply(PixelPath&cache) { - QTransform trans; + transform.reset(); double centerX = cache.getBoundingRect().center().x(); double centerY = cache.getBoundingRect().center().y(); //qDebug() << name << " " << cache.boundingRect().center(); //qDebug() << name << " " << cache.boundingRect(); - trans.translate(centerX, centerY); - trans.translate(offset.x(), offset.y()); - trans.rotate(rotation); - trans.scale(scale.x(), scale.y()); - trans.translate(-centerX, -centerY); - cache = cache.trans(trans); + transform.translate(-centerX, -centerY); + transform.translate(offset.x(), offset.y()); + transform.rotate(rotation); + transform.scale(scale.x(), scale.y()); + transform.translate(centerX, centerY); + cache = cache.trans(transform); } QTransform LayerWrapper::getTransform() @@ -250,31 +251,35 @@ int FolderLayerWrapper::getReferencedBy()const return -1; } -void LayerWrapper::paint(QPainter* painter) +void LayerWrapper::paint(QPainter* painter, QTransform transform) { - + if (this->selected) + { + painter->save(); + painter->setTransform(transform); + painter->setPen(QPen(Qt::gray, 2)); + painter->setPen(Qt::DashLine); + painter->drawRect(cache.getBoundingRect()); + painter->restore(); + } } -void FolderLayerWrapper::paint(QPainter* painter) +void FolderLayerWrapper::paint(QPainter* painter, QTransform transform) { + LayerWrapper::paint(painter, transform); + transform = property.transform * transform; + qDebug() << transform; for (auto& child : children) - child->paint(painter); + child->paint(painter, transform); } -void LeafLayerWrapper::paint(QPainter* painter) +void LeafLayerWrapper::paint(QPainter* painter, QTransform transform) { + LayerWrapper::paint(painter, transform); + transform = property.transform * transform; + qDebug() << transform; if (wrappedElement != nullptr) { - //painter->save(); - QTransform trans; - LayerWrapper* layer = this; - while (layer != nullptr) { - trans *= layer->getTransform(); - layer = layer->getParent(); - } - auto p = wrappedElement->getPaintObject(&this->styles); - p.trans(trans); - painter->drawPixmap(0, 0, p.getPixmap()); - //painter->restore(); + wrappedElement->paint(painter, transform, styles); } } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 2186cd6..7df5d13 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -35,6 +35,7 @@ class LayerWrapper public: QTreeWidgetItem* qTreeWidgetItem; + bool selected; struct SimpleProperty { QString name = ""; @@ -43,8 +44,9 @@ class LayerWrapper double rotation = 0; bool flipHorizontally = 0; bool flipVertically = 0; + QTransform transform; // TODO: ½«QPainterPath¸ÄΪBitmapPath - void apply(PixelPath&cache) const; + void apply(PixelPath&cache); } property; virtual void setParent(FolderLayerWrapper*newParent); virtual void refresh(LayerWrapper* layer = nullptr); @@ -55,7 +57,7 @@ class LayerWrapper FolderLayerWrapper*getParent() const; // invoke by manager, then invoke parent's applyStyles LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementManager* elementManager=nullptr); LayerWrapper() = default; - virtual void paint(QPainter* painter); + virtual void paint(QPainter* painter, QTransform transform=QTransform()); // TODO : export Function // virtual LayerWrapper *addChild() = 0; // Leaf Child Only // virtual LayerWrapper *addParent() = 0; // Folder Parent Only @@ -88,7 +90,7 @@ class FolderLayerWrapper : public LayerWrapper QTreeWidgetItem* getQTreeItem() override; QJsonObject toJson() const override; int getReferencedBy()const; - void paint(QPainter* painter) override; + void paint(QPainter* painter, QTransform transform = QTransform()) override; }; class LeafLayerWrapper : public LayerWrapper @@ -104,7 +106,7 @@ class LeafLayerWrapper : public LayerWrapper LeafLayerWrapper() = default; LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; - void paint(QPainter* painter) override; + void paint(QPainter* painter, QTransform transform = QTransform()) override; }; Q_DECLARE_METATYPE(LayerWrapper *) diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index f6962db..1e84c88 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -12,9 +12,13 @@ LayerTreeWidget::LayerTreeWidget(QWidget *parent) this->setHeaderLabel("Layer Content"); connect(this, &QTreeWidget::customContextMenuRequested, this, &LayerTreeWidget::popMenu); connect(this, &QTreeWidget::currentItemChanged, [=](QTreeWidgetItem *currentItem) { + if (this->selectedItem != nullptr) { + this->selectedItem->data(0, Qt::UserRole).value()->selected = false; + } this->selectedItem = currentItem; if (this->selectedItem != nullptr) { auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + layer->selected = true; emit displayLayerChange(layer); } else {