00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "graphDocument.h"
00022 #include "graph.h"
00023 #include <QString>
00024 #include <KSaveFile>
00025 #include <QByteArray>
00026 #include <KDebug>
00027 #include "node.h"
00028 #include "edge.h"
00029
00030
00031 GraphDocument::GraphDocument( const QString name, int width, int height )
00032 : QObject( 0 ), QList<Graph*>()
00033 {
00034 _name = name;
00035 _width = width;
00036 _height = height;
00037 _modified = false;
00038 _saved = false;
00039 }
00040
00041 GraphDocument::GraphDocument( const GraphDocument& gd )
00042 : QObject( 0 ), QList<Graph*>()
00043 {
00044 _name = gd.name();
00045 _width = gd.width();
00046 _height = gd.height();
00047
00048 foreach( Graph * g, gd )
00049 {
00050 append( g );
00051 }
00052 }
00053
00054
00055 GraphDocument::~GraphDocument()
00056 {
00057 kDebug() << "Deleting Graph Document";
00058 kDebug() << this;
00059 kDebug() << size();
00060
00061 for( int i = 0; i < size(); i ++ )
00062 {
00063 Graph* g = at( i );
00064 kDebug() << "Deleting graph" << g->name();
00065 delete g;
00066 }
00067 }
00068
00069
00070
00071 void GraphDocument::setName( const QString& name )
00072 {
00073 _name = name;
00074 emit nameChanged( name );
00075 }
00076
00077
00078 QString GraphDocument::name() const
00079 {
00080 return _name;
00081 }
00082
00083
00084 void GraphDocument::setWidth( qreal width )
00085 {
00086 _width = width;
00087 }
00088
00089
00090 void GraphDocument::setHeight( qreal height )
00091 {
00092 _height = height;
00093 }
00094
00095
00096 qreal GraphDocument::height() const
00097 {
00098 return _height;
00099 }
00100
00101
00102 qreal GraphDocument::width() const
00103 {
00104 return _width;
00105 }
00106
00107 bool GraphDocument::isModified()
00108 {
00109 return _modified;
00110 }
00111
00112 void GraphDocument::setActiveGraph( Graph* g )
00113 {
00114 if( indexOf( g ) != -1 )
00115 {
00116 _activeGraph = g;
00117 emit activeGraphChanged( g );
00118 }
00119 }
00120
00121 Graph* GraphDocument::addGraph( QString name )
00122 {
00123 Graph* g = new Graph( this );
00124 g->setName( name );
00125 append( g );
00126 _activeGraph = g;
00127 emit graphCreated( g );
00128 kDebug() << "Graph Added" << g->name();
00129 return g;
00130 }
00131
00132
00133 void GraphDocument::savedDocumentAt( const QString& fileName )
00134 {
00135 _lastSavedDocumentPath = fileName;
00136 }
00137
00138 const QString& GraphDocument::documentPath() const
00139 {
00140 return _lastSavedDocumentPath;
00141 }
00142
00143
00144 bool GraphDocument::saveAsInternalFormat( const QString& filename )
00145 {
00146 k_buf.clear();
00147
00148 KSaveFile saveFile( !filename.endsWith( ".graph" ) ? QString( "%1.graph" ).arg( filename ) : filename );
00149
00150 if( !saveFile.open() )
00151 {
00152 kDebug() << "Error: File Not Open";
00153 return false;
00154 }
00155
00156 QTextStream stream( &saveFile );
00157 stream.setCodec( "UTF-8" );
00158
00159 int graphSize = count();
00160
00161 for( int i = 0; i < graphSize; i++ )
00162 {
00163 Graph* g = this->at( i );
00164
00165 k_buf += QString( "[Graph %1] \n" ).arg( i ).toUtf8();
00166
00167 savePropertiesInternalFormat( g );
00168
00169 foreach( ::Node * n, g->nodes() )
00170 {
00171 k_buf += QString( "[Node %1]\n" ).arg( g->nodes().indexOf( n ) ).toUtf8();
00172 savePropertiesInternalFormat( n );
00173 }
00174
00175 int from, to;
00176 foreach( Edge * e, g->edges() )
00177 {
00178 from = g->nodes().indexOf( e->from() );
00179 to = g->nodes().indexOf( e->to() );
00180
00181 k_buf += QString( "[Edge %1->%2]\n" ).arg( from ).arg( to ).toUtf8();
00182 savePropertiesInternalFormat( e );
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 }
00194 kDebug() << k_buf;
00195
00196 stream << k_buf;
00197
00198 if( !saveFile.finalize() )
00199 {
00200 kDebug() << "Error, file not saved.";
00201 return false;
00202 }
00203 _lastSavedDocumentPath = filename;
00204 return true;
00205 }
00206
00207 void GraphDocument::savePropertiesInternalFormat( QObject* o )
00208 {
00209 const QMetaObject* metaObject = o->metaObject();
00210 int propertyCount = metaObject->propertyCount();
00211
00212 for( int i = 0; i < propertyCount; ++i )
00213 {
00214 QMetaProperty metaProperty = metaObject->property( i );
00215 const char* name = metaProperty.name();
00216 QVariant value = o->property( name );
00217
00218 if( QString( "objectName" ).compare( metaProperty.name() ) == 0 )
00219 {
00220 continue;
00221 }
00222 else if( QString( "name" ).compare( metaProperty.name() ) == 0 )
00223 {
00224 QString namevalue = QString( "%1 : %2 \n" ).arg( name ).arg( value.toString() );
00225 kDebug() << "Normal" << namevalue;
00226 kDebug() << "Ascii " << namevalue.toAscii();
00227 kDebug() << "Latin1" << namevalue.toLatin1();
00228 kDebug() << "UTF-8" << namevalue.toUtf8();
00229 kDebug() << "Local8bit" << namevalue.toLocal8Bit();
00230 }
00231
00232 k_buf += QString( "%1 : %2 \n" ).arg( name, value.toString() );
00233 }
00234
00235 QList<QByteArray> propertyNames = o->dynamicPropertyNames();
00236 foreach( const QByteArray & name, propertyNames )
00237 {
00238 QVariant value = o->property( name );
00239 k_buf += QString( "%1 : %2 \n" ).arg( name, value.toString() ).toUtf8();
00240 }
00241
00242 k_buf += '\n';
00243 }
00244
00245 void GraphDocument::loadFromInternalFormat( const QString& filename )
00246 {
00247 QFile f( filename );
00248 if( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
00249 {
00250 kDebug() << "File not open " << filename.toUtf8();
00251 return;
00252 }
00253
00254 Graph* tmpGraph = 0;
00255
00256 QObject* tmpObject = 0;
00257
00258
00259 QTextStream in( &f );
00260 in.setCodec( "UTF-8" );
00261
00262 while( !in.atEnd() )
00263 {
00264 QString str = in.readLine().simplified();
00265
00266 if( str.startsWith( '#' ) )
00267 {
00268 continue;
00269 }
00270
00271 else if( str.startsWith( "[Graph" ) )
00272 {
00273 QString gName = str.section( ' ', 1, 1 );
00274 gName.remove( ']' );
00275 tmpGraph = new Graph( this );
00276 tmpGraph->setName( gName.toAscii() );
00277 tmpObject = tmpGraph;
00278 append( tmpGraph );
00279 kDebug() << "Graph Created";
00280 }
00281
00282 else if( str.startsWith( "[Node" ) )
00283 {
00284 QString nName = str.section( ' ', 1, 1 );
00285 nName.remove( ']' );
00286 tmpObject = tmpGraph->addNode( nName );
00287 kDebug() << "Node Created";
00288 }
00289
00290 else if( str.startsWith( "[Edge" ) )
00291 {
00292 QString eName = str.section( ' ', 1, 1 );
00293 eName.remove( ']' );
00294
00295 QString nameFrom = eName.section( "->", 0, 0 );
00296 QString nameTo = eName.section( "->", 1, 1 );
00297
00298
00299
00300 }
00301 else if( str.startsWith( "[Group" ) )
00302 {
00303
00304
00305
00306 }
00307 else if( str.contains( ':' ) )
00308 {
00309 QString propertyName = str.section( ':', 0, 0 ).trimmed();
00310 QString propertyValue = str.section( ':', 1, 1 ).trimmed();
00311 tmpObject->setProperty( propertyName.toUtf8() , propertyValue );
00312 }
00313 else
00314 {
00315
00316 }
00317 }
00318 kDebug() << "Graph Document Loaded.";
00319 }
00320
00321
00322
00323
00324
00325
00326