diff --git a/ArchitectureColoredPainting/EditorWidget.ui b/ArchitectureColoredPainting/EditorWidget.ui index b3ad826..7f00648 100644 --- a/ArchitectureColoredPainting/EditorWidget.ui +++ b/ArchitectureColoredPainting/EditorWidget.ui @@ -27,7 +27,7 @@ 0 - + @@ -46,7 +46,7 @@ PreviewWindow - QGraphicsView + QOpenGLWidget
PreviewWindow.h
diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp index 1e0544e..cc00019 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp @@ -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 -using namespace std; + void EditorWidget::paintEvent(QPaintEvent *event) { - previewWindow->show(); } diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidget.h b/ArchitectureColoredPainting/src/Editor/EditorWidget.h index 10ae5b8..14ce226 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidget.h +++ b/ArchitectureColoredPainting/src/Editor/EditorWidget.h @@ -1,6 +1,7 @@ #pragma once -#include "GraphicElement.h" +#include "ElementManager.h" +#include "LayerManager.h" #include "PreviewWindow.h" #include "ui_EditorWidget.h" #include @@ -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; }; diff --git a/ArchitectureColoredPainting/src/Editor/ElementManager.cpp b/ArchitectureColoredPainting/src/Editor/ElementManager.cpp index af7cbde..954a87d 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/ElementManager.cpp @@ -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; } diff --git a/ArchitectureColoredPainting/src/Editor/ElementManager.h b/ArchitectureColoredPainting/src/Editor/ElementManager.h index 0397576..bd07491 100644 --- a/ArchitectureColoredPainting/src/Editor/ElementManager.h +++ b/ArchitectureColoredPainting/src/Editor/ElementManager.h @@ -1,19 +1,23 @@ #pragma once #include "GraphicElement.h" +#include "LayerManager.h" +#include #include using std::vector; +class LayerManager; + class ElementManager { private: - static ElementManager *INSTANCE; - vector mElements; + vector 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); }; diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index ba9977e..cc3b7ca 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -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(); +} diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index 8d59515..ea32b3a 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -1,8 +1,13 @@ #pragma once +#include "LayerWrapper.h" #include #include #include #include +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; +}; diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp index 0ebbb92..35ca603 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.cpp @@ -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 { } diff --git a/ArchitectureColoredPainting/src/Editor/LayerManager.h b/ArchitectureColoredPainting/src/Editor/LayerManager.h index df52e82..a1fc218 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerManager.h +++ b/ArchitectureColoredPainting/src/Editor/LayerManager.h @@ -1,10 +1,14 @@ #pragma once +#include "ElementManager.h" #include "LayerWrapper.h" -#include +#include +#include #include #include 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; }; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index 74c0401..2c0851b 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -3,6 +3,12 @@ void FolderLayerWrapper::addChild(LayerWrapper *child) { } + void FolderLayerWrapper::removeChild(LayerWrapper *child) { } + +QPainterPath LayerWrapper::getCache() const +{ + return cache; +} diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h index f18f6a4..e1dd524 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.h +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.h @@ -10,6 +10,10 @@ #include 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 }; diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp index 038cc72..3b30cff 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.cpp @@ -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) +{ } diff --git a/ArchitectureColoredPainting/src/Editor/PreviewWindow.h b/ArchitectureColoredPainting/src/Editor/PreviewWindow.h index b5a2f99..3f040ef 100644 --- a/ArchitectureColoredPainting/src/Editor/PreviewWindow.h +++ b/ArchitectureColoredPainting/src/Editor/PreviewWindow.h @@ -1,20 +1,26 @@ #pragma once #include "GraphicElement.h" #include "LayerManager.h" -#include "QGraphicsItem.h" -#include "QGraphicsView.h" #include #include #include #include #include -class PreviewWindow : public QGraphicsView +#include +#include +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; };