1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Jochen Becher
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include <QGraphicsItem>
29 
30 QT_BEGIN_NAMESPACE
31 class QGraphicsPathItem;
32 class QPainterPath;
33 QT_END_NAMESPACE
34 
35 namespace qmt {
36 
37 class Style;
38 
39 class ArrowItem : public QGraphicsItem
40 {
41     class GraphicsHeadItem;
42     class GraphicsShaftItem;
43 
44 public:
45     enum Shaft {
46         ShaftSolid,
47         ShaftDashed,
48         ShaftDot,
49         ShaftDashDot,
50         ShaftDashDotDot
51     };
52 
53     enum Head {
54         HeadNone,
55         HeadCustom,
56         HeadOpen,
57         HeadTriangle,
58         HeadFilledTriangle,
59         HeadDiamond,
60         HeadFilledDiamond,
61         HeadDiamondFilledTriangle,
62         HeadFilledDiamondFilledTriangle
63     };
64 
65     explicit ArrowItem(QGraphicsItem *parent = nullptr);
66     explicit ArrowItem(const ArrowItem &rhs, QGraphicsItem *parent = nullptr);
67     ~ArrowItem() override;
68 
69     void setShaft(Shaft shaft);
70     void setArrowSize(double arrowSize);
71     void setDiamondSize(double diamondSize);
72     void setStartHead(Head head);
73     void setStartHead(QGraphicsItem *startHeadItem);
74     void setEndHead(Head head);
75     void setEndHead(QGraphicsItem *endHeadItem);
76     void setPoints(const QList<QPointF> &points);
77 
78     QRectF boundingRect() const override;
79     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
80     QPainterPath shape() const override;
81 
82     QPointF calcPointAtPercent(double percentage) const;
83     QLineF firstLineSegment() const;
84     QLineF lastLineSegment() const;
85     double startHeadLength() const;
86     double endHeadLength() const;
87 
88     void update(const Style *style);
89 
90 private:
91     void updateShaft(const Style *style);
92     void deleteHead(QGraphicsItem **headItem);
93     void updateHead(QGraphicsItem **headItem, Head head, const Style *style);
94     void updateHeadGeometry(QGraphicsItem *headItem, const QPointF &pos,
95                             const QPointF &otherPos);
96     void updateGeometry();
97     double calcHeadLength(QGraphicsItem *headItem) const;
98 
99     Shaft m_shaft = ShaftSolid;
100     GraphicsShaftItem *m_shaftItem = nullptr;
101     double m_arrowSize = 10.0;
102     double m_diamondSize = 15.0;
103     Head m_startHead = HeadNone;
104     QGraphicsItem *m_startHeadItem = nullptr;
105     Head m_endHead = HeadNone;
106     QGraphicsItem *m_endHeadItem = nullptr;
107     QList<QPointF> m_points;
108 };
109 
110 } // namespace qmt
111