改用了GLWidget,完成了Manager初始化的逻辑结构和大量头文件包含

dev-VirtualTexture
白封羽 2022-11-22 19:02:36 +08:00
parent 59ca15dcb9
commit c6a6b68687
13 changed files with 147 additions and 71 deletions

View File

@ -27,7 +27,7 @@
<number>0</number> <number>0</number>
</property> </property>
<item> <item>
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="0,0"> <layout class="QVBoxLayout" name="verticalLayout_3" stretch="1,30">
<item> <item>
<widget class="QLabel" name="title"> <widget class="QLabel" name="title">
<property name="text"> <property name="text">
@ -46,7 +46,7 @@
<customwidgets> <customwidgets>
<customwidget> <customwidget>
<class>PreviewWindow</class> <class>PreviewWindow</class>
<extends>QGraphicsView</extends> <extends>QOpenGLWidget</extends>
<header>PreviewWindow.h</header> <header>PreviewWindow.h</header>
</customwidget> </customwidget>
</customwidgets> </customwidgets>

View File

@ -4,16 +4,18 @@ EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
previewWindow = ui.Preview; previewWindow = ui.Preview;
QJsonArray elements;
QJsonArray layers;
elementManager = new ElementManager(elements);
layerManager = new LayerManager(layers, elementManager);
elementManager->initialize(layerManager);
previewWindow->initialize(layerManager);
} }
EditorWidget::~EditorWidget() EditorWidget::~EditorWidget()
{ {
} }
#include "QGraphicsItem.h"
#include "QGraphicsScene.h"
#include <iostream>
using namespace std;
void EditorWidget::paintEvent(QPaintEvent *event) void EditorWidget::paintEvent(QPaintEvent *event)
{ {
previewWindow->show();
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "GraphicElement.h" #include "ElementManager.h"
#include "LayerManager.h"
#include "PreviewWindow.h" #include "PreviewWindow.h"
#include "ui_EditorWidget.h" #include "ui_EditorWidget.h"
#include <QPainter> #include <QPainter>
@ -10,12 +11,14 @@ class EditorWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
private:
Ui::EditorWidgetClass ui;
PreviewWindow *previewWindow;
ElementManager *elementManager;
LayerManager *layerManager;
public: public:
EditorWidget(QWidget *parent = nullptr); EditorWidget(QWidget *parent = nullptr);
~EditorWidget(); ~EditorWidget();
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
private:
Ui::EditorWidgetClass ui;
PreviewWindow *previewWindow;
}; };

View File

@ -1,15 +1,25 @@
#include "ElementManager.h" #include "ElementManager.h"
ElementManager *ElementManager::getInstance() ElementManager::ElementManager(QJsonArray jsonElementArray)
{ {
return nullptr; elements.resize(jsonElementArray.size());
} }
void ElementManager::initialize(LayerManager *layerManager)
{
}
void ElementManager::addElement(GraphicElement *element) void ElementManager::addElement(GraphicElement *element)
{ {
} }
void ElementManager::removeElement(GraphicElement *pElement) void ElementManager::removeElement(GraphicElement *pElement)
{ {
} }
GraphicElement *ElementManager::getElementById(int index)
GraphicElement **ElementManager::getElementById(int index)
{ {
return nullptr; if (index <= elements.size())
return &elements[index];
else
return nullptr;
} }

View File

@ -1,19 +1,23 @@
#pragma once #pragma once
#include "GraphicElement.h" #include "GraphicElement.h"
#include "LayerManager.h"
#include <QJsonArray>
#include <vector> #include <vector>
using std::vector; using std::vector;
class LayerManager;
class ElementManager class ElementManager
{ {
private: private:
static ElementManager *INSTANCE; vector<GraphicElement *> elements;
vector<GraphicElement> mElements;
public: public:
static ElementManager *getInstance(); ElementManager(QJsonArray jsonElementArray);
void initialize(LayerManager *layerManager);
void addElement(GraphicElement *element); void addElement(GraphicElement *element);
void removeElement(GraphicElement *pElement); void removeElement(GraphicElement *pElement);
/** /**
* only used in initialization * only used in initialization
*/ */
GraphicElement *getElementById(int index); GraphicElement **getElementById(int index);
}; };

View File

@ -19,7 +19,7 @@ ElementType ElementTypeStringValue::enumType(std::string elementType)
} }
QPainterPath SimpleElement::getPaintObject() const QPainterPath SimpleElement::getPaintObject() const
{ {
return mPainterPath; return painterPath;
} }
void SimpleElement::pathOperate(QJsonArray paths) void SimpleElement::pathOperate(QJsonArray paths)
{ {
@ -31,39 +31,51 @@ void SimpleElement::polygonOperate(QJsonArray points)
{ {
polygon.append(QPointF(point.toObject().value("x").toDouble(), point.toObject().value("y").toDouble())); polygon.append(QPointF(point.toObject().value("x").toDouble(), point.toObject().value("y").toDouble()));
} }
mPainterPath.addPolygon(polygon); painterPath.addPolygon(polygon);
} }
void SimpleElement::roundOperate(QJsonObject json) void SimpleElement::roundOperate(QJsonObject json)
{ {
double xAxis = json.value("x-axis").toDouble(); double xAxis = json.value("x-axis").toDouble();
double yAxis = json.value("y-axis").toDouble(); double yAxis = json.value("y-axis").toDouble();
double angle = json.value("angle").toDouble(); double angle = json.value("angle").toDouble();
mPainterPath.arcMoveTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0); painterPath.arcMoveTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0);
mPainterPath.arcTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0, angle); painterPath.arcTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0, angle);
} }
SimpleElement::SimpleElement(QJsonObject jsonSource) : mJsonSource(jsonSource) SimpleElement::SimpleElement(QJsonObject jsonSource) : jsonSource(jsonSource)
{ {
mPainterPath.clear(); painterPath.clear();
mElementType = ElementTypeStringValue::enumType(mJsonSource.value("type").toString().toStdString()); elementType = ElementTypeStringValue::enumType(jsonSource.value("type").toString().toStdString());
switch (mElementType) switch (elementType)
{ {
case ElementType::undefined: case ElementType::undefined:
break; break;
case ElementType::path: { case ElementType::path: {
QJsonArray paths = mJsonSource.value("data").toObject().value("operations").toArray(); QJsonArray paths = jsonSource.value("data").toObject().value("operations").toArray();
pathOperate(paths); pathOperate(paths);
break; break;
} }
case ElementType::polygon: { case ElementType::polygon: {
QJsonArray points = mJsonSource.value("data").toObject().value("points").toArray(); QJsonArray points = jsonSource.value("data").toObject().value("points").toArray();
polygonOperate(points); polygonOperate(points);
break; break;
} }
case ElementType::round: { case ElementType::round: {
roundOperate(mJsonSource.value("data").toObject()); roundOperate(jsonSource.value("data").toObject());
break; break;
} }
default: default:
break; break;
} }
} }
GroupElement::GroupElement(FolderLayerWrapper *sourceLayer)
{
this->sourceLayer = sourceLayer;
}
QPainterPath GroupElement::getPaintObject() const
{
if (sourceLayer != nullptr)
return sourceLayer->getCache();
else
return QPainterPath();
}

View File

@ -1,8 +1,13 @@
#pragma once #pragma once
#include "LayerWrapper.h"
#include <QJsonArray> #include <QJsonArray>
#include <QJsonObject> #include <QJsonObject>
#include <QPainterPath> #include <QPainterPath>
#include <string> #include <string>
class LayerWrapper;
class LeafLayerWrapper;
class FolderLayerWrapper;
enum class ElementType enum class ElementType
{ {
undefined = 0, undefined = 0,
@ -28,9 +33,9 @@ class GraphicElement
class SimpleElement : public GraphicElement class SimpleElement : public GraphicElement
{ {
private: private:
QJsonObject mJsonSource; QJsonObject jsonSource;
QPainterPath mPainterPath; QPainterPath painterPath;
ElementType mElementType; ElementType elementType;
void pathOperate(QJsonArray paths); void pathOperate(QJsonArray paths);
void polygonOperate(QJsonArray points); void polygonOperate(QJsonArray points);
void roundOperate(QJsonObject json); void roundOperate(QJsonObject json);
@ -40,14 +45,13 @@ class SimpleElement : public GraphicElement
~SimpleElement() = default; ~SimpleElement() = default;
QPainterPath getPaintObject() const override; QPainterPath getPaintObject() const override;
}; };
// class GroupElement : public GraphicElement class GroupElement : public GraphicElement
//{ {
// private: private:
// FolderLayerWrapper *mSourceLayer; FolderLayerWrapper *sourceLayer;
//
// public: public:
// GroupElement(QJsonObject jsonSource); GroupElement(FolderLayerWrapper *mSourceLayer);
// GroupElement(FolderLayerWrapper *mSourceLayer); ~GroupElement() = default;
// ~GroupElement() = default; QPainterPath getPaintObject() const override;
// QPainterPath getPaintObject() const override; };
// };

View File

@ -1,8 +1,7 @@
#include "LayerManager.h" #include "LayerManager.h"
LayerManager::LayerManager(QObject *parent) LayerManager::LayerManager(QJsonArray layers, ElementManager *elementManager)
{ {
scene = new QGraphicsScene(parent); }
} void LayerManager::paint(QPainter *painter) const
void LayerManager::paint(QGraphicsView *view) const
{ {
} }

View File

@ -1,10 +1,14 @@
#pragma once #pragma once
#include "ElementManager.h"
#include "LayerWrapper.h" #include "LayerWrapper.h"
#include <QGraphicsView> #include <QJsonArray>
#include <QPainter>
#include <utility> #include <utility>
#include <vector> #include <vector>
using std::pair; using std::pair;
using std::vector; using std::vector;
class ElementManager;
class LayerManager class LayerManager
{ {
private: private:
@ -12,9 +16,8 @@ class LayerManager
LayerWrapper *root; LayerWrapper *root;
LayerPtrs selectedLayers; LayerPtrs selectedLayers;
LayerPtrs involvedLeafLayersCache; LayerPtrs involvedLeafLayersCache;
QGraphicsScene *scene;
public: public:
LayerManager(QObject *parent = 0); LayerManager(QJsonArray layers, ElementManager *elementManager);
void paint(QGraphicsView *view) const; void paint(QPainter *painter) const;
}; };

View File

@ -3,6 +3,12 @@
void FolderLayerWrapper::addChild(LayerWrapper *child) void FolderLayerWrapper::addChild(LayerWrapper *child)
{ {
} }
void FolderLayerWrapper::removeChild(LayerWrapper *child) void FolderLayerWrapper::removeChild(LayerWrapper *child)
{ {
} }
QPainterPath LayerWrapper::getCache() const
{
return cache;
}

View File

@ -10,6 +10,10 @@
#include <vector> #include <vector>
using std::shared_ptr; using std::shared_ptr;
using std::vector; using std::vector;
class GraphicElement;
class SimpleElement;
class GroupElement;
class LayerWrapper class LayerWrapper
{ {
protected: protected:
@ -19,7 +23,7 @@ class LayerWrapper
QPainterPath cache; QPainterPath cache;
public: public:
virtual void draw(QGraphicsScene *scene) = 0; // invoke by manager, then invoke parent's applyStyles QPainterPath getCache() const; // invoke by manager, then invoke parent's applyStyles
// todo: provide atomic operations for Events // todo: provide atomic operations for Events
}; };

View File

@ -1,28 +1,51 @@
#include "PreviewWindow.h" #include "PreviewWindow.h"
PreviewWindow::PreviewWindow(QWidget *parent) : QGraphicsView(parent) PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent)
{ {
this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); QSurfaceFormat surfaceFormat;
mainScene = new QGraphicsScene(this); surfaceFormat.setSamples(16);
this->setScene(mainScene); setFormat(surfaceFormat);
painter = new QPainter(this);
layerManager = nullptr;
} }
void PreviewWindow::initialize(LayerManager *layerManager)
{
this->layerManager = layerManager;
}
void PreviewWindow::show() void PreviewWindow::show()
{ {
mainScene->clear();
QFile settingFile; QFile settingFile;
settingFile.setFileName("../data.json");
settingFile.setFileName("../data.json"); settingFile.setFileName("../data.json");
settingFile.open(QFile::ReadOnly); settingFile.open(QFile::ReadOnly);
QByteArray setting = settingFile.readAll().trimmed(); QByteArray setting = settingFile.readAll().trimmed();
QJsonDocument jsonDoc(QJsonDocument::fromJson(setting)); QJsonDocument jsonDoc(QJsonDocument::fromJson(setting));
auto jElements = jsonDoc.object().value("elements").toArray(); auto jElements = jsonDoc.object().value("elements").toArray();
for (auto &&ele : jElements) for (auto &&ele : jElements)
{ {
SimpleElement element(ele.toObject()); SimpleElement element(ele.toObject());
QGraphicsPathItem *item = new QGraphicsPathItem(element.getPaintObject()); painter->drawPath(element.getPaintObject());
mainScene->addItem(item);
} }
}
QGraphicsView::show();
void PreviewWindow::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void PreviewWindow::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
painter->begin(this);
painter->setRenderHint(QPainter::Antialiasing);
painter->setRenderHint(QPainter::HighQualityAntialiasing);
layerManager->paint(painter);
painter->end();
}
void PreviewWindow::resizeGL(int w, int h)
{
} }

View File

@ -1,20 +1,26 @@
#pragma once #pragma once
#include "GraphicElement.h" #include "GraphicElement.h"
#include "LayerManager.h" #include "LayerManager.h"
#include "QGraphicsItem.h"
#include "QGraphicsView.h"
#include <QFile> #include <QFile>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
class PreviewWindow : public QGraphicsView #include <QOpenGLFunctions>
#include <QOpenGLWidget>
class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions
{ {
Q_OBJECT Q_OBJECT
private: private:
QGraphicsScene *mainScene; QPainter *painter;
LayerManager *layerManager;
public: public:
PreviewWindow(QWidget *parent = 0); PreviewWindow(QWidget *parent = nullptr);
void initialize(LayerManager *layerManager);
void show(); void show();
void initializeGL() override;
void paintGL() override;
void resizeGL(int w, int h) override;
}; };