00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <QtGui>
00023
00024 #include "link.h"
00025 #include "node.h"
00026
00027 Link::Link(Node *fromNode, QString fromProp, Node *toNode, QString toProp, QGraphicsItem * parent, QGraphicsScene * scene): QGraphicsLineItem(parent, scene), line2(this, scene), line3(this, scene), line4(this, scene), line5(this, scene)
00028 {
00029 myFromNode = fromNode;
00030 myToNode = toNode;
00031 myFromProp = fromProp;
00032 myToProp = toProp;
00033
00034 myFromNode->addOutLink(this);
00035 myToNode->addInLink(this);
00036
00037 setFlags(QGraphicsItem::ItemIsSelectable);
00038 line2.setFlags(QGraphicsItem::ItemIsSelectable);
00039 line3.setFlags(QGraphicsItem::ItemIsSelectable);
00040 line4.setFlags(QGraphicsItem::ItemIsSelectable);
00041 line5.setFlags(QGraphicsItem::ItemIsSelectable);
00042
00043 setHandlesChildEvents(true);
00044
00045 trackNodes();
00046 }
00047
00048 Link::~Link()
00049 {
00050 myFromNode->removeLink(this);
00051 myToNode->removeLink(this);
00052 }
00053
00054 Node *Link::fromNode() const
00055 {
00056 return myFromNode;
00057 }
00058
00059 Node *Link::toNode() const
00060 {
00061 return myToNode;
00062 }
00063
00064 QString Link::fromProp() const
00065 {
00066 return myFromProp;
00067 }
00068
00069 QString Link::toProp() const
00070 {
00071 return myToProp;
00072 }
00073
00074 void Link::setColor(const QColor &color)
00075 {
00076 setPen(QPen(color, 1.0));
00077 }
00078
00079 QColor Link::color() const
00080 {
00081 return pen().color();
00082 }
00083
00084
00085 void Link::trackNodes()
00086 {
00087
00088
00089
00090 double zvalue = (myFromNode->zValue() > myToNode->zValue()) ? myFromNode->zValue() + 1 : myToNode->zValue() + 1;
00091
00092
00093 QGraphicsItem *ancestor = myFromNode->parentItem();
00094 QPen myPen = pen();
00095 while (ancestor) {
00096 setZValue(ancestor->zValue() + 1);
00097 myPen.setWidthF(myPen.width() * 0.4);
00098 ancestor = ancestor->parentItem();
00099 }
00100
00101
00102 QPointF orig = myFromNode->scenePointPos(myFromProp, FALSE);
00103 QPointF dest = myToNode->scenePointPos(myToProp, TRUE);
00104
00105 if (orig.x() <= dest.x()) {
00106 setZValue(zvalue);
00107 setPen(myPen);
00108 setLine(QLineF(orig, dest));
00109 line2.hide();
00110 line3.hide();
00111 line4.hide();
00112 line5.hide();
00113 }
00114 else {
00115
00116 double origDesp = 10.0 * (fromNode()->numProps() - fromNode()->propPoint(fromProp(), true) + 1);
00117 double destDesp = 10.0 * (toNode()->numProps() - toNode()->propPoint(toProp(), true) + 1);
00118
00119
00120 double origBottom = fromNode()->sceneBoundingRect().bottom() + origDesp;
00121 double destBottom = toNode()->sceneBoundingRect().bottom() + destDesp;
00122 double bottom = origBottom > destBottom ? origBottom : destBottom;
00123
00124 setZValue(zvalue);
00125 line2.setZValue(zvalue);
00126 line3.setZValue(zvalue);
00127 line4.setZValue(zvalue);
00128 line5.setZValue(zvalue);
00129 setPen(myPen);
00130 line2.setPen(myPen);
00131 line3.setPen(myPen);
00132 line4.setPen(myPen);
00133 line5.setPen(myPen);
00134 setLine(QLineF(orig, orig + QPointF(origDesp, 0.0)));
00135 line2.setLine(QLineF(orig + QPointF(origDesp, 0.0), QPointF(orig.x() + origDesp, bottom)));
00136 line3.setLine(QLineF(QPointF(orig.x() + origDesp, bottom), QPointF(dest.x() - destDesp, bottom)));
00137 line4.setLine(QLineF(QPointF(dest.x() - destDesp, bottom), dest + QPointF(0.0 - destDesp, 0.0)));
00138 line5.setLine(QLineF(dest + QPointF(0.0 - destDesp, 0.0), dest));
00139 line2.show();
00140 line3.show();
00141 line4.show();
00142 line5.show();
00143 }
00144 }
00145
00146 bool Link::sceneEvent(QEvent * event)
00147 {
00148 setSelected(true);
00149 line2.setSelected(true);
00150 line3.setSelected(true);
00151 line4.setSelected(true);
00152 line5.setSelected(true);
00153
00154 return true;
00155 }
00156
00157