Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

edge.cpp

Go to the documentation of this file.
00001 /*
00002  Modular Psi - A graph tracker of the Parapsychologcal scientific development
00003  Copyright (C) 2005 Leonardo Stern
00004 
00005  This program is free software; you can redistribute it and/or modify it under
00006  the terms of the Common Public License Version 1.0 or (at your option) any later version.
00007 
00008  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
00009  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010  See the Common Public License for more details.
00011 
00012  You should have received a copy of the Common Public License along with this program;
00013 */
00014 
00015 
00016 #include "const.hpp"
00017 #include "edge.hpp"
00018 #include "node.hpp"
00019 #include "edgelist.hpp"
00020 #include "propositiongraph.hpp"
00021 #include "utils.hpp"
00022 #include <qbitmap.h>
00023 #include <qpainter.h>
00024 #include <qstring.h>
00025 #include <qwidgetstack.h>
00026 #include <math.h>
00027 
00028 Edge::Edge (QWidget* parent, Node* origem, Node* destino)  
00029     : QWidget ( parent, 0, 0 ) 
00030 {
00031     init();
00032     setSource(origem);
00033     setTarget(destino);
00034     Edge* E = this;
00035     source()->outgoing->append(E);
00036     target()->incoming->append(E);  
00037 }
00038 
00039 Edge::~Edge()
00040 {
00041     //NooP
00042 }
00043 
00044 void Edge::init()
00045 {
00046     lineColor = QColor(0,0,0);
00047     propLineColor = lineColor;
00048     setAutoMask(true);
00049     setTrust((float)-1);
00050 }
00051 
00052 QString Edge::sourceName()
00053 {
00054     return (QString) source()->name();  
00055 }
00056 
00057 QString Edge::targetName()
00058 {
00059     return (QString) target()->name();  
00060 }
00061 
00062 void Edge::setChanged(bool alterado)
00063 {
00064     p_changed = alterado;
00065 }
00066 
00067 void Edge::setTrust (float confianca )
00068 {
00069     p_trust = confianca;
00070     setChanged(true);
00071     if (confianca >= 0) {
00072         lineColor = trust2rgb (confianca);
00073     } else lineColor = QColor(0,0,0);
00074     propLineColor = lineColor;
00075 }
00076 
00077 void Edge::setTrust (QString s_trust )
00078 {
00079     setTrust((float)s_trust.toFloat());
00080 }
00081 
00082 void Edge::setCombinedTrust (float combined )
00083 {
00084     p_combinedTrust = combined;
00085 }
00086 
00087 void Edge::setSource ( Node* origem ) 
00088 {
00089     p_source = origem;
00090 }
00091 
00092 void Edge::setTarget ( Node* destino ) 
00093 {
00094     p_target = destino;
00095 }
00096 
00097 void Edge::setStart ( QPoint inicio ) 
00098 {
00099     p_start = inicio;
00100 }
00101 
00102 void Edge::setEnd ( QPoint fim )
00103 {
00104     p_end = fim;
00105 }
00106     
00107 Node  Edge::opposite (Node* an_end) 
00108 {
00109     if (source() == an_end )
00110         return target();
00111     return source();
00112 }
00113 
00114 void Edge::processBends()
00115 {
00116     // initialize
00117     int L  = vNodes.at(0).x();
00118     int R = L;
00119     int U = vNodes.at(0).y();
00120     int D = U;
00121     QPoint ptemp;
00122     
00123     // FIXME
00124     for (unsigned int b1=0 ; b1 < vNodes.count(); b1++)
00125     {
00126                 ptemp = vNodes.at(b1);
00127                 if (ptemp.x() < L)
00128                         { L = ptemp.x(); } else
00129                 if (ptemp.x() > R)
00130                         { R = ptemp.x(); }
00131                 if (ptemp.y() < D) 
00132                         { D = ptemp.y(); } else
00133                 if (ptemp.y() > U) 
00134                         { U = ptemp.y(); }
00135     }
00136     
00137     
00138     const int h = 10;
00139     const int l = 10;
00140     const int b = (h*h/(8*l) + l/2);
00141     
00142     QPoint C  = QPoint ( vNodes.at(vNodes.count()-1).x(), vNodes.at(vNodes.count()-1).y()); // fim do spline
00143     
00144     int dx = ( target()->center.x() - C.x() );
00145     int dy = ( target()->center.y() - C.y() );
00146     int d = (int)sqrt((long double)(dx*dx + dy*dy));
00147     
00148     QPoint O ( C.x() + b * ( dx / d ) , C.y() - b * ( dy / d ) );
00149     
00150     QPoint A ( - b * ( 0.866 * dy/d + 0.5 * dx / d ) + O.x() , 
00151         - b * ( 0.5 * dy/d - 0.866 * dx/d ) + O.y() );
00152         
00153     QPoint B (  b* ( 0.866 * dy/d - 0.5 * dx / d ) + O.x() , 
00154         - b * ( 0.5 * dy/d + 0.866 * dx/d ) + O.y() );
00155         
00156     arrowPoints.putPoints(0 ,3 , A.x(), A.y() , B.x(), B.y(), C.x() , C.y()) ; 
00157     DL = QPoint(L,D);
00158 
00159     for (unsigned int b2=0 ; b2 < arrowPoints.count(); b2++)
00160     {
00161                 ptemp = arrowPoints.at(b2);
00162                 if (ptemp.x() < L)
00163                         { L = ptemp.x(); } else
00164                 if (ptemp.x() > R)
00165                         { R = ptemp.x(); }
00166                 if (ptemp.y() < D) 
00167                         { D = ptemp.y(); } else
00168                 if (ptemp.y() > U) 
00169                         { U = ptemp.y(); }
00170     }
00171 
00172     //update geometry
00173     //little dirty hack until the other bezier function arrives
00174     setGeometry(L, D+37, R-L, U-D );
00175     
00176     //global to local points
00177     for (unsigned int c=0 ; c < vNodes.count(); c++) {
00178         vNodes.putPoints ( c, 1, vNodes.at(c).x() - L , vNodes.at(c).y() - D);
00179     }
00180     for (unsigned int c1=0 ; c1 < arrowPoints.count(); c1++) {
00181         arrowPoints.putPoints ( c1, 1, arrowPoints.at(c1).x() - L , arrowPoints.at(c1).y() - D);
00182     }
00183 }
00184 
00185 void Edge::paintEvent (QPaintEvent*)
00186 {
00187     QPainter painter(this);
00188     painter.setViewport(0,0,width() , height()  );
00189     draw(&painter);
00190 }
00191 
00192 void Edge::updateMask()
00193 {
00194         QPainter painter;
00195         QBitmap mask(this->size(), TRUE);
00196         painter.begin(&mask);   
00197         painter.setPen(QPen((Qt::color1),4, SolidLine));
00198     for (unsigned int ab= 0 ; ab < (vNodes.count()/3); ab++)
00199     {
00200         painter.drawCubicBezier( vNodes , 3*ab ); 
00201     }
00202 
00203    painter.setBrush(SolidPattern);
00204    painter.drawPolygon(arrowPoints);
00205 
00206    setMask(mask);
00207 }
00208 
00209 void Edge::draw(QPainter* painter)
00210 {
00211 
00212 
00213         painter->begin(this);
00214     painter->setPen(QPen(lineColor,1, SolidLine));
00215     // Bezier curves
00216     for (int co = 0 ; co < (vNodes.count()/3); co++)
00217     {
00218         painter->drawCubicBezier( vNodes , 3*co ); 
00219     }
00220    
00221     //Arrow
00222    painter->setBrush(QBrush(lineColor,SolidPattern));
00223    painter->drawPolygon(arrowPoints);
00224 }
00225 
00226 void Edge::changeActive( QColor thisColor)
00227 {
00228         PropositionGraph *graph = ((Proposition*)source())->graph;
00229         lineColor = thisColor;
00230         repaint();
00231         if ( (graph->edgeSelected() != NULL)
00232                         && (graph->edgeSelected() != this))
00233                 {
00234                         graph->edgeSelected()->lineColor = graph->edgeSelected()->propLineColor;
00235                         graph->edgeSelected()->repaint();
00236                 }
00237         graph->setEdgeSelected(this);
00238 }
00239 
00240 void Edge::mousePressEvent (QMouseEvent *e)
00241 {
00242         changeActive(COLOR_EDGE_SELECTED);
00243         PropositionGraph *graph = ((Proposition*)source())->graph;
00244         if (graph->status() == STATUS_EDGE_DELETE)
00245         {
00246                 graph->deleteEdge(this);
00247         }
00248         repaint();
00249 }
Untitled Document Pesquisa Psi SourceForge.net Logo