bug: pixmap溢出

TaoZhang-Branch
karlis 2023-03-13 23:36:31 +08:00
parent de9d7143b6
commit 4c9fe168a9
9 changed files with 58 additions and 27 deletions

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>1124</width>
<height>1010</height>
<width>1473</width>
<height>1103</height>
</rect>
</property>
<property name="minimumSize">
@ -56,7 +56,20 @@
<widget class="QWidget" name="LeftBar" native="true"/>
</item>
<item>
<widget class="PreviewWindow" name="Preview"/>
<widget class="PreviewWindow" name="Preview">
<property name="minimumSize">
<size>
<width>1080</width>
<height>1080</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>1080</width>
<height>1080</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="RightBar" native="true">

View File

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

View File

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

View File

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

View File

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

View File

@ -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<LayerWrapper> 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;

View File

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

View File

@ -26,4 +26,5 @@ public:
PixelPath trans(QTransform& mat)const;
QPixmap resizedPixel(QSize size)const;
QPixmap getDetail()const;
void highLight();
};

View File

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