OGS
QArrow Class Reference

Detailed Description

An arrow as a QGraphicsObject.

Definition at line 25 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 30 of file QArrow.cpp.

32 : QGraphicsItem(parent)
33{
34 _arrowLength = l;
35 _arrowAngle = a;
36 _headLength = hl;
37 _headWidth = hw;
38 _arrowPen = pen;
39}
QPen _arrowPen
Definition QArrow.h:49
qreal _arrowAngle
Definition QArrow.h:46
qreal _headLength
Definition QArrow.h:47
qreal _headWidth
Definition QArrow.h:48
qreal _arrowLength
Definition QArrow.h:45

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 47 of file QArrow.cpp.

48 : QGraphicsItem(parent)
49{
50 _arrowLength = l;
51 _arrowAngle = a;
52 _headLength = 8; // default headlength
53 _headWidth = 5; // default headwidth
54 _arrowPen = pen;
55}

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 70 of file QArrow.cpp.

71{
72 double deltaX = cos(_arrowAngle) * _arrowLength;
73 double deltaY = sin(_arrowAngle) * _arrowLength;
74
75 return QRectF(0, 0, deltaX, deltaY);
76}

References _arrowAngle, and _arrowLength.

◆ calcCos()

double QArrow::calcCos ( double angle)
private

Definition at line 59 of file QArrow.cpp.

60{
61 return cos(angle * PI / 180);
62}
const double PI
Definition QArrow.h:20

References PI.

Referenced by paint().

◆ calcSin()

double QArrow::calcSin ( double angle)
private

Definition at line 64 of file QArrow.cpp.

65{
66 return sin(angle * PI / 180);
67}

References PI.

Referenced by paint().

◆ getAngle()

double QArrow::getAngle ( )

Returns the orientation of the arrow.

Definition at line 85 of file QArrow.cpp.

86{
87 return _arrowAngle;
88}

References _arrowAngle.

◆ getLength()

double QArrow::getLength ( )

Returns the length of the arrow.

Definition at line 79 of file QArrow.cpp.

80{
81 return _arrowLength;
82}

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 95 of file QArrow.cpp.

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

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

◆ setAngle()

void QArrow::setAngle ( qreal a)

Changes orientation of the arrow.

Definition at line 124 of file QArrow.cpp.

125{
126 _arrowAngle = a;
127}

References _arrowAngle.

◆ setLength()

void QArrow::setLength ( qreal l)

Changes the length of the arrow.

Definition at line 130 of file QArrow.cpp.

131{
132 _arrowLength = l;
133}

References _arrowLength.

Referenced by DiagramScene::update().

Member Data Documentation

◆ _arrowAngle

qreal QArrow::_arrowAngle
private

Definition at line 46 of file QArrow.h.

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

◆ _arrowLength

qreal QArrow::_arrowLength
private

Definition at line 45 of file QArrow.h.

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

◆ _arrowPen

QPen QArrow::_arrowPen
private

Definition at line 49 of file QArrow.h.

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

◆ _headLength

qreal QArrow::_headLength
private

Definition at line 47 of file QArrow.h.

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

◆ _headWidth

qreal QArrow::_headWidth
private

Definition at line 48 of file QArrow.h.

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


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