forked from l3enQ/nodeeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionStyle.cpp
More file actions
234 lines (177 loc) · 4.64 KB
/
ConnectionStyle.cpp
File metadata and controls
234 lines (177 loc) · 4.64 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "ConnectionStyle.hpp"
#include "StyleCollection.hpp"
#include <QtCore/QFile>
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonValueRef>
#include <QtCore/QJsonArray>
#include <QDebug>
#include <random>
using QtNodes::ConnectionStyle;
inline void initResources() { Q_INIT_RESOURCE(resources); }
ConnectionStyle::
ConnectionStyle()
{
// Explicit resources inialization for preventing the static initialization
// order fiasco: https://isocpp.org/wiki/faq/ctors#static-init-order
initResources();
// This configuration is stored inside the compiled unit and is loaded statically
loadJsonFile(":DefaultStyle.json");
}
ConnectionStyle::
ConnectionStyle(QString jsonText)
{
loadJsonFile(":DefaultStyle.json");
loadJsonText(jsonText);
}
void
ConnectionStyle::
setConnectionStyle(QString jsonText)
{
ConnectionStyle style(jsonText);
StyleCollection::setConnectionStyle(style);
}
#ifdef STYLE_DEBUG
#define CONNECTION_STYLE_CHECK_UNDEFINED_VALUE(v, variable) { \
if (v.type() == QJsonValue::Undefined || \
v.type() == QJsonValue::Null) \
qWarning() << "Undefined value for parameter:" << #variable; \
}
#else
#define CONNECTION_STYLE_CHECK_UNDEFINED_VALUE(v, variable)
#endif
#define CONNECTION_VALUE_EXISTS(v) \
(v.type() != QJsonValue::Undefined && \
v.type() != QJsonValue::Null)
#define CONNECTION_STYLE_READ_COLOR(values, variable) { \
auto valueRef = values[#variable]; \
CONNECTION_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
if (CONNECTION_VALUE_EXISTS(valueRef)) {\
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 CONNECTION_STYLE_READ_FLOAT(values, variable) { \
auto valueRef = values[#variable]; \
CONNECTION_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
if (CONNECTION_VALUE_EXISTS(valueRef)) \
variable = valueRef.toDouble(); \
}
#define CONNECTION_STYLE_READ_BOOL(values, variable) { \
auto valueRef = values[#variable]; \
CONNECTION_STYLE_CHECK_UNDEFINED_VALUE(valueRef, variable) \
if (CONNECTION_VALUE_EXISTS(valueRef)) \
variable = valueRef.toBool(); \
}
void
ConnectionStyle::
loadJsonFile(QString styleFile)
{
QFile file(styleFile);
if (!file.open(QIODevice::ReadOnly))
{
qWarning() << "Couldn't open file " << styleFile;
return;
}
loadJsonFromByteArray(file.readAll());
}
void
ConnectionStyle::
loadJsonText(QString jsonText)
{
loadJsonFromByteArray(jsonText.toUtf8());
}
void
ConnectionStyle::
loadJsonFromByteArray(QByteArray const &byteArray)
{
QJsonDocument json(QJsonDocument::fromJson(byteArray));
QJsonObject topLevelObject = json.object();
QJsonValueRef nodeStyleValues = topLevelObject["ConnectionStyle"];
QJsonObject obj = nodeStyleValues.toObject();
CONNECTION_STYLE_READ_COLOR(obj, ConstructionColor);
CONNECTION_STYLE_READ_COLOR(obj, NormalColor);
CONNECTION_STYLE_READ_COLOR(obj, SelectedColor);
CONNECTION_STYLE_READ_COLOR(obj, SelectedHaloColor);
CONNECTION_STYLE_READ_COLOR(obj, HoveredColor);
CONNECTION_STYLE_READ_FLOAT(obj, LineWidth);
CONNECTION_STYLE_READ_FLOAT(obj, ConstructionLineWidth);
CONNECTION_STYLE_READ_FLOAT(obj, PointDiameter);
CONNECTION_STYLE_READ_BOOL(obj, UseDataDefinedColors);
}
QColor
ConnectionStyle::
constructionColor() const
{
return ConstructionColor;
}
QColor
ConnectionStyle::
normalColor() const
{
return NormalColor;
}
QColor
ConnectionStyle::
normalColor(QString typeId) const
{
std::size_t hash = qHash(typeId);
std::size_t const hue_range = 0xFF;
std::mt19937 gen(static_cast<unsigned int>(hash));
std::uniform_int_distribution<int> distrib(0, hue_range);
int hue = distrib(gen);
int sat = 120 + hash % 129;
return QColor::fromHsl(hue,
sat,
160);
}
QColor
ConnectionStyle::
selectedColor() const
{
return SelectedColor;
}
QColor
ConnectionStyle::
selectedHaloColor() const
{
return SelectedHaloColor;
}
QColor
ConnectionStyle::
hoveredColor() const
{
return HoveredColor;
}
float
ConnectionStyle::
lineWidth() const
{
return LineWidth;
}
float
ConnectionStyle::
constructionLineWidth() const
{
return ConstructionLineWidth;
}
float
ConnectionStyle::
pointDiameter() const
{
return PointDiameter;
}
bool
ConnectionStyle::
useDataDefinedColors() const
{
return UseDataDefinedColors;
}