Compare commits

...

2 Commits

Author SHA1 Message Date
白封羽 3a3374a886 merge 2022-11-17 23:29:09 +08:00
白封羽 64e81d2e0a 完成了SimpleElement 2022-11-17 23:27:06 +08:00
5 changed files with 187 additions and 11 deletions

View File

@ -1,10 +1,30 @@
#include "EditorWidget.h" #include "EditorWidget.h"
EditorWidget::EditorWidget(QWidget *parent) EditorWidget::EditorWidget(QWidget *parent) : QWidget(parent)
: QWidget(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
} }
EditorWidget::~EditorWidget() 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();
}

View File

@ -1,8 +1,15 @@
#pragma once #pragma once
#include <QWidget> #include "GraphicElement.h"
#include "ui_EditorWidget.h" #include "ui_EditorWidget.h"
#include <QPainter>
#include <QWidget>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
class EditorWidget : public QWidget class EditorWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
@ -10,6 +17,7 @@ class EditorWidget : public QWidget
public: public:
EditorWidget(QWidget *parent = nullptr); EditorWidget(QWidget *parent = nullptr);
~EditorWidget(); ~EditorWidget();
void paintEvent(QPaintEvent *event) override;
private: private:
Ui::EditorWidgetClass ui; Ui::EditorWidgetClass ui;

View File

@ -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<int>(elementType);
return sStringTag[type];
}
ElementType ElementTypeStringValue::enumType(std::string elementType)
{
for (int i = 1; i <= sElementType; i++)
{
if (elementType == sStringTag[i])
{
return static_cast<ElementType>(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;
}
}

View File

@ -0,0 +1,53 @@
#pragma once
#include <QJsonArray>
#include <QJsonObject>
#include <QPainterPath>
#include <string>
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;
// };

View File

@ -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
}
}
]
}