-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHelperFunctions.h
254 lines (227 loc) · 9.68 KB
/
HelperFunctions.h
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef HELPERFUNCTIONS_H
#define HELPERFUNCTIONS_H
#include "Configuration.h"
#include <QCoreApplication>
#include <QGraphicsItem>
#include <QPropertyAnimation>
#include <QGraphicsEffect>
#include <QPoint>
static const std::map<ComponentType, ConfiguratorMode> CONFIGURATOR_MODE_MAP{{ComponentType::AND_GATE, ConfiguratorMode::DIRECTION_AND_INPUT_COUNT},
{ComponentType::OR_GATE, ConfiguratorMode::DIRECTION_AND_INPUT_COUNT},
{ComponentType::XOR_GATE, ConfiguratorMode::DIRECTION_AND_INPUT_COUNT},
{ComponentType::NOT_GATE, ConfiguratorMode::DIRECTION_ONLY},
{ComponentType::BUFFER_GATE, ConfiguratorMode::DIRECTION_ONLY},
{ComponentType::INPUT, ConfiguratorMode::NO_CONFIGURATION},
{ComponentType::CONSTANT, ConfiguratorMode::CONSTANT_STATE},
{ComponentType::BUTTON, ConfiguratorMode::NO_CONFIGURATION},
{ComponentType::CLOCK, ConfiguratorMode::DIRECTION_ONLY},
{ComponentType::OUTPUT, ConfiguratorMode::NO_CONFIGURATION},
{ComponentType::TEXT_LABEL, ConfiguratorMode::NO_CONFIGURATION},
{ComponentType::HALF_ADDER, ConfiguratorMode::DIRECTION_ONLY},
{ComponentType::FULL_ADDER, ConfiguratorMode::DIRECTION_ONLY},
{ComponentType::RS_FLIPFLOP, ConfiguratorMode::RS_FLIPFLOP_TYPE},
{ComponentType::D_FLIPFLOP, ConfiguratorMode::MASTER_SLAVE},
{ComponentType::T_FLIPFLOP, ConfiguratorMode::DIRECTION_ONLY},
{ComponentType::JK_FLIPFLOP, ConfiguratorMode::MASTER_SLAVE},
{ComponentType::MULTIPLEXER, ConfiguratorMode::MULTIPLEXER_BITS},
{ComponentType::DEMULTIPLEXER, ConfiguratorMode::MULTIPLEXER_BITS},
{ComponentType::DECODER, ConfiguratorMode::ENCODER_DECODER},
{ComponentType::ENCODER, ConfiguratorMode::ENCODER_DECODER},
{ComponentType::SHIFTREGISTER, ConfiguratorMode::SHIFTREGISTER_BITS},
{ComponentType::COUNTER, ConfiguratorMode::COUNTER_BITS}};
/// \brief Rounds the given point to the nearest grid point
/// \param pPoint: The point to round
/// \return The rounded point
inline QPointF SnapToGrid(QPointF pPoint)
{
return QPointF(std::floor(pPoint.x() / canvas::GRID_SIZE + 0.5f) * canvas::GRID_SIZE,
std::floor(pPoint.y() / canvas::GRID_SIZE + 0.5f) * canvas::GRID_SIZE);
}
/// \brief Calculates the Euclidian distance between pA and pB
/// \param pA: Point A
/// \param pB: Point B
/// \return The distance between point A and point B
inline uint32_t EuclidianDistance(QPointF pA, QPointF pB)
{
return std::sqrt(std::pow(pA.x() - pB.x(), 2) + std::pow(pA.y() - pB.y(), 2));
}
/// \brief Creates a superscript string for the given number
/// \param pNumber: The number to write as superscript
/// \return A string containing the number as superscript
inline QString BuildSuperscriptString(uint32_t pNumber)
{
auto rest = pNumber;
QString result{""};
if (rest == 0)
{
return helpers::SUPERSCRIPTS[0];
}
while (rest != 0)
{
result = helpers::SUPERSCRIPTS[rest % 10] + result;
rest /= 10;
}
return result;
}
/// \brief Inverts the given logic state
/// \param pState: The state to invert
/// \return The inverted state
inline LogicState InvertState(LogicState pState)
{
switch (pState)
{
case LogicState::LOW:
{
return LogicState::HIGH;
}
case LogicState::HIGH:
{
return LogicState::LOW;
}
default:
{
throw std::logic_error("Logic state invalid");
}
}
return LogicState::LOW;
}
/// \brief Returns the configurator mode for the given component type
/// \param pType: A component type
/// \return A configurator mode
inline ConfiguratorMode GetConfiguratorModeForComponentType(ComponentType pType)
{
if (CONFIGURATOR_MODE_MAP.find(pType) != CONFIGURATOR_MODE_MAP.end())
{
return CONFIGURATOR_MODE_MAP.at(pType);
}
return ConfiguratorMode::NO_CONFIGURATION;
}
/// \brief Returns the absolute path to the runtime config JSON file
/// \return The absolute path as a QString
inline QString GetRuntimeConfigAbsolutePath(void)
{
// QCoreApplication::applicationDirPath() gets the path to the executable instead of the path from where it is executed
return QCoreApplication::applicationDirPath() + file::runtime_config::RUNTIME_CONFIG_RELATIVE_PATH;
}
/// \brief Compares the two given versions
/// \param pVersion1: The first version
/// \param pVersion2: The second version
/// \return negative if version 1 is older, 0 if it is the same, positive if it is newer
inline int8_t CompareVersions(SwVersion pVersion1, SwVersion pVersion2)
{
if (pVersion1.major < pVersion2.major)
{
return -1;
}
else if (pVersion1.major > pVersion2.major)
{
return 1;
}
else
{
if (pVersion1.minor < pVersion2.minor)
{
return -1;
}
else if (pVersion1.minor > pVersion2.minor)
{
return 1;
}
else
{
if (pVersion1.patch < pVersion2.patch)
{
return -1;
}
else if (pVersion1.patch > pVersion2.patch)
{
return 1;
}
else
{
return 0;
}
}
}
}
/// \brief Compares the given semantic version to the version of the current software
/// \param pVersion: The version to compare
/// \return negative if the given version is older, 0 if it is the same, positive if it is newer
inline int8_t CompareWithCurrentVersion(SwVersion pVersion)
{
return CompareVersions(pVersion, SwVersion(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION));
}
/// \brief Returns the newer of the two given software versions
/// \param pVersion1: The first version
/// \param pVersion2: The second version
/// \return The newer software version
inline SwVersion GetNewerVersion(SwVersion pVersion1, SwVersion pVersion2)
{
if (CompareVersions(pVersion1, pVersion2) > 0)
{
return pVersion1;
}
else
{
return pVersion2;
}
}
/// \brief Fades in the given GUI widget of type T
/// \param pWidget: The GUI widget to fade in
/// \param pDuration: The time the animation will take
template <typename T>
void FadeInWidget(T& pWidget, uint32_t pDuration)
{
bool stoppedCurrentAnimation = false;
if (pWidget.graphicsEffect() != nullptr)
{
delete pWidget.graphicsEffect();
stoppedCurrentAnimation = true;
}
if (stoppedCurrentAnimation || !pWidget.isVisible())
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect();
pWidget.setGraphicsEffect(effect);
pWidget.show();
QPropertyAnimation *anim = new QPropertyAnimation(effect, "opacity");
anim->setDuration(pDuration);
anim->setStartValue(0.0f);
anim->setEndValue(1.0f);
anim->setEasingCurve(QEasingCurve::InCirc);
QObject::connect(anim, &QPropertyAnimation::finished, [&]()
{
delete pWidget.graphicsEffect();
});
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
}
/// \brief Fades out the given GUI widget of type T
/// \param pWidget: The GUI widget to fade out
/// \param pDuration: The time the animation will take
template <typename T>
void FadeOutWidget(T& pWidget, uint32_t pDuration)
{
bool stoppedCurrentAnimation = false;
if (pWidget.graphicsEffect() != nullptr)
{
delete pWidget.graphicsEffect();
stoppedCurrentAnimation = true;
}
if (stoppedCurrentAnimation || pWidget.isVisible())
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect();
pWidget.setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(effect, "opacity");
anim->setDuration(pDuration);
anim->setStartValue(1.0f);
anim->setEndValue(0.0f);
anim->setEasingCurve(QEasingCurve::OutCirc);
QObject::connect(anim, &QPropertyAnimation::finished, [&]()
{
delete pWidget.graphicsEffect();
pWidget.hide();
});
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
}
#endif // HELPERFUNCTIONS_H