-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutility.h
More file actions
69 lines (60 loc) · 1.57 KB
/
utility.h
File metadata and controls
69 lines (60 loc) · 1.57 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
#ifndef UTILITY_H
#define UTILITY_H
#include <QWidget>
#include <QLayout>
#include <QDir>
template<typename... A>
struct resolve{
template<typename C, typename F>
static constexpr auto from(F (C::*function)(A...)) -> decltype(function)
{
return function;
}
};
template<typename T>
T clamp(T value, T low, T high)
{
return (value < low) ? low :
(value > high) ? high :
value;
}
template <typename T1, typename T2> void conditional_variant_copy(T1 &a, T2 &b){ Q_UNUSED(a); Q_UNUSED(b); }
template <typename T = QVariant> void conditional_variant_copy(T &a, T &b)
{
int type = a.type();
a = b;
a.convert(type);
}
inline void propagate_resize(QWidget *child)
{
QWidget *parent = child->parentWidget();
if(parent->layout()){
parent->layout()->invalidate();
}
do{
parent->adjustSize();
parent = parent->parentWidget();
}while(parent);
}
inline QString absolute_path(const QString file_path)
{
QDir directory;
return directory.absoluteFilePath(file_path);
}
inline QString to_hex(int data, int pad = 2)
{
QString hex = QString::number(data, 16).rightJustified(pad, '0').toUpper();
hex.truncate(pad);
return hex;
}
//add optional endian flip in the future maybe
inline unsigned short read_word(const QByteArray &data, int offset)
{
return (unsigned char)data.at(offset) | ((unsigned char)data.at(offset+1) << 8);
}
inline unsigned int read_long(const QByteArray &data, int offset)
{
return (unsigned char)data.at(offset) | ((unsigned char)data.at(offset+1) << 8) |
((unsigned char)data.at(offset+2) << 16);
}
#endif // UTILITY_H