From 0d42af920074ee7847e50fab0988f078dd8e7b31 Mon Sep 17 00:00:00 2001 From: wuyize Date: Mon, 13 Mar 2023 13:46:17 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=BD=A9=E7=BB=98?= =?UTF-8?q?=E7=BA=B9=E7=90=86=E9=83=A8=E5=88=86=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../res/Shaders/painting.comp | 2 +- .../src/Renderer/Mesh.h | 9 ++- .../src/Renderer/Model.cpp | 68 +++++++++++-------- .../src/Renderer/Model.h | 6 +- 4 files changed, 49 insertions(+), 36 deletions(-) diff --git a/ArchitectureColoredPainting/res/Shaders/painting.comp b/ArchitectureColoredPainting/res/Shaders/painting.comp index ffd0e38..e7280a0 100644 --- a/ArchitectureColoredPainting/res/Shaders/painting.comp +++ b/ArchitectureColoredPainting/res/Shaders/painting.comp @@ -1190,7 +1190,7 @@ void main() //imageStore(gBaseColor, pixelLocation, vec4(uv,1,1)); //imageStore(gMetallicRoughness, pixelLocation, vec4(uv,1,1)); //return; - uv = vec2(1)-uv*2; + uv = uv*2-vec2(1); //vec2 uv = imageLoad(gPaintingTexCoord, pixelLocation).rg; vec3 debugBVH = vec3(0); diff --git a/ArchitectureColoredPainting/src/Renderer/Mesh.h b/ArchitectureColoredPainting/src/Renderer/Mesh.h index dd1d705..ec452fd 100644 --- a/ArchitectureColoredPainting/src/Renderer/Mesh.h +++ b/ArchitectureColoredPainting/src/Renderer/Mesh.h @@ -2,23 +2,22 @@ #include #include #include -#include -#include #include #include #include #include #include #include +#include #include "Drawable.h" namespace Renderer { struct Vertex { - QVector3D Position; - QVector3D Normal; - QVector2D TexCoords; + glm::vec3 Position; + glm::vec3 Normal; + glm::vec2 TexCoords; Vertex(const aiVector3D& position, const aiVector3D& Normal, const aiVector3D& TexCoords); }; diff --git a/ArchitectureColoredPainting/src/Renderer/Model.cpp b/ArchitectureColoredPainting/src/Renderer/Model.cpp index a22be70..644863d 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Model.cpp @@ -50,7 +50,7 @@ void Renderer::Model::loadModel(QString path) directory = modelFile.dir(); Assimp::Importer importer; - const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate | aiProcess_FlipUVs); + const aiScene* scene = importer.ReadFile(modelFile.absoluteFilePath().toUtf8(), aiProcess_Triangulate /*| aiProcess_FlipUVs*/); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { qCritical() << "ERROR::ASSIMP::" << importer.GetErrorString() << endl; @@ -117,12 +117,8 @@ void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4) std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 model) { - aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; QMatrix4x4 modelQ((float*)&model); - aiString str; - material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); - std::vector vertices; for (unsigned int i = 0; i < mesh->mNumVertices; i++) { @@ -139,45 +135,59 @@ std::unique_ptr Model::processMesh(aiMesh* mesh, const aiScene* scene, maxZ = std::max(maxZ, worldPos.z); } } + std::vector indices; for (auto face = mesh->mFaces; face < mesh->mFaces + mesh->mNumFaces; face++) indices.insert(indices.end(), face->mIndices, face->mIndices + face->mNumIndices); - - if (auto iter = paintingMap.find(std::string(str.C_Str())); paintingProgram != nullptr && iter != paintingMap.end()) + aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; + + if (auto iter = paintingMap.find([&] { + aiString str; + material->GetTexture(aiTextureType_BASE_COLOR, 0, &str); + return std::string(str.C_Str()); + }()); paintingProgram != nullptr && iter != paintingMap.end()) { + qDebug() << iter->first.c_str() << "Replaced"; + + auto mesh = std::make_unique(glFunc, paintingProgram, shadowProgram, modelQ); + auto& [paintingPath, leftBottom, rightTop] = iter->second; - qDebug() << str.C_Str() << "Replaced"; - auto m_mesh = std::make_unique(glFunc, paintingProgram, shadowProgram, modelQ); - m_mesh->vertices = vertices; - m_mesh->indices = indices; - - m_mesh->paintingId = loadPainting(paintingPath); - auto& handle = vtManager->getPaintingHandle(m_mesh->paintingId); - m_mesh->textureBasecolor = handle.baseColor; - m_mesh->textureMetallicRoughness = handle.metallicRoughness; - m_mesh->setupMesh(); - return m_mesh; + for (auto& v : vertices) + { + //qDebug() << v.TexCoords.x << v.TexCoords.y; + v.TexCoords = (v.TexCoords - leftBottom) / (rightTop - leftBottom); + qDebug() << v.TexCoords.x << v.TexCoords.y; + } + + mesh->vertices = vertices; + mesh->indices = indices; + + mesh->paintingId = loadPainting(paintingPath); + auto& handle = vtManager->getPaintingHandle(mesh->paintingId); + mesh->textureBasecolor = handle.baseColor; + mesh->textureMetallicRoughness = handle.metallicRoughness; + mesh->setupMesh(); + return mesh; } else { - auto m_mesh = std::make_unique(glFunc, shaderProgram, shadowProgram, modelQ); - m_mesh->vertices = vertices; - m_mesh->indices = indices; + auto mesh = std::make_unique(glFunc, shaderProgram, shadowProgram, modelQ); + mesh->vertices = vertices; + mesh->indices = indices; - // 处理材质 - if (!(m_mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) + if (!(mesh->textureBasecolor = loadMaterialTextures(material, aiTextureType_BASE_COLOR))) qWarning() << "Basecolor Texture Loading Failed!"; - if (!(m_mesh->textureMetallicRoughness = loadMaterialTextures(material, aiTextureType_METALNESS))) + if (!(mesh->textureMetallicRoughness = loadMaterialTextures(material, aiTextureType_METALNESS))) qWarning() << "MetallicRoughness Texture Loading Failed!"; - if (!(m_mesh->textureNormal = loadMaterialTextures(material, aiTextureType_NORMALS))) + if (!(mesh->textureNormal = loadMaterialTextures(material, aiTextureType_NORMALS))) qWarning() << "Normal Texture Loading Failed!"; - if (m_mesh->textureBasecolor && m_mesh->textureMetallicRoughness && m_mesh->textureNormal) + if (mesh->textureBasecolor && mesh->textureMetallicRoughness && mesh->textureNormal) { - m_mesh->setupMesh(); - return m_mesh; + mesh->setupMesh(); + return mesh; } else return nullptr; @@ -208,7 +218,7 @@ GLuint Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type) texture.setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat); texture.setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat); texture.setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::Linear); - texture.setData(data); + texture.setData(data.mirrored()); return texture.textureId(); } diff --git a/ArchitectureColoredPainting/src/Renderer/Model.h b/ArchitectureColoredPainting/src/Renderer/Model.h index c19fdf3..d9a6bea 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.h +++ b/ArchitectureColoredPainting/src/Renderer/Model.h @@ -24,7 +24,11 @@ namespace Renderer QOpenGLShaderProgram* shadowProgram = nullptr; VirtualTextureManager* vtManager = nullptr; - std::unordered_map> paintingMap; + /** + * @param key BaseColor路径 + * @param value json路径, 纹理坐标 + */ + std::unordered_map> paintingMap; std::unordered_map paintingLoaded; std::unordered_map texturesLoaded; std::vector> meshes; From ed4c3c00647cf36d9b02b844056c9620e84b0501 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Mon, 13 Mar 2023 20:47:35 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E3=80=90=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E9=94=99=E8=AF=AF=E7=9A=84=E3=80=91LayerBounding?= =?UTF-8?q?=E8=99=9A=E7=BA=BF=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/EditorWidgetItem.cpp | 1 + .../src/Editor/GraphicElement.cpp | 1 + .../src/Editor/PreviewWindow.cpp | 19 +++++++++++++++++++ .../src/Editor/PreviewWindow.h | 6 ++++++ .../src/Editor/RightBar/LayerTreeWidget.cpp | 12 ++++++++---- .../src/Editor/RightBar/LayerTreeWidget.h | 2 +- 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp index 01ca5b3..9411fe8 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp @@ -14,6 +14,7 @@ EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(p elementInfoDisplayWidget = dynamic_cast(tabWidget->widget(1)); qDebug() << layerInfoDisplayWidget; qDebug() << elementInfoDisplayWidget; + connect(treeWidget, &LayerTreeWidget::displayLayerChange, previewWindow, &PreviewWindow::currentLayerChanged); connect(treeWidget, &LayerTreeWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh); connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh); connect(treeWidget, &LayerTreeWidget::displayLayerChange, this, &EditorWidgetItem::onLayerChange); diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 0c7bf52..403ae3f 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -47,6 +47,7 @@ PixelPath SimpleElement::getPaintObject(std::vector> PixelPath result; Renderer::ElementStyleStrokeDemo demo(2); auto [img, mov] = renderer->drawElement(painterPath, demo, 1.0); + //qDebug() << mov; //qDebug() << img << " ------"; result.addImage(img, mov); //result.addPath(painterPath); diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index 0db3c48..2bdcdc7 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -10,6 +10,7 @@ PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent) painter->setRenderHint(QPainter::SmoothPixmapTransform); painter->setRenderHint(QPainter::HighQualityAntialiasing); layerManager = nullptr; + currentLayer = nullptr; } void PreviewWindow::initialize(LayerManager *layerManager,QSize windowSize) @@ -47,6 +48,14 @@ void PreviewWindow::paintGL() painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::HighQualityAntialiasing); layerManager->paint(painter,this->size()); + if (currentLayer != nullptr) + { + QPen pen = painter->pen(); // 获取当前的画笔 + pen.setStyle(Qt::DashLine); // 设置画笔样式为虚线 + painter->setPen(pen); // 应用画笔设置 + painter->drawRect(this->currentLayer->getCache().getBoundingRect()); // 绘制矩形 + } + painter->end(); } @@ -56,4 +65,14 @@ void PreviewWindow::resizeGL(int w, int h) Renderer::ElementRenderer* const PreviewWindow::getRenderer()const { return this->renderer; +} + +void PreviewWindow::currentLayerChanged(LayerWrapper* layer) +{ + this->currentLayer = layer; +} + +void PreviewWindow::refresh() +{ + this->repaint(); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.h b/ArchitectureColoredPainting/src/Editor/PreviewWindow.h index 9a0a4bc..9c89f80 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.h +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.h @@ -19,6 +19,8 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions LayerManager *layerManager; Renderer::ElementRenderer* renderer; QSize logicalSize; + QRectF viewportRect; + LayerWrapper* currentLayer; public: PreviewWindow(QWidget *parent = nullptr); @@ -28,4 +30,8 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions void paintGL() override; void resizeGL(int w, int h) override; Renderer::ElementRenderer* const getRenderer()const; + + public slots: + void currentLayerChanged(LayerWrapper*); + void refresh(); }; diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index 1889d70..f6962db 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -13,10 +13,14 @@ LayerTreeWidget::LayerTreeWidget(QWidget *parent) connect(this, &QTreeWidget::customContextMenuRequested, this, &LayerTreeWidget::popMenu); connect(this, &QTreeWidget::currentItemChanged, [=](QTreeWidgetItem *currentItem) { this->selectedItem = currentItem; - if(this->selectedItem !=nullptr) - emit displayLayerChange(this->selectedItem->data(0, Qt::UserRole).value()); - else - emit displayLayerChange(nullptr); + if (this->selectedItem != nullptr) { + auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + emit displayLayerChange(layer); + } + else { + emit displayLayerChange(nullptr); + } + emit requireRefreshPreview(); }); // connect(this, &QTreeWidget::itemDoubleClicked, this, &LayerTreeWidget::onItemDoubleClicked); } diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h index ca784fd..e7472dd 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.h @@ -20,7 +20,7 @@ class LayerTreeWidget : public QTreeWidget // void mouseDoubleClickEvent(QMouseEvent *event) override; // void onItemDoubleClicked(QTreeWidgetItem *item, int column = 0); - signals: +signals: void displayLayerChange(LayerWrapper *); void requireRefreshPreview(); void requireRefreshElementWidget(); From de9d7143b6260f606cfe37f6cb1a9a2cb8d50731 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Mon, 13 Mar 2023 21:06:05 +0800 Subject: [PATCH 3/8] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E3=80=90=E5=8F=AF?= =?UTF-8?q?=E8=83=BD=E5=AD=98=E5=9C=A8=E9=97=AE=E9=A2=98=E7=9A=84=E3=80=91?= =?UTF-8?q?=E6=8B=96=E6=8B=BD=E5=92=8C=E6=97=8B=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/EditorWidgetItem.cpp | 1 + .../src/Editor/PreviewWindow.cpp | 36 +++++++++++++++++++ .../src/Editor/PreviewWindow.h | 10 +++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp index 9411fe8..af02032 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp @@ -14,6 +14,7 @@ EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(p elementInfoDisplayWidget = dynamic_cast(tabWidget->widget(1)); qDebug() << layerInfoDisplayWidget; qDebug() << elementInfoDisplayWidget; + connect(previewWindow, &PreviewWindow::layerInfoChanged, layerInfoDisplayWidget, &InfoDisplayWidget::triggerSelfRefresh); connect(treeWidget, &LayerTreeWidget::displayLayerChange, previewWindow, &PreviewWindow::currentLayerChanged); connect(treeWidget, &LayerTreeWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh); connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh); diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index 2bdcdc7..af17bbf 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -75,4 +75,40 @@ void PreviewWindow::currentLayerChanged(LayerWrapper* layer) void PreviewWindow::refresh() { this->repaint(); +} + +void PreviewWindow::mousePressEvent(QMouseEvent* event) +{ + // 当鼠标按下时,记录当前的位置 + m_lastPos = event->pos(); +} + +void PreviewWindow::mouseMoveEvent(QMouseEvent* event) +{ + // 当鼠标移动时,计算移动的距离,并根据需要更新图形的状态 + int dx = event->x() - m_lastPos.x(); + int dy = event->y() - m_lastPos.y(); + if (currentLayer != nullptr) { + if (event->buttons() & Qt::LeftButton) { + // 如果按下的是左键,那么平移图形 + currentLayer->property.offset.setX(currentLayer->property.offset.x() + dx); + currentLayer->property.offset.setY(currentLayer->property.offset.y() + dy); + qDebug() << dx << "----" << dy; + emit layerInfoChanged(); + } + else if (event->buttons() & Qt::RightButton) { + // 如果按下的是右键,那么旋转图形 + qreal angle = -sqrt(dx * dx + dy * dy) / 1.0; + currentLayer->property.rotation += angle; + emit layerInfoChanged(); + } + } + // 更新上一次的位置 + m_lastPos = event->pos(); + this->repaint(); +} + +void PreviewWindow::mouseReleaseEvent(QMouseEvent* event) +{ + // 当鼠标释放时,不做任何操作 } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.h b/ArchitectureColoredPainting/src/Editor/PreviewWindow.h index 9c89f80..66f4a52 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.h +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "../Renderer/Preview/ElementRenderer.h" @@ -21,7 +22,11 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions QSize logicalSize; QRectF viewportRect; LayerWrapper* currentLayer; - + QPointF m_lastPos; + void mousePressEvent(QMouseEvent* event) override; + void mouseMoveEvent(QMouseEvent* event) override; + void mouseReleaseEvent(QMouseEvent* event) override; + public: PreviewWindow(QWidget *parent = nullptr); void initialize(LayerManager *layerManager, QSize windowSize = QSize(1920, 1080)); @@ -34,4 +39,7 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions public slots: void currentLayerChanged(LayerWrapper*); void refresh(); + + signals: + void layerInfoChanged(); }; From 4c9fe168a939f8bb266d42968ee54b7526527f5c Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Mon, 13 Mar 2023 23:36:31 +0800 Subject: [PATCH 4/8] =?UTF-8?q?bug:=20pixmap=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EditorWidgetItem.ui | 19 ++++++++++++++--- .../src/Editor/EditorWidgetItem.cpp | 4 ++++ .../src/Editor/LayerManager.cpp | 4 ++-- .../src/Editor/LayerManager.h | 2 +- .../src/Editor/LayerWrapper.cpp | 21 ++++++++++++------- .../src/Editor/LayerWrapper.h | 8 +++---- .../src/Editor/PixelPath.cpp | 15 +++++++++++++ .../src/Editor/PixelPath.h | 1 + .../src/Editor/PreviewWindow.cpp | 11 ++-------- 9 files changed, 58 insertions(+), 27 deletions(-) diff --git a/ArchitectureColoredPainting/EditorWidgetItem.ui b/ArchitectureColoredPainting/EditorWidgetItem.ui index 93811f0..aea28b0 100644 --- a/ArchitectureColoredPainting/EditorWidgetItem.ui +++ b/ArchitectureColoredPainting/EditorWidgetItem.ui @@ -6,8 +6,8 @@ 0 0 - 1124 - 1010 + 1473 + 1103 @@ -56,7 +56,20 @@ - + + + + 1080 + 1080 + + + + + 1080 + 1080 + + + diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp index af02032..0a3f01b 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetItem.cpp @@ -59,7 +59,11 @@ EditorWidgetItem::~EditorWidgetItem() void EditorWidgetItem::paintEvent(QPaintEvent *event) { + QPainter painter(this); + // 设置画刷的颜色为灰色,并填充整个窗口区域 + painter.setBrush(Qt::gray); + painter.drawRect(this->rect()); } void EditorWidgetItem::onLayerChange(LayerWrapper *layer) diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp index a9b73b8..958e958 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp @@ -11,9 +11,9 @@ LayerWrapper *LayerManager::getRoot() const { return root; } -void LayerManager::paint(QPainter *painter, QSize size) const +void LayerManager::paint(QPainter *painter, QSize size,LayerWrapper* selecetedLayer) const { - auto p = root->getCache().resizedPixel(size); + auto p = root->getCache(selecetedLayer).resizedPixel(size); painter->drawPixmap(0, 0, p); } bool LayerManager::singleSelectedCheck() const diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.h b/ArchitectureColoredPainting/src/Editor/LayerManager.h index 390cfed..6f065fc 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.h +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.h @@ -32,7 +32,7 @@ class LayerManager LayerManager() = default; LayerManager(QJsonObject source, ElementManager* elementManager); QJsonObject toJson() const; - void paint(QPainter *painter, QSize size) const; + void paint(QPainter *painter, QSize size, LayerWrapper* selecetedLayer=nullptr) const; bool rename(QString newName) const; bool combine() const; // bool seperate() const; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index c149185..a38eff5 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -23,9 +23,13 @@ FolderLayerWrapper*LayerWrapper::getParent() const return this == nullptr ? nullptr : this->parent; } -PixelPath LayerWrapper::getCache() +PixelPath LayerWrapper::getCache(LayerWrapper* selectedLayer) { - this->refresh(); + this->refresh(selectedLayer); + if (selectedLayer == this) + { + this->cache.highLight(); + } return cache; } @@ -82,28 +86,29 @@ void LayerWrapper::SimpleProperty::apply(PixelPath&cache) const double centerY = cache.getBoundingRect().center().y(); //qDebug() << name << " " << cache.boundingRect().center(); //qDebug() << name << " " << cache.boundingRect(); + trans.translate(centerX, centerY); trans.translate(offset.x(), offset.y()); - trans.translate(-centerX, -centerY); + qDebug() << offset; trans.rotate(rotation); trans.scale(scale.x(), scale.y()); - trans.translate(centerX, centerY); + trans.translate(-centerX, -centerY); cache = cache.trans(trans); } -void LayerWrapper::refresh() +void LayerWrapper::refresh(LayerWrapper* layer) { property.apply(cache); } -void FolderLayerWrapper::refresh() +void FolderLayerWrapper::refresh(LayerWrapper* layer) { cache.clear(); for (auto& child : children) { - cache.addPath(child.get()->getCache()); + cache.addPath(child.get()->getCache(layer)); } LayerWrapper::refresh(); } -void LeafLayerWrapper::refresh() +void LeafLayerWrapper::refresh(LayerWrapper* layer) { cache.clear(); if (wrappedElement != nullptr) diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 84e7cab..5728b70 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -47,10 +47,10 @@ class LayerWrapper void apply(PixelPath&cache) const; } property; virtual void setParent(FolderLayerWrapper*newParent); - virtual void refresh(); + virtual void refresh(LayerWrapper* layer = nullptr); virtual QTreeWidgetItem* getQTreeItem(); // TODO: 将QPainterPath改为BitmapPath/QImage,或者直接将其删除,绘制时直接使用BitmapPath的paint方法 - virtual PixelPath getCache(); + virtual PixelPath getCache(LayerWrapper* selectedLayer=nullptr); FolderLayerWrapper*getParent() const; // invoke by manager, then invoke parent's applyStyles LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementManager* elementManager=nullptr); LayerWrapper() = default; @@ -75,7 +75,7 @@ class FolderLayerWrapper : public LayerWrapper public: ~FolderLayerWrapper() = default; - void refresh() override; + void refresh(LayerWrapper* layer=nullptr) override; FolderLayerWrapper() = default; FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); void addChild(shared_ptr child); @@ -97,7 +97,7 @@ class LeafLayerWrapper : public LayerWrapper public: ~LeafLayerWrapper() = default; - void refresh() override; + void refresh(LayerWrapper* layer = nullptr) override; LeafLayerWrapper() = default; LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; diff --git a/ArchitectureColoredPainting/src/Editor/PixelPath.cpp b/ArchitectureColoredPainting/src/Editor/PixelPath.cpp index 447f8aa..89f4bbc 100644 --- a/ArchitectureColoredPainting/src/Editor/PixelPath.cpp +++ b/ArchitectureColoredPainting/src/Editor/PixelPath.cpp @@ -102,4 +102,19 @@ QPixmap PixelPath::getDetail()const qDebug() << rect; result = pixmap.copy(rect); return result; +} + +void PixelPath::highLight() +{ + // 创建一个QPainter对象,关联到QPixmap对象 + QPainter painter(&pixmap); + + // 设置画笔的颜色、宽度和样式 + painter.setPen(QPen(Qt::black, 1, Qt::DashLine)); + + // 绘制一个矩形,指定左上角和右下角的坐标 + painter.drawRect(boundingRect); + + // 结束绘制 + painter.end(); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/PixelPath.h b/ArchitectureColoredPainting/src/Editor/PixelPath.h index 33268db..a764ce8 100644 --- a/ArchitectureColoredPainting/src/Editor/PixelPath.h +++ b/ArchitectureColoredPainting/src/Editor/PixelPath.h @@ -26,4 +26,5 @@ public: PixelPath trans(QTransform& mat)const; QPixmap resizedPixel(QSize size)const; QPixmap getDetail()const; + void highLight(); }; \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index af17bbf..723f0b1 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -2,6 +2,7 @@ PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent) { + this->setFixedSize(QSize(1080, 1080)); this->renderer = Renderer::ElementRenderer::instance(); QSurfaceFormat surfaceFormat; surfaceFormat.setSamples(16); @@ -47,15 +48,7 @@ void PreviewWindow::paintGL() painter->begin(this); painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::HighQualityAntialiasing); - layerManager->paint(painter,this->size()); - if (currentLayer != nullptr) - { - QPen pen = painter->pen(); // 获取当前的画笔 - pen.setStyle(Qt::DashLine); // 设置画笔样式为虚线 - painter->setPen(pen); // 应用画笔设置 - painter->drawRect(this->currentLayer->getCache().getBoundingRect()); // 绘制矩形 - } - + layerManager->paint(painter,this->size(),currentLayer); painter->end(); } From 3f1421a1bd0c73f4f2ab56cb9ae3c32be5443337 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Tue, 14 Mar 2023 00:20:31 +0800 Subject: [PATCH 5/8] =?UTF-8?q?bug:=E9=BB=91=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/LayerManager.cpp | 4 +- .../src/Editor/LayerWrapper.cpp | 47 ++++++++++++++++++- .../src/Editor/LayerWrapper.h | 4 ++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp index 958e958..06f0170 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp @@ -13,8 +13,8 @@ LayerWrapper *LayerManager::getRoot() const } void LayerManager::paint(QPainter *painter, QSize size,LayerWrapper* selecetedLayer) const { - auto p = root->getCache(selecetedLayer).resizedPixel(size); - painter->drawPixmap(0, 0, p); + root->getCache(); + root->paint(painter); } bool LayerManager::singleSelectedCheck() const { diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index a38eff5..9e1febe 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -79,6 +79,7 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana int elementIndex = json.value("element").toInt(); wrappedElement = elementManager->getElementById(elementIndex); } + void LayerWrapper::SimpleProperty::apply(PixelPath&cache) const { QTransform trans; @@ -88,12 +89,27 @@ void LayerWrapper::SimpleProperty::apply(PixelPath&cache) const //qDebug() << name << " " << cache.boundingRect(); trans.translate(centerX, centerY); trans.translate(offset.x(), offset.y()); - qDebug() << offset; trans.rotate(rotation); trans.scale(scale.x(), scale.y()); trans.translate(-centerX, -centerY); cache = cache.trans(trans); } + +QTransform LayerWrapper::getTransform() +{ + QTransform trans; + double centerX = cache.getBoundingRect().center().x(); + double centerY = cache.getBoundingRect().center().y(); + //qDebug() << name << " " << cache.boundingRect().center(); + //qDebug() << name << " " << cache.boundingRect(); + trans.translate(centerX, centerY); + trans.translate(property.offset.x(), property.offset.y()); + trans.rotate(property.rotation); + trans.scale(property.scale.x(), property.scale.y()); + trans.translate(-centerX, -centerY); + return trans; +} + void LayerWrapper::refresh(LayerWrapper* layer) { property.apply(cache); @@ -232,4 +248,33 @@ int FolderLayerWrapper::getReferencedBy()const return this->elementManager->getLayerReferencedBy(this); else return -1; +} + +void LayerWrapper::paint(QPainter* painter) +{ + +} + +void FolderLayerWrapper::paint(QPainter* painter) +{ + for (auto& child : children) + child->paint(painter); +} + +void LeafLayerWrapper::paint(QPainter* painter) +{ + if (wrappedElement != nullptr) + { + //painter->save(); + QTransform trans; + LayerWrapper* layer = this; + while (layer != nullptr) { + trans *= layer->getTransform(); + layer = layer->getParent(); + } + auto p = wrappedElement->getPaintObject(&this->styles); + p.trans(trans); + painter->drawPixmap(0, 0, p.getPixmap()); + //painter->restore(); + } } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 5728b70..2186cd6 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -51,9 +51,11 @@ class LayerWrapper virtual QTreeWidgetItem* getQTreeItem(); // TODO: 将QPainterPath改为BitmapPath/QImage,或者直接将其删除,绘制时直接使用BitmapPath的paint方法 virtual PixelPath getCache(LayerWrapper* selectedLayer=nullptr); + QTransform getTransform(); FolderLayerWrapper*getParent() const; // invoke by manager, then invoke parent's applyStyles LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementManager* elementManager=nullptr); LayerWrapper() = default; + virtual void paint(QPainter* painter); // TODO : export Function // virtual LayerWrapper *addChild() = 0; // Leaf Child Only // virtual LayerWrapper *addParent() = 0; // Folder Parent Only @@ -86,6 +88,7 @@ class FolderLayerWrapper : public LayerWrapper QTreeWidgetItem* getQTreeItem() override; QJsonObject toJson() const override; int getReferencedBy()const; + void paint(QPainter* painter) override; }; class LeafLayerWrapper : public LayerWrapper @@ -101,6 +104,7 @@ class LeafLayerWrapper : public LayerWrapper LeafLayerWrapper() = default; LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; + void paint(QPainter* painter) override; }; Q_DECLARE_METATYPE(LayerWrapper *) From 72f0f78e644a783aa8096a479a25804c8d37e936 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Tue, 14 Mar 2023 14:53:36 +0800 Subject: [PATCH 6/8] bug fix --- .../src/Editor/GraphicElement.cpp | 15 ++++++ .../src/Editor/GraphicElement.h | 3 ++ .../src/Editor/LayerWrapper.cpp | 53 ++++++++++--------- .../src/Editor/LayerWrapper.h | 10 ++-- .../src/Editor/RightBar/LayerTreeWidget.cpp | 4 ++ 5 files changed, 57 insertions(+), 28 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 403ae3f..d3dc67b 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -83,4 +83,19 @@ QJsonObject GraphicElement::toJson() const QJsonObject result; result.insert("name", name); return result; +} + +void SimpleElement::paint(QPainter* painter, QTransform transform, vector> styles) +{ + Renderer::ElementStyleStrokeDemo demo(2); + auto [img, mov] = renderer->drawElement(painterPath, demo, 1.0); + painter->save(); + painter->setTransform(transform); + painter->drawImage(mov, img); + painter->restore(); +} + +void GroupElement::paint(QPainter* painter, QTransform transform, vector> styles) +{ + sourceLayer->paint(painter, transform); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index c0b3c09..577f7db 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -28,6 +28,7 @@ public: virtual QJsonObject toJson() const; virtual PixelPath getPaintObject() const = 0; virtual PixelPath getPaintObject(std::vector>*) const = 0; + virtual void paint(QPainter* painter, QTransform transform, std::vector> styles) = 0; }; class SimpleElement : public GraphicElement @@ -44,6 +45,7 @@ public: ~SimpleElement() = default; PixelPath getPaintObject() const override; PixelPath getPaintObject(std::vector>*) const override; + void paint(QPainter* painter, QTransform transform, std::vector> styles) override; }; class GroupElement : public GraphicElement @@ -58,6 +60,7 @@ public: PixelPath getPaintObject() const override; PixelPath getPaintObject(std::vector>*) const override; void setSourceLayer(FolderLayerWrapper* sourceLayer); + void paint(QPainter* painter, QTransform transform, std::vector> styles) override; }; //******************************** BitmapPath ********************************// diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 9e1febe..c914236 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -46,6 +46,7 @@ LayerWrapper::LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementM property.scale = {transformJson.value("scale").toObject().value("x").toDouble(), transformJson.value("scale").toObject().value("y").toDouble()}; property.rotation = {transformJson.value("rotation").toDouble()}; + selected = false; } FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent) @@ -80,19 +81,19 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana wrappedElement = elementManager->getElementById(elementIndex); } -void LayerWrapper::SimpleProperty::apply(PixelPath&cache) const +void LayerWrapper::SimpleProperty::apply(PixelPath&cache) { - QTransform trans; + transform.reset(); double centerX = cache.getBoundingRect().center().x(); double centerY = cache.getBoundingRect().center().y(); //qDebug() << name << " " << cache.boundingRect().center(); //qDebug() << name << " " << cache.boundingRect(); - trans.translate(centerX, centerY); - trans.translate(offset.x(), offset.y()); - trans.rotate(rotation); - trans.scale(scale.x(), scale.y()); - trans.translate(-centerX, -centerY); - cache = cache.trans(trans); + transform.translate(-centerX, -centerY); + transform.translate(offset.x(), offset.y()); + transform.rotate(rotation); + transform.scale(scale.x(), scale.y()); + transform.translate(centerX, centerY); + cache = cache.trans(transform); } QTransform LayerWrapper::getTransform() @@ -250,31 +251,35 @@ int FolderLayerWrapper::getReferencedBy()const return -1; } -void LayerWrapper::paint(QPainter* painter) +void LayerWrapper::paint(QPainter* painter, QTransform transform) { - + if (this->selected) + { + painter->save(); + painter->setTransform(transform); + painter->setPen(QPen(Qt::gray, 2)); + painter->setPen(Qt::DashLine); + painter->drawRect(cache.getBoundingRect()); + painter->restore(); + } } -void FolderLayerWrapper::paint(QPainter* painter) +void FolderLayerWrapper::paint(QPainter* painter, QTransform transform) { + LayerWrapper::paint(painter, transform); + transform = property.transform * transform; + qDebug() << transform; for (auto& child : children) - child->paint(painter); + child->paint(painter, transform); } -void LeafLayerWrapper::paint(QPainter* painter) +void LeafLayerWrapper::paint(QPainter* painter, QTransform transform) { + LayerWrapper::paint(painter, transform); + transform = property.transform * transform; + qDebug() << transform; if (wrappedElement != nullptr) { - //painter->save(); - QTransform trans; - LayerWrapper* layer = this; - while (layer != nullptr) { - trans *= layer->getTransform(); - layer = layer->getParent(); - } - auto p = wrappedElement->getPaintObject(&this->styles); - p.trans(trans); - painter->drawPixmap(0, 0, p.getPixmap()); - //painter->restore(); + wrappedElement->paint(painter, transform, styles); } } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 2186cd6..7df5d13 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -35,6 +35,7 @@ class LayerWrapper public: QTreeWidgetItem* qTreeWidgetItem; + bool selected; struct SimpleProperty { QString name = ""; @@ -43,8 +44,9 @@ class LayerWrapper double rotation = 0; bool flipHorizontally = 0; bool flipVertically = 0; + QTransform transform; // TODO: 将QPainterPath改为BitmapPath - void apply(PixelPath&cache) const; + void apply(PixelPath&cache); } property; virtual void setParent(FolderLayerWrapper*newParent); virtual void refresh(LayerWrapper* layer = nullptr); @@ -55,7 +57,7 @@ class LayerWrapper FolderLayerWrapper*getParent() const; // invoke by manager, then invoke parent's applyStyles LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementManager* elementManager=nullptr); LayerWrapper() = default; - virtual void paint(QPainter* painter); + virtual void paint(QPainter* painter, QTransform transform=QTransform()); // TODO : export Function // virtual LayerWrapper *addChild() = 0; // Leaf Child Only // virtual LayerWrapper *addParent() = 0; // Folder Parent Only @@ -88,7 +90,7 @@ class FolderLayerWrapper : public LayerWrapper QTreeWidgetItem* getQTreeItem() override; QJsonObject toJson() const override; int getReferencedBy()const; - void paint(QPainter* painter) override; + void paint(QPainter* painter, QTransform transform = QTransform()) override; }; class LeafLayerWrapper : public LayerWrapper @@ -104,7 +106,7 @@ class LeafLayerWrapper : public LayerWrapper LeafLayerWrapper() = default; LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; - void paint(QPainter* painter) override; + void paint(QPainter* painter, QTransform transform = QTransform()) override; }; Q_DECLARE_METATYPE(LayerWrapper *) diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index f6962db..1e84c88 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -12,9 +12,13 @@ LayerTreeWidget::LayerTreeWidget(QWidget *parent) this->setHeaderLabel("Layer Content"); connect(this, &QTreeWidget::customContextMenuRequested, this, &LayerTreeWidget::popMenu); connect(this, &QTreeWidget::currentItemChanged, [=](QTreeWidgetItem *currentItem) { + if (this->selectedItem != nullptr) { + this->selectedItem->data(0, Qt::UserRole).value()->selected = false; + } this->selectedItem = currentItem; if (this->selectedItem != nullptr) { auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + layer->selected = true; emit displayLayerChange(layer); } else { From 684c28dafd2dd99c0831dabfcb2d8af65d336a64 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Tue, 14 Mar 2023 14:53:36 +0800 Subject: [PATCH 7/8] bug fix --- .../src/Editor/GraphicElement.cpp | 15 ++++++ .../src/Editor/GraphicElement.h | 3 ++ .../src/Editor/LayerWrapper.cpp | 53 ++++++++++--------- .../src/Editor/LayerWrapper.h | 10 ++-- .../src/Editor/PreviewWindow.cpp | 4 +- .../src/Editor/RightBar/LayerTreeWidget.cpp | 4 ++ 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 403ae3f..d3dc67b 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -83,4 +83,19 @@ QJsonObject GraphicElement::toJson() const QJsonObject result; result.insert("name", name); return result; +} + +void SimpleElement::paint(QPainter* painter, QTransform transform, vector> styles) +{ + Renderer::ElementStyleStrokeDemo demo(2); + auto [img, mov] = renderer->drawElement(painterPath, demo, 1.0); + painter->save(); + painter->setTransform(transform); + painter->drawImage(mov, img); + painter->restore(); +} + +void GroupElement::paint(QPainter* painter, QTransform transform, vector> styles) +{ + sourceLayer->paint(painter, transform); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index c0b3c09..577f7db 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -28,6 +28,7 @@ public: virtual QJsonObject toJson() const; virtual PixelPath getPaintObject() const = 0; virtual PixelPath getPaintObject(std::vector>*) const = 0; + virtual void paint(QPainter* painter, QTransform transform, std::vector> styles) = 0; }; class SimpleElement : public GraphicElement @@ -44,6 +45,7 @@ public: ~SimpleElement() = default; PixelPath getPaintObject() const override; PixelPath getPaintObject(std::vector>*) const override; + void paint(QPainter* painter, QTransform transform, std::vector> styles) override; }; class GroupElement : public GraphicElement @@ -58,6 +60,7 @@ public: PixelPath getPaintObject() const override; PixelPath getPaintObject(std::vector>*) const override; void setSourceLayer(FolderLayerWrapper* sourceLayer); + void paint(QPainter* painter, QTransform transform, std::vector> styles) override; }; //******************************** BitmapPath ********************************// diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 9e1febe..c914236 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -46,6 +46,7 @@ LayerWrapper::LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementM property.scale = {transformJson.value("scale").toObject().value("x").toDouble(), transformJson.value("scale").toObject().value("y").toDouble()}; property.rotation = {transformJson.value("rotation").toDouble()}; + selected = false; } FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent) @@ -80,19 +81,19 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana wrappedElement = elementManager->getElementById(elementIndex); } -void LayerWrapper::SimpleProperty::apply(PixelPath&cache) const +void LayerWrapper::SimpleProperty::apply(PixelPath&cache) { - QTransform trans; + transform.reset(); double centerX = cache.getBoundingRect().center().x(); double centerY = cache.getBoundingRect().center().y(); //qDebug() << name << " " << cache.boundingRect().center(); //qDebug() << name << " " << cache.boundingRect(); - trans.translate(centerX, centerY); - trans.translate(offset.x(), offset.y()); - trans.rotate(rotation); - trans.scale(scale.x(), scale.y()); - trans.translate(-centerX, -centerY); - cache = cache.trans(trans); + transform.translate(-centerX, -centerY); + transform.translate(offset.x(), offset.y()); + transform.rotate(rotation); + transform.scale(scale.x(), scale.y()); + transform.translate(centerX, centerY); + cache = cache.trans(transform); } QTransform LayerWrapper::getTransform() @@ -250,31 +251,35 @@ int FolderLayerWrapper::getReferencedBy()const return -1; } -void LayerWrapper::paint(QPainter* painter) +void LayerWrapper::paint(QPainter* painter, QTransform transform) { - + if (this->selected) + { + painter->save(); + painter->setTransform(transform); + painter->setPen(QPen(Qt::gray, 2)); + painter->setPen(Qt::DashLine); + painter->drawRect(cache.getBoundingRect()); + painter->restore(); + } } -void FolderLayerWrapper::paint(QPainter* painter) +void FolderLayerWrapper::paint(QPainter* painter, QTransform transform) { + LayerWrapper::paint(painter, transform); + transform = property.transform * transform; + qDebug() << transform; for (auto& child : children) - child->paint(painter); + child->paint(painter, transform); } -void LeafLayerWrapper::paint(QPainter* painter) +void LeafLayerWrapper::paint(QPainter* painter, QTransform transform) { + LayerWrapper::paint(painter, transform); + transform = property.transform * transform; + qDebug() << transform; if (wrappedElement != nullptr) { - //painter->save(); - QTransform trans; - LayerWrapper* layer = this; - while (layer != nullptr) { - trans *= layer->getTransform(); - layer = layer->getParent(); - } - auto p = wrappedElement->getPaintObject(&this->styles); - p.trans(trans); - painter->drawPixmap(0, 0, p.getPixmap()); - //painter->restore(); + wrappedElement->paint(painter, transform, styles); } } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index 2186cd6..7df5d13 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -35,6 +35,7 @@ class LayerWrapper public: QTreeWidgetItem* qTreeWidgetItem; + bool selected; struct SimpleProperty { QString name = ""; @@ -43,8 +44,9 @@ class LayerWrapper double rotation = 0; bool flipHorizontally = 0; bool flipVertically = 0; + QTransform transform; // TODO: 将QPainterPath改为BitmapPath - void apply(PixelPath&cache) const; + void apply(PixelPath&cache); } property; virtual void setParent(FolderLayerWrapper*newParent); virtual void refresh(LayerWrapper* layer = nullptr); @@ -55,7 +57,7 @@ class LayerWrapper FolderLayerWrapper*getParent() const; // invoke by manager, then invoke parent's applyStyles LayerWrapper(QJsonObject json, FolderLayerWrapper*parent, ElementManager* elementManager=nullptr); LayerWrapper() = default; - virtual void paint(QPainter* painter); + virtual void paint(QPainter* painter, QTransform transform=QTransform()); // TODO : export Function // virtual LayerWrapper *addChild() = 0; // Leaf Child Only // virtual LayerWrapper *addParent() = 0; // Folder Parent Only @@ -88,7 +90,7 @@ class FolderLayerWrapper : public LayerWrapper QTreeWidgetItem* getQTreeItem() override; QJsonObject toJson() const override; int getReferencedBy()const; - void paint(QPainter* painter) override; + void paint(QPainter* painter, QTransform transform = QTransform()) override; }; class LeafLayerWrapper : public LayerWrapper @@ -104,7 +106,7 @@ class LeafLayerWrapper : public LayerWrapper LeafLayerWrapper() = default; LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent); QJsonObject toJson() const override; - void paint(QPainter* painter) override; + void paint(QPainter* painter, QTransform transform = QTransform()) override; }; Q_DECLARE_METATYPE(LayerWrapper *) diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index 723f0b1..d8199c7 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -87,13 +87,11 @@ void PreviewWindow::mouseMoveEvent(QMouseEvent* event) currentLayer->property.offset.setX(currentLayer->property.offset.x() + dx); currentLayer->property.offset.setY(currentLayer->property.offset.y() + dy); qDebug() << dx << "----" << dy; - emit layerInfoChanged(); } else if (event->buttons() & Qt::RightButton) { // 如果按下的是右键,那么旋转图形 qreal angle = -sqrt(dx * dx + dy * dy) / 1.0; currentLayer->property.rotation += angle; - emit layerInfoChanged(); } } // 更新上一次的位置 @@ -103,5 +101,5 @@ void PreviewWindow::mouseMoveEvent(QMouseEvent* event) void PreviewWindow::mouseReleaseEvent(QMouseEvent* event) { - // 当鼠标释放时,不做任何操作 + emit layerInfoChanged(); } \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp index f6962db..1e84c88 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/LayerTreeWidget.cpp @@ -12,9 +12,13 @@ LayerTreeWidget::LayerTreeWidget(QWidget *parent) this->setHeaderLabel("Layer Content"); connect(this, &QTreeWidget::customContextMenuRequested, this, &LayerTreeWidget::popMenu); connect(this, &QTreeWidget::currentItemChanged, [=](QTreeWidgetItem *currentItem) { + if (this->selectedItem != nullptr) { + this->selectedItem->data(0, Qt::UserRole).value()->selected = false; + } this->selectedItem = currentItem; if (this->selectedItem != nullptr) { auto layer = this->selectedItem->data(0, Qt::UserRole).value(); + layer->selected = true; emit displayLayerChange(layer); } else { From 97097fcc3abccdbd4230ec8553e5b952853eff47 Mon Sep 17 00:00:00 2001 From: karlis <2995621482@qq.com> Date: Tue, 14 Mar 2023 15:36:19 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E5=88=A4=E5=AE=9A=EF=BC=8C=E6=B7=BB=E5=8A=A0style=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/GraphicElement.cpp | 15 ++++++++++++--- .../src/Editor/PreviewWindow.cpp | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index d3dc67b..9602f2d 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -87,11 +87,20 @@ QJsonObject GraphicElement::toJson() const void SimpleElement::paint(QPainter* painter, QTransform transform, vector> styles) { - Renderer::ElementStyleStrokeDemo demo(2); - auto [img, mov] = renderer->drawElement(painterPath, demo, 1.0); painter->save(); painter->setTransform(transform); - painter->drawImage(mov, img); + if (styles.empty()) + { + painter->drawPath(painterPath); + } + else + { + // TODO:应用style + Renderer::ElementStyleStrokeDemo demo(2); + auto [img, mov] = renderer->drawElement(painterPath, demo, 1.0); + painter->drawImage(mov, img); + } + painter->restore(); } diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index d8199c7..485977d 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -88,9 +88,9 @@ void PreviewWindow::mouseMoveEvent(QMouseEvent* event) currentLayer->property.offset.setY(currentLayer->property.offset.y() + dy); qDebug() << dx << "----" << dy; } - else if (event->buttons() & Qt::RightButton) { + else if (event->buttons() & Qt::RightButton) { // 如果按下的是右键,那么旋转图形 - qreal angle = -sqrt(dx * dx + dy * dy) / 1.0; + qreal angle = dx; currentLayer->property.rotation += angle; } }