-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharrowitem.cpp
More file actions
107 lines (93 loc) · 2.39 KB
/
Copy patharrowitem.cpp
File metadata and controls
107 lines (93 loc) · 2.39 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
/**
* Copyright (c) 2023-2024, Pedro López-Cabanillas
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "arrowitem.h"
#include <QTextStream>
#include <cmath>
ArrowItem::ArrowItem(qreal x, qreal y, QGraphicsItem *parent)
: QGraphicsPathItem(parent)
, m_origin{x, y}
, m_length{1}
, m_angle{0}
, m_tip{x + m_length, y}
{
setFlag(QGraphicsItem::ItemIsSelectable);
setFlag(QGraphicsItem::ItemIsMovable);
setTransformOriginPoint(m_origin);
}
ArrowItem::ArrowItem(QPointF origin, QGraphicsItem *parent)
: ArrowItem(origin.rx(), origin.ry(), parent)
{}
ArrowItem::ArrowItem(qreal x, qreal y, qreal length, qreal angle, QGraphicsItem *parent)
: ArrowItem(x, y, parent)
{
m_length = length;
m_angle = angle;
m_tip = QPointF{x + m_length, y};
setRotation(m_angle);
updatePath();
}
ArrowItem::ArrowItem(QPointF origin, qreal length, qreal angle, QGraphicsItem *parent)
: ArrowItem(origin.rx(), origin.ry(), parent)
{
m_length = length;
m_angle = angle;
m_tip = QPointF{origin.rx() + m_length, origin.ry()};
setRotation(m_angle);
updatePath();
}
ArrowItem::ArrowItem(QPointF origin, QPointF tip, QGraphicsItem *parent)
: ArrowItem(origin.rx(), origin.ry(), parent)
{
QLineF line(origin, tip);
m_length = line.length();
m_angle = std::remainder(360.0 - line.angle(), 360.0);
line.setAngle(0);
m_tip = line.p2();
setRotation(m_angle);
updatePath();
}
void ArrowItem::updatePath()
{
const qreal headsize = m_length / 10.0;
QPainterPath path;
path.moveTo(m_origin);
path.lineTo(m_tip);
path.moveTo(m_tip.rx() - headsize, m_tip.ry() - headsize / 2);
path.lineTo(m_tip);
path.lineTo(m_tip.rx() - headsize, m_tip.ry() + headsize / 2);
setPath(path);
}
QPointF ArrowItem::origin() const
{
return m_origin;
}
void ArrowItem::setOrigin(QPointF newOrigin)
{
m_origin = newOrigin;
}
qreal ArrowItem::length() const
{
return m_length;
}
void ArrowItem::setLength(qreal newLength)
{
m_length = newLength;
}
qreal ArrowItem::angle() const
{
return m_angle;
}
void ArrowItem::setAngle(qreal newAngle)
{
m_angle = newAngle;
}
QString ArrowItem::toString() const
{
QString buffer;
QTextStream stream(&buffer);
stream << "origin x: " << origin().rx() << " y: " << origin().ry() << "\nlength: " << length()
<< "\norientation: " << angle();
return buffer;
}