改用了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>
</property>
<item>
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="0,0">
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="1,30">
<item>
<widget class="QLabel" name="title">
<property name="text">
@ -46,7 +46,7 @@
<customwidgets>
<customwidget>
<class>PreviewWindow</class>
<extends>QGraphicsView</extends>
<extends>QOpenGLWidget</extends>
<header>PreviewWindow.h</header>
</customwidget>
</customwidgets>

View File

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

View File

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

View File

@ -1,15 +1,25 @@
#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::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
#include "GraphicElement.h"
#include "LayerManager.h"
#include <QJsonArray>
#include <vector>
using std::vector;
class LayerManager;
class ElementManager
{
private:
static ElementManager *INSTANCE;
vector<GraphicElement> mElements;
vector<GraphicElement *> elements;
public:
static ElementManager *getInstance();
ElementManager(QJsonArray jsonElementArray);
void initialize(LayerManager *layerManager);
void addElement(GraphicElement *element);
void removeElement(GraphicElement *pElement);
/**
* 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
{
return mPainterPath;
return painterPath;
}
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()));
}
mPainterPath.addPolygon(polygon);
painterPath.addPolygon(polygon);
}
void SimpleElement::roundOperate(QJsonObject json)
{
double xAxis = json.value("x-axis").toDouble();
double yAxis = json.value("y-axis").toDouble();
double angle = json.value("angle").toDouble();
mPainterPath.arcMoveTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0);
mPainterPath.arcTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0, angle);
painterPath.arcMoveTo(xAxis * -0.5, yAxis * -0.5, xAxis, yAxis, 0);
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();
mElementType = ElementTypeStringValue::enumType(mJsonSource.value("type").toString().toStdString());
switch (mElementType)
painterPath.clear();
elementType = ElementTypeStringValue::enumType(jsonSource.value("type").toString().toStdString());
switch (elementType)
{
case ElementType::undefined:
break;
case ElementType::path: {
QJsonArray paths = mJsonSource.value("data").toObject().value("operations").toArray();
QJsonArray paths = jsonSource.value("data").toObject().value("operations").toArray();
pathOperate(paths);
break;
}
case ElementType::polygon: {
QJsonArray points = mJsonSource.value("data").toObject().value("points").toArray();
QJsonArray points = jsonSource.value("data").toObject().value("points").toArray();
polygonOperate(points);
break;
}
case ElementType::round: {
roundOperate(mJsonSource.value("data").toObject());
roundOperate(jsonSource.value("data").toObject());
break;
}
default:
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
#include "LayerWrapper.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QPainterPath>
#include <string>
class LayerWrapper;
class LeafLayerWrapper;
class FolderLayerWrapper;
enum class ElementType
{
undefined = 0,
@ -28,9 +33,9 @@ class GraphicElement
class SimpleElement : public GraphicElement
{
private:
QJsonObject mJsonSource;
QPainterPath mPainterPath;
ElementType mElementType;
QJsonObject jsonSource;
QPainterPath painterPath;
ElementType elementType;
void pathOperate(QJsonArray paths);
void polygonOperate(QJsonArray points);
void roundOperate(QJsonObject json);
@ -40,14 +45,13 @@ class SimpleElement : public GraphicElement
~SimpleElement() = default;
QPainterPath getPaintObject() const override;
};
// class GroupElement : public GraphicElement
//{
// private:
// FolderLayerWrapper *mSourceLayer;
//
// public:
// GroupElement(QJsonObject jsonSource);
// GroupElement(FolderLayerWrapper *mSourceLayer);
// ~GroupElement() = default;
// QPainterPath getPaintObject() const override;
// };
class GroupElement : public GraphicElement
{
private:
FolderLayerWrapper *sourceLayer;
public:
GroupElement(FolderLayerWrapper *mSourceLayer);
~GroupElement() = default;
QPainterPath getPaintObject() const override;
};

View File

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

View File

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

View File

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

View File

@ -10,6 +10,10 @@
#include <vector>
using std::shared_ptr;
using std::vector;
class GraphicElement;
class SimpleElement;
class GroupElement;
class LayerWrapper
{
protected:
@ -19,7 +23,7 @@ class LayerWrapper
QPainterPath cache;
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
};

View File

@ -1,28 +1,51 @@
#include "PreviewWindow.h"
PreviewWindow::PreviewWindow(QWidget *parent) : QGraphicsView(parent)
PreviewWindow::PreviewWindow(QWidget *parent) : QOpenGLWidget(parent)
{
this->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
mainScene = new QGraphicsScene(this);
this->setScene(mainScene);
QSurfaceFormat surfaceFormat;
surfaceFormat.setSamples(16);
setFormat(surfaceFormat);
painter = new QPainter(this);
layerManager = nullptr;
}
void PreviewWindow::initialize(LayerManager *layerManager)
{
this->layerManager = layerManager;
}
void PreviewWindow::show()
{
mainScene->clear();
QFile settingFile;
settingFile.setFileName("../data.json");
settingFile.setFileName("../data.json");
settingFile.open(QFile::ReadOnly);
QByteArray setting = settingFile.readAll().trimmed();
QJsonDocument jsonDoc(QJsonDocument::fromJson(setting));
auto jElements = jsonDoc.object().value("elements").toArray();
for (auto &&ele : jElements)
{
SimpleElement element(ele.toObject());
QGraphicsPathItem *item = new QGraphicsPathItem(element.getPaintObject());
mainScene->addItem(item);
painter->drawPath(element.getPaintObject());
}
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
#include "GraphicElement.h"
#include "LayerManager.h"
#include "QGraphicsItem.h"
#include "QGraphicsView.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
class PreviewWindow : public QGraphicsView
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
class PreviewWindow : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
private:
QGraphicsScene *mainScene;
QPainter *painter;
LayerManager *layerManager;
public:
PreviewWindow(QWidget *parent = 0);
PreviewWindow(QWidget *parent = nullptr);
void initialize(LayerManager *layerManager);
void show();
void initializeGL() override;
void paintGL() override;
void resizeGL(int w, int h) override;
};