Compare commits
2 Commits
c8cc72b0be
...
3a3374a886
Author | SHA1 | Date |
---|---|---|
白封羽 | 3a3374a886 | |
白封羽 | 64e81d2e0a |
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <QWidget>
|
||||
#include "GraphicElement.h"
|
||||
#include "ui_EditorWidget.h"
|
||||
#include <QPainter>
|
||||
#include <QWidget>
|
||||
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
// };
|
|
@ -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
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue