00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "node.h"
00021 #include "edge.h"
00022 #include "graph.h"
00023 #include <KDebug>
00024 #include <KGlobal>
00025 #include <kstandarddirs.h>
00026
00027 #include "DynamicPropertiesList.h"
00028
00029
00030 Node::Node( Graph* parent ) : QObject( parent )
00031 {
00032 _graph = parent;
00033 _x = 0;
00034 _y = 0;
00035 _width = 0.5;
00036 _showName = true;
00037 _showValue = true;
00038 _showImage = false;
00039 _begin = false;
00040 _end = false;
00041 _color = _graph->nodeDefaultColor();
00042 _changing = false;
00043 _value = 0;
00044 _valueHeader = "Value";
00045 _image = QPixmap( 100, 100 );
00046 _image.fill();
00047 _maxInEdges = 1;
00048 _maxOutEdges = 1;
00049 _maxSelfEdges = 0;
00050 _icon = "rocs_method";
00051 _type = "";
00052 _nitem = 0;
00053 _iconpackage = KGlobal::dirs()->locate( "data", "iconpacks/default.svg" );
00054 kDebug() << "Node successfully created" << _iconpackage;
00055 }
00056
00057 Node::~Node()
00058 {
00059 emit removed();
00060 empty( _in_edges );
00061 empty( _out_edges );
00062 empty( _self_edges );
00063
00064 }
00065
00066 void Node::empty( EdgeList& list )
00067 {
00068 foreach( Edge * e, list )
00069 {
00070 e->remove();
00071 }
00072 }
00073
00074 bool Node::showName()
00075 {
00076 return _showName;
00077 }
00078
00079 bool Node::showValue()
00080 {
00081 return _showValue;
00082 }
00083
00084 bool Node::showImage()
00085 {
00086 return _showImage;
00087 }
00088
00089 void Node::hideImage( bool b )
00090 {
00091 _showImage = b;
00092 emit changed();
00093 }
00094
00095 void Node::hideName( bool b )
00096 {
00097 _showName = b;
00098 emit changed();
00099 }
00100
00101 void Node::hideValue( bool b )
00102 {
00103 _showValue = b;
00104 emit changed();
00105 }
00106
00107 void Node::setIcon( const QString& s )
00108 {
00109 _icon = s;
00110 emit changed();
00111 }
00112
00113 void Node::setIconPackage( const QString& s )
00114 {
00115 _iconpackage = s;
00116 }
00117
00118 void Node::setType( QString type )
00119 {
00120 _type = type;
00121 }
00122
00123 QString Node::type()
00124 {
00125 return _type;
00126 }
00127
00128 QGraphicsSvgItem* Node::nodeItem()
00129 {
00130 return _nitem;
00131 }
00132
00133 void Node::setNodeItem( QGraphicsSvgItem* ni )
00134 {
00135 _nitem = ni;
00136 }
00137
00138 const QString& Node::icon() const
00139 {
00140 return _icon;
00141 }
00142
00143 const QString& Node::iconPackage() const
00144 {
00145 return _iconpackage;
00146 }
00147
00148 QList<Node*> Node::adjacent_nodes() const
00149 {
00150 QList<Node*> adjacent;
00151
00152 foreach( Edge * e, _out_edges )
00153 {
00154 adjacent.append( e->to() );
00155 }
00156
00157 if( _graph -> directed() )
00158 {
00159 foreach( Edge * e, _self_edges )
00160 {
00161 adjacent.append( e->to() );
00162 }
00163 return adjacent;
00164 }
00165
00166 foreach( Edge * e, _in_edges )
00167 {
00168 adjacent.append( e->from() );
00169 }
00170 return adjacent;
00171 }
00172
00173
00174 EdgeList Node::adjacent_edges() const
00175 {
00176 EdgeList adjacent;
00177
00178 adjacent << _out_edges;
00179
00180 if( _graph -> directed() )
00181 {
00182 adjacent << _self_edges;
00183 }
00184 else
00185 {
00186 adjacent << _in_edges;
00187 }
00188
00189 return adjacent;
00190 }
00191
00192 void Node::addInEdge( Edge* e )
00193 {
00194 _in_edges.append( e );
00195 }
00196
00197 void Node::addOutEdge( Edge* e )
00198 {
00199 _out_edges.append( e );
00200 }
00201
00202 void Node::addSelfEdge( Edge* e )
00203 {
00204 _self_edges.append( e );
00205 }
00206
00207 EdgeList Node::in_edges() const
00208 {
00209 return _in_edges;
00210 }
00211
00212 EdgeList Node::out_edges() const
00213 {
00214 return _out_edges;
00215 }
00216
00217 EdgeList Node::self_edges() const
00218 {
00219 return _self_edges;
00220 }
00221
00222
00223
00224
00225
00226 void Node::removeEdge( Edge* e, int edgeList )
00227 {
00228 switch( edgeList )
00229 {
00230 case In :
00231 removeEdge( e, _in_edges );
00232 break;
00233 case Out :
00234 removeEdge( e, _out_edges );
00235 break;
00236 case Self:
00237 removeEdge( e, _self_edges );
00238 break;
00239 }
00240 }
00241
00242 void Node::removeEdge( Edge* e, EdgeList& list )
00243 {
00244 if( list.contains( e ) ) list.removeOne( e );
00245 }
00246
00247 EdgeList Node::edges( Node* n )
00248 {
00249 EdgeList list;
00250 if( n == this )
00251 {
00252 return _self_edges;
00253 }
00254 foreach( Edge * tmp, _out_edges )
00255 {
00256 if( tmp->to() == n )
00257 {
00258 list.append( tmp );
00259 }
00260 }
00261 foreach( Edge * tmp, _in_edges )
00262 {
00263 if( tmp->from() == n )
00264 {
00265 list.append( tmp );
00266 }
00267 }
00268 return list;
00269 }
00270
00271 void Node::remove()
00272 {
00273 _graph->remove( this );
00274 }
00275
00277 void Node::setX( int x )
00278 {
00279 _x = x;
00280 if( ! _changing )
00281 {
00282 emit changed();
00283 }
00284 }
00285
00286 qreal Node::x() const
00287 {
00288 return _x;
00289 }
00290
00291 void Node::setY( int y )
00292 {
00293 _y = y;
00294 if( ! _changing )
00295 {
00296 emit changed();
00297 }
00298 }
00299
00300 bool Node::inEdgesCapacityReached() const
00301 {
00302 if( maxInEdges() == -1 ) return false;
00303 if( maxInEdges() < in_edges().count() + 1 ) return true;
00304 return false;
00305 }
00306
00307 bool Node::outEdgesCapacityReached() const
00308 {
00309 if( maxOutEdges() == -1 ) return false;
00310 if( maxOutEdges() < out_edges().count() + 1 ) return true;
00311 return false;
00312 }
00313
00314 bool Node::selfEdgesCapacityReached() const
00315 {
00316 if( maxSelfEdges() == -1 ) return false;
00317 if( maxSelfEdges() < self_edges().count() + 1 ) return true;
00318 return false;
00319 }
00320
00321
00322 void Node::setWidth( qreal w )
00323 {
00324 _width = w;
00325 if( ! _changing )
00326 {
00327 emit changed();
00328 kDebug() << "Updating node drawing";
00329 }
00330 }
00331
00332 void Node::setPos( qreal x, qreal y )
00333 {
00334 _x = x;
00335 _y = y;
00336 if( ! _changing )
00337 {
00338 emit changed();
00339 }
00340
00341 }
00342
00343 qreal Node::y() const
00344 {
00345 return _y;
00346 }
00347
00348 qreal Node::width() const
00349 {
00350 return _width;
00351 }
00352
00353 void Node::setColor( const QString& s )
00354 {
00355 _color = s;
00356 if( ! _changing )
00357 {
00358 emit changed();
00359 }
00360 }
00361
00362 const QString& Node::color() const
00363 {
00364 return _color;
00365 }
00366
00367 void Node::setMaxInEdges( const int& m )
00368 {
00369 _maxInEdges = m;
00370 if( ! _changing )
00371 {
00372 emit changed();
00373 }
00374 }
00375
00376 const int& Node::maxInEdges() const
00377 {
00378 return _maxInEdges;
00379 }
00380
00381 void Node::setMaxOutEdges( const int& m )
00382 {
00383 _maxOutEdges = m;
00384 if( ! _changing )
00385 {
00386 emit changed();
00387 }
00388 }
00389
00390 const int& Node::maxSelfEdges() const
00391 {
00392 return _maxSelfEdges;
00393 }
00394
00395 void Node::setMaxSelfEdges( const int& m )
00396 {
00397 _maxSelfEdges = m;
00398 if( ! _changing )
00399 {
00400 emit changed();
00401 }
00402 }
00403
00404 const int& Node::maxOutEdges() const
00405 {
00406 return _maxOutEdges;
00407 }
00408
00409 void Node::setImage( const QPixmap& p )
00410 {
00411 _image = p;
00412 if( ! _changing )
00413 {
00414 emit changed();
00415 }
00416 }
00417
00418 const QPixmap& Node::image() const
00419 {
00420 return _image;
00421 }
00422
00423 void Node::setName( const QString& s )
00424 {
00425 _name = s;
00426 if( ! _changing )
00427 {
00428 emit changed();
00429 }
00430 }
00431
00432 const QString& Node::name() const
00433 {
00434 return _name;
00435 }
00436
00437 void Node::setBegin( bool begin )
00438 {
00439 if( !begin )
00440 {
00441 _begin = false;
00442 if( _graph->begin() == this )
00443 {
00444 _graph->setBegin( 0 );
00445 }
00446 }
00447 else if( _graph->begin() == this )
00448 {
00449 return;
00450 }
00451 else if( _graph->setBegin( this ) )
00452 {
00453 _begin = true;
00454 }
00455 else
00456 {
00457 return;
00458 }
00459
00460 if( ! _changing )
00461 {
00462 emit changed();
00463 }
00464 }
00465
00466 void Node::setEnd( bool end )
00467 {
00468 _end = end;
00469
00470 if( end )
00471 {
00472 _graph->addEnd( this );
00473 }
00474 else
00475 {
00476 _graph->removeEnd( this );
00477 }
00478 if( ! _changing )
00479 {
00480 emit changed();
00481 }
00482 }
00483
00484 bool Node::begin() const
00485 {
00486 return _begin;
00487 }
00488
00489 bool Node::end() const
00490 {
00491 return _end;
00492 }
00493
00494 const QVariant Node::value() const
00495 {
00496 return _value;
00497 }
00498 void Node::setValue( QVariant s )
00499 {
00500 _value = s;
00501 if( ! _changing )
00502 {
00503 emit changed();
00504 }
00505 }
00506
00507 void Node::setValue( const QString& c )
00508 {
00509 _value = c;
00510 if( _changing )
00511 {
00512 emit changed();
00513 }
00514 }
00515 void Node::startChange()
00516 {
00517 _changing = true;
00518 }
00519
00520 void Node::endChange()
00521 {
00522 _changing = false;
00523 emit changed();
00524 }
00525
00526 void Node::addDynamicProperty( QString property, QVariant value )
00527 {
00528 this->setProperty( property.toUtf8(), value );
00529 if( value.isValid() )
00530 {
00531 DynamicPropertiesList::New()->addProperty( this, property );
00532 }
00533 }
00534
00535 void Node::removeDynamicProperty( QString property )
00536 {
00537 this->addDynamicProperty( property.toUtf8(), QVariant::Invalid );
00538 DynamicPropertiesList::New()->removeProperty( this, property );
00539 }