00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "AddEdge.h"
00022 #include "GraphScene.h"
00023 #include "graph.h"
00024 #include "node.h"
00025 #include "edge.h"
00026 #include "NodeItem.h"
00027 #include "OrientedEdgeItem.h"
00028 #include <KLocale>
00029
00030 #include <KDebug>
00031
00032 AddEdgeAction::AddEdgeAction( GraphScene* scene, QObject* parent )
00033 : AbstractAction( scene, parent )
00034 {
00035 setText( i18n( "Add Edge" ) );
00036 setToolTip( i18n( "Creates a new edge between 2 nodes" ) );
00037 setIcon( KIcon( "rocsaddedge" ) );
00038
00039 _nodeFrom = 0;
00040 _nodeTo = 0;
00041 _tmpLine = 0;
00042 _working = false;
00043 _name = "add-edge";
00044 }
00045
00046 AddEdgeAction::~AddEdgeAction()
00047 {
00048 kDebug() << "Destroyed";
00049 }
00050
00051 void AddEdgeAction::executePress( QPointF pos )
00052 {
00053 if( _working ) return;
00054 if( ! _graph ) return;
00055 if( _graph->readOnly() ) return;
00056 _working = true;
00057 _nodeFrom = qgraphicsitem_cast<NodeItem*>( _graphScene->itemAt( pos ) );
00058
00059 if( ! _nodeFrom )
00060 {
00061 _working = false;
00062 return;
00063 }
00064 _startPos = QPointF( _nodeFrom->node()->x(), _nodeFrom->node()->y() );
00065 }
00066
00067 void AddEdgeAction::executeMove( QPointF pos )
00068 {
00069 if( !_graph ) return;
00070 if( !_nodeFrom ) return;
00071
00072 if( !_tmpLine )
00073 {
00074 _tmpLine = new QGraphicsLineItem( _startPos.x(), _startPos.y(), pos.x(), pos.y() );
00075 _graphScene->addItem( _tmpLine );
00076 }
00077 else
00078 {
00079 _tmpLine->setLine( _startPos.x(), _startPos.y(), pos.x(), pos.y() );
00080 }
00081 }
00082
00083 void AddEdgeAction::executeRelease( QPointF pos )
00084 {
00085 if( !_working ) return;
00086 if( !_graph ) return;
00087
00088 if( _tmpLine )
00089 {
00090 delete _tmpLine;
00091 _tmpLine = 0;
00092 }
00093
00094 _nodeTo = qgraphicsitem_cast<NodeItem*>( _graphScene->itemAt( pos ) );
00095 if( _nodeTo )
00096 {
00097
00098 emit addEdge( _nodeFrom->node(), _nodeTo->node() );
00099 }
00100
00101
00102 _nodeFrom = 0;
00103 _nodeTo = 0;
00104 _working = false;
00105 }
00106
00107 void AddEdgeAction::setActiveGraph( Graph* graph )
00108 {
00109 if( _graph ) disconnect( this, 0, _graph, 0 );
00110 _graph = graph;
00111 connect( this, SIGNAL( addEdge( Node*, Node* ) ), _graph, SLOT( addEdge( Node*, Node* ) ) );
00112 }