删除冗余计算,重写预览逻辑,修复BoundRect,添加预览刷新触发

dev-LayerStyle
karlis 2023-03-16 14:51:28 +08:00
parent 627b947738
commit a9fb9197e6
11 changed files with 72 additions and 83 deletions

View File

@ -15,6 +15,7 @@ EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(p
elementInfoDisplayWidget->enableEdit();
qDebug() << layerInfoDisplayWidget;
qDebug() << elementInfoDisplayWidget;
connect(previewWindow, &PreviewWindow::refreshElementPreviewByIndex, elementInfoDisplayWidget, &ElementPoolWidget::refreshPictureByIndex);
connect(previewWindow, &PreviewWindow::layerInfoChanged, layerInfoDisplayWidget, &InfoDisplayWidget::triggerSelfRefresh);
connect(treeWidget, &LayerTreeWidget::displayLayerChange, previewWindow, &PreviewWindow::currentLayerChanged);
connect(treeWidget, &LayerTreeWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh);

View File

@ -2,7 +2,6 @@
ElementManager::ElementManager(QJsonObject source,Renderer::ElementRenderer* renderer)
{
auto elementsJson = source.value("elements").toArray();
qDebug() << elementsJson.size();
int index = 0;
for (auto elementJson : elementsJson)
{
@ -68,9 +67,6 @@ int ElementManager::getLayerReferencedBy(const FolderLayerWrapper* layer)
{
for (int i = 0; i < elements.size(); i++)
if (typeid(*elements[i]) == typeid(GroupElement)) {
qDebug() << ((GroupElement*)elements[i])->sourceLayer;
qDebug() << layer;
qDebug() << "------------";
if (((GroupElement*)elements[i])->sourceLayer == layer)
return i;
}
@ -97,7 +93,6 @@ void ElementManager::createSimpleElement(QString name, QString filePath) {
data.insert("include", filePath);
json.insert("data", data);
auto element = new SimpleElement(json);
qDebug() << element->painterPath;
element->name = name;
addElement(element);
}

View File

@ -37,23 +37,8 @@ void ElementPoolWidget::setElementList(std::vector<GraphicElement*> elements) {
pictureList->clear();
this->elements = elements;
for (int index = 0; index < elements.size(); index++) {
//
//QString strPath = QString("C:\\Users\\86177\\Pictures\\Screenshots\\test.png");
//QPixmap itemPixmap(strPath);
//QPixmap itemPixmap(QSize(200, 200));
//itemPixmap.fill(Qt::red);
QPixmap itemPixmap = elements[index]->getPaintObject().getDetail();
qDebug() << this->parentWidget()->size();
//auto p = new QWidget();
//auto lb = new QLabel(p);
//lb->setPixmap(itemPixmap);
//lb->setFixedSize(1920, 1080);
//p->setFixedSize(1920, 1080);
//lb->show();
//p->show();
QListWidgetItem* pItem = new QListWidgetItem(
itemPixmap.scaled(QSize(iconWidth - 25, iconHeight - 25)),
elements[index]->getPreview(QSize(iconWidth - 25, iconHeight - 25)),
elements[index]->name);
pItem->setSizeHint(QSize(iconWidth, iconHeight));
pictureList->insertItem(index, pItem);
@ -88,13 +73,21 @@ void ElementPoolWidget::refresh() {
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)));
pictureList->item(i)->setIcon(elements[i]->getPreview(QSize(iconWidth - 25, iconHeight - 25)));
// update();
return;
}
}
}
void ElementPoolWidget::refreshPictureByIndex(int index) {
if (index >= 0 && index < elements.size())
{
pictureList->item(index)->setIcon(elements[index]->getPreview(QSize(iconWidth - 25, iconHeight - 25)));
// update();
}
}
void ElementPoolWidget::enableEdit()
{
connect(this->pictureList, &QListWidget::customContextMenuRequested, this, &ElementPoolWidget::popMenu);

View File

@ -30,6 +30,7 @@ public slots:
int pictureItemClicked(QListWidgetItem* item);
void refresh();
void refreshPicture(GraphicElement* element);
void refreshPictureByIndex(int index);
void popMenu(const QPoint& pos);
};

View File

@ -49,35 +49,7 @@ PixelPath GroupElement::getPaintObject() const
//TODO: apply styles and send back
PixelPath SimpleElement::getPaintObject(std::vector<std::shared_ptr<LayerStyle>>* styles) const {
PixelPath result;
std::shared_ptr<Renderer::ElementStyle> style;
if ((*styles).empty())
{
return this->getPaintObject();
}
else
{
style = (*styles)[0];
/*qDebug() << std::dynamic_pointer_cast<Renderer::StrokePlain>(
std::dynamic_pointer_cast<StrokeElementLayerStyle>(style)->materialStyles[0]->materialStroke
)->material.color.name();*/
/*qDebug() << std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(
std::dynamic_pointer_cast<StrokeElementLayerStyle>(style)->materialStyles[0]->materialStroke
)->materialMap[1.0].color;*/
}
auto [img, mov] = Renderer::ElementRenderer::instance()->drawElement(painterPath, *style, 1.0);
//qDebug() << img << " ------";
result.addImage(img, mov);
result.addPath(painterPath);
//result.addPath(painterPath);
// QImage img(80,80,QImage::Format_ARGB32);
// QPainter pt(&img);
//pt.setPen(QPen(Qt::red, 2));
//pt.drawLine(0, 0, 80, 80);
//pt.end();
//result.addImage(img, QPoint(0, 0));
return result;
return this->getPaintObject();
}
PixelPath GroupElement::getPaintObject(std::vector<std::shared_ptr<LayerStyle>>* styles) const {
@ -132,31 +104,16 @@ void SimpleElement::paint(QPainter* painter, QTransform transform, const vector<
{
Renderer::ElementStyleStrokeDemo demo(2);
std::shared_ptr<Renderer::ElementStyle> style;
if (styles.empty())
{
style = std::make_shared<Renderer::ElementStyleStrokeDemo>(2);
}
else
{
style = styles[0];
/*qDebug() << std::dynamic_pointer_cast<Renderer::StrokePlain>(
std::dynamic_pointer_cast<StrokeElementLayerStyle>(style)->materialStyles[0]->materialStroke
)->material.color.name();*/
/*qDebug() << std::dynamic_pointer_cast<Renderer::StrokeRadialGradient>(
std::dynamic_pointer_cast<StrokeElementLayerStyle>(style)->materialStyles[0]->materialStroke
)->materialMap[1.0].color;*/
}
style = styles[0];
QVector2D scale(transform.m11(), transform.m22());
scale /= transform.m33();
double maxScale = std::max(scale.x(), scale.y());
double pixelRatio = maxScale * QGuiApplication::primaryScreen()->devicePixelRatio();
auto [img, mov] = Renderer::ElementRenderer::instance()->drawElement(painterPath, *style, pixelRatio);
transform.translate(mov.x(), mov.y());
painter->setTransform(transform.scale(1 / pixelRatio, 1 / pixelRatio));
//img = img.scaled(img.width() / pixelRatio, img.height() / pixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter->drawImage(mov, img);
painter->drawImage(0, 0, img);
}
painter->restore();
}
@ -164,3 +121,31 @@ void GroupElement::paint(QPainter* painter, QTransform transform, const vector<s
{
sourceLayer->paint(painter, transform);
}
QPixmap SimpleElement::getPreview(QSize size)
{
QPixmap result(size + QSize(5,5));
QPainter painter(&result);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.scale(size.width() / painterPath.boundingRect().width(), size.height() / painterPath.boundingRect().height());
painter.drawPath(painterPath);
return result;
}
QPixmap GroupElement::getPreview(QSize size)
{
auto cache = sourceLayer->getCache();
QPixmap result(QSize(1024, 1024));
QPainter painter(&result);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
sourceLayer->paint(&painter, QTransform(), true);
painter.end();
QRect rect (cache.getBoundingRect().toRect());
rect.setTopLeft(rect.topLeft() - QPoint(5, 5));
rect.setBottomRight(rect.bottomRight() + QPoint(5, 5));
result = result.copy(rect);
return result.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}

View File

@ -29,6 +29,7 @@ public:
virtual PixelPath getPaintObject() const = 0;
virtual PixelPath getPaintObject(std::vector<std::shared_ptr<LayerStyle>>*) const = 0;
virtual void paint(QPainter* painter, QTransform transform, const std::vector<std::shared_ptr<LayerStyle>> &styles) = 0;
virtual QPixmap getPreview(QSize size) = 0;
};
class SimpleElement : public GraphicElement
@ -48,6 +49,7 @@ public:
PixelPath getPaintObject() const override;
PixelPath getPaintObject(std::vector<std::shared_ptr<LayerStyle>>*) const override;
void paint(QPainter* painter, QTransform transform, const std::vector<std::shared_ptr<LayerStyle>> &styles) override;
QPixmap getPreview(QSize size) override;
};
class GroupElement : public GraphicElement
@ -64,6 +66,7 @@ public:
PixelPath getPaintObject(std::vector<std::shared_ptr<LayerStyle>>*) const override;
void setSourceLayer(FolderLayerWrapper* sourceLayer);
void paint(QPainter* painter, QTransform transform, const std::vector<std::shared_ptr<LayerStyle>> &styles) override;
QPixmap getPreview(QSize size) override;
};
//******************************** BitmapPath ********************************//

View File

@ -251,12 +251,12 @@ int FolderLayerWrapper::getReferencedBy()const
return -1;
}
void LayerWrapper::paint(QPainter* painter, QTransform transform)
void LayerWrapper::paint(QPainter* painter, QTransform transform, bool ignoreSelected)
{
if (this->selected)
if (!ignoreSelected && this->selected)
{
painter->save();
painter->setTransform(transform);
painter->setTransform(transform, ignoreSelected);
painter->setPen(QPen(Qt::gray, 2));
painter->setPen(Qt::DashLine);
painter->drawRect(cache.getBoundingRect());
@ -264,18 +264,18 @@ void LayerWrapper::paint(QPainter* painter, QTransform transform)
}
}
void FolderLayerWrapper::paint(QPainter* painter, QTransform transform)
void FolderLayerWrapper::paint(QPainter* painter, QTransform transform, bool ignoreSelected)
{
LayerWrapper::paint(painter, transform);
LayerWrapper::paint(painter, transform, ignoreSelected);
transform = property.transform * transform;
//qDebug() << transform;
for (auto& child : children)
child->paint(painter, transform);
child->paint(painter, transform, ignoreSelected);
}
void LeafLayerWrapper::paint(QPainter* painter, QTransform transform)
void LeafLayerWrapper::paint(QPainter* painter, QTransform transform, bool ignoreSelected)
{
LayerWrapper::paint(painter, transform);
LayerWrapper::paint(painter, transform, ignoreSelected);
transform = property.transform * transform;
//qDebug() << transform;
if (wrappedElement != nullptr)

View File

@ -57,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, QTransform transform=QTransform());
virtual void paint(QPainter* painter, QTransform transform=QTransform(), bool ignoreSelected = false);
// TODO : export Function
// virtual LayerWrapper *addChild() = 0; // Leaf Child Only
// virtual LayerWrapper *addParent() = 0; // Folder Parent Only
@ -90,7 +90,7 @@ class FolderLayerWrapper : public LayerWrapper
QTreeWidgetItem* getQTreeItem() override;
QJsonObject toJson() const override;
int getReferencedBy()const;
void paint(QPainter* painter, QTransform transform = QTransform()) override;
void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override;
};
class LeafLayerWrapper : public LayerWrapper
@ -106,7 +106,7 @@ class LeafLayerWrapper : public LayerWrapper
LeafLayerWrapper() = default;
LeafLayerWrapper(QJsonObject json, ElementManager *elementManager, FolderLayerWrapper*parent);
QJsonObject toJson() const override;
void paint(QPainter* painter, QTransform transform = QTransform()) override;
void paint(QPainter* painter, QTransform transform = QTransform(), bool ignoreSelected = false) override;
};
Q_DECLARE_METATYPE(LayerWrapper *)

View File

@ -13,8 +13,8 @@ public:
QPainterPath painterPath;
int w,h;
public:
PixelPath(int w=1024, int h= 1024);
PixelPath(QPainterPath painterPath,int w = 1024, int h = 1024);
PixelPath(int w=16, int h= 16);
PixelPath(QPainterPath painterPath,int w = 16, int h = 16);
~PixelPath() = default;
QRectF getBoundingRect() const;
QPixmap getPixmap() const;

View File

@ -87,13 +87,22 @@ 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;
}
else if (event->buttons() & Qt::RightButton) {
// 如果按下的是右键,那么旋转图形
qreal angle = dx;
currentLayer->property.rotation += angle;
}
auto layer = currentLayer;
while (layer != nullptr)
{
auto index = -1;
if (typeid(*layer) == typeid(FolderLayerWrapper))
index = dynamic_cast<FolderLayerWrapper*>(layer)->getReferencedBy();
if (index != -1)
emit refreshElementPreviewByIndex(index);
layer = layer->getParent();
}
}
// 更新上一次的位置
m_lastPos = event->pos();

View File

@ -42,4 +42,6 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions
signals:
void layerInfoChanged();
void refreshElementPreview(GraphicElement*);
void refreshElementPreviewByIndex(int);
};