1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | |
52 | #include "arrow.h" |
53 | #include "diagramitem.h" |
54 | |
55 | #include <QPainter> |
56 | #include <QPen> |
57 | #include <QtMath> |
58 | |
59 | //! [0] |
60 | Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, QGraphicsItem *parent) |
61 | : QGraphicsLineItem(parent), myStartItem(startItem), myEndItem(endItem) |
62 | { |
63 | setFlag(flag: QGraphicsItem::ItemIsSelectable, enabled: true); |
64 | setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); |
65 | } |
66 | //! [0] |
67 | |
68 | //! [1] |
69 | QRectF Arrow::boundingRect() const |
70 | { |
71 | qreal = (pen().width() + 20) / 2.0; |
72 | |
73 | return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(), |
74 | line().p2().y() - line().p1().y())) |
75 | .normalized() |
76 | .adjusted(xp1: -extra, yp1: -extra, xp2: extra, yp2: extra); |
77 | } |
78 | //! [1] |
79 | |
80 | //! [2] |
81 | QPainterPath Arrow::shape() const |
82 | { |
83 | QPainterPath path = QGraphicsLineItem::shape(); |
84 | path.addPolygon(polygon: arrowHead); |
85 | return path; |
86 | } |
87 | //! [2] |
88 | |
89 | //! [3] |
90 | void Arrow::updatePosition() |
91 | { |
92 | QLineF line(mapFromItem(item: myStartItem, ax: 0, ay: 0), mapFromItem(item: myEndItem, ax: 0, ay: 0)); |
93 | setLine(line); |
94 | } |
95 | //! [3] |
96 | |
97 | //! [4] |
98 | void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, |
99 | QWidget *) |
100 | { |
101 | if (myStartItem->collidesWithItem(other: myEndItem)) |
102 | return; |
103 | |
104 | QPen myPen = pen(); |
105 | myPen.setColor(myColor); |
106 | qreal arrowSize = 20; |
107 | painter->setPen(myPen); |
108 | painter->setBrush(myColor); |
109 | //! [4] //! [5] |
110 | |
111 | QLineF centerLine(myStartItem->pos(), myEndItem->pos()); |
112 | QPolygonF endPolygon = myEndItem->polygon(); |
113 | QPointF p1 = endPolygon.first() + myEndItem->pos(); |
114 | QPointF intersectPoint; |
115 | for (int i = 1; i < endPolygon.count(); ++i) { |
116 | QPointF p2 = endPolygon.at(i) + myEndItem->pos(); |
117 | QLineF polyLine = QLineF(p1, p2); |
118 | QLineF::IntersectionType intersectionType = |
119 | polyLine.intersects(l: centerLine, intersectionPoint: &intersectPoint); |
120 | if (intersectionType == QLineF::BoundedIntersection) |
121 | break; |
122 | p1 = p2; |
123 | } |
124 | |
125 | setLine(QLineF(intersectPoint, myStartItem->pos())); |
126 | //! [5] //! [6] |
127 | |
128 | double angle = std::atan2(y: -line().dy(), x: line().dx()); |
129 | |
130 | QPointF arrowP1 = line().p1() + QPointF(sin(x: angle + M_PI / 3) * arrowSize, |
131 | cos(x: angle + M_PI / 3) * arrowSize); |
132 | QPointF arrowP2 = line().p1() + QPointF(sin(x: angle + M_PI - M_PI / 3) * arrowSize, |
133 | cos(x: angle + M_PI - M_PI / 3) * arrowSize); |
134 | |
135 | arrowHead.clear(); |
136 | arrowHead << line().p1() << arrowP1 << arrowP2; |
137 | //! [6] //! [7] |
138 | painter->drawLine(l: line()); |
139 | painter->drawPolygon(polygon: arrowHead); |
140 | if (isSelected()) { |
141 | painter->setPen(QPen(myColor, 1, Qt::DashLine)); |
142 | QLineF myLine = line(); |
143 | myLine.translate(adx: 0, ady: 4.0); |
144 | painter->drawLine(l: myLine); |
145 | myLine.translate(adx: 0,ady: -8.0); |
146 | painter->drawLine(l: myLine); |
147 | } |
148 | } |
149 | //! [7] |
150 | |