• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
  • DataStructures
  • Graph
EdgeItem.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright 2008 Tomaz Canabrava <tomaz.canabrava@gmail.com>
4  Copyright 2008 Ugo Sangiori <ugorox@gmail.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation; either version 2 of
9  the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "EdgeItem.h"
21 #include "GraphStructure.h"
22 
23 #include "NodeItem.h"
24 #include "Scene/GraphScene.h"
25 #include "Data.h"
26 #include "Pointer.h"
27 #include "DataStructure.h"
28 
29 #include <QGraphicsScene>
30 #include <QGraphicsSceneMouseEvent>
31 #include <QPainter>
32 #include <QPen>
33 #include <QPainterPath>
34 #include <QLine>
35 #include <QPolygonF>
36 #include <QtAlgorithms>
37 #include <KDebug>
38 #include <QGraphicsSimpleTextItem>
39 #include <QtCore/qmath.h>
40 #include <boost/math/constants/constants.hpp>
41 
42 EdgeItem::EdgeItem(PointerPtr edge, QGraphicsItem *parent)
43  : PointerItem(edge, parent)
44 {
45  _loop = (pointer()->from() == pointer()->to());
46  setPath(createCurves());
47  updatePos(); //TODO this is a workaround: updatePos should be automatically called when
48  // curves are drawn at Pointer.h; proper fix requires interface changes
49  // delayed for 4.11
50 
51  connect(edge.get(), SIGNAL(changed()), this, SLOT(updatePathLayout()));
52  connect(edge.get(), SIGNAL(directionChanged(PointerType::Direction)), this, SLOT(updatePathLayout()));
53 }
54 
55 EdgeItem::~EdgeItem()
56 {
57 }
58 
59 void EdgeItem::updatePathLayout()
60 {
61  setPath(createCurves());
62 }
63 
64 QPolygonF EdgeItem::createArrow(const QPointF &pos1, const QPointF &pos2) const
65 {
66  const qreal pi = boost::math::constants::pi<double>();
67  QLineF line(pos1, pos2);
68  qreal angle = ::qAcos(line.dx() / line.length());
69  if (line.dy() >= 0) {
70  angle = pi*2.0 - angle;
71  }
72  qreal arrowSize = 10;
73 
74  QPointF destArrowP1 = pos2 + QPointF(qSin(angle - pi/3.0) * arrowSize, qCos(angle - pi/3.0) * arrowSize);
75  QPointF destArrowP2 = pos2 + QPointF(qSin(angle - pi + pi/3.0) * arrowSize, qCos(angle - pi + pi/3.0) * arrowSize);
76  QPolygonF arrow(QPolygonF() << destArrowP1 << pos2 << destArrowP2);
77 
79  qreal x = pos2.x() - pos1.x();
80  qreal y = pos2.y() - pos1.y();
81 
82  x -= x / 2;
83  y -= y / 2;
84  arrow.translate(-x, -y);
85  return arrow;
86 }
87 
88 QPainterPath EdgeItem::createLoop(const QPointF& pos) const
89 {
90  if (!pointer()) {
91  return QPainterPath();
92  }
93  QPainterPath p;
94 
95  DataStructurePtr g = pointer()->dataStructure();
96  QPointF documentCenter = g->document()->sceneRect().center(); // TODO this computation is really strange
97  qreal size = 30 + (20 * pointer()->relativeIndex());
98  qreal angle = qAtan2((pos.x() - documentCenter.x()), (pos.y() - documentCenter.y()));
99  qreal posx = (pos.x() - (((size / 2) * qSin(angle)) * -1) - (size / 2));
100  qreal posy = (pos.y() + (((size / 2) * qCos(angle))) - (size / 2));
101  p.addEllipse(posx, posy, size, size);
102  return p;
103 }
104 
105 QPainterPath EdgeItem::createCurves()
106 {
107  const qreal pi = boost::math::constants::pi<double>();
108 
109  Q_ASSERT(pointer());
110  if (!pointer()) {
111  return QPainterPath();
112  }
113 
114  if (!pointer()->from() || !pointer()->to()) {
115  pointer()->remove();
116  return QPainterPath();
117  }
118 
119  QPointF startPos(pointer()->from()->x(), pointer()->from()->y());
120 
121  // test for self loop
122  if (_loop) {
123  return createLoop(startPos);
124  }
125 
126  QPointF endPos(pointer()->to()->x(), pointer()->to()->y());
127 
128  QPolygonF arrow = createArrow(startPos, endPos);
129 
130  if (startPos.x() > endPos.x()) {
131  qSwap(startPos, endPos);
132  }
133 
134  if (pointer()->direction() == PointerType::Bidirectional) {
135  QPainterPath p;
136  p.moveTo(startPos);
137  p.lineTo(endPos);
138  return p;
139  }
140 
141  qreal x = endPos.x() - startPos.x();
142  qreal y = endPos.y() - startPos.y();
143  qreal angle = atan2(y, x);
144 
146  qreal theta = angle + pi/2.0;
147  qreal finalX = qCos(theta);
148  qreal finalY = qSin(theta);
149  int lastIndex = pointer()->relativeIndex();
150 
151  if (lastIndex & 1) { // If the number is odd.
152  ++lastIndex;
153  finalX *= (-1);
154  finalY *= (-1);
155  }
156 
157  qreal size = qSqrt(qPow((x * 0.1), 2) + qPow((y * 0.1), 2)) * lastIndex;
158 
159  finalX *= size;
160  finalY *= size;
161  finalX += startPos.x() + x / 2;
162  finalY += startPos.y() + y / 2;
163 
165  QPainterPath p;
166  p.moveTo(startPos);
167  p.quadTo(finalX, finalY, endPos.x(), endPos.y());
168 
170  QPointF middle = p.pointAtPercent(0.5);
171 
172  x = startPos.x() + (endPos.x() - startPos.x()) / 2;
173  y = startPos.y() + (endPos.y() - startPos.y()) / 2;
174  QLineF line2(QPointF(x, y), middle);
175 
176  arrow.translate(+line2.dx(), +line2.dy());
177  p.addPolygon(arrow);
178 
179  return p;
180 }
181 
182 #include "EdgeItem.moc"
EdgeItem.h
PointerType::Bidirectional
Definition: PointerType.h:48
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
Data.h
EdgeItem::~EdgeItem
virtual ~EdgeItem()
Definition: EdgeItem.cpp:55
GraphStructure.h
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure.h
EdgeItem::EdgeItem
EdgeItem(PointerPtr edge, QGraphicsItem *parent=0)
Default constructor.
Definition: EdgeItem.cpp:42
PointerType::Direction
Direction
Definition: PointerType.h:46
Pointer.h
NodeItem.h
PointerItem
EdgeItem::updatePathLayout
void updatePathLayout()
Definition: EdgeItem.cpp:59
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:25 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal