增加了Svg文件路径的读入

dev-VirtualTexture
yang.yongquan 2023-01-14 17:52:34 +08:00
parent dc89e02515
commit 96dac4151e
6 changed files with 255 additions and 11 deletions

View File

@ -13,7 +13,7 @@
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{3FE96A33-2BB7-4686-A710-3EB8E3BBD709}</ProjectGuid> <ProjectGuid>{3FE96A33-2BB7-4686-A710-3EB8E3BBD709}</ProjectGuid>
<Keyword>QtVS_v304</Keyword> <Keyword>QtVS_v304</Keyword>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">10.0.19041.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">10.0.19041.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">10.0.19041.0</WindowsTargetPlatformVersion>
<QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild> <QtMsBuild Condition="'$(QtMsBuild)'=='' OR !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup> </PropertyGroup>
@ -106,6 +106,7 @@
<ClCompile Include="src\Editor\LayerWrapper.cpp" /> <ClCompile Include="src\Editor\LayerWrapper.cpp" />
<ClCompile Include="src\Editor\PreviewWindow.cpp" /> <ClCompile Include="src\Editor\PreviewWindow.cpp" />
<ClCompile Include="src\Editor\third-party modules\qquick\qquicksvgparser.cpp" /> <ClCompile Include="src\Editor\third-party modules\qquick\qquicksvgparser.cpp" />
<ClCompile Include="src\Editor\third-party modules\util\SvgFileLoader.cpp" />
<ClCompile Include="src\IconWidget.cpp" /> <ClCompile Include="src\IconWidget.cpp" />
<ClCompile Include="src\main.cpp" /> <ClCompile Include="src\main.cpp" />
<ClCompile Include="src\MainWindow.cpp" /> <ClCompile Include="src\MainWindow.cpp" />
@ -171,6 +172,7 @@
<ClInclude Include="src\Editor\LayerStyle.h" /> <ClInclude Include="src\Editor\LayerStyle.h" />
<ClInclude Include="src\Editor\LayerWrapper.h" /> <ClInclude Include="src\Editor\LayerWrapper.h" />
<QtMoc Include="src\Editor\PreviewWindow.h" /> <QtMoc Include="src\Editor\PreviewWindow.h" />
<ClInclude Include="src\Editor\third-party modules\util\SvgFileLoader.h" />
<ClInclude Include="src\Renderer\Painting\CubicBezierSignedDistance.h" /> <ClInclude Include="src\Renderer\Painting\CubicBezierSignedDistance.h" />
<ClInclude Include="src\Renderer\Painting\Element.h" /> <ClInclude Include="src\Renderer\Painting\Element.h" />
<ClInclude Include="src\Renderer\Painting\LineTree.h" /> <ClInclude Include="src\Renderer\Painting\LineTree.h" />

View File

@ -41,6 +41,24 @@
<Filter Include="Header Files\Renderer\Painting"> <Filter Include="Header Files\Renderer\Painting">
<UniqueIdentifier>{22909273-2b23-49fa-84ab-444cefb09656}</UniqueIdentifier> <UniqueIdentifier>{22909273-2b23-49fa-84ab-444cefb09656}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Header Files\Editor\util">
<UniqueIdentifier>{e3c323ec-d150-4876-8618-5800c87a4941}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Editor\third-party modules">
<UniqueIdentifier>{30b46cf2-d980-47be-94c4-d2ec1dcc54ed}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Editor\third-party modules\qquick">
<UniqueIdentifier>{5028c879-8b07-4033-81ac-e538a873a837}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Editor\third-party modules">
<UniqueIdentifier>{555d169f-4fa2-4501-b67c-695197b9e6ae}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Editor\third-party modules\qquick">
<UniqueIdentifier>{0b29331b-03b9-44fe-916f-28f5061b4147}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Editor\util">
<UniqueIdentifier>{d7c7ab61-0d05-4e67-9e89-852f3e56fa2f}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtUic Include="MainWindow.ui"> <QtUic Include="MainWindow.ui">
@ -147,11 +165,14 @@
<ClCompile Include="src\Renderer\Painting\CubicBezierSignedDistance.cpp"> <ClCompile Include="src\Renderer\Painting\CubicBezierSignedDistance.cpp">
<Filter>Source Files\Renderer\Painting</Filter> <Filter>Source Files\Renderer\Painting</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Editor\third-party modules\SvgHelper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Editor\third-party modules\qquick\qquicksvgparser.cpp"> <ClCompile Include="src\Editor\third-party modules\qquick\qquicksvgparser.cpp">
<Filter>Source Files</Filter> <Filter>Source Files\Editor\third-party modules\qquick</Filter>
</ClCompile>
<ClCompile Include="src\Editor\third-party modules\util\SvgFileLoader.cpp">
<Filter>Source Files\Editor\util</Filter>
</ClCompile>
<ClCompile Include="src\Editor\third-party modules\SvgHelper.cpp">
<Filter>Source Files\Editor\third-party modules</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -319,16 +340,19 @@
<Filter>Header Files\Renderer\Painting</Filter> <Filter>Header Files\Renderer\Painting</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Editor\third-party modules\SvgHelper.h"> <ClInclude Include="src\Editor\third-party modules\SvgHelper.h">
<Filter>Header Files</Filter> <Filter>Header Files\Editor\third-party modules</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Editor\third-party modules\qquick\qquicksvgparser_p.h"> <ClInclude Include="src\Editor\third-party modules\qquick\qquicksvgparser_p.h">
<Filter>Header Files</Filter> <Filter>Header Files\Editor\third-party modules\qquick</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Editor\third-party modules\qquick\qtquickglobal.h"> <ClInclude Include="src\Editor\third-party modules\qquick\qtquickglobal.h">
<Filter>Header Files</Filter> <Filter>Header Files\Editor\third-party modules\qquick</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Editor\third-party modules\qquick\qtquickglobal_p.h"> <ClInclude Include="src\Editor\third-party modules\qquick\qtquickglobal_p.h">
<Filter>Header Files</Filter> <Filter>Header Files\Editor\third-party modules\qquick</Filter>
</ClInclude>
<ClInclude Include="src\Editor\third-party modules\util\SvgFileLoader.h">
<Filter>Header Files\Editor\util</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,4 +1,5 @@
#include "GraphicElement.h" #include "GraphicElement.h"
#include "third-party modules/util/SvgFileLoader.h"
using namespace std; using namespace std;
QPainterPath SimpleElement::getPaintObject() const QPainterPath SimpleElement::getPaintObject() const
{ {
@ -7,7 +8,9 @@ QPainterPath SimpleElement::getPaintObject() const
void SimpleElement::loadSvgFile(const QString& filePath) void SimpleElement::loadSvgFile(const QString& filePath)
{ {
// TODO // TODO ÑùʽÎÊÌâ
SvgFileLoader loader;
loader.loadSvgFile(filePath, painterPath);
} }
SimpleElement::SimpleElement(QJsonObject jsonSource) : jsonSource(jsonSource) SimpleElement::SimpleElement(QJsonObject jsonSource) : jsonSource(jsonSource)

View File

@ -0,0 +1,190 @@
#include "SvgFileLoader.h"
#include <QFile>
#include "../qquick/qquicksvgparser_p.h"
#include <QPolygonF>
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<QString, QString> SvgFileLoader::transformStyle(QString style) {
QMap<QString, QString> 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<QPointF> SvgFileLoader::transformPoints(QString points) {
QVector<QPointF> 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<QString, QString> 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<QString, QString> 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<QString, QString> 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<QString, QString> 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<QString, QString> 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<QString, QString> 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());
}
}
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <QString>
#include <QPainterPath>
#include <QMap>
#include <QtXml>
#include <memory>
#include <QVector>
class SvgFileLoader
{
public:
bool loadSvgFile(const QString& filePath, QPainterPath& painterPath);
private:
bool existFatherLabelG;
QMap<QString, QString> styleMap;
std::shared_ptr<QXmlStreamReader> xmlReader;
QVector<QPointF> transformPoints(QString points);
QMap<QString, QString> 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);
};

View File

@ -14,7 +14,7 @@
<ProjectGuid>{B982E745-C0B1-46B3-A27B-743AF105F2D0}</ProjectGuid> <ProjectGuid>{B982E745-C0B1-46B3-A27B-743AF105F2D0}</ProjectGuid>
<RootNamespace>QGoodWindow</RootNamespace> <RootNamespace>QGoodWindow</RootNamespace>
<Keyword>QtVS_v304</Keyword> <Keyword>QtVS_v304</Keyword>
<WindowsTargetPlatformVersion>10.0.19041.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformMinVersion>10.0.19041.0</WindowsTargetPlatformMinVersion> <WindowsTargetPlatformMinVersion>10.0.19041.0</WindowsTargetPlatformMinVersion>
<QtMsBuild Condition="'$(QtMsBuild)'=='' or !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild> <QtMsBuild Condition="'$(QtMsBuild)'=='' or !Exists('$(QtMsBuild)\qt.targets')">$(MSBuildProjectDirectory)\QtMsBuild</QtMsBuild>
</PropertyGroup> </PropertyGroup>