forked from ZhujinLi/SomeRuler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometrycalculator.cpp
94 lines (81 loc) · 2.44 KB
/
geometrycalculator.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
#include "geometrycalculator.h"
#include <QtMath>
GeometryCalculator::GeometryCalculator()
{
m_rotation = 0;
m_rotationMode = RotationMode_both;
m_rulerSize = QSize(100, 100);
m_paddings = 0;
_update();
}
void GeometryCalculator::setRulerLength(int len)
{
len = qMax(len, 100);
QSize size(len, 100);
if (m_rulerSize != size) {
m_rulerSize = size;
_update();
}
}
void GeometryCalculator::setRotation(qreal rotation)
{
switch (m_rotationMode) {
case RotationMode_up:
rotation = qMax(rotation, -90.0);
rotation = qMin(rotation, 0.0);
break;
case RotationMode_down:
rotation = qMax(rotation, 0.0);
rotation = qMin(rotation, 90.0);
break;
case RotationMode_both:
default:
rotation = qMax(rotation, -90.0);
rotation = qMin(rotation, 90.0);
break;
}
if (rotation != m_rotation) {
m_rotation = rotation;
_update();
if (m_rotationMode == RotationMode_both && m_rotation < 0)
m_rotationMode = RotationMode_up;
else if (m_rotationMode == RotationMode_both && m_rotation > 0)
m_rotationMode = RotationMode_down;
}
}
QPoint GeometryCalculator::transformPos(const QPoint &pos) const
{
return pos * m_transform;
}
QPoint GeometryCalculator::inversePos(const QPoint &pos) const
{
return pos * m_invTransform;
}
void GeometryCalculator::_update()
{
int w = m_rulerSize.width();
int h = m_rulerSize.height();
qreal rotationInRadius = qDegreesToRadians(m_rotation);
qreal winW, winH;
if (m_rotation > 0) {
winW = h + w * cos(rotationInRadius);
winH = w * abs(sin(rotationInRadius)) + h * cos(rotationInRadius);
} else {
winW = h + w * cos(rotationInRadius) + h * abs(sin(rotationInRadius));
winH = h + w * abs(sin(rotationInRadius));
}
m_windowSize = {static_cast<int>(winW + 2 * m_paddings), static_cast<int>(winH + 2 * m_paddings)};
if (m_rotation > 0) {
m_transform = QTransform()
.translate(m_paddings, m_paddings)
.translate(m_rulerSize.height(), 0)
.rotate(m_rotation);
} else {
m_transform = QTransform()
.translate(m_paddings, m_paddings)
.translate(m_rulerSize.height(), winH - h)
.rotate(m_rotation);
}
assert(m_transform.isInvertible());
m_invTransform = m_transform.inverted();
}