From fe6e10c882e1d9f9b8ce1d03b877dc8807d1085b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=B0=81=E7=BE=BD?= <2360164671@qq.com> Date: Mon, 26 Dec 2022 21:24:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86Layer=E9=83=A8?= =?UTF-8?q?=E5=88=86=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/LayerManager.cpp | 46 +++++++++++++++++++ .../src/Editor/LayerManager.h | 10 ++++ .../src/Editor/LayerWrapper.cpp | 31 ++++++++++++- .../src/Editor/LayerWrapper.h | 15 +++++- 4 files changed, 100 insertions(+), 2 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp index 0cb2d37..dfc7ea6 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp @@ -11,3 +11,49 @@ void LayerManager::paint(QPainter *painter) const { painter->drawPath(root->getCache()); } +bool LayerManager::singleSelectedCheck() const +{ + if (selectedLayers.size() != 1) + return false; + return selectedLayers[0] != nullptr; +} +bool LayerManager::multipleSelectedCheck() const +{ + if (selectedLayers.size() <= 1) + return false; + for (auto &layer : selectedLayers) + if (layer == nullptr) + return false; + return true; +} +bool LayerManager::rename(QString newName) const +{ + if (!singleSelectedCheck()) + return false; + auto ¤Layer = selectedLayers[0]; + currenLayer->property.name = newName; + return true; +} +bool LayerManager::combine() const +{ + if (!multipleSelectedCheck()) + return false; + auto prevCommonFather = selectedLayers[0]->getParent(); + for (auto &layer : selectedLayers) + if (layer->getParent() != prevCommonFather) + return false; + auto newCommonFather = new FolderLayerWrapper(); + newCommonFather->setParent(prevCommonFather); + for (auto &layer : selectedLayers) + layer->setParent(newCommonFather); + return true; +} +bool LayerManager::changeParent(FolderLayerWrapper *newParent) const +{ + if (!singleSelectedCheck()) + return false; + if (selectedLayers[0] == root) + return false; + selectedLayers[0]->setParent(newParent); + return true; +} diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.h b/ArchitectureColoredPainting/src/Editor/LayerManager.h index 4dc7909..370861a 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.h +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.h @@ -9,6 +9,8 @@ using std::pair; using std::vector; class ElementManager; class LayerWrapper; +class FolderLayerWrapper; +class LeafLayerWrapper; class LayerManager { @@ -17,8 +19,16 @@ class LayerManager LayerWrapper *root; LayerPtrs selectedLayers; LayerPtrs involvedLeafLayersCache; + bool singleSelectedCheck() const; + bool multipleSelectedCheck() const; public: LayerManager(QJsonObject source, ElementManager *elementManager); void paint(QPainter *painter) const; + bool rename(QString newName) const; + bool combine() const; + // bool seperate() const; + // bool erase() const; + // bool makeReference() const; + bool changeParent(FolderLayerWrapper *newParent) const; }; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 9a16084..d9f5336 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -18,6 +18,11 @@ void FolderLayerWrapper::removeChild(LayerWrapper *child) } } +LayerWrapper *LayerWrapper::getParent() const +{ + return this == nullptr ? nullptr : this->parent.get(); +} + QPainterPath LayerWrapper::getCache() { this->refresh(); @@ -26,6 +31,9 @@ QPainterPath LayerWrapper::getCache() // TODO: undone LayerWrapper::LayerWrapper(QJsonObject json, LayerWrapper *parent) { + this->parent = shared_ptr(parent); + auto offsetJson = json.value("offset").toObject(); + property.offset = {offsetJson.value("x").toDouble(), offsetJson.value("y").toDouble()}; } FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent) @@ -58,9 +66,18 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana int elementIndex = json.value("element").toInt(); wrappedElement = elementManager->getElementById(elementIndex); } - +void LayerWrapper::SimpleProperty::apply(QPainterPath &cache) const +{ + QTransform trans; + trans.scale(zoom.x(), zoom.y()); + trans.rotate(rotation); + trans.translate(offset.x(), offset.y()); + cache = trans.map(cache); + // cache.translate(offset); +} void LayerWrapper::refresh() { + property.apply(cache); } void FolderLayerWrapper::refresh() @@ -68,6 +85,7 @@ void FolderLayerWrapper::refresh() cache.clear(); for (auto &child : children) cache.addPath(child.get()->getCache()); + LayerWrapper::refresh(); } void LeafLayerWrapper::refresh() @@ -75,4 +93,15 @@ void LeafLayerWrapper::refresh() cache.clear(); if (wrappedElement != nullptr) cache.addPath(wrappedElement->getPaintObject()); + LayerWrapper::refresh(); +} + +void LayerWrapper::setParent(LayerWrapper *newParent) +{ + this->parent = shared_ptr(newParent); +} + +void FolderLayerWrapper::removeAllChild() +{ + children.clear(); } diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 77e7cfb..826dbb6 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -26,8 +26,20 @@ class LayerWrapper QPainterPath cache; public: + struct SimpleProperty + { + QString name = ""; + QPointF zoom = {1.0, 1.0}; + QPointF offset = {0, 0}; + double rotation = 0; + bool flipHorizontally = 0; + bool flipVertically = 0; + void apply(QPainterPath &cache) const; + } property; + void setParent(LayerWrapper *newParent); virtual void refresh(); - QPainterPath getCache(); // invoke by manager, then invoke parent's applyStyles + QPainterPath getCache(); + LayerWrapper *getParent() const; // invoke by manager, then invoke parent's applyStyles LayerWrapper(QJsonObject json, LayerWrapper *parent); LayerWrapper() = default; }; @@ -43,6 +55,7 @@ class FolderLayerWrapper : public LayerWrapper FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent); void addChild(LayerWrapper *child); void removeChild(LayerWrapper *child); + void removeAllChild(); }; class LeafLayerWrapper : public LayerWrapper