-
Notifications
You must be signed in to change notification settings - Fork 15
/
OrientationWidget.cpp
169 lines (140 loc) · 4.15 KB
/
OrientationWidget.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
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
/****************************************************************************
** Deling Final Fantasy VIII Field Editor
** Copyright (C) 2009-2012 Arzel Jérôme <myst6re@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "OrientationWidget.h"
OrientationWidget::OrientationWidget(QWidget *parent) :
QWidget(parent), _value(0), _readOnly(false)
{
}
OrientationWidget::OrientationWidget(quint8 value, QWidget *parent) :
QWidget(parent), _value(0), _readOnly(false)
{
byte2degree(value);
}
quint8 OrientationWidget::value() const
{
return degree2byte();
}
void OrientationWidget::setValue(int value)
{
byte2degree(value);
emit valueChanged(value);
update();
}
void OrientationWidget::byte2degree(quint8 v)
{
_value = (256 - v) * 360 / 256;
}
quint8 OrientationWidget::degree2byte() const
{
return (360 - _value) * 256 / 360;
}
bool OrientationWidget::isReadOnly() const
{
return _readOnly;
}
void OrientationWidget::setReadOnly(bool ro)
{
_readOnly = ro;
}
QSize OrientationWidget::minimumSizeHint() const
{
return QSize(100, 100);
}
QSize OrientationWidget::sizeHint() const
{
return minimumSizeHint();
}
void OrientationWidget::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.setPen(palette().color(QPalette::Text));
p.setBrush(palette().window());
double radius = radiusCircle();
QPointF centerCircle = this->centerCircle();
p.drawEllipse(centerCircle, radius, radius);
p.translate(centerCircle);
p.rotate(_value);
p.drawLines(QVector<QLineF>()
<< QLineF(QPointF(-radius, 0), QPointF(radius, 0))
<< QLineF(QPointF(0, -radius), QPointF(0, radius)));
if(_value >= 90 && _value < 270) {
p.rotate(180);
p.drawText(QRectF(0.0, 0.0, radius-4, radius-4), tr("Droite"), QTextOption(Qt::AlignRight));
} else {
p.drawText(QPointF(-radius+4, -4), tr("Droite"));
}
}
void OrientationWidget::mouseEvent(QMouseEvent *e)
{
if(_readOnly) {
e->ignore();
return;
}
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
const QPointF &relativePos = e->posF();
#else
const QPointF &relativePos = e->localPos();
#endif
if(isInCircle(relativePos)) {
moveCursor(relativePos);
}
}
QPointF OrientationWidget::centerCircle() const
{
return QPointF(width()/2.0, height()/2.0);
}
double OrientationWidget::radiusCircle() const
{
return (qMin(width(), height()) - 1)/2.0;
}
bool OrientationWidget::isInCircle(const QPointF &pos)
{
QPointF centerCircle = this->centerCircle();
qreal sizeX=pos.x() - centerCircle.x(), sizeY=pos.y() - centerCircle.y();
double distance = sqrt(sizeX*sizeX + sizeY*sizeY);
return distance <= radiusCircle();
}
void OrientationWidget::moveCursor(const QPointF &pos)
{
QPointF centerCircle = this->centerCircle();
qreal sizeX=pos.x() - centerCircle.x(), sizeY=pos.y() - centerCircle.y();
double angle;
if(sizeX != 0) {
angle = atan2(abs(sizeY), abs(sizeX)) * 57.29577951;// rad2deg
} else {
angle = 0;
}
if(sizeX == 0 && sizeY == 0) {
return;
} else if(sizeX < 0 && sizeY <= 0) {
_value = 0 + angle;
} else if(sizeX == 0 && sizeY < 0) {
_value = 90;
} else if(sizeX > 0 && sizeY < 0) {
_value = 180 - angle;
} else if(sizeX == 0 && sizeY > 0) {
_value = 270;
} else if(sizeX < 0 && sizeY > 0) {
_value = 360 - angle;
} else if(sizeX > 0 && sizeY >= 0) {
_value = 180 + angle;
}
quint8 value = degree2byte();
setValue(value);
emit valueEdited(value);
}