OGS
QArrow Class Reference

Detailed Description

An arrow as a QGraphicsObject.

Definition at line 14 of file QArrow.h.

#include <QArrow.h>

Inheritance diagram for QArrow:
[legend]
Collaboration diagram for QArrow:
[legend]

Public Member Functions

 QArrow (qreal l, qreal a, qreal hl, qreal hw, QPen &pen, QGraphicsItem *parent=nullptr)
 QArrow (qreal l, qreal a, QPen &pen, QGraphicsItem *parent=nullptr)
 ~QArrow () override
double getLength ()
 Returns the length of the arrow.
double getAngle ()
 Returns the orientation of the arrow.
void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
QRectF boundingRect () const override
 The bounding box of the arrow.
void setAngle (qreal a)
 Changes orientation of the arrow.
void setLength (qreal l)
 Changes the length of the arrow.

Private Member Functions

double calcCos (double angle)
double calcSin (double angle)

Private Attributes

qreal _arrowLength
qreal _arrowAngle
qreal _headLength
qreal _headWidth
QPen _arrowPen

Constructor & Destructor Documentation

◆ QArrow() [1/2]

QArrow::QArrow ( qreal l,
qreal a,
qreal hl,
qreal hw,
QPen & pen,
QGraphicsItem * parent = nullptr )

Creates an arrow as a QGraphicItem.

Parameters
lLength of the arrow
aOrientation of the arrow
hlLength of the arrow head
hwWidth of the arrow head
penThe pen for drawing the arrow
parentThe parent QGraphicsItem.

Definition at line 19 of file QArrow.cpp.

21 : QGraphicsItem(parent)
22{
23 _arrowLength = l;
24 _arrowAngle = a;
25 _headLength = hl;
26 _headWidth = hw;
27 _arrowPen = pen;
28}
QPen _arrowPen
Definition QArrow.h:38
qreal _arrowAngle
Definition QArrow.h:35
qreal _headLength
Definition QArrow.h:36
qreal _headWidth
Definition QArrow.h:37
qreal _arrowLength
Definition QArrow.h:34

References _arrowAngle, _arrowLength, _arrowPen, _headLength, and _headWidth.

◆ QArrow() [2/2]

QArrow::QArrow ( qreal l,
qreal a,
QPen & pen,
QGraphicsItem * parent = nullptr )

Creates an arrow as a QGraphicItem. Length and width of the arrow head are given by default values.

Parameters
lLength of the arrow
aOrientation of the arrow
penThe pen for drawing the arrow
parentThe parent QGraphicsItem.

Definition at line 36 of file QArrow.cpp.

37 : QGraphicsItem(parent)
38{
39 _arrowLength = l;
40 _arrowAngle = a;
41 _headLength = 8; // default headlength
42 _headWidth = 5; // default headwidth
43 _arrowPen = pen;
44}

References _arrowAngle, _arrowLength, _arrowPen, _headLength, and _headWidth.

◆ ~QArrow()

QArrow::~QArrow ( )
overridedefault

Member Function Documentation

◆ boundingRect()

QRectF QArrow::boundingRect ( ) const
override

The bounding box of the arrow.

Definition at line 59 of file QArrow.cpp.

60{
61 double deltaX = cos(_arrowAngle) * _arrowLength;
62 double deltaY = sin(_arrowAngle) * _arrowLength;
63
64 return QRectF(0, 0, deltaX, deltaY);
65}

References _arrowAngle, and _arrowLength.

◆ calcCos()

double QArrow::calcCos ( double angle)
private

Definition at line 48 of file QArrow.cpp.

49{
50 return cos(angle * PI / 180);
51}
const double PI
Definition QArrow.h:9

References PI.

Referenced by paint().

◆ calcSin()

double QArrow::calcSin ( double angle)
private

Definition at line 53 of file QArrow.cpp.

54{
55 return sin(angle * PI / 180);
56}

References PI.

Referenced by paint().

◆ getAngle()

double QArrow::getAngle ( )

Returns the orientation of the arrow.

Definition at line 74 of file QArrow.cpp.

75{
76 return _arrowAngle;
77}

References _arrowAngle.

◆ getLength()

double QArrow::getLength ( )

Returns the length of the arrow.

Definition at line 68 of file QArrow.cpp.

69{
70 return _arrowLength;
71}

References _arrowLength.

◆ paint()

void QArrow::paint ( QPainter * painter,
const QStyleOptionGraphicsItem * option,
QWidget * widget )
override

Overloaded paint-method from QGraphicsItem. Basically it draws a line with an arrowhead consisting of two short lines at the end

Definition at line 84 of file QArrow.cpp.

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}
double calcSin(double angle)
Definition QArrow.cpp:53
double calcCos(double angle)
Definition QArrow.cpp:48

References _arrowAngle, _arrowLength, _arrowPen, _headLength, _headWidth, calcCos(), calcSin(), and PI.

◆ setAngle()

void QArrow::setAngle ( qreal a)

Changes orientation of the arrow.

Definition at line 113 of file QArrow.cpp.

114{
115 _arrowAngle = a;
116}

References _arrowAngle.

◆ setLength()

void QArrow::setLength ( qreal l)

Changes the length of the arrow.

Definition at line 119 of file QArrow.cpp.

120{
121 _arrowLength = l;
122}

References _arrowLength.

Member Data Documentation

◆ _arrowAngle

qreal QArrow::_arrowAngle
private

Definition at line 35 of file QArrow.h.

Referenced by QArrow(), QArrow(), boundingRect(), getAngle(), paint(), and setAngle().

◆ _arrowLength

qreal QArrow::_arrowLength
private

Definition at line 34 of file QArrow.h.

Referenced by QArrow(), QArrow(), boundingRect(), getLength(), paint(), and setLength().

◆ _arrowPen

QPen QArrow::_arrowPen
private

Definition at line 38 of file QArrow.h.

Referenced by QArrow(), QArrow(), and paint().

◆ _headLength

qreal QArrow::_headLength
private

Definition at line 36 of file QArrow.h.

Referenced by QArrow(), QArrow(), and paint().

◆ _headWidth

qreal QArrow::_headWidth
private

Definition at line 37 of file QArrow.h.

Referenced by QArrow(), QArrow(), and paint().


The documentation for this class was generated from the following files: