diff --git a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp index b9f09c7..0d17267 100644 --- a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp +++ b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.cpp @@ -13,12 +13,12 @@ using std::max; using std::shared_ptr; using std::make_shared; using std::min; -using std::queue; -struct LayerNode { - LayerWrapper* nowLayer; - QTransform transfrom; -}; +int PaintingUtil::zIndexCount = 0; + +void PaintingUtil::clear() { + zIndexCount = 0; +} QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) { QFile jsonFile(jsonFilePath); @@ -34,9 +34,15 @@ QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) { Painting PaintingUtil::transfromToPainting(QString jsonFilePath) { QJsonObject jsonObj = readJsonFile(jsonFilePath); //qDebug() << jsonObj; + std::unordered_map contourMap; Painting painting(Renderer::Material(jsonObj.value("background-color").toVariant().value())); shared_ptr elementManager = make_shared(jsonObj, QFileInfo(jsonFilePath).absolutePath()); shared_ptr layerManager = make_shared(jsonObj, elementManager.get()); + LayerWrapper* root = layerManager->getRoot(); + root->getCache(); + handleLayerWrapper(root, QTransform(), painting, contourMap); + clear(); + return painting; //qDebug() << elementManager->toJson(); //qDebug() << layerManager->toJson(); //qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath; @@ -45,12 +51,11 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) { //qDebug() << layerManager->toJson(); //qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath; //qDebug() << ((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->getCache().painterPath; - queue layerQueue; - LayerWrapper* root = layerManager->getRoot(); - root->getCache(); + //queue layerQueue; + //double maxLineWidth = getMaxLineWidth(root); - layerQueue.push({ root, QTransform()}); - while (!layerQueue.empty()) { + //layerQueue.push({ root, QTransform()}); + /*while (!layerQueue.empty()) { auto layerNode = layerQueue.front(); layerQueue.pop(); FolderLayerWrapper* nowLayer = handleLayerWrapper(layerNode.nowLayer, layerNode.transfrom, painting); @@ -59,13 +64,14 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) { layerQueue.push({ sonLayer.get(), layerNode.transfrom }); } } - } + }*/ - return painting; + } -FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform& transform, Painting& painting) { +void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Painting& painting, std::unordered_map& contourMap) { LeafLayerWrapper* leafLayer = dynamic_cast(nowLayer); + qDebug() << nowLayer; transform = nowLayer->property.transform * transform; @@ -73,8 +79,8 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr GroupElement* wrapperElement = dynamic_cast(leafLayer->wrappedElement); if (wrapperElement != nullptr) { - transform = wrapperElement->sourceLayer->property.transform * transform; - return wrapperElement->sourceLayer; + handleLayerWrapper(wrapperElement->sourceLayer, transform, painting, contourMap); + return; } PixelPath pixelPath = nowLayer->getCache(); @@ -84,18 +90,28 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr // transform to -1£¬ 1 QTransform trans; double maxLen = std::max(bound.width(), bound.height()) * 1.00001; - qDebug() << maxLen << bound; + //qDebug() << maxLen << bound; trans.scale(1 / maxLen, 1 / maxLen); trans.translate(-bound.center().x(), -bound.center().y()); - painterPath = trans.map(painterPath); - shared_ptr >> contour = std::make_shared >>(PainterPathUtil::transformToLines(painterPath)); + //std::shared_ptr keyLayerPtr(nowLayer); + auto iterContour = contourMap.find(nowLayer); + PaintingUtil::Contour contour = nullptr; + if (iterContour != contourMap.end()) { + contour = iterContour->second; + } + else { + painterPath = trans.map(painterPath); + contour = std::make_shared >>(PainterPathUtil::transformToLines(painterPath)); + contourMap.insert({ nowLayer, contour }); +; } QSize screenSize = QSize(1080, 1080); ElementTransform elementTransform; QTransform leafTransform = trans.inverted() * transform * QTransform::fromScale(2. / screenSize.width(), 2. / screenSize.height()) * QTransform::fromTranslate(-1, -1) * QTransform::fromScale(1, -1); QTransform transformInverted = leafTransform.inverted(); + ++zIndexCount; auto baseStyles = leafLayer->styles.toBaseStyles(); for (auto& baseStyle : baseStyles) { @@ -123,13 +139,18 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr transformInverted.m11(), transformInverted.m12(), transformInverted.m21(), transformInverted.m22(), transformInverted.m31(), transformInverted.m32() ); - elementTransform.zIndex = 0; + elementTransform.zIndex = zIndexCount; painting.addElement(BaseElement{ contour, material }, elementTransform); } - return nullptr; + return; } FolderLayerWrapper* folderLayer = dynamic_cast(nowLayer); - return folderLayer; + if (folderLayer != nullptr) { + for (auto sonLayer : folderLayer->children) { + handleLayerWrapper(sonLayer.get(), transform, painting, contourMap); + } + } + return; } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.h b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.h index 332a0a4..b11806b 100644 --- a/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.h +++ b/ArchitectureColoredPainting/src/Editor/util/PaintingUtil.h @@ -2,12 +2,17 @@ #include "../../Renderer/Painting/Painting.h" #include #include +#include + class PaintingUtil { + using Contour = std::shared_ptr > >; private: + static int zIndexCount; + static void clear(); //static const double pi; static QJsonObject readJsonFile(QString jsonFilePath); - static FolderLayerWrapper* handleLayerWrapper(LayerWrapper* nowLayer, QTransform& transform, Renderer::Painting& painting); + static void handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Renderer::Painting& painting, std::unordered_map& contourMap); //static double getMaxLineWidth(LayerWrapper* root); public: static Renderer::Painting transfromToPainting(QString jsonFilePath);