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