| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include <QtGui> | 
|---|
| 43 |  | 
|---|
| 44 | #include "arrow.h" | 
|---|
| 45 | #include <math.h> | 
|---|
| 46 |  | 
|---|
| 47 | const qreal Pi = 3.14; | 
|---|
| 48 |  | 
|---|
| 49 | //! [0] | 
|---|
| 50 | Arrow::Arrow(DiagramItem *startItem, DiagramItem *endItem, | 
|---|
| 51 | QGraphicsItem *parent, QGraphicsScene *scene) | 
|---|
| 52 | : QGraphicsLineItem(parent, scene) | 
|---|
| 53 | { | 
|---|
| 54 | myStartItem = startItem; | 
|---|
| 55 | myEndItem = endItem; | 
|---|
| 56 | setFlag(QGraphicsItem::ItemIsSelectable, true); | 
|---|
| 57 | myColor = Qt::black; | 
|---|
| 58 | setPen(QPen(myColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); | 
|---|
| 59 | } | 
|---|
| 60 | //! [0] | 
|---|
| 61 |  | 
|---|
| 62 | //! [1] | 
|---|
| 63 | QRectF Arrow::boundingRect() const | 
|---|
| 64 | { | 
|---|
| 65 | qreal extra = (pen().width() + 20) / 2.0; | 
|---|
| 66 |  | 
|---|
| 67 | return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(), | 
|---|
| 68 | line().p2().y() - line().p1().y())) | 
|---|
| 69 | .normalized() | 
|---|
| 70 | .adjusted(-extra, -extra, extra, extra); | 
|---|
| 71 | } | 
|---|
| 72 | //! [1] | 
|---|
| 73 |  | 
|---|
| 74 | //! [2] | 
|---|
| 75 | QPainterPath Arrow::shape() const | 
|---|
| 76 | { | 
|---|
| 77 | QPainterPath path = QGraphicsLineItem::shape(); | 
|---|
| 78 | path.addPolygon(arrowHead); | 
|---|
| 79 | return path; | 
|---|
| 80 | } | 
|---|
| 81 | //! [2] | 
|---|
| 82 |  | 
|---|
| 83 | //! [3] | 
|---|
| 84 | void Arrow::updatePosition() | 
|---|
| 85 | { | 
|---|
| 86 | QLineF line(mapFromItem(myStartItem, 0, 0), mapFromItem(myEndItem, 0, 0)); | 
|---|
| 87 | setLine(line); | 
|---|
| 88 | } | 
|---|
| 89 | //! [3] | 
|---|
| 90 |  | 
|---|
| 91 | //! [4] | 
|---|
| 92 | void Arrow::paint(QPainter *painter, const QStyleOptionGraphicsItem *, | 
|---|
| 93 | QWidget *) | 
|---|
| 94 | { | 
|---|
| 95 | if (myStartItem->collidesWithItem(myEndItem)) | 
|---|
| 96 | return; | 
|---|
| 97 |  | 
|---|
| 98 | QPen myPen = pen(); | 
|---|
| 99 | myPen.setColor(myColor); | 
|---|
| 100 | qreal arrowSize = 20; | 
|---|
| 101 | painter->setPen(myPen); | 
|---|
| 102 | painter->setBrush(myColor); | 
|---|
| 103 | //! [4] //! [5] | 
|---|
| 104 |  | 
|---|
| 105 | QLineF centerLine(myStartItem->pos(), myEndItem->pos()); | 
|---|
| 106 | QPolygonF endPolygon = myEndItem->polygon(); | 
|---|
| 107 | QPointF p1 = endPolygon.first() + myEndItem->pos(); | 
|---|
| 108 | QPointF p2; | 
|---|
| 109 | QPointF intersectPoint; | 
|---|
| 110 | QLineF polyLine; | 
|---|
| 111 | for (int i = 1; i < endPolygon.count(); ++i) { | 
|---|
| 112 | p2 = endPolygon.at(i) + myEndItem->pos(); | 
|---|
| 113 | polyLine = QLineF(p1, p2); | 
|---|
| 114 | QLineF::IntersectType intersectType = | 
|---|
| 115 | polyLine.intersect(centerLine, &intersectPoint); | 
|---|
| 116 | if (intersectType == QLineF::BoundedIntersection) | 
|---|
| 117 | break; | 
|---|
| 118 | p1 = p2; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | setLine(QLineF(intersectPoint, myStartItem->pos())); | 
|---|
| 122 | //! [5] //! [6] | 
|---|
| 123 |  | 
|---|
| 124 | double angle = ::acos(line().dx() / line().length()); | 
|---|
| 125 | if (line().dy() >= 0) | 
|---|
| 126 | angle = (Pi * 2) - angle; | 
|---|
| 127 |  | 
|---|
| 128 | QPointF arrowP1 = line().p1() + QPointF(sin(angle + Pi / 3) * arrowSize, | 
|---|
| 129 | cos(angle + Pi / 3) * arrowSize); | 
|---|
| 130 | QPointF arrowP2 = line().p1() + QPointF(sin(angle + Pi - Pi / 3) * arrowSize, | 
|---|
| 131 | cos(angle + Pi - Pi / 3) * arrowSize); | 
|---|
| 132 |  | 
|---|
| 133 | arrowHead.clear(); | 
|---|
| 134 | arrowHead << line().p1() << arrowP1 << arrowP2; | 
|---|
| 135 | //! [6] //! [7] | 
|---|
| 136 | painter->drawLine(line()); | 
|---|
| 137 | painter->drawPolygon(arrowHead); | 
|---|
| 138 | if (isSelected()) { | 
|---|
| 139 | painter->setPen(QPen(myColor, 1, Qt::DashLine)); | 
|---|
| 140 | QLineF myLine = line(); | 
|---|
| 141 | myLine.translate(0, 4.0); | 
|---|
| 142 | painter->drawLine(myLine); | 
|---|
| 143 | myLine.translate(0,-8.0); | 
|---|
| 144 | painter->drawLine(myLine); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | //! [7] | 
|---|