diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj index cfc5a50..360074c 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -99,6 +99,7 @@ + @@ -145,9 +146,11 @@ + + diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp index 9d1da57..c3ff919 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidget.cpp @@ -1,10 +1,30 @@ #include "EditorWidget.h" -EditorWidget::EditorWidget(QWidget *parent) - : QWidget(parent) +EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent) { - ui.setupUi(this); + ui.setupUi(this); } EditorWidget::~EditorWidget() -{} +{ +} + +using namespace std; +void EditorWidget::paintEvent(QPaintEvent *event) +{ + QPainter pt; + pt.begin(this); + pt.setRenderHint(QPainter::Antialiasing); + QFile settingFile; + settingFile.setFileName("C:/Users/Karlis/Desktop/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()); + pt.drawPath(element.getPaintObject()); + } + pt.end(); +} diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidget.h b/ArchitectureColoredPainting/src/Editor/EditorWidget.h index e5063a8..09d7eb4 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidget.h +++ b/ArchitectureColoredPainting/src/Editor/EditorWidget.h @@ -1,16 +1,24 @@ #pragma once -#include +#include "GraphicElement.h" #include "ui_EditorWidget.h" +#include +#include +#include +#include +#include +#include +#include class EditorWidget : public QWidget { - Q_OBJECT + Q_OBJECT -public: - EditorWidget(QWidget *parent = nullptr); - ~EditorWidget(); + public: + EditorWidget(QWidget *parent = nullptr); + ~EditorWidget(); + void paintEvent(QPaintEvent *event) override; -private: - Ui::EditorWidgetClass ui; + private: + Ui::EditorWidgetClass ui; }; diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp new file mode 100644 index 0000000..ba9977e --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -0,0 +1,69 @@ +#include "GraphicElement.h" +using namespace std; +string ElementTypeStringValue::sStringTag[] = {"undefined", "path", "polygon", "round"}; +string ElementTypeStringValue::stringType(ElementType elementType) +{ + int type = static_cast(elementType); + return sStringTag[type]; +} +ElementType ElementTypeStringValue::enumType(std::string elementType) +{ + for (int i = 1; i <= sElementType; i++) + { + if (elementType == sStringTag[i]) + { + return static_cast(i); + } + } + return ElementType::undefined; +} +QPainterPath SimpleElement::getPaintObject() const +{ + return mPainterPath; +} +void SimpleElement::pathOperate(QJsonArray paths) +{ +} +void SimpleElement::polygonOperate(QJsonArray points) +{ + QPolygonF polygon; + for (auto &&point : points) + { + polygon.append(QPointF(point.toObject().value("x").toDouble(), point.toObject().value("y").toDouble())); + } + mPainterPath.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); +} +SimpleElement::SimpleElement(QJsonObject jsonSource) : mJsonSource(jsonSource) +{ + mPainterPath.clear(); + mElementType = ElementTypeStringValue::enumType(mJsonSource.value("type").toString().toStdString()); + switch (mElementType) + { + case ElementType::undefined: + break; + case ElementType::path: { + QJsonArray paths = mJsonSource.value("data").toObject().value("operations").toArray(); + pathOperate(paths); + break; + } + case ElementType::polygon: { + QJsonArray points = mJsonSource.value("data").toObject().value("points").toArray(); + polygonOperate(points); + break; + } + case ElementType::round: { + roundOperate(mJsonSource.value("data").toObject()); + break; + } + default: + break; + } +} diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h new file mode 100644 index 0000000..8d59515 --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -0,0 +1,53 @@ +#pragma once +#include +#include +#include +#include +enum class ElementType +{ + undefined = 0, + path = 1, + polygon = 2, + round = 3 +}; +class ElementTypeStringValue +{ + private: + static std::string sStringTag[]; + static const int sElementType = 3; + + public: + static std::string stringType(ElementType elementType); + static ElementType enumType(std::string elementType); +}; +class GraphicElement +{ + public: + virtual QPainterPath getPaintObject() const = 0; +}; +class SimpleElement : public GraphicElement +{ + private: + QJsonObject mJsonSource; + QPainterPath mPainterPath; + ElementType mElementType; + void pathOperate(QJsonArray paths); + void polygonOperate(QJsonArray points); + void roundOperate(QJsonObject json); + + public: + SimpleElement(QJsonObject jsonSource); + ~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; +// }; diff --git a/ArchitectureColoredPainting/src/Editor/data.json b/ArchitectureColoredPainting/src/Editor/data.json new file mode 100644 index 0000000..e9053a7 --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/data.json @@ -0,0 +1,26 @@ +{ +"elements":[ + { + "type":"polygon", + "data":{ + "points":[{"x":20,"y":30},{"x":20,"y":50},{"x":50,"y":100},{"x":20,"y":30}] + } + }, + { + "type":"polygon", + "data":{ + "points":[{"x":100,"y":100},{"x":100,"y":500},{"x":500,"y":500},{"x":100,"y":100}] + } + }, + { + "type":"round", + "data":{ + "x-axis":500, + "y-axis":500, + "angle":360 + } + } +] + + +} \ No newline at end of file