增加图片刷新的槽函数

dev-LayerStyle
yang.yongquan 2023-03-15 17:37:12 +08:00
parent fa91d80b70
commit 5d88ddf0ca
4 changed files with 69 additions and 25 deletions

View File

@ -78,4 +78,14 @@ void ElementPoolWidget::setElementManager(ElementManager* element)
void ElementPoolWidget::refresh() { void ElementPoolWidget::refresh() {
this->setElementList(this->elementManager->elements); this->setElementList(this->elementManager->elements);
// update(); // update();
}
void ElementPoolWidget::refreshPicture(GraphicElement* element) {
for (int i = 0; i < elements.size(); i++) {
if (element == elements[i]) {
pictureList->item(i)->setIcon(element->getPaintObject().getDetail().scaled(QSize(iconWidth - 25, iconHeight - 25)));
// update();
return;
}
}
} }

View File

@ -28,5 +28,6 @@ signals:
public slots: public slots:
int pictureItemClicked(QListWidgetItem* item); int pictureItemClicked(QListWidgetItem* item);
void refresh(); void refresh();
void refreshPicture(GraphicElement* element);
}; };

View File

@ -2,16 +2,26 @@
#include <QFile> #include <QFile>
#include <QJsondocument> #include <QJsondocument>
#include "PainterPathUtil.h" #include "PainterPathUtil.h"
#include <queue>
using Renderer::Painting; using Renderer::Painting;
using Renderer::Element; using Renderer::Element;
using Renderer::ElementTransform; using Renderer::ElementTransform;
using glm::bvec2; using glm::bvec2;
using std::max; using std::max;
using std::shared_ptr;
using std::make_shared;
using std::min; using std::min;
using std::queue;
const double PaintingUtil::pi = acos(-1); const double PaintingUtil::pi = acos(-1);
struct LayerNode {
LayerWrapper* nowLayer;
QTransform transfrom;
bvec2 flip;
};
QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) { QJsonObject PaintingUtil::readJsonFile(QString jsonFilePath) {
QFile jsonFile(jsonFilePath); QFile jsonFile(jsonFilePath);
jsonFile.open(QFile::ReadOnly); jsonFile.open(QFile::ReadOnly);
@ -28,29 +38,47 @@ Painting PaintingUtil::transfromToPainting(QString jsonFilePath) {
glm::bvec2 flip(0, 0); glm::bvec2 flip(0, 0);
QJsonObject jsonObj = readJsonFile(jsonFilePath); QJsonObject jsonObj = readJsonFile(jsonFilePath);
qDebug() << jsonObj; qDebug() << jsonObj;
ElementManager *elementManager = new ElementManager(jsonObj, Renderer::ElementRenderer::instance()); shared_ptr<ElementManager> elementManager = make_shared<ElementManager>(jsonObj, Renderer::ElementRenderer::instance());
LayerManager* layerManager = new LayerManager(jsonObj, elementManager); shared_ptr<LayerManager> layerManager = make_shared<LayerManager>(jsonObj, elementManager.get());
//qDebug() << elementManager->toJson(); //qDebug() << elementManager->toJson();
//qDebug() << layerManager->toJson(); //qDebug() << layerManager->toJson();
qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath; //qDebug() << ((SimpleElement*)((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->wrappedElement)->painterPath;
qDebug() << ((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->getCache().painterPath; //qDebug() << ((LeafLayerWrapper*)((FolderLayerWrapper*)((FolderLayerWrapper*)layerManager->getRoot())->children[0].get())->children[0].get())->getCache().painterPath;
queue<LayerNode> layerQueue;
LayerWrapper* root = layerManager->getRoot();
traverseLayTree(layerManager->getRoot(), transform, flip, painting); root->getCache();
layerQueue.push({ root, transform, flip });
while (!layerQueue.empty()) {
auto layerNode = layerQueue.front();
layerQueue.pop();
FolderLayerWrapper* nowLayer = handleLayerWrapper(layerNode.nowLayer, layerNode.transfrom, layerNode.flip, painting);
if (nowLayer != nullptr) {
for (auto sonLayer : nowLayer->children) {
layerQueue.push({ sonLayer.get(), layerNode.transfrom, layerNode.flip});
}
}
}
return painting; return painting;
} }
void PaintingUtil::traverseLayTree(LayerWrapper* nowLayer, QTransform transform, bvec2 flip, Painting& painting) { FolderLayerWrapper* PaintingUtil::handleLayerWrapper(LayerWrapper* nowLayer, QTransform& transform, bvec2& flip, Painting& painting) {
LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer); LeafLayerWrapper* leafLayer = dynamic_cast<LeafLayerWrapper*>(nowLayer);
PixelPath pixelPath = nowLayer->getCache();
QPainterPath painterPath = pixelPath.getPainterPath();
QRectF bound = painterPath.boundingRect();
flip ^= bvec2(nowLayer->property.flipHorizontally, nowLayer->property.flipVertically); flip ^= bvec2(nowLayer->property.flipHorizontally, nowLayer->property.flipVertically);
transform = nowLayer->property.transform * transform; transform = nowLayer->property.transform * transform;
if (leafLayer != nullptr) { if (leafLayer != nullptr) {
qDebug() << leafLayer<<"------" << painterPath;
GroupElement* wrapperElement = dynamic_cast<GroupElement*>(leafLayer->wrappedElement);
if (wrapperElement != nullptr)
return wrapperElement->sourceLayer;
PixelPath pixelPath = nowLayer->getCache();
QPainterPath painterPath = pixelPath.getPainterPath();
QRectF bound = painterPath.boundingRect();
//qDebug() << leafLayer<<"------" << painterPath;
//qDebug() << transform;
Element element; Element element;
ElementTransform elementTrans; ElementTransform elementTrans;
element.ratio = bound.width() / bound.height(); element.ratio = bound.width() / bound.height();
@ -60,36 +88,40 @@ void PaintingUtil::traverseLayTree(LayerWrapper* nowLayer, QTransform transform,
trans.scale(1 / bound.width(), 1 / bound.height()); trans.scale(1 / bound.width(), 1 / bound.height());
trans.translate(-bound.center().x(), -bound.center().y()); trans.translate(-bound.center().x(), -bound.center().y());
qDebug() << trans.map(painterPath);
element.contour = std::make_shared<vector<vector<Renderer::Point> >>(PainterPathUtil::transformToLines(trans.map(painterPath))); element.contour = std::make_shared<vector<vector<Renderer::Point> >>(PainterPathUtil::transformToLines(trans.map(painterPath)));
QSize screenSize = pixelPath.getPixmap().size(); QSize screenSize = pixelPath.getPixmap().size();
element.style = std::make_shared<Renderer::ElementStyleStrokeDemo>(0.06); element.style = std::make_shared<Renderer::ElementStyleStrokeDemo>(0.06);
painterPath = transform.map(painterPath); painterPath = transform.map(painterPath);
qDebug() << painterPath;
bound = painterPath.boundingRect(); bound = painterPath.boundingRect();
qDebug() << bound;
elementTrans.center = glm::vec2( elementTrans.center = glm::vec2(
(bound.center().x() - screenSize.width()) / screenSize.width(), (2 * bound.center().x() - screenSize.width()) / screenSize.width(),
(bound.center().y() - screenSize.height()) / screenSize.height() (2 * bound.center().y() - screenSize.height()) / screenSize.height()
); );
qDebug() << elementTrans.center.x << elementTrans.center.y;
decomposeTransform(transform, elementTrans.rotation, elementTrans.scale); decomposeTransform(transform, elementTrans.rotation, elementTrans.scale);
elementTrans.scale = glm::vec2(
bound.width() / screenSize.width(),
bound.height() / screenSize.height()
);
elementTrans.flip = glm::bvec2( elementTrans.flip = glm::bvec2(
nowLayer->property.flipHorizontally, nowLayer->property.flipHorizontally,
nowLayer->property.flipVertically nowLayer->property.flipVertically
); );
qDebug() << elementTrans.scale.x << elementTrans.scale.y;
painting.addElement(element, elementTrans); painting.addElement(element, elementTrans);
return; return nullptr;
} }
FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer); FolderLayerWrapper* folderLayer = dynamic_cast<FolderLayerWrapper*>(nowLayer);
if (folderLayer != nullptr) { return folderLayer;
for (auto sonLayer : folderLayer->children) {
traverseLayTree(sonLayer.get(), transform, flip, painting);
}
}
return;
} }
void PaintingUtil::decomposeTransform(QTransform trans, float& angle, glm::vec2& scale) { void PaintingUtil::decomposeTransform(QTransform trans, float& angle, glm::vec2& scale) {
qDebug() << trans; //qDebug() << trans;
trans.setMatrix( trans.setMatrix(
trans.m11(), trans.m12(), trans.m13(), trans.m11(), trans.m12(), trans.m13(),
trans.m21(), trans.m22(), trans.m23(), trans.m21(), trans.m22(), trans.m23(),
@ -134,7 +166,8 @@ void PaintingUtil::decomposeTransform(QTransform trans, float& angle, glm::vec2&
angle = 360 - angle; angle = 360 - angle;
} }
qDebug() << angle; qDebug() << angle;
R = R.inverted() * trans; //R = R.inverted() * trans;
scale = glm::vec2(R.m11(), R.m22()); //scale = glm::vec2(R.m11(), R.m22());
//qDebug() << scale.x << scale.y;
return; return;
} }

View File

@ -7,7 +7,7 @@ class PaintingUtil
private: private:
static const double pi; static const double pi;
static QJsonObject readJsonFile(QString jsonFilePath); static QJsonObject readJsonFile(QString jsonFilePath);
static void traverseLayTree(LayerWrapper* nowLayer, QTransform transform, glm::bvec2 flip, Renderer::Painting& painting); static FolderLayerWrapper* handleLayerWrapper(LayerWrapper* nowLayer, QTransform& transform, glm::bvec2& flip, Renderer::Painting& painting);
public: public:
static Renderer::Painting transfromToPainting(QString jsonFilePath); static Renderer::Painting transfromToPainting(QString jsonFilePath);
static void decomposeTransform(QTransform trans, float& angle, glm::vec2& scale); static void decomposeTransform(QTransform trans, float& angle, glm::vec2& scale);