OGS
QArrow.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "QArrow.h"
5
6#include <math.h>
7
8#include <QPainter>
9
19QArrow::QArrow(qreal l, qreal a, qreal hl, qreal hw, QPen& pen,
20 QGraphicsItem* parent)
21 : QGraphicsItem(parent)
22{
23 _arrowLength = l;
24 _arrowAngle = a;
25 _headLength = hl;
26 _headWidth = hw;
27 _arrowPen = pen;
28}
29
36QArrow::QArrow(qreal l, qreal a, QPen& pen, QGraphicsItem* parent)
37 : QGraphicsItem(parent)
38{
39 _arrowLength = l;
40 _arrowAngle = a;
41 _headLength = 8; // default headlength
42 _headWidth = 5; // default headwidth
43 _arrowPen = pen;
44}
45
46QArrow::~QArrow() = default;
47
48double QArrow::calcCos(double angle)
49{
50 return cos(angle * PI / 180);
51}
52
53double QArrow::calcSin(double angle)
54{
55 return sin(angle * PI / 180);
56}
57
60{
61 double deltaX = cos(_arrowAngle) * _arrowLength;
62 double deltaY = sin(_arrowAngle) * _arrowLength;
63
64 return QRectF(0, 0, deltaX, deltaY);
65}
66
69{
70 return _arrowLength;
71}
72
75{
76 return _arrowAngle;
77}
78
84void QArrow::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
85 QWidget* widget)
86{
87 Q_UNUSED(option)
88 Q_UNUSED(widget)
89
90 double ddeltaX = calcCos(_arrowAngle) * _arrowLength;
91 double ddeltaY = calcSin(_arrowAngle) * _arrowLength;
92 double theta = atan(ddeltaY / ddeltaX);
93 double theta2 = (ddeltaX < 0.0) ? (theta + PI) : theta;
94 int lengthdeltaX = -static_cast<int>(cos(theta2) * _headLength);
95 int lengthdeltaY = -static_cast<int>(sin(theta2) * _headLength);
96 auto widthdeltaX = static_cast<int>(sin(theta2) * _headWidth);
97 auto widthdeltaY = static_cast<int>(cos(theta2) * _headWidth);
98 auto deltaX = static_cast<int>(ddeltaX);
99 auto deltaY = static_cast<int>(ddeltaY);
100 painter->setPen(_arrowPen);
101 painter->drawLine(0, 0, deltaX, deltaY);
102 painter->drawLine(deltaX,
103 deltaY,
104 deltaX + lengthdeltaX + widthdeltaX,
105 deltaY + lengthdeltaY - widthdeltaY);
106 painter->drawLine(deltaX,
107 deltaY,
108 deltaX + lengthdeltaX - widthdeltaX,
109 deltaY + lengthdeltaY + widthdeltaY);
110}
111
113void QArrow::setAngle(qreal a)
114{
115 _arrowAngle = a;
116}
117
119void QArrow::setLength(qreal l)
120{
121 _arrowLength = l;
122}
const double PI
Definition QArrow.h:9
~QArrow() override
QPen _arrowPen
Definition QArrow.h:38
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
Definition QArrow.cpp:84
qreal _arrowAngle
Definition QArrow.h:35
void setLength(qreal l)
Changes the length of the arrow.
Definition QArrow.cpp:119
QArrow(qreal l, qreal a, qreal hl, qreal hw, QPen &pen, QGraphicsItem *parent=nullptr)
Definition QArrow.cpp:19
QRectF boundingRect() const override
The bounding box of the arrow.
Definition QArrow.cpp:59
qreal _headLength
Definition QArrow.h:36
double getLength()
Returns the length of the arrow.
Definition QArrow.cpp:68
double calcSin(double angle)
Definition QArrow.cpp:53
qreal _headWidth
Definition QArrow.h:37
double calcCos(double angle)
Definition QArrow.cpp:48
double getAngle()
Returns the orientation of the arrow.
Definition QArrow.cpp:74
void setAngle(qreal a)
Changes orientation of the arrow.
Definition QArrow.cpp:113
qreal _arrowLength
Definition QArrow.h:34