添加了更改画布大小的功能 | #3

main
karlis 2023-03-27 13:24:22 +08:00
parent 5ac322fb30
commit f6ebe58322
7 changed files with 60 additions and 17 deletions

View File

@ -54,6 +54,19 @@
</property> </property>
</widget> </widget>
</item> </item>
<item alignment="Qt::AlignHCenter">
<widget class="QPushButton" name="resizeButton">
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>设置画布尺寸</string>
</property>
</widget>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>

View File

@ -70,9 +70,9 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>-17</y> <y>0</y>
<width>1024</width> <width>0</width>
<height>1024</height> <height>0</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
@ -83,14 +83,14 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>1024</width> <width>0</width>
<height>1024</height> <height>0</height>
</size> </size>
</property> </property>
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>1024</width> <width>16777215</width>
<height>1024</height> <height>16777215</height>
</size> </size>
</property> </property>
</widget> </widget>

View File

@ -40,6 +40,7 @@ EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(p
//connect(treeWidget, &LayerTreeWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh); //connect(treeWidget, &LayerTreeWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh);
// connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh); // connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshElementWidget, elementInfoDisplayWidget, &ElementPoolWidget::refresh);
connect(treeWidget, &LayerTreeWidget::displayLayerChange, this, &EditorWidgetItem::onLayerChange); connect(treeWidget, &LayerTreeWidget::displayLayerChange, this, &EditorWidgetItem::onLayerChange);
connect(editorSettingWidget, &EditorSettingWidget::canvasSizeChanged, previewWindow, &PreviewWindow::resize);
// connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshPreview, this, // connect(layerInfoDisplayWidget, &InfoDisplayWidget::requireRefreshPreview, this,
// &EditorWidgetItem::triggerRefreshPreview); // &EditorWidgetItem::triggerRefreshPreview);
// connect(treeWidget, &LayerTreeWidget::requireRefreshPreview, this, // connect(treeWidget, &LayerTreeWidget::requireRefreshPreview, this,
@ -72,7 +73,7 @@ EditorWidgetItem::EditorWidgetItem(QString filePath,QWidget *parent) : QWidget(p
elementInfoDisplayWidget->setElementManager(elementManager); elementInfoDisplayWidget->setElementManager(elementManager);
treeWidget->elementManager = elementManager; treeWidget->elementManager = elementManager;
//qDebug() << layerManager->toJson(); //qDebug() << layerManager->toJson();
previewWindow->initialize(layerManager,QSize(jsonDoc.object().value("width").toDouble(),jsonDoc.object().value("height").toDouble())); previewWindow->initialize(layerManager,QSize(jsonDoc.object().value("width").toInt(),jsonDoc.object().value("height").toInt()));
if (layerManager->getRoot() != nullptr) if (layerManager->getRoot() != nullptr)
@ -151,8 +152,8 @@ void EditorWidgetItem::saveImpl(QString filePath) const
QJsonObject source1 = layerManager->toJson(); QJsonObject source1 = layerManager->toJson();
QJsonObject source2 = elementManager->toJson(); QJsonObject source2 = elementManager->toJson();
QJsonObject json; QJsonObject json;
json.insert("width", 1080); json.insert("width", previewWindow->referSize.width());
json.insert("height", 1080); json.insert("height", previewWindow->referSize.height());
json.insert("root-layer", source1.value("root-layer")); json.insert("root-layer", source1.value("root-layer"));
json.insert("elements", source2.value("elements")); json.insert("elements", source2.value("elements"));
json.insert("project-name", this->projectName); json.insert("project-name", this->projectName);

View File

@ -18,11 +18,32 @@ PreviewWindow::PreviewWindow(QWidget* parent) : QOpenGLWidget(parent)
backgroundColor = QColor(255, 255, 255, 255); backgroundColor = QColor(255, 255, 255, 255);
} }
void PreviewWindow::initialize(LayerManager* layerManager, QSize windowSize) void PreviewWindow::initialize(LayerManager* layerManager, QSize referSize, QSize windowSize)
{ {
this->logicalSize = windowSize; this->logicalSize = windowSize;
this->layerManager = layerManager; this->layerManager = layerManager;
this->setFixedSize(windowSize); this->resize(referSize);
}
void PreviewWindow::resize(QSize _referSize)
{
if (!_referSize.isValid())
return;
if (_referSize.width() == 0 || _referSize.height() == 0)
return;
this->referSize = _referSize;
if (referSize.width() > referSize.height())
{
actualSize.setWidth(logicalSize.width());
actualSize.setHeight(logicalSize.width() * referSize.height() / referSize.width());
}
else
{
actualSize.setHeight(logicalSize.height());
actualSize.setWidth(logicalSize.height() * referSize.width() / referSize.height());
}
this->setFixedSize(actualSize);
this->viewportRect = QRectF(QPointF(0, 0), actualSize);
} }
void PreviewWindow::show() void PreviewWindow::show()
@ -51,7 +72,7 @@ void PreviewWindow::paintGL()
glClearColor(backgroundColor.redF(), backgroundColor.greenF(), backgroundColor.blueF(), backgroundColor.alphaF()); glClearColor(backgroundColor.redF(), backgroundColor.greenF(), backgroundColor.blueF(), backgroundColor.alphaF());
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
painter->begin(this); painter->begin(this);
painter->setWindow(0, 0, logicalSize.width() * devicePixelRatioF(), logicalSize.height() * devicePixelRatioF()); painter->setWindow(0, 0, actualSize.width() * devicePixelRatioF(), actualSize.height() * devicePixelRatioF());
painter->setRenderHint(QPainter::Antialiasing); painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::HighQualityAntialiasing); painter->setRenderHint(QPainter::HighQualityAntialiasing);
layerManager->paint(painter, this->size(), currentLayer); layerManager->paint(painter, this->size(), currentLayer);
@ -141,12 +162,12 @@ void PreviewWindow::wheelEvent(QWheelEvent* event)
if (event->delta() > 0 && zoomStep < ZOOM_STEP_MAX) if (event->delta() > 0 && zoomStep < ZOOM_STEP_MAX)
{ {
zoomStep++; zoomStep++;
this->setFixedSize(logicalSize * (1 + zoomStep * ZOOM_RATE)); this->setFixedSize(actualSize * (1 + zoomStep * ZOOM_RATE));
} }
else if (event->delta() < 0 && zoomStep > ZOOM_STEP_MIN) else if (event->delta() < 0 && zoomStep > ZOOM_STEP_MIN)
{ {
zoomStep--; zoomStep--;
this->setFixedSize(logicalSize * (1 + zoomStep * ZOOM_RATE)); this->setFixedSize(actualSize * (1 + zoomStep * ZOOM_RATE));
} }
this->repaint(); this->repaint();
} }

View File

@ -28,6 +28,7 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions
LayerManager *layerManager; LayerManager *layerManager;
Renderer::ElementRenderer* renderer; Renderer::ElementRenderer* renderer;
QSize logicalSize; QSize logicalSize;
QSize actualSize;
QRectF viewportRect; QRectF viewportRect;
LayerWrapper* currentLayer; LayerWrapper* currentLayer;
QPointF m_lastPos; QPointF m_lastPos;
@ -38,8 +39,10 @@ class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions
void wheelEvent(QWheelEvent* event) override; void wheelEvent(QWheelEvent* event) override;
public: public:
QSize referSize;
PreviewWindow(QWidget *parent = nullptr); PreviewWindow(QWidget *parent = nullptr);
void initialize(LayerManager *layerManager, QSize windowSize = QSize(1920, 1080)); void initialize(LayerManager *layerManager,QSize referSize, QSize windowSize = QSize(720, 720));
void resize(QSize _referSize);
void show(); void show();
void initializeGL() override; void initializeGL() override;
void paintGL() override; void paintGL() override;

View File

@ -18,4 +18,9 @@ EditorSettingWidget::EditorSettingWidget(QWidget* parent)
QString name = QInputDialog::getText(this, QString::fromLocal8Bit("重命名"), QString::fromLocal8Bit("请输入新的项目名称")); QString name = QInputDialog::getText(this, QString::fromLocal8Bit("重命名"), QString::fromLocal8Bit("请输入新的项目名称"));
emit projectNameChanged(name); emit projectNameChanged(name);
}); });
connect(ui.resizeButton, &QPushButton::clicked, this, [this]() {
int width = QInputDialog::getInt(this, QString::fromLocal8Bit("重设画布大小"), QString::fromLocal8Bit("请输入新的画布宽度"));
int height = QInputDialog::getInt(this, QString::fromLocal8Bit("重设画布大小"), QString::fromLocal8Bit("请输入新的画布高度"));
emit canvasSizeChanged(QSize(width, height));
});
} }

View File

@ -16,6 +16,6 @@ class EditorSettingWidget :
signals: signals:
void backgroundColorChanged(QColor); void backgroundColorChanged(QColor);
void projectNameChanged(QString); void projectNameChanged(QString);
void canvasSizeChanged(QSize);
}; };