-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnits.cpp
99 lines (78 loc) · 2.26 KB
/
Units.cpp
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
#include "Units.h"
#include <QtMath>
#include <QDebug>
Units::Units(QObject *parent) :
QObject(parent)
{
init();
}
Units::Units(const QSize& screenSize, QObject *parent) :
QObject(parent)
{
init(screenSize);
}
Units::Units(const QSize &screenSize, const QSize &designedForSize, QObject *parent) :
QObject(parent)
{
init(screenSize, designedForSize);
}
Units::Units(const QSize &screenSize, const QSize &designedForSize, const qreal ratioMin, QObject *parent) :
QObject(parent)
{
init(screenSize, designedForSize, ratioMin);
}
Units::Units(const QSize &screenSize, const QSize &designedForSize, const qreal ratioMin, const qreal ratioMax, QObject *parent) :
QObject(parent)
{
init(screenSize, designedForSize, ratioMin, ratioMax);
}
Units::Units(const QSize &screenSize, const QSize &designedForSize, const qreal ratioMin, const qreal ratioMax, const bool roundUp, QObject *parent) :
QObject(parent)
{
init(screenSize, designedForSize, ratioMin, ratioMax, roundUp);
}
qreal Units::pt(int pixel)
{
return qreal(pixel) / mRatio;
}
int Units::px(qreal point)
{
return scale(point, mRatio);
}
int Units::scale(qreal point, qreal ratio)
{
return mRoudUp?qCeil(point * ratio) : qFloor(point * ratio);
}
qreal Units::ratio()
{
return mRatio;
}
QString Units::size()
{
return QString("%1 x %2").arg(mCurrentSize.width()).arg(mCurrentSize.height());
}
void Units::roundUp(bool r)
{
mRoudUp = r;
}
void Units::computeRatio()
{
qreal d= qSqrt(qPow(mIntendedSize.width(),2)+ qPow(mIntendedSize.height(), 2));
//Compute the diagonal length of the current app window (IS)
qreal appd = qSqrt(qPow(mCurrentSize.width(),2)+ qPow(mCurrentSize.height(), 2));
//Calculate the ration between what IS and what SHOULD be
mRatio = appd/d;
if (qAbs(mRatio) < qAbs(mRatioMin)) mRatio = mRatioMin;
if (qAbs(mRatio) > qAbs(mRatioMax)) mRatio = mRatioMax;
qDebug()<<"RATIO COMPUTED "<<mRatio;
emit ratioChanged();
}
void Units::init(const QSize &screenSize, const QSize &designedForSize, const qreal ratioMin, const qreal ratioMax, const bool roundUp)
{
mCurrentSize = screenSize;
mIntendedSize = designedForSize;
mRatioMin = ratioMin;
mRatioMax = ratioMax;
mRoudUp = roundUp;
computeRatio();
}