Fix: style宽度转换问题

dev-wuyize
wuyize 2023-03-21 13:54:01 +08:00
parent 2581624388
commit d3cf84f479
3 changed files with 130 additions and 100 deletions

View File

@ -1,61 +1,61 @@
#include "PreviewWindow.h" #include "PreviewWindow.h"
#include <QApplication> #include <QApplication>
PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent) PreviewWindow::PreviewWindow(QWidget* parent) : QOpenGLWidget(parent)
{ {
//this->setFixedSize(QSize(108, 108)); //this->setFixedSize(QSize(108, 108));
this->setStyleSheet("border: 1px solid black"); this->setStyleSheet("border: 1px solid black");
this->renderer = Renderer::ElementRenderer::instance(); this->renderer = Renderer::ElementRenderer::instance();
QSurfaceFormat surfaceFormat; QSurfaceFormat surfaceFormat;
surfaceFormat.setSamples(16); surfaceFormat.setSamples(16);
setFormat(surfaceFormat); setFormat(surfaceFormat);
painter = new QPainter(this); painter = new QPainter(this);
painter->setRenderHint(QPainter::SmoothPixmapTransform); painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::HighQualityAntialiasing); painter->setRenderHint(QPainter::HighQualityAntialiasing);
layerManager = nullptr; layerManager = nullptr;
currentLayer = nullptr; currentLayer = nullptr;
zoomStep = 0; zoomStep = 0;
backgroundColor = QColor(255, 255, 255, 255); backgroundColor = QColor(255, 255, 255, 255);
} }
void PreviewWindow::initialize(LayerManager *layerManager,QSize windowSize) void PreviewWindow::initialize(LayerManager* layerManager, QSize windowSize)
{ {
this->logicalSize = windowSize; this->logicalSize = windowSize;
this->layerManager = layerManager; this->layerManager = layerManager;
this->setFixedSize(windowSize); this->setFixedSize(windowSize);
} }
void PreviewWindow::show() void PreviewWindow::show()
{ {
//QFile settingFile; //QFile settingFile;
//settingFile.setFileName("../data.json"); //settingFile.setFileName("../data.json");
//settingFile.open(QFile::ReadOnly); //settingFile.open(QFile::ReadOnly);
//QByteArray setting = settingFile.readAll().trimmed(); //QByteArray setting = settingFile.readAll().trimmed();
//QJsonDocument jsonDoc(QJsonDocument::fromJson(setting)); //QJsonDocument jsonDoc(QJsonDocument::fromJson(setting));
//auto jElements = jsonDoc.object().value("elements").toArray(); //auto jElements = jsonDoc.object().value("elements").toArray();
//for (auto &&ele : jElements) //for (auto &&ele : jElements)
//{ //{
// SimpleElement element(ele.toObject()); // SimpleElement element(ele.toObject());
// painter->drawPath(element.getPaintObject()); // painter->drawPath(element.getPaintObject());
//} //}
} }
void PreviewWindow::initializeGL() void PreviewWindow::initializeGL()
{ {
initializeOpenGLFunctions(); initializeOpenGLFunctions();
} }
void PreviewWindow::paintGL() void PreviewWindow::paintGL()
{ {
glClearColor(backgroundColor.redF(), backgroundColor.greenF(), backgroundColor.blueF(), backgroundColor.alphaF()); glClearColor(backgroundColor.redF(), backgroundColor.greenF(), backgroundColor.blueF(), backgroundColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
painter->begin(this); painter->begin(this);
painter->setWindow(0, 0, logicalSize.width() *devicePixelRatioF(), logicalSize.height() * devicePixelRatioF()); painter->setWindow(0, 0, logicalSize.width() * devicePixelRatioF(), logicalSize.height() * devicePixelRatioF());
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::HighQualityAntialiasing); painter->setRenderHint(QPainter::HighQualityAntialiasing);
layerManager->paint(painter,this->size(),currentLayer); layerManager->paint(painter, this->size(), currentLayer);
painter->end(); painter->end();
} }
void PreviewWindow::resizeGL(int w, int h) void PreviewWindow::resizeGL(int w, int h)
@ -63,64 +63,64 @@ void PreviewWindow::resizeGL(int w, int h)
} }
Renderer::ElementRenderer* const PreviewWindow::getRenderer()const { Renderer::ElementRenderer* const PreviewWindow::getRenderer()const {
return this->renderer; return this->renderer;
} }
void PreviewWindow::currentLayerChanged(LayerWrapper* layer) void PreviewWindow::currentLayerChanged(LayerWrapper* layer)
{ {
this->currentLayer = layer; this->currentLayer = layer;
} }
void PreviewWindow::refresh() void PreviewWindow::refresh()
{ {
this->repaint(); this->repaint();
} }
void PreviewWindow::mousePressEvent(QMouseEvent* event) void PreviewWindow::mousePressEvent(QMouseEvent* event)
{ {
// 当鼠标按下时,记录当前的位置 // 当鼠标按下时,记录当前的位置
m_lastPos = event->pos(); m_lastPos = event->pos();
} }
void PreviewWindow::mouseMoveEvent(QMouseEvent* event) void PreviewWindow::mouseMoveEvent(QMouseEvent* event)
{ {
// 当鼠标移动时,计算移动的距离,并根据需要更新图形的状态 // 当鼠标移动时,计算移动的距离,并根据需要更新图形的状态
int dx = event->x() - m_lastPos.x(); int dx = event->x() - m_lastPos.x();
int dy = event->y() - m_lastPos.y(); int dy = event->y() - m_lastPos.y();
if (currentLayer != nullptr) { if (currentLayer != nullptr) {
if (QApplication::keyboardModifiers() == Qt::ControlModifier && (event->buttons() & Qt::LeftButton)) if (QApplication::keyboardModifiers() == Qt::ControlModifier && (event->buttons() & Qt::LeftButton))
{ {
currentLayer->property.scale.setX(std::max(0.0, currentLayer->property.scale.x() + dx / 50.0)); currentLayer->property.scale.setX(std::max(0.0, currentLayer->property.scale.x() + dx / 50.0));
currentLayer->property.scale.setY(std::max(0.0, currentLayer->property.scale.y() + dy / 50.0)); currentLayer->property.scale.setY(std::max(0.0, currentLayer->property.scale.y() + dy / 50.0));
} }
else if (QApplication::keyboardModifiers() == (Qt::ControlModifier | Qt::ShiftModifier) && (event->buttons() & Qt::LeftButton)) else if (QApplication::keyboardModifiers() == (Qt::ControlModifier | Qt::ShiftModifier) && (event->buttons() & Qt::LeftButton))
{ {
currentLayer->property.scale.setX(std::max(0.0, currentLayer->property.scale.x() * (1.0 + dx / 50.0))); currentLayer->property.scale.setX(std::max(0.0, currentLayer->property.scale.x() * (1.0 + dx / 50.0)));
currentLayer->property.scale.setY(std::max(0.0, currentLayer->property.scale.y() * (1.0 + dx / 50.0))); currentLayer->property.scale.setY(std::max(0.0, currentLayer->property.scale.y() * (1.0 + dx / 50.0)));
} }
else if (event->buttons() & Qt::LeftButton) { else if (event->buttons() & Qt::LeftButton) {
// 如果按下的是左键,那么平移图形 // 如果按下的是左键,那么平移图形
currentLayer->property.offset.setX(currentLayer->property.offset.x() + dx); currentLayer->property.offset.setX(currentLayer->property.offset.x() + dx);
currentLayer->property.offset.setY(currentLayer->property.offset.y() + dy); currentLayer->property.offset.setY(currentLayer->property.offset.y() + dy);
} }
else if (event->buttons() & Qt::RightButton) { else if (event->buttons() & Qt::RightButton) {
// 如果按下的是右键,那么旋转图形 // 如果按下的是右键,那么旋转图形
qreal angle = dx; qreal angle = dx;
currentLayer->property.rotation += angle; currentLayer->property.rotation += angle;
} }
auto layer = currentLayer; auto layer = currentLayer;
while (layer != nullptr) while (layer != nullptr)
{ {
auto index = -1; auto index = -1;
if (typeid(*layer) == typeid(FolderLayerWrapper)) if (typeid(*layer) == typeid(FolderLayerWrapper))
index = dynamic_cast<FolderLayerWrapper*>(layer)->getReferencedBy(); index = dynamic_cast<FolderLayerWrapper*>(layer)->getReferencedBy();
layer = layer->getParent(); layer = layer->getParent();
} }
} }
// 更新上一次的位置 // 更新上一次的位置
emit triggerCentralRefresh(); emit triggerCentralRefresh();
m_lastPos = event->pos(); m_lastPos = event->pos();
this->repaint(); this->repaint();
} }
void PreviewWindow::mouseReleaseEvent(QMouseEvent* event) void PreviewWindow::mouseReleaseEvent(QMouseEvent* event)
@ -131,23 +131,23 @@ void PreviewWindow::mouseReleaseEvent(QMouseEvent* event)
void PreviewWindow::setBackgroundColor(QColor color) void PreviewWindow::setBackgroundColor(QColor color)
{ {
this->backgroundColor = color; this->backgroundColor = color;
this->repaint(); this->repaint();
} }
void PreviewWindow::wheelEvent(QWheelEvent* event) void PreviewWindow::wheelEvent(QWheelEvent* event)
{ {
if (QApplication::keyboardModifiers() == Qt::ControlModifier) if (QApplication::keyboardModifiers() == Qt::ControlModifier)
{ {
if (event->delta() > 0 && zoomStep < ZOOM_STEP_MAX) if (event->delta() > 0 && zoomStep < ZOOM_STEP_MAX)
{ {
zoomStep++; zoomStep++;
this->setFixedSize(logicalSize * (1 + zoomStep * ZOOM_RATE)); this->setFixedSize(logicalSize * (1 + zoomStep * ZOOM_RATE));
} }
else if(event->delta() < 0 && zoomStep > ZOOM_STEP_MIN) else if (event->delta() < 0 && zoomStep > ZOOM_STEP_MIN)
{ {
zoomStep--; zoomStep--;
this->setFixedSize(logicalSize * (1 + zoomStep * ZOOM_RATE)); this->setFixedSize(logicalSize * (1 + zoomStep * ZOOM_RATE));
} }
this->repaint(); this->repaint();
} }
} }

View File

@ -91,22 +91,26 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr
painterPath = trans.map(painterPath); painterPath = trans.map(painterPath);
shared_ptr<vector<vector<Renderer::Point> >> contour = std::make_shared<vector<vector<Renderer::Point> >>(PainterPathUtil::transformToLines(painterPath)); shared_ptr<vector<vector<Renderer::Point> >> contour = std::make_shared<vector<vector<Renderer::Point> >>(PainterPathUtil::transformToLines(painterPath));
QSize screenSize = QSize(1024, 1024); QSize screenSize = QSize(1080, 1080);
ElementTransform elementTransform; ElementTransform elementTransform;
transform = trans.inverted() * transform * QTransform::fromScale(2. / screenSize.width(), 2. / screenSize.height()) * QTransform::fromTranslate(-1, -1) * QTransform::fromScale(1, -1); transform = trans.inverted() * transform * QTransform::fromScale(2. / screenSize.width(), 2. / screenSize.height()) * QTransform::fromTranslate(-1, -1) * QTransform::fromScale(1, -1);
auto baseStyles = leafLayer->styles.toBaseStyles(); auto baseStyles = leafLayer->styles.toBaseStyles();
Renderer::BaseElement element;
element.contour = contour;
for (auto& baseStyle : baseStyles) { for (auto& baseStyle : baseStyles) {
double lineWidth = 0; double lineWidth = 0;
std::shared_ptr<MaterialStyle> material;
if (baseStyle.material->type() == Renderer::MaterialStyleType::kStroke) { if (baseStyle.material->type() == Renderer::MaterialStyleType::kStroke) {
auto material = std::static_pointer_cast<MaterialStyleStroke>(baseStyle.material); std::shared_ptr copy = baseStyle.material->clone();
material->halfWidth /= maxLen; std::static_pointer_cast<MaterialStyleStroke>(copy)->halfWidth /= maxLen;
lineWidth = material->halfWidth; lineWidth = std::static_pointer_cast<MaterialStyleStroke>(copy)->halfWidth;
qDebug() << material->halfWidth; material = copy;
} }
else
material = baseStyle.material;
QPainterPathStroker stroker; QPainterPathStroker stroker;
stroker.setWidth(lineWidth * 2); stroker.setWidth(lineWidth * 2);
stroker.setCapStyle(Qt::RoundCap); stroker.setCapStyle(Qt::RoundCap);
@ -114,17 +118,14 @@ FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTr
QPainterPath strokePath = stroker.createStroke(painterPath); QPainterPath strokePath = stroker.createStroke(painterPath);
auto rect = transform.map(strokePath).boundingRect(); auto rect = transform.map(strokePath).boundingRect();
elementTransform.bound = glm::vec4(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() + rect.height()); elementTransform.bound = glm::vec4(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() + rect.height());
qDebug() << elementTransform.bound.x << elementTransform.bound.y << elementTransform.bound.z << elementTransform.bound.z; //qDebug() << elementTransform.bound.x << elementTransform.bound.y << elementTransform.bound.z << elementTransform.bound.z;
transform = transform.inverted(); transform = transform.inverted();
elementTransform.transform = glm::mat3x2( elementTransform.transform = glm::mat3x2(
transform.m11(), transform.m12(), transform.m21(), transform.m11(), transform.m12(), transform.m21(),
transform.m22(), transform.m31(), transform.m32() transform.m22(), transform.m31(), transform.m32()
); );
//qDebug() << transform;
elementTransform.zIndex = 0; elementTransform.zIndex = 0;
painting.addElement(BaseElement{ contour, material }, elementTransform);
element.style = baseStyle.material;
painting.addElement(element, elementTransform);
} }
return nullptr; return nullptr;

View File

@ -21,6 +21,13 @@
}, },
"name": "ababa2", "name": "ababa2",
"type": "svg-file" "type": "svg-file"
},
{
"data": {
"include": "/svg/4_L0.svg"
},
"name": "4_L0.svg",
"type": "svg-file"
} }
], ],
"height": 1080, "height": 1080,
@ -35,9 +42,9 @@
"name": "Leaf2", "name": "Leaf2",
"styles": [ "styles": [
{ {
"enableEachSideIndependent": false, "enableEachSideIndependent": true,
"left": "AABAQAEAIZwAf///AFqe/w==", "left": "AABAQAEAIZwAf///AFqe/w==",
"right": "AABAQAEACJwAf////1UA/w==", "right": "AABAQAAACJw=",
"type": "stroke" "type": "stroke"
} }
], ],
@ -59,8 +66,8 @@
"referenced-by": 1, "referenced-by": 1,
"transform": { "transform": {
"offset": { "offset": {
"x": 50, "x": 503,
"y": 50 "y": 36
}, },
"rotation": 0, "rotation": 0,
"scale": { "scale": {
@ -77,8 +84,8 @@
], ],
"transform": { "transform": {
"offset": { "offset": {
"x": -2, "x": 1,
"y": 515 "y": 986
}, },
"rotation": 0, "rotation": 0,
"scale": { "scale": {
@ -95,8 +102,8 @@
], ],
"transform": { "transform": {
"offset": { "offset": {
"x": -503, "x": -959,
"y": -1 "y": -5
}, },
"rotation": 0, "rotation": 0,
"scale": { "scale": {
@ -113,8 +120,30 @@
], ],
"transform": { "transform": {
"offset": { "offset": {
"x": -503, "x": -958,
"y": 511 "y": 980
},
"rotation": 0,
"scale": {
"x": 1,
"y": 1
}
}
},
{
"element": 3,
"is-folder": false,
"name": "子图层-5",
"styles": [
{
"material": "AH8A/wBanv8=",
"type": "fill"
}
],
"transform": {
"offset": {
"x": 473,
"y": 419
}, },
"rotation": 0, "rotation": 0,
"scale": { "scale": {