Monitor
edge.cpp
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the examples of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:BSD$
00010 ** You may use this file under the terms of the BSD license as follows:
00011 **
00012 ** "Redistribution and use in source and binary forms, with or without
00013 ** modification, are permitted provided that the following conditions are
00014 ** met:
00015 **   * Redistributions of source code must retain the above copyright
00016 **     notice, this list of conditions and the following disclaimer.
00017 **   * Redistributions in binary form must reproduce the above copyright
00018 **     notice, this list of conditions and the following disclaimer in
00019 **     the documentation and/or other materials provided with the
00020 **     distribution.
00021 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022 **     the names of its contributors may be used to endorse or promote
00023 **     products derived from this software without specific prior written
00024 **     permission.
00025 **
00026 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037 ** $QT_END_LICENSE$
00038 **
00039 ****************************************************************************/
00040 
00041 #include <QPainter>
00042 
00043 #include "edge.h"
00044 #include "node.h"
00045 
00046 #include <math.h>
00047 
00048 static const double Pi = 3.14159265358979323846264338327950288419717;
00049 static double TwoPi = 2.0 * Pi;
00050 
00059 Edge::Edge(Node *sourceNode, Node *destNode, CommModel *comm, MainWindow *mw, bool highlight)
00060     : arrowSize(10)
00061 {
00062     if(destNode == NULL || sourceNode == NULL)
00063         return;
00064     setAcceptedMouseButtons(0);
00065 
00066     highlighted = highlight;
00067 
00068     source = sourceNode;
00069     dest = destNode;
00070     myComm = comm;
00071     myMW = mw;
00072     //Here's the problem!
00073     source->addEdge(this);
00074     dest->addEdge(this);
00075     adjust();
00076 }
00077 
00082 bool Edge::tooShort()
00083 {
00084     QLineF line(source->pos(), dest->pos());
00085     if (line.length() < 100)
00086     {
00087         return true;
00088     }
00089 
00090     return false;
00091 }
00092 
00097 Node *Edge::sourceNode() const
00098 {
00099     return source;
00100 }
00101 
00106 Node *Edge::destNode() const
00107 {
00108     return dest;
00109 }
00110 
00114 void Edge::adjust()
00115 {
00116     if (!source || !dest)
00117         return;
00118 
00119     QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
00120     qreal length = line.length();
00121 
00122     prepareGeometryChange();
00123 
00124     if (length > qreal(20.)) {
00125         QPointF edgeOffset((line.dx() * 10) / length, (line.dy() * 10) / length);
00126         sourcePoint = line.p1() + edgeOffset;
00127         destPoint = line.p2() - edgeOffset;
00128     } else {
00129         sourcePoint = destPoint = line.p1();
00130     }
00131 }
00132 
00137 QRectF Edge::boundingRect() const
00138 {
00139     if (!source || !dest)
00140         return QRectF();
00141 
00142     qreal penWidth = 1;
00143     qreal extra = (penWidth + arrowSize) / 2.0;
00144 
00145     return QRectF(sourcePoint, QSizeF(destPoint.x() - sourcePoint.x(),
00146                                       destPoint.y() - sourcePoint.y()))
00147         .normalized()
00148         .adjusted(-extra, -extra, extra, extra);
00149 }
00150 
00155 void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
00156 {
00157     if (!source || !dest)
00158         return;
00159 
00160     QLineF line(sourcePoint, destPoint);
00161     if (qFuzzyCompare(line.length(), qreal(0.)))
00162         return;
00163     
00164     // Draw the line itself
00165     if (!highlighted) {
00166         painter->setPen(QPen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
00167 
00168         int inTime = myComm->get_inactive_time(source->getNodePointer(), dest->getNodePointer());
00169         if (inTime > myMW->getInactiveThreshold())
00170         {
00171             painter->setOpacity(0);
00172         }
00173         else
00174         {
00175             painter->setOpacity(1 - inTime/MAX_TIME);
00176         }
00177         painter->drawLine(line);
00178     }
00179     else {
00180         painter->setPen(QPen(Qt::blue, 10, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
00181         painter->setOpacity(0.5);
00182         painter->drawLine(line);
00183     }
00184 
00185     
00186     // Draw the arrows
00187     if (!highlighted) {
00188         double angle = ::acos(line.dx() / line.length());
00189         if (line.dy() >= 0)
00190             angle = TwoPi - angle;
00191 
00192         /*QPointF sourceArrowP1 = sourcePoint + QPointF(sin(angle + Pi / 3) * arrowSize,
00193                                                       cos(angle + Pi / 3) * arrowSize);
00194         QPointF sourceArrowP2 = sourcePoint + QPointF(sin(angle + Pi - Pi / 3) * arrowSize,
00195                                                       cos(angle + Pi - Pi / 3) * arrowSize);*/
00196         QPointF destArrowP1 = destPoint + QPointF(sin(angle - Pi / 3) * arrowSize,
00197                                                   cos(angle - Pi / 3) * arrowSize);
00198         QPointF destArrowP2 = destPoint + QPointF(sin(angle - Pi + Pi / 3) * arrowSize,
00199                                                   cos(angle - Pi + Pi / 3) * arrowSize);
00200 
00201         painter->setBrush(Qt::black);
00202         //painter->drawPolygon(QPolygonF() << line.p1() << sourceArrowP1 << sourceArrowP2);
00203         painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
00204     }
00205 
00206     /*painter->drawText( QPointF((line.x1()+line.x2())/2, (line.y1()+line.y2())/2),
00207                       //QString("Dongs"));
00208                        (QString("Weight: %0").arg(myComm->get_weight(source->getNodePointer(), dest->getNodePointer()))));*/
00209 }
 All Classes Functions