00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "GraphScene.h"
00021 #include "AbstractAction.h"
00022
00023 #include "node.h"
00024 #include "edge.h"
00025 #include "graphDocument.h"
00026 #include "NodeItem.h"
00027 #include "OrientedEdgeItem.h"
00028 #include <QGraphicsItem>
00029 #include <QGraphicsSceneMouseEvent>
00030 #include <QGraphicsSceneWheelEvent>
00031 #include <QKeyEvent>
00032 #include <QtGui/QInputDialog>
00033 #include <QtGui/QLineEdit>
00034 #include <KDebug>
00035 #include "node.h"
00036 #include "graph.h"
00037 #include "NodePropertiesWidget.h"
00038 #include "MainWindow.h"
00039 #include "edgepropertieswidget.h"
00040
00041 GraphScene::GraphScene( QObject* parent ) : QGraphicsScene( parent )
00042 {
00043 _graphDocument = 0;
00044 _hideEdges = false;
00045 _nodePropertiesWidget = new NodePropertiesWidget( 0 );
00046 _edgePropertiesWidget = new EdgePropertiesWidget( 0 );
00047 _action = 0;
00048 _parent = parent;
00049 }
00050
00051 bool GraphScene::hideEdges()
00052 {
00053 return _hideEdges;
00054 }
00055
00056 void GraphScene::setHideEdges( bool h )
00057 {
00058 _hideEdges = h;
00059 if( ! _hideEdges )
00060 {
00061 foreach( QGraphicsItem * i, _hidedEdges )
00062 {
00063 i->update();
00064 }
00065 }
00066 }
00067
00068 void GraphScene::setActiveGraph( Graph* g )
00069 {
00070 kDebug() << "Active Graph Set";
00071 _graph = g;
00072 disconnect( SIGNAL( forceUpdate() ) );
00073 connect( this, SIGNAL( forceUpdate() ), _graph, SIGNAL( forceUpdate() ) );
00074 }
00075
00076 QAction* GraphScene::action()
00077 {
00078 return _action;
00079 }
00080
00081 void GraphScene::updateAfter( QGraphicsItem* item )
00082 {
00083 if( _hidedEdges.contains( item ) ) return;
00084 _hidedEdges << item;
00085 }
00086
00087 void GraphScene::hideGraph( Graph* g, bool visibility )
00088 {
00089 QList<QGraphicsItem*> list = _hashGraphs.values( g );
00090 foreach( QGraphicsItem * i, list )
00091 {
00092 i->setVisible( visibility );
00093 }
00094 }
00095
00096
00097 void GraphScene::setAction( QAction* action )
00098 {
00099 _action = qobject_cast<AbstractAction*>( action );
00100 }
00101
00102 void GraphScene::setActiveGraphDocument( GraphDocument* gd )
00103 {
00104 _graphDocument = gd;
00105 if( gd == 0 )
00106 {
00107 return;
00108 }
00109
00110 setSceneRect( QRectF( 0, 0, gd->width(), gd->height() ) );
00111 QGraphicsRectItem* n = new QGraphicsRectItem( 0, 0, gd->width(), gd->height() );
00112 n->setFlag( QGraphicsItem::ItemIsSelectable, false );
00113 n->setZValue( -1000 );
00114 addItem( n );
00115
00116 int size = _graphDocument->size();
00117 if( size == 0 ) kDebug() << "############## SIZE = 0 ################";
00118 for( int i = 0; i < size; i++ )
00119 {
00120 kDebug() << "Updating Graph at position: " << i;
00121 updateGraph( _graphDocument->at( i ) );
00122 connectGraphSignals( _graphDocument->at( i ) );
00123 kDebug() << "Graph Updated.";
00124 }
00125 connect( _graphDocument, SIGNAL( graphCreated( Graph* ) ), this, SLOT( connectGraphSignals( Graph* ) ) );
00126 kDebug() << "Graph Document Set" << _graphDocument -> name();
00127 }
00128 void GraphScene::createItems()
00129 {
00130 kDebug() << "Creating the graph items.";
00131 int size = _graphDocument->size();
00132 for( int i = 0; i < size; i++ )
00133 {
00134 Graph* g = _graphDocument->at( i );
00135 kDebug() << "Creating " << g->nodes().size() << "nodes";
00136 for( int n = 0; n < g->nodes().size(); n++ )
00137 {
00138 createNode( g->nodes()[n] );
00139 }
00140 kDebug() << "Creating" << g->edges().size() << "edges";
00141 for( int v = 0; v < g->edges().size(); v++ )
00142 {
00143 createEdge( g->edges()[v] );
00144 }
00145 }
00146 }
00147
00148 void GraphScene::connectGraphSignals( Graph* g )
00149 {
00150 connect( g, SIGNAL( nodeCreated( Node* ) ), this, SLOT( createNode( Node* ) ) );
00151 connect( g, SIGNAL( edgeCreated( Edge* ) ), this, SLOT( createEdge( Edge* ) ) );
00152 }
00153
00154 QGraphicsItem* GraphScene::createNode( Node* n )
00155 {
00156 NodeItem* nItem = new NodeItem( n );
00157 addItem( nItem );
00158 kDebug() << "Node Item Created";
00159 return nItem;
00160 }
00161
00162 QGraphicsItem* GraphScene::createEdge( Edge* e )
00163 {
00164 QGraphicsItem* edgeItem = 0;
00165 edgeItem = new OrientedEdgeItem( e );
00166
00167 addItem( edgeItem );
00168 kDebug() << "Edge Created";
00169 return edgeItem;
00170 }
00171
00172 void GraphScene::mouseDoubleClickEvent( QGraphicsSceneMouseEvent* mouseEvent )
00173 {
00174 mouseEvent->accept();
00175 }
00176
00177 void GraphScene::wheelEvent( QGraphicsSceneWheelEvent* wheelEvent )
00178 {
00179 kDebug() << "Entering on Whell Event";
00180 NodeItem* nitem = qgraphicsitem_cast<NodeItem*>( itemAt( wheelEvent->scenePos() ) );
00181 if( !nitem )
00182 {
00183 kDebug() << "No Node Item to Spand";
00184 wheelEvent->ignore();
00185 return;
00186 }
00187
00188 Node* movableNode = nitem->node();
00189 int numDegrees = wheelEvent->delta();
00190 if( wheelEvent->orientation() == Qt::Vertical )
00191 {
00192 kDebug() << "Upgrading node size";
00193 if( numDegrees > 0 )
00194 {
00195 movableNode->setWidth( movableNode->width() + 0.25 );
00196 nitem->update();
00197 }
00198 else if( movableNode->width() > 0.5 )
00199 {
00200 movableNode->setWidth( movableNode->width() - 0.25 );
00201 nitem->update();
00202 }
00203 }
00204 wheelEvent->accept();
00205 }
00206
00207 void GraphScene::mouseMoveEvent( QGraphicsSceneMouseEvent* mouseEvent )
00208 {
00209 _action->executeMove( mouseEvent->scenePos() );
00210 }
00211
00212 void GraphScene::mousePressEvent( QGraphicsSceneMouseEvent* mouseEvent )
00213 {
00214 qDebug() << "Entrou aqui." << _action;
00215 if( mouseEvent->button() == Qt::MidButton )
00216 {
00217 NodeItem* nitem = qgraphicsitem_cast<NodeItem*>( itemAt( mouseEvent->scenePos() ) );
00218 if( !nitem ) return;
00219 Node* movableNode = nitem->node();
00220 movableNode->setWidth( 1 );
00221 }
00222 else if( mouseEvent->button() == Qt::RightButton )
00223 {
00224 QListIterator<QGraphicsItem*> list( items( mouseEvent->scenePos() ) );
00225 NodeItem* nItem = 0;
00226 while( list.hasNext() )
00227 {
00228 QGraphicsItem* item = list.next();
00229 if( item->type() == 65578 ) nItem = qgraphicsitem_cast<NodeItem*>( item );
00230 }
00231 if( nItem && nItem->node()->type() == "if" )
00232 {
00233
00234 bool ok;
00235 QString response = QInputDialog::getText( qobject_cast<QWidget*>( _parent ), "Change If Test", "New If Test:", QLineEdit::Normal, nItem->node()->value().toString(), &ok );
00236 if( ok && !response.isEmpty() )
00237 {
00238 nItem->node()->setValue( response );
00239 emit forceUpdate();
00240 }
00241 }
00242
00243
00244
00245
00246
00247
00248
00249 }
00250 else if( mouseEvent -> button() == Qt::LeftButton )
00251 {
00252 qDebug() << "Executando a ação";
00253 _action->executePress( mouseEvent->scenePos() );
00254 }
00255 }
00256
00257 void GraphScene::mouseReleaseEvent( QGraphicsSceneMouseEvent* mouseEvent )
00258 {
00259 qDebug() << "Entrou no release Action";
00260 if( mouseEvent->button() == Qt::LeftButton )
00261 {
00262 _action->executeRelease( mouseEvent->scenePos() );
00263 }
00264 }
00265
00266 void GraphScene::keyPressEvent( QKeyEvent* )
00267 {
00268
00269 }
00270
00271 void GraphScene::updateGraph( Graph* g )
00272 {
00273 kDebug() << "Removed Graph from the hash";
00274
00275 kDebug() << "Creating" << g->nodes().size() << "nodes";
00276 QList<Node*> nodes = g->nodes();
00277 foreach( Node * n, nodes )
00278 {
00279 n->setName( n->name() );
00280 }
00281
00282 kDebug() << "Creating" << g->nodes().size() << "edges";
00283 QList<Edge*> edges = g->edges();
00284 foreach( Edge * e, edges )
00285 {
00286 e->setName( e->name() );
00287 }
00288 }
00289
00290 void GraphScene::updateDocument()
00291 {
00292 if( _graphDocument == 0 )
00293 {
00294 kDebug() << "Graph Document is null. Please hit the developer.";
00295 return;
00296 }
00297
00298 clear();
00299 kDebug() << "Graph Document Size: " << _graphDocument->size();
00300 int size = _graphDocument->size();
00301
00302 for( int i = 0; i < size; i++ )
00303 {
00304 updateGraph( _graphDocument->at( i ) );
00305 kDebug() << "updated Graph at " << i;
00306 }
00307 }