annotate examples/graphicsview/elasticnodes/edge.d @ 374:681af90e1d53

Removed debug writelns. Fixed build script.
author Max Samukha <maxter@spambox.com>
date Thu, 08 Jul 2010 22:21:56 +0300
parents 12f60887ed15
children
rev   line source
Eldar@371 1 /****************************************************************************
Eldar@371 2 **
Eldar@371 3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
Eldar@371 4 ** All rights reserved.
Eldar@371 5 ** Contact: Nokia Corporation (qt-info@nokia.com)
Eldar@371 6 **
Eldar@371 7 ** This file is part of the examples of the Qt Toolkit.
Eldar@371 8 **
Eldar@371 9 ** $QT_BEGIN_LICENSE:LGPL$
Eldar@371 10 ** Commercial Usage
Eldar@371 11 ** Licensees holding valid Qt Commercial licenses may use this file in
Eldar@371 12 ** accordance with the Qt Commercial License Agreement provided with the
Eldar@371 13 ** Software or, alternatively, in accordance with the terms contained in
Eldar@371 14 ** a written agreement between you and Nokia.
Eldar@371 15 **
Eldar@371 16 ** GNU Lesser General Public License Usage
Eldar@371 17 ** Alternatively, this file may be used under the terms of the GNU Lesser
Eldar@371 18 ** General Public License version 2.1 as published by the Free Software
Eldar@371 19 ** Foundation and appearing in the file LICENSE.LGPL included in the
Eldar@371 20 ** packaging of this file. Please review the following information to
Eldar@371 21 ** ensure the GNU Lesser General Public License version 2.1 requirements
Eldar@371 22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
Eldar@371 23 **
Eldar@371 24 ** In addition, as a special exception, Nokia gives you certain additional
Eldar@371 25 ** rights. These rights are described in the Nokia Qt LGPL Exception
Eldar@371 26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
Eldar@371 27 **
Eldar@371 28 ** GNU General Public License Usage
Eldar@371 29 ** Alternatively, this file may be used under the terms of the GNU
Eldar@371 30 ** General Public License version 3.0 as published by the Free Software
Eldar@371 31 ** Foundation and appearing in the file LICENSE.GPL included in the
Eldar@371 32 ** packaging of this file. Please review the following information to
Eldar@371 33 ** ensure the GNU General Public License version 3.0 requirements will be
Eldar@371 34 ** met: http://www.gnu.org/copyleft/gpl.html.
Eldar@371 35 **
Eldar@371 36 ** If you have questions regarding the use of this file, please contact
Eldar@371 37 ** Nokia at qt-info@nokia.com.
Eldar@371 38 ** $QT_END_LICENSE$
Eldar@371 39 **
Eldar@371 40 ****************************************************************************/
Eldar@371 41 module edge;
Eldar@371 42
Eldar@371 43 import std.math;
Eldar@371 44 import qt.gui.QGraphicsItem;
Eldar@371 45
Eldar@371 46 import node;
Eldar@371 47
Eldar@371 48 static const double Pi = 3.14159265358979323846264338327950288419717;
Eldar@371 49 static double TwoPi = 2.0 * Pi;
Eldar@371 50
Eldar@371 51 class Edge : QGraphicsItem
Eldar@371 52 {
Eldar@371 53 public:
Eldar@371 54 this(Node sourceNode, Node destNode)
Eldar@371 55 {
Eldar@371 56 arrowSize = 10;
Eldar@371 57 setAcceptedMouseButtons(0);
Eldar@371 58 source = sourceNode;
Eldar@371 59 dest = destNode;
Eldar@371 60 source.addEdge(this);
Eldar@371 61 dest.addEdge(this);
Eldar@371 62 adjust();
Eldar@371 63 }
Eldar@371 64
Eldar@371 65 Node sourceNode() const
Eldar@371 66 {
Eldar@371 67 return cast(Node)source;
Eldar@371 68 }
Eldar@371 69
Eldar@371 70 void setSourceNode(Node node)
Eldar@371 71 {
Eldar@371 72 source = node;
Eldar@371 73 adjust();
Eldar@371 74 }
Eldar@371 75
Eldar@371 76 Node destNode() const
Eldar@371 77 {
Eldar@371 78 return cast(Node)dest;
Eldar@371 79 }
Eldar@371 80
Eldar@371 81 void setDestNode(Node node)
Eldar@371 82 {
Eldar@371 83 dest = node;
Eldar@371 84 adjust();
Eldar@371 85 }
Eldar@371 86
Eldar@371 87 void adjust()
Eldar@371 88 {
Eldar@371 89 if (!source || !dest)
Eldar@371 90 return;
Eldar@371 91
Eldar@371 92 auto line = QLineF(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
Eldar@371 93 qreal length = line.length();
Eldar@371 94
Eldar@371 95 prepareGeometryChange();
Eldar@371 96
Eldar@371 97 if (length > 20.) {
Eldar@371 98 auto edgeOffset = QPointF((line.dx() * 10) / length, (line.dy() * 10) / length);
Eldar@371 99 sourcePoint = line.p1() + edgeOffset;
Eldar@371 100 destPoint = line.p2() - edgeOffset;
Eldar@371 101 } else {
Eldar@371 102 sourcePoint = destPoint = line.p1();
Eldar@371 103 }
Eldar@371 104 }
Eldar@371 105
Eldar@371 106 enum { Type = UserType + 2 };
Eldar@371 107 int type() const { return Type; }
Eldar@371 108
Eldar@371 109 protected:
Eldar@371 110 override QRectF boundingRect() const
Eldar@371 111 {
Eldar@371 112 if (!source || !dest)
Eldar@371 113 return QRectF();
Eldar@371 114
Eldar@371 115 qreal penWidth = 1;
Eldar@371 116 qreal extra = (penWidth + arrowSize) / 2.0;
Eldar@371 117
Eldar@371 118 return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
Eldar@371 119 destPoint.y() - sourcePoint.y()))
Eldar@371 120 .normalized()
Eldar@371 121 .adjusted(-extra, -extra, extra, extra);
Eldar@371 122 }
Eldar@371 123
Eldar@371 124 override void paint(QPainter painter, QStyleOptionGraphicsItem option, QWidget widget)
Eldar@371 125 {
Eldar@371 126 if (!source || !dest)
Eldar@371 127 return;
Eldar@371 128
Eldar@371 129 auto line = QLineF(sourcePoint, destPoint);
Eldar@371 130 if (qFuzzyCompare(line.length(), 0.))
Eldar@371 131 return;
Eldar@371 132
Eldar@371 133 // Draw the line itself
Eldar@371 134 painter.setPen(new QPen(new QBrush(Qt.black), 1, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin));
Eldar@371 135 painter.drawLine(line);
Eldar@371 136
Eldar@371 137 // Draw the arrows
Eldar@371 138 double angle = .acos(line.dx() / line.length());
Eldar@371 139 if (line.dy() >= 0)
Eldar@371 140 angle = TwoPi - angle;
Eldar@371 141
Eldar@371 142 QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
Eldar@371 143 cos(angle + Pi / 3) * arrowSize);
Eldar@371 144 QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
Eldar@371 145 cos(angle + Pi - Pi / 3) * arrowSize);
Eldar@371 146 QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
Eldar@371 147 cos(angle - Pi / 3) * arrowSize);
Eldar@371 148 QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
Eldar@371 149 cos(angle - Pi + Pi / 3) * arrowSize);
Eldar@371 150
Eldar@371 151 painter.setBrush(new QBrush(Qt.black));
Eldar@371 152 auto p1 = new QPolygonF();
Eldar@371 153 p1.append(line.p1());
Eldar@371 154 p1.append(sourceArrowP1);
Eldar@371 155 p1.append(sourceArrowP2);
Eldar@371 156 painter.drawPolygon(p1);
Eldar@371 157 auto p2 = new QPolygonF();
Eldar@371 158 p2.append(line.p2());
Eldar@371 159 p2.append(destArrowP1);
Eldar@371 160 p2.append(destArrowP2);
Eldar@371 161 painter.drawPolygon(p2);
Eldar@371 162 }
Eldar@371 163
Eldar@371 164 private:
Eldar@371 165 Node source, dest;
Eldar@371 166
Eldar@371 167 QPointF sourcePoint;
Eldar@371 168 QPointF destPoint;
Eldar@371 169 qreal arrowSize;
Eldar@371 170 };
Eldar@371 171