dev-LayerStyle
karlis 2023-03-14 14:53:36 +08:00
parent 3f1421a1bd
commit 684c28dafd
6 changed files with 58 additions and 31 deletions

View File

@ -83,4 +83,19 @@ QJsonObject GraphicElement::toJson() const
QJsonObject result;
result.insert("name", name);
return result;
}
void SimpleElement::paint(QPainter* painter, QTransform transform, vector<std::shared_ptr<LayerStyle>> 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<std::shared_ptr<LayerStyle>> styles)
{
sourceLayer->paint(painter, transform);
}

View File

@ -28,6 +28,7 @@ public:
virtual QJsonObject toJson() const;
virtual PixelPath getPaintObject() const = 0;
virtual PixelPath getPaintObject(std::vector<std::shared_ptr<LayerStyle>>*) const = 0;
virtual void paint(QPainter* painter, QTransform transform, std::vector<std::shared_ptr<LayerStyle>> styles) = 0;
};
class SimpleElement : public GraphicElement
@ -44,6 +45,7 @@ public:
~SimpleElement() = default;
PixelPath getPaintObject() const override;
PixelPath getPaintObject(std::vector<std::shared_ptr<LayerStyle>>*) const override;
void paint(QPainter* painter, QTransform transform, std::vector<std::shared_ptr<LayerStyle>> styles) override;
};
class GroupElement : public GraphicElement
@ -58,6 +60,7 @@ public:
PixelPath getPaintObject() const override;
PixelPath getPaintObject(std::vector<std::shared_ptr<LayerStyle>>*) const override;
void setSourceLayer(FolderLayerWrapper* sourceLayer);
void paint(QPainter* painter, QTransform transform, std::vector<std::shared_ptr<LayerStyle>> styles) override;
};
//******************************** BitmapPath ********************************//

View File

@ -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);
}
}

View File

@ -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 *)

View File

@ -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();
}

View File

@ -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<LayerWrapper*>()->selected = false;
}
this->selectedItem = currentItem;
if (this->selectedItem != nullptr) {
auto layer = this->selectedItem->data(0, Qt::UserRole).value<LayerWrapper*>();
layer->selected = true;
emit displayLayerChange(layer);
}
else {