From 96dac4151e956503ee44a312135046e87b00a710 Mon Sep 17 00:00:00 2001 From: "yang.yongquan" <3395816735@qq.com> Date: Sat, 14 Jan 2023 17:52:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86Svg=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E7=9A=84=E8=AF=BB=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArchitectureColoredPainting.vcxproj | 4 +- ...rchitectureColoredPainting.vcxproj.filters | 40 +++- .../src/Editor/GraphicElement.cpp | 5 +- .../util/SvgFileLoader.cpp | 190 ++++++++++++++++++ .../third-party modules/util/SvgFileLoader.h | 25 +++ QGoodWindow/QGoodWindow.vcxproj | 2 +- 6 files changed, 255 insertions(+), 11 deletions(-) create mode 100644 ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.cpp create mode 100644 ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.h diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj index 6fd14e9..b06b955 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -13,7 +13,7 @@ {3FE96A33-2BB7-4686-A710-3EB8E3BBD709} QtVS_v304 - 10.0.19041.0 + 10.0 10.0.19041.0 $(MSBuildProjectDirectory)\QtMsBuild @@ -106,6 +106,7 @@ + @@ -171,6 +172,7 @@ + diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters index a54f912..0d4ccee 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters @@ -41,6 +41,24 @@ {22909273-2b23-49fa-84ab-444cefb09656} + + {e3c323ec-d150-4876-8618-5800c87a4941} + + + {30b46cf2-d980-47be-94c4-d2ec1dcc54ed} + + + {5028c879-8b07-4033-81ac-e538a873a837} + + + {555d169f-4fa2-4501-b67c-695197b9e6ae} + + + {0b29331b-03b9-44fe-916f-28f5061b4147} + + + {d7c7ab61-0d05-4e67-9e89-852f3e56fa2f} + @@ -147,11 +165,14 @@ Source Files\Renderer\Painting - - Source Files - - Source Files + Source Files\Editor\third-party modules\qquick + + + Source Files\Editor\util + + + Source Files\Editor\third-party modules @@ -319,16 +340,19 @@ Header Files\Renderer\Painting - Header Files + Header Files\Editor\third-party modules - Header Files + Header Files\Editor\third-party modules\qquick - Header Files + Header Files\Editor\third-party modules\qquick - Header Files + Header Files\Editor\third-party modules\qquick + + + Header Files\Editor\util diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index cdd8e42..73fe807 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -1,4 +1,5 @@ #include "GraphicElement.h" +#include "third-party modules/util/SvgFileLoader.h" using namespace std; QPainterPath SimpleElement::getPaintObject() const { @@ -7,7 +8,9 @@ QPainterPath SimpleElement::getPaintObject() const void SimpleElement::loadSvgFile(const QString& filePath) { - // TODO + // TODO ÑùʽÎÊÌâ + SvgFileLoader loader; + loader.loadSvgFile(filePath, painterPath); } SimpleElement::SimpleElement(QJsonObject jsonSource) : jsonSource(jsonSource) diff --git a/ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.cpp b/ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.cpp new file mode 100644 index 0000000..3e01994 --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.cpp @@ -0,0 +1,190 @@ +#include "SvgFileLoader.h" +#include +#include "../qquick/qquicksvgparser_p.h" +#include + + +using std::map; + +bool SvgFileLoader::loadSvgFile(const QString& filePath, QPainterPath& painterPath) { + QFile file(filePath); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + return false; + } + xmlReader.reset(new QXmlStreamReader(&file)); + while (!xmlReader->atEnd() && !xmlReader->hasError()) { + QXmlStreamReader::TokenType token = xmlReader->readNext(); + if (token == QXmlStreamReader::StartDocument) + continue; + if (token == QXmlStreamReader::StartElement) { + if (xmlReader->name() == "g") { + handleLabelG(painterPath); + } + if (xmlReader->name() == "path") { + handleLabelPath(painterPath); + } + if (xmlReader->name() == "rect") { + handleLabelRect(painterPath); + } + if (xmlReader->name() == "circle ") { + handleLabelCircle(painterPath); + } + if (xmlReader->name() == "ellipse") { + handleLabelEllipse(painterPath); + } + if (xmlReader->name() == "polyline") { + handleLabelPolyline(painterPath); + } + if (xmlReader->name() == "polygon") { + handleLabelPolygon(painterPath); + } + } + if (token == QXmlStreamReader::EndElement) { + if (xmlReader->name() == "g") { + existFatherLabelG = false; + styleMap.clear(); + } + } + } + + file.close(); + return true; +} + +QMap SvgFileLoader::transformStyle(QString style) { + QMap resStyleMap; + for (auto& attr : style.split(';')) { + if (attr.isEmpty() || attr.trimmed() == "") continue; + QString name = *(attr.split(':').begin()); + QString value = *(attr.split(':').rbegin()); + resStyleMap.insert(name, value); + } + return resStyleMap; +} + +QVector SvgFileLoader::transformPoints(QString points) { + QVector resPointVector; + QPointF pointBegin(0, 0); + for (auto& onePoint : points.split(' ')) { + QString x = *(onePoint.split(',').begin()); + QString y = *(onePoint.split(',').rbegin()); + resPointVector.push_back(QPointF(x.toDouble(), y.toDouble())); + + } + return resPointVector; +} + +void SvgFileLoader::handleLabelG(QPainterPath& painterPath) { + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("style")) { + styleMap = transformStyle(attr.value().toLatin1()); + } + } + existFatherLabelG = true; + qDebug() << styleMap; +} + +void SvgFileLoader::handleLabelPath(QPainterPath& painterPath) { + QMap labelStyle; + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("d")) { + QQuickSvgParser::parsePathDataFast(attr.value().toLatin1(), painterPath); + } + if (attr.name().toString() == QLatin1String("style")) { + labelStyle = transformStyle(attr.value().toLatin1()); + } + } +} + +void SvgFileLoader::handleLabelRect(QPainterPath& painterPath) { + QMap labelStyle; + double xBegin = 0, yBegin = 0, width = 0, height = 0;; + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("x")) { + xBegin = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("y")) { + yBegin = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("width")) { + width = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("height")) { + height = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("style")) { + labelStyle = transformStyle(attr.value().toLatin1()); + } + } + painterPath.addRect(xBegin, yBegin, xBegin + width, yBegin + height); +} + +void SvgFileLoader::handleLabelCircle(QPainterPath& painterPath) { + QMap labelStyle; + double cx = 0, cy = 0, r = 0; + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("cx")) { + cx = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("cy")) { + cy = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("r")) { + r = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("style")) { + labelStyle = transformStyle(attr.value().toLatin1()); + } + } + painterPath.addEllipse(cx, cy, r, r); +} + +void SvgFileLoader::handleLabelEllipse(QPainterPath& painterPath) { + QMap labelStyle; + double cx = 0, cy = 0, rx = 0, ry = 0; + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("cx")) { + cx = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("cy")) { + cy = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("rx")) { + rx = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("ry")) { + rx = attr.value().toDouble(); + } + if (attr.name().toString() == QLatin1String("style")) { + labelStyle = transformStyle(attr.value().toLatin1()); + } + } + painterPath.addEllipse(cx, cy, rx, ry); +} + +void SvgFileLoader::handleLabelPolyline(QPainterPath& painterPath) { + QMap labelStyle; + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("points")) { + QPolygonF points = transformPoints(attr.value().toLatin1()); + painterPath.addPolygon(points); + } + if (attr.name().toString() == QLatin1String("style")) { + labelStyle = transformStyle(attr.value().toLatin1()); + } + } +} + +void SvgFileLoader::handleLabelPolygon(QPainterPath & painterPath) { + QMap labelStyle; + for (auto& attr : xmlReader->attributes()) { + if (attr.name().toString() == QLatin1String("points")) { + QPolygonF points = transformPoints(attr.value().toLatin1()); + points.push_back(*points.begin()); + painterPath.addPolygon(points); + } + if (attr.name().toString() == QLatin1String("style")) { + labelStyle = transformStyle(attr.value().toLatin1()); + } + } +} \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.h b/ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.h new file mode 100644 index 0000000..55086bb --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/third-party modules/util/SvgFileLoader.h @@ -0,0 +1,25 @@ +#pragma once +#include +#include +#include +#include +#include +#include +class SvgFileLoader +{ +public: + bool loadSvgFile(const QString& filePath, QPainterPath& painterPath); +private: + bool existFatherLabelG; + QMap styleMap; + std::shared_ptr xmlReader; + QVector transformPoints(QString points); + QMap transformStyle(QString style); + void handleLabelG(QPainterPath& painterPath); + void handleLabelPath(QPainterPath& painterPath); + void handleLabelRect(QPainterPath& painterPath); + void handleLabelCircle(QPainterPath& painterPath); + void handleLabelEllipse(QPainterPath& painterPath); + void handleLabelPolyline(QPainterPath& painterPath); + void handleLabelPolygon(QPainterPath& painterPath); +}; diff --git a/QGoodWindow/QGoodWindow.vcxproj b/QGoodWindow/QGoodWindow.vcxproj index 2b9b86f..757ea42 100644 --- a/QGoodWindow/QGoodWindow.vcxproj +++ b/QGoodWindow/QGoodWindow.vcxproj @@ -14,7 +14,7 @@ {B982E745-C0B1-46B3-A27B-743AF105F2D0} QGoodWindow QtVS_v304 - 10.0.19041.0 + 10.0 10.0.19041.0 $(MSBuildProjectDirectory)\QtMsBuild