增加了获得最大线宽的函数

main
yang.yongquan 2023-04-05 10:33:36 +08:00
parent 262eb0e19f
commit b249eff8f4
2 changed files with 60 additions and 7 deletions

View File

@ -41,7 +41,8 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) {
shared_ptr<LayerManager> layerManager = make_shared<LayerManager>(jsonObj, elementManager.get()); shared_ptr<LayerManager> layerManager = make_shared<LayerManager>(jsonObj, elementManager.get());
LayerWrapper* root = layerManager->getRoot(); LayerWrapper* root = layerManager->getRoot();
root->getCache(); root->getCache();
handleLayerWrapper(root, QTransform(), painting, contourMap, QSize(jsonObj.value("width").toVariant().value<int>(), jsonObj.value("height").toVariant().value<int>())); double maxWidth = getMaxLineWidth(root, QTransform());
handleLayerWrapper(root, QTransform(), painting, maxWidth, contourMap, QSize(jsonObj.value("width").toVariant().value<int>(), jsonObj.value("height").toVariant().value<int>()));
clear(); clear();
return painting; return painting;
//qDebug() << elementManager->toJson(); //qDebug() << elementManager->toJson();
@ -70,7 +71,7 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) {
} }
void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Painting& painting, std::unordered_map<LayerWrapper*, Contour>& contourMap, const QSize& screenSize) { void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Painting& painting, double& maxWidth, std::unordered_map<LayerWrapper*, Contour>& contourMap, const QSize& screenSize) {
LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer); LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer);
qDebug() << nowLayer; qDebug() << nowLayer;
@ -80,7 +81,7 @@ void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transfo
GroupElement* wrapperElement = dynamic_cast<GroupElement*>(leafLayer->wrappedElement); GroupElement* wrapperElement = dynamic_cast<GroupElement*>(leafLayer->wrappedElement);
if (wrapperElement != nullptr) { if (wrapperElement != nullptr) {
handleLayerWrapper(wrapperElement->sourceLayer, transform, painting, contourMap, screenSize); handleLayerWrapper(wrapperElement->sourceLayer, transform, painting, maxWidth, contourMap, screenSize);
return; return;
} }
@ -90,7 +91,7 @@ void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transfo
//qDebug() << leafLayer<<"------" << painterPath; //qDebug() << leafLayer<<"------" << painterPath;
// transform to -1£¬ 1 // transform to -1£¬ 1
QTransform trans; QTransform trans;
double maxLen = std::max(bound.width(), bound.height()) * 1.00001 * 2; double maxLen = (std::max(bound.width(), bound.height()) + 2 * maxWidth) * 1.00001 * 2;
//qDebug() << maxLen << bound; //qDebug() << maxLen << bound;
trans.scale(1 / maxLen, 1 / maxLen); trans.scale(1 / maxLen, 1 / maxLen);
trans.translate(-bound.center().x(), -bound.center().y()); trans.translate(-bound.center().x(), -bound.center().y());
@ -157,8 +158,60 @@ void PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform transfo
FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer); FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer);
if (folderLayer != nullptr) { if (folderLayer != nullptr) {
for (auto sonLayer : folderLayer->children) { for (auto sonLayer : folderLayer->children) {
handleLayerWrapper(sonLayer.get(), transform, painting, contourMap, screenSize); handleLayerWrapper(sonLayer.get(), transform, painting, maxWidth, contourMap, screenSize);
} }
} }
return; return;
}
double PaintingUtil::getMaxLineWidth(LayerWrapper* nowLayer, QTransform transform) {
LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer);
transform = nowLayer->property.transform * transform;
double maxWidth = 0;
if (leafLayer != nullptr) {
GroupElement* wrapperElement = dynamic_cast<GroupElement*>(leafLayer->wrappedElement);
if (wrapperElement != nullptr) {
return getMaxLineWidth(wrapperElement->sourceLayer, transform);
}
PixelPath pixelPath = nowLayer->getCache();
QPainterPath painterPath = pixelPath.originPath;
QRectF bound = painterPath.boundingRect();
//qDebug() << maxLen << bound;
//
//++zIndexCount;
auto baseStyles = leafLayer->styles->toBaseStyles();
qDebug() << baseStyles.size();
for (auto& baseStyle : std::views::reverse(baseStyles)) {
double lineWidth = 0;
QPainterPath path;
std::shared_ptr<MaterialStyle> material;
if (baseStyle.material->type() == Renderer::MaterialStyleType::kStroke) {
qDebug() << "MaterialStyleType::kStroke";
std::shared_ptr copy = baseStyle.material->clone();
lineWidth = std::static_pointer_cast<MaterialStyleStroke>(copy)->halfWidth;
maxWidth = std::max(maxWidth, lineWidth);
}
else
{
qDebug() << "MaterialStyleType::kFill";
material = baseStyle.material;
path = painterPath;
}
}
return maxWidth;
}
FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer);
if (folderLayer != nullptr) {
for (auto sonLayer : folderLayer->children) {
maxWidth = std::max(maxWidth, getMaxLineWidth(sonLayer.get(), transform));
}
}
return maxWidth;
} }

View File

@ -12,8 +12,8 @@ private:
static void clear(); static void clear();
//static const double pi; //static const double pi;
static QJsonObject readJsonFile(QString jsonFilePath); static QJsonObject readJsonFile(QString jsonFilePath);
static void handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Renderer::Painting& painting, std::unordered_map<LayerWrapper*, Contour>& contourMap, const QSize& screenSize); static void handleLayerWrapper(LayerWrapper* nowLayer, QTransform transform, Renderer::Painting& painting, double &maxWidth, std::unordered_map<LayerWrapper*, Contour>& contourMap, const QSize& screenSize);
//static double getMaxLineWidth(LayerWrapper* root); static double getMaxLineWidth(LayerWrapper* nowLayer, QTransform transform);
public: public:
static Renderer::Painting transfromToPainting(QString jsonFilePath); static Renderer::Painting transfromToPainting(QString jsonFilePath);
}; };