Repository navigation
Expand file tree
/
Copy pathArrow2D.cpp
More file actions
273 lines (247 loc) · 6.95 KB
/
Copy pathArrow2D.cpp
File metadata and controls
273 lines (247 loc) · 6.95 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "wplot/Arrow2D.h"
#include "wplot/Plot2D.h"
#include <QTransform>
#include <cmath>
namespace WPlot {
///////////////////////////////////////////////////////////////////////////////
// PUBLIC SECTION //
///////////////////////////////////////////////////////////////////////////////
Arrow2D::Arrow2D() :
Item2D(),
m_finalPoint(),
m_lineWidth(1.0),
m_arrowLineWidth(1.0),
m_arrowWidth(1.0),
m_arrowLength(1.0),
m_lineColor(Qt::black),
m_arrowLineColor(Qt::black),
m_arrowFillColor(Qt::white),
m_lineStyle(Qt::SolidLine),
m_arrowLineStyle(Qt::SolidLine) {
m_linePen.setColor(m_lineColor);
m_linePen.setWidthF(m_lineWidth);
m_linePen.setStyle(m_lineStyle);
m_arrowLinePen.setColor(m_arrowLineColor);
m_arrowLinePen.setWidthF(m_arrowWidth);
m_arrowLinePen.setStyle(m_arrowLineStyle);
m_arrowBrush.setColor(m_arrowFillColor);
m_arrowBrush.setStyle(Qt::SolidPattern);
}
/**
* Set the final point of the arrow. This is the coordinate where arrow ends.
* The initial point is defined by setPivot() in base class.
*
* @param[in] point Final point coordinates.
*/
void Arrow2D::setFinalPoint(const QPointF &point) {
m_finalPoint = point;
}
/**
* Set the final point of the arrow. This is the coordinate where arrow ends.
* The initial point is defined by setPivot() in base class.
*
* @param[in] x Final point X coordinate.
* @param[in] y Final point Y coordinate.
*/
void Arrow2D::setFinalPoint(const double &x, const double &y) {
m_finalPoint.setX(x);
m_finalPoint.setY(y);
}
/**
* Set line coordinates.
*
* @param[in] initialPoint Initial point coordinates.
* @param[in] finalPoint Final point coordinates.
*/
void Arrow2D::setLine(const QPointF &initialPoint, const QPointF &finalPoint) {
setPivot(initialPoint);
setFinalPoint(finalPoint);
}
/**
* Set line coordinates.
*
* @param[in] x1 Initial point X coordinate.
* @param[in] y1 Initial point Y coordinate.
* @param[in] x2 Final point X coordinate.
* @param[in] y2 Final point Y coordinate.
*/
void Arrow2D::setLine(const double &x1, const double &y1, const double &x2, const double &y2) {
setPivot(x1, y1);
setFinalPoint(x2, y2);
}
/**
* Set the color of arrow line.
*
* @const color Color.
*/
void Arrow2D::setLineColor(const QColor &color) {
m_lineColor = color;
m_linePen.setColor(color);
}
/**
* Set the width of the arrow line.
*
* @param[in] width width.
*/
void Arrow2D::setLineWidth(const double &width) {
m_lineWidth = width;
m_linePen.setWidthF(width);
}
/**
* Set the style of the arrow line.
*
* @param[in] style Style.
*/
void Arrow2D::setLineStyle(const Qt::PenStyle &style) {
m_lineStyle = style;
m_linePen.setStyle(style);
}
/**
* Set the color of the internal part of the arrow.
*
* @param[in] color Color.
*/
void Arrow2D::setArrowFillColor(const QColor &color) {
m_arrowFillColor = color;
m_arrowBrush.setColor(color);
}
/**
* Set the color of the triangle arrow line.
*
* @param[in] color Color.
*/
void Arrow2D::setArrowLineColor(const QColor &color) {
m_arrowLineColor = color;
m_arrowLinePen.setColor(color);
}
/**
* Set the width of the triangle arrow line.
*
* @param[in] width Width.
*/
void Arrow2D::setArrowLineWidth(const double &width) {
m_arrowLineWidth = width;
m_arrowLinePen.setWidthF(width);
}
/**
* Set the style of the triangle arrow line.
*
* @param[in] style Style.
*/
void Arrow2D::setArrowLineStyle(const Qt::PenStyle &style) {
m_arrowLineStyle = style;
m_arrowLinePen.setStyle(style);
}
/**
* Set the length of the arrow triangle.
*
* @param[in] length Length.
*/
void Arrow2D::setArrowLength(const double &length) {
m_arrowLength = length;
}
/**
* Set the width of the arrow triangle.
*
* @param[in] width Width.
*/
void Arrow2D::setArrowWidth(const double &width) {
m_arrowWidth = width;
}
/**
* Set the dimension of the arrow at the same time.
*
* @param[in] width Width.
* @param[in] length Length.
*/
void Arrow2D::setArrowDimensions(const double &width, const double &length) {
m_arrowLength = length;
m_arrowWidth = width;
}
/**
* Retrieve the length of the arrow.
*
* @return Length.
*/
double Arrow2D::getArrowLength() const {
return m_arrowLength;
}
/**
* Retrieve the width of the arrow.
*
* @return Width.
*/
double Arrow2D::getArrowWidth() const {
return m_arrowWidth;
}
///////////////////////////////////////////////////////////////////////////////
// VIRTUAL PUBLIC SECTION //
///////////////////////////////////////////////////////////////////////////////
/**
* Set the item color.
*
* It's an implementation of pure virtual method of base class used when in the
* Item2D the color is needed.
*
* @param[in] color Color.
*/
void Arrow2D::setColor(const QColor &color) {
setArrowFillColor(color);
setArrowLineColor(color);
setLineColor(color);
}
/**
* Draw the arrow.
*
* @param[inout] plot Plot where arrow must be plotted.
*/
void Arrow2D::draw(Plot2D *plot) {
QPainter painter(plot);
plot->initializePainter(painter);
limitPainterInPaddingArea(painter, plot);
drawLine(painter, plot);
drawArrow(painter, plot);
}
///////////////////////////////////////////////////////////////////////////////
// PRIVATE SECTION //
///////////////////////////////////////////////////////////////////////////////
/**
* Draw the arrow line.
*
* @param[in] painter Painter that must be used.
* @param[in] plot Plot where arrow must be plotted.
*/
void Arrow2D::drawLine(QPainter &painter, Plot2D *plot) {
const auto initialPoint = getScaledPosition(m_pivot, plot);
const auto finalPoint = getScaledPosition(m_finalPoint, plot);
painter.setPen(m_linePen);
painter.drawLine(initialPoint, finalPoint);
}
/**
* Draw the arrow triangle.
*
* @param[in] painter Painter that must be used.
* @param[in] plot Plot where arrow must be plotted.
*/
void Arrow2D::drawArrow(QPainter &painter, Plot2D *plot){
painter.setPen(m_arrowLinePen);
painter.setBrush(m_arrowBrush);
const auto initialPoint = getScaledPosition(m_pivot, plot);
const auto finalPoint = getScaledPosition(m_finalPoint, plot);
const double angle = atan2(finalPoint.y() - initialPoint.y(), finalPoint.x() - initialPoint.x());
const double arrowLength = getScaledDimensionX(m_arrowLength, plot);
const double arrowWidth = getScaledDimensionX(m_arrowWidth, plot);
const QPointF p1(-m_arrowLength + finalPoint.x(), -m_arrowWidth * 0.5 + finalPoint.y());
const QPointF p2(-m_arrowLength + finalPoint.x(), m_arrowWidth * 0.5 + finalPoint.y());
QPolygonF arrow;
arrow << finalPoint << p1 << p2;
QTransform transform;
transform.rotateRadians(angle);
const QPolygonF rotatedArrow = transform.map(arrow);
const auto deltaPosition = arrow.front() - rotatedArrow.front();
QTransform translateTransform;
translateTransform.translate(deltaPosition.x(), deltaPosition.y());
const QPolygonF rotoTranslatedArros = translateTransform.map(rotatedArrow);
painter.drawPolygon(rotoTranslatedArros);
}
} // namespace WPlot