OGS
QArrow.cpp
Go to the documentation of this file.
1
15#include "QArrow.h"
16
17#include <math.h>
18
19#include <QPainter>
20
30QArrow::QArrow(qreal l, qreal a, qreal hl, qreal hw, QPen& pen,
31 QGraphicsItem* parent)
32 : QGraphicsItem(parent)
33{
34 _arrowLength = l;
35 _arrowAngle = a;
36 _headLength = hl;
37 _headWidth = hw;
38 _arrowPen = pen;
39}
40
47QArrow::QArrow(qreal l, qreal a, QPen& pen, QGraphicsItem* parent)
48 : QGraphicsItem(parent)
49{
50 _arrowLength = l;
51 _arrowAngle = a;
52 _headLength = 8; // default headlength
53 _headWidth = 5; // default headwidth
54 _arrowPen = pen;
55}
56
57QArrow::~QArrow() = default;
58
59double QArrow::calcCos(double angle)
60{
61 return cos(angle * PI / 180);
62}
63
64double QArrow::calcSin(double angle)
65{
66 return sin(angle * PI / 180);
67}
68
71{
72 double deltaX = cos(_arrowAngle) * _arrowLength;
73 double deltaY = sin(_arrowAngle) * _arrowLength;
74
75 return QRectF(0, 0, deltaX, deltaY);
76}
77
80{
81 return _arrowLength;
82}
83
86{
87 return _arrowAngle;
88}
89
95void QArrow::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
96 QWidget* widget)
97{
98 Q_UNUSED(option)
99 Q_UNUSED(widget)
100
101 double ddeltaX = calcCos(_arrowAngle) * _arrowLength;
102 double ddeltaY = calcSin(_arrowAngle) * _arrowLength;
103 double theta = atan(ddeltaY / ddeltaX);
104 double theta2 = (ddeltaX < 0.0) ? (theta + PI) : theta;
105 int lengthdeltaX = -static_cast<int>(cos(theta2) * _headLength);
106 int lengthdeltaY = -static_cast<int>(sin(theta2) * _headLength);
107 auto widthdeltaX = static_cast<int>(sin(theta2) * _headWidth);
108 auto widthdeltaY = static_cast<int>(cos(theta2) * _headWidth);
109 auto deltaX = static_cast<int>(ddeltaX);
110 auto deltaY = static_cast<int>(ddeltaY);
111 painter->setPen(_arrowPen);
112 painter->drawLine(0, 0, deltaX, deltaY);
113 painter->drawLine(deltaX,
114 deltaY,
115 deltaX + lengthdeltaX + widthdeltaX,
116 deltaY + lengthdeltaY - widthdeltaY);
117 painter->drawLine(deltaX,
118 deltaY,
119 deltaX + lengthdeltaX - widthdeltaX,
120 deltaY + lengthdeltaY + widthdeltaY);
121}
122
124void QArrow::setAngle(qreal a)
125{
126 _arrowAngle = a;
127}
128
130void QArrow::setLength(qreal l)
131{
132 _arrowLength = l;
133}
Definition of the QArrow class.
const double PI
Definition QArrow.h:20
~QArrow() override
QPen _arrowPen
Definition QArrow.h:49
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition QArrow.cpp:95
qreal _arrowAngle
Definition QArrow.h:46
void setLength(qreal l)
Changes the length of the arrow.
Definition QArrow.cpp:130
QArrow(qreal l, qreal a, qreal hl, qreal hw, QPen &pen, QGraphicsItem *parent=nullptr)
Definition QArrow.cpp:30
QRectF boundingRect() const override
The bounding box of the arrow.
Definition QArrow.cpp:70
qreal _headLength
Definition QArrow.h:47
double getLength()
Returns the length of the arrow.
Definition QArrow.cpp:79
double calcSin(double angle)
Definition QArrow.cpp:64
qreal _headWidth
Definition QArrow.h:48
double calcCos(double angle)
Definition QArrow.cpp:59
double getAngle()
Returns the orientation of the arrow.
Definition QArrow.cpp:85
void setAngle(qreal a)
Changes orientation of the arrow.
Definition QArrow.cpp:124
qreal _arrowLength
Definition QArrow.h:45