loom
94 строки · 2.5 Кб
1#include "GraphicsViewStyle.hpp"
2
3#include <QtCore/QFile>
4#include <QtCore/QJsonArray>
5#include <QtCore/QJsonObject>
6#include <QtCore/QJsonValueRef>
7
8#include "StyleCollection.hpp"
9
10using QtNodes::GraphicsViewStyle;
11
12inline void initResources()
13{
14Q_INIT_RESOURCE(resources);
15}
16
17GraphicsViewStyle::GraphicsViewStyle()
18{
19// Explicit resources inialization for preventing the static initialization
20// order fiasco: https://isocpp.org/wiki/faq/ctors#static-init-order
21initResources();
22
23// This configuration is stored inside the compiled unit and is loaded statically
24loadJsonFile(":DefaultStyle.json");
25}
26
27GraphicsViewStyle::GraphicsViewStyle(QString jsonText)
28{
29loadJsonText(jsonText);
30}
31
32void GraphicsViewStyle::setStyle(QString jsonText)
33{
34GraphicsViewStyle style(jsonText);
35
36StyleCollection::setGraphicsViewStyle(style);
37}
38
39#ifdef STYLE_DEBUG
40#define FLOW_VIEW_STYLE_CHECK_UNDEFINED_VALUE(v, variable) \
41{ \
42if (v.type() == QJsonValue::Undefined || v.type() == QJsonValue::Null) \
43qWarning() << "Undefined value for parameter:" << #variable; \
44}
45#else
46#define FLOW_VIEW_STYLE_CHECK_UNDEFINED_VALUE(v, variable)
47#endif
48
49#define FLOW_VIEW_STYLE_READ_COLOR(values, variable) \
50{ \
51auto valueRef = values[#variable]; \
52FLOW_VIEW_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
53if (valueRef.isArray()) { \
54auto colorArray = valueRef.toArray(); \
55std::vector<int> rgb; \
56rgb.reserve(3); \
57for (auto it = colorArray.begin(); it != colorArray.end(); ++it) { \
58rgb.push_back((*it).toInt()); \
59} \
60variable = QColor(rgb[0], rgb[1], rgb[2]); \
61} else { \
62variable = QColor(valueRef.toString()); \
63} \
64}
65
66#define FLOW_VIEW_STYLE_WRITE_COLOR(values, variable) \
67{ \
68values[#variable] = variable.name(); \
69}
70
71void GraphicsViewStyle::loadJson(QJsonObject const &json)
72{
73QJsonValue nodeStyleValues = json["GraphicsViewStyle"];
74
75QJsonObject obj = nodeStyleValues.toObject();
76
77FLOW_VIEW_STYLE_READ_COLOR(obj, BackgroundColor);
78FLOW_VIEW_STYLE_READ_COLOR(obj, FineGridColor);
79FLOW_VIEW_STYLE_READ_COLOR(obj, CoarseGridColor);
80}
81
82QJsonObject GraphicsViewStyle::toJson() const
83{
84QJsonObject obj;
85
86FLOW_VIEW_STYLE_WRITE_COLOR(obj, BackgroundColor);
87FLOW_VIEW_STYLE_WRITE_COLOR(obj, FineGridColor);
88FLOW_VIEW_STYLE_WRITE_COLOR(obj, CoarseGridColor);
89
90QJsonObject root;
91root["GraphicsViewStyle"] = obj;
92
93return root;
94}
95