forked from paceholder/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeStyle.cpp
More file actions
179 lines (147 loc) · 5.13 KB
/
NodeStyle.cpp
File metadata and controls
179 lines (147 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "NodeStyle.hpp"
#include "StyleCollection.hpp"
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonValueRef>
#include <QtCore/QDebug>
using QtNodes::NodeStyle;
inline void initResources()
{
Q_INIT_RESOURCE(resources);
}
NodeStyle::NodeStyle()
{
// Explicit resources inialization for preventing the static initialization
// order fiasco: https://isocpp.org/wiki/faq/ctors#static-init-order
initResources();
// Initialize status icons after resources are loaded
statusUpdated = QIcon(":/status_icons/updated.svg");
statusProcessing = QIcon(":/status_icons/processing.svg");
statusPending = QIcon(":/status_icons/pending.svg");
statusInvalid = QIcon(":/status_icons/failed.svg");
statusEmpty = QIcon(":/status_icons/empty.svg");
statusPartial = QIcon(":/status_icons/partial.svg");
// This configuration is stored inside the compiled unit and is loaded statically
loadJsonFile(":DefaultStyle.json");
}
NodeStyle::NodeStyle(QString jsonText)
{
loadJsonText(jsonText);
}
NodeStyle::NodeStyle(QJsonObject const &json)
{
loadJson(json);
}
void NodeStyle::setNodeStyle(QString jsonText)
{
NodeStyle style(jsonText);
StyleCollection::setNodeStyle(style);
}
#ifdef STYLE_DEBUG
#define NODE_STYLE_CHECK_UNDEFINED_VALUE(v, variable) \
{ \
if (v.type() == QJsonValue::Undefined || v.type() == QJsonValue::Null) \
qWarning() << "Undefined value for parameter:" << #variable; \
}
#else
#define NODE_STYLE_CHECK_UNDEFINED_VALUE(v, variable)
#endif
#define NODE_STYLE_READ_COLOR(values, variable) \
{ \
auto valueRef = values[#variable]; \
NODE_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
if (valueRef.isArray()) { \
auto colorArray = valueRef.toArray(); \
std::vector<int> rgb; \
rgb.reserve(3); \
for (auto it = colorArray.begin(); it != colorArray.end(); ++it) { \
rgb.push_back((*it).toInt()); \
} \
variable = QColor(rgb[0], rgb[1], rgb[2]); \
} else { \
variable = QColor(valueRef.toString()); \
} \
}
#define NODE_STYLE_WRITE_COLOR(values, variable) \
{ \
values[#variable] = variable.name(); \
}
#define NODE_STYLE_READ_FLOAT(values, variable) \
{ \
auto valueRef = values[#variable]; \
NODE_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
variable = valueRef.toDouble(); \
}
#define NODE_STYLE_WRITE_FLOAT(values, variable) \
{ \
values[#variable] = variable; \
}
#define NODE_STYLE_READ_BOOL(values, variable) \
{ \
auto valueRef = values[#variable]; \
NODE_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
variable = valueRef.toBool(); \
}
#define NODE_STYLE_WRITE_BOOL(values, variable) \
{ \
values[#variable] = variable; \
}
void NodeStyle::loadJson(QJsonObject const &json)
{
QJsonValue nodeStyleValues = json["NodeStyle"];
QJsonObject obj = nodeStyleValues.toObject();
NODE_STYLE_READ_COLOR(obj, NormalBoundaryColor);
NODE_STYLE_READ_COLOR(obj, SelectedBoundaryColor);
NODE_STYLE_READ_COLOR(obj, GradientColor0);
NODE_STYLE_READ_COLOR(obj, GradientColor1);
NODE_STYLE_READ_COLOR(obj, GradientColor2);
NODE_STYLE_READ_COLOR(obj, GradientColor3);
NODE_STYLE_READ_COLOR(obj, ShadowColor);
NODE_STYLE_READ_BOOL(obj, ShadowEnabled);
NODE_STYLE_READ_COLOR(obj, FontColor);
NODE_STYLE_READ_COLOR(obj, FontColorFaded);
NODE_STYLE_READ_COLOR(obj, ConnectionPointColor);
NODE_STYLE_READ_COLOR(obj, FilledConnectionPointColor);
NODE_STYLE_READ_COLOR(obj, WarningColor);
NODE_STYLE_READ_COLOR(obj, ErrorColor);
NODE_STYLE_READ_FLOAT(obj, PenWidth);
NODE_STYLE_READ_FLOAT(obj, HoveredPenWidth);
NODE_STYLE_READ_FLOAT(obj, ConnectionPointDiameter);
NODE_STYLE_READ_FLOAT(obj, Opacity);
}
QJsonObject NodeStyle::toJson() const
{
QJsonObject obj;
NODE_STYLE_WRITE_COLOR(obj, NormalBoundaryColor);
NODE_STYLE_WRITE_COLOR(obj, SelectedBoundaryColor);
NODE_STYLE_WRITE_COLOR(obj, GradientColor0);
NODE_STYLE_WRITE_COLOR(obj, GradientColor1);
NODE_STYLE_WRITE_COLOR(obj, GradientColor2);
NODE_STYLE_WRITE_COLOR(obj, GradientColor3);
NODE_STYLE_WRITE_COLOR(obj, ShadowColor);
NODE_STYLE_WRITE_BOOL(obj, ShadowEnabled);
NODE_STYLE_WRITE_COLOR(obj, FontColor);
NODE_STYLE_WRITE_COLOR(obj, FontColorFaded);
NODE_STYLE_WRITE_COLOR(obj, ConnectionPointColor);
NODE_STYLE_WRITE_COLOR(obj, FilledConnectionPointColor);
NODE_STYLE_WRITE_COLOR(obj, WarningColor);
NODE_STYLE_WRITE_COLOR(obj, ErrorColor);
NODE_STYLE_WRITE_FLOAT(obj, PenWidth);
NODE_STYLE_WRITE_FLOAT(obj, HoveredPenWidth);
NODE_STYLE_WRITE_FLOAT(obj, ConnectionPointDiameter);
NODE_STYLE_WRITE_FLOAT(obj, Opacity);
QJsonObject root;
root["NodeStyle"] = obj;
return root;
}
void NodeStyle::setBackgroundColor(QColor const &color)
{
GradientColor0 = color;
GradientColor1 = color;
GradientColor2 = color;
GradientColor3 = color;
}
QColor NodeStyle::backgroundColor() const
{
return GradientColor0;
}