00001 #include "NodeItem.h"
00002 #include <kstandarddirs.h>
00003 #include <QDir>
00004 #include <KGlobal>
00005 #include <KDebug>
00006 #include <QGraphicsColorizeEffect>
00007 #include <QFont>
00008 #include <QGraphicsScene>
00009 #include <KLocale>
00010
00011 QMap<QString, QSvgRenderer*> NodeItem::_renders;
00012
00013 NodeItem::NodeItem( Node* n ) : QGraphicsSvgItem( 0 )
00014 {
00015 _node = n;
00016 n->setNodeItem( qobject_cast<QGraphicsSvgItem*>( this ) );
00017 _name = 0;
00018 _value = 0;
00019 _image = new QGraphicsPixmapItem( QPixmap( 0, 0 ), this );
00020 _originalWidth = _node->width();
00021 _iconPackage = _node->iconPackage();
00022 _colorizer = new QGraphicsColorizeEffect( this );
00023 _font = QFont( "Helvetica [Cronyx]", 18 );
00024
00025 connect( n, SIGNAL( changed() ), this, SLOT( setupNode() ) );
00026 connect( n, SIGNAL( removed() ), this, SLOT( deleteLater() ) );
00027
00028 setupNode();
00029 setZValue( 1 );
00030 setFlag( ItemIsSelectable, true );
00031
00032 kDebug() << "Visual Node Item Created";
00033 }
00034
00035 void NodeItem::setupNode()
00036 {
00037 updateRenderer();
00038 updateIcon();
00039 updateConnectors();
00040 updateName();
00041 updateValue();
00042 updateColor();
00043 updateSize();
00044 updatePos();
00045 updateImage();
00046 update();
00047 }
00048
00049 QHash<QString, QGraphicsSvgItem*> NodeItem::connectors()
00050 {
00051 return _connectors;
00052 }
00053
00054
00055
00056 void NodeItem::updateConnectors()
00057 {
00058 QListIterator<QByteArray> plist = _node->dynamicPropertyNames();
00059 rdown = 0;
00060 ldown = 0;
00061 QRectF bound = boundingRect();
00062 QGraphicsSvgItem* tSvg;
00063 QGraphicsSimpleTextItem* tText;
00064 if( _connectors.count() == 0 || ( _node->dynamicPropertyNames().count() != previousCount ) )
00065 {
00066 if( !plist.hasNext() )
00067 {
00068 return;
00069 }
00070 else
00071 {
00072 while( plist.hasNext() )
00073 {
00074 const char* temp = plist.next().data();
00075 if( _node->property( temp ) == "in" && !_connectors.contains( QString( temp ) ) )
00076 {
00077 tSvg = new QGraphicsSvgItem( KGlobal::dirs()->locate( "data", "iconpacks/default.svg" ), this );
00078 tSvg->setElementId( "rocs_connector" );
00079 tText = new QGraphicsSimpleTextItem( QString( temp ), this );
00080 tText->setFont( _font );
00081 if( _node->name() == "if" )
00082 {
00083 tSvg->setPos(( bound.height() / 2 ) - 7.95, 0 );
00084 tText->setPos(( bound.height() / 2 ) - 7.95, -40 );
00085 }
00086 else
00087 {
00088 tSvg->setPos( bound.left(), ldown );
00089 tText->setPos( bound.left() - ( tText->boundingRect().width() + 20 ), ldown );
00090 ldown += 50;
00091 }
00092 _connectors[tText->text()] = tSvg;
00093 }
00094 else if( _node->property( temp ) == "out" && !_connectors.contains( QString( temp ) ) )
00095 {
00096 tSvg = new QGraphicsSvgItem( KGlobal::dirs()->locate( "data", "iconpacks/default.svg" ), this );
00097 tSvg->setElementId( "rocs_connector" );
00098 tText = new QGraphicsSimpleTextItem( QString( temp ), this );
00099 tText->setFont( _font );
00100 if( _node->name() == "if" )
00101 {
00102 if( QString( temp ) == "true" )
00103 {
00104 tSvg->setPos( bound.left() - 7.95, ( bound.width() / 2 ) - 8.25 );
00105 tText->setPos( bound.left() - ( tText->boundingRect().width() + 20 ), ( bound.width() / 2 ) - 8.25 );
00106 }
00107 else if( QString( temp ) == "false" )
00108 {
00109 tSvg->setPos( bound.right() - 7.95, ( bound.width() / 2 ) - 8.25 );
00110 tText->setPos( bound.right() + 20, ( bound.width() / 2 ) - 8.25 );
00111 }
00112 }
00113 else
00114 {
00115 tSvg->setPos( bound.right() - tSvg->boundingRect().width(), rdown );
00116 tText->setPos( bound.right() + ( 20 - tSvg->boundingRect().width() ), rdown );
00117 rdown += 50;
00118 }
00119 _connectors[tText->text()] = tSvg;
00120 }
00121 }
00122 previousCount = _node->dynamicPropertyNames().count();
00123 }
00124 }
00125 }
00126
00127
00128 void NodeItem::updatePos()
00129 {
00130 int fixPos = boundingRect().width() / 2;
00131 setPos( _node->x() - fixPos, _node->y() - fixPos );
00132 }
00133
00134 void NodeItem::updateSize()
00135 {
00136 if( _node->width() == _width ) return;
00137 resetMatrix();
00138 _width = _node->width();
00139 setScale( _node->width() );
00140 kDebug() << "Scale" << scale() << "Node Width" << _node->width() << "Shét" << _node->width() - 0.5;
00141 }
00142
00143 void NodeItem::updateRenderer()
00144 {
00145 _iconPackage = _node->iconPackage();
00146 if( _renders.count( _iconPackage ) == 0 )
00147 {
00148 QSvgRenderer* z = new QSvgRenderer( _node->iconPackage() );
00149 _renders.insert( _iconPackage, z );
00150 setSharedRenderer( z );
00151 }
00152 else
00153 {
00154 setSharedRenderer( _renders.value( _iconPackage ) );
00155 }
00156 }
00157
00158 void NodeItem::updateIcon()
00159 {
00160 if( elementId().isEmpty() )
00161 {
00162 _element = _node->icon();
00163 setElementId( _element );
00164 setTransformOriginPoint( boundingRect().width() / 2, boundingRect().width() / 2 );
00165 }
00166 else if( elementId() != _node->icon() )
00167 {
00168 _element = _node->icon();
00169 setElementId( _element );
00170 setTransformOriginPoint( boundingRect().width() / 2, boundingRect().width() / 2 );
00171 }
00172 }
00173
00174 void NodeItem::updateColor()
00175 {
00176 _colorizer->setColor( _node->color() );
00177 setGraphicsEffect( _colorizer );
00178 }
00179
00180 void NodeItem::updateName()
00181 {
00182 if( !_name )
00183 {
00184 _name = new QGraphicsSimpleTextItem( i18n( "Name: %1" ).arg( _node->name() ), this );
00185 _name->setFont( _font );
00186 }
00187 else if( _name->text() != _node->name() )
00188 {
00189 _name->setText( i18n( "Name: %1" ).arg( _node->name() ) );
00190 }
00191 _name->setVisible( _node->showName() );
00192 _name->setPos( 0, 75 );
00193 }
00194
00195 void NodeItem::updateValue()
00196 {
00197 if( !_value )
00198 {
00199 _value = new QGraphicsSimpleTextItem( i18n( "Value: %1" ).arg( _node->value().toString() ), this );
00200 _value->setFont( _font );
00201 }
00202 else if( _value->text() != _node->value().toString() )
00203 {
00204 _value ->setText( i18n( QString( _node->valueHeader() + ": %1" ).toUtf8().data() ).arg( _node->value().toString() ) );
00205 }
00206 _value->setVisible( _node->showValue() );
00207 _value->setPos( 0, 100 );
00208 }
00209
00210 void NodeItem::updateImage()
00211 {
00212 if( _image->pixmap().isNull() )
00213 {
00214 _image->setPixmap( _node->image() );
00215 }
00216 else if( !( _image->pixmap().toImage() == _node->image().toImage() ) )
00217 {
00218 _image->setPixmap( _node->image() );
00219 }
00220 _image->setVisible( _node->showImage() );
00221 _image->setPos( 0, 125 );
00222 }