-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializationfunctions.cpp
More file actions
104 lines (99 loc) · 3.36 KB
/
serializationfunctions.cpp
File metadata and controls
104 lines (99 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "box.h"
#include "editor.h"
#include "graphicelement.h"
#include "nodes/qneconnection.h"
#include "serializationfunctions.h"
#include <QApplication>
#include <QDebug>
#include <QGraphicsView>
#include <QMessageBox>
#include <stdexcept>
void SerializationFunctions::serialize( const QList< QGraphicsItem* > &items, QDataStream &ds ) {
foreach( QGraphicsItem * item, items ) {
if( item->type( ) == GraphicElement::Type ) {
GraphicElement *elm = qgraphicsitem_cast< GraphicElement* >( item );
ds << item->type( );
ds << static_cast< quint64 >( elm->elementType( ) );
elm->save( ds );
}
}
foreach( QGraphicsItem * item, items ) {
if( item->type( ) == QNEConnection::Type ) {
QNEConnection *conn = qgraphicsitem_cast< QNEConnection* >( item );
ds << item->type( );
conn->save( ds );
}
}
}
QList< QGraphicsItem* > SerializationFunctions::deserialize( Editor *editor,
QDataStream &ds,
double version,
QString parentFile,
QMap< quint64, QNEPort* > portMap) {
QList< QGraphicsItem* > itemList;
while( !ds.atEnd( ) ) {
int type;
ds >> type;
if( type == GraphicElement::Type ) {
quint64 elmType;
ds >> elmType;
GraphicElement *elm = ElementFactory::buildElement( ( ElementType ) elmType, editor );
if( elm ) {
itemList.append( elm );
elm->load( ds, portMap, version );
if( elm->elementType( ) == ElementType::BOX ) {
Box *box = qgraphicsitem_cast< Box* >( elm );
box->setParentFile( parentFile );
editor->loadBox( box, box->getFile( ) );
}
elm->setSelected( true );
}
else {
throw( std::runtime_error( "Could not build element." ) );
}
}
else if( type == QNEConnection::Type ) {
QNEConnection *conn = ElementFactory::buildConnection();
conn->setSelected( true );
if( !conn->load( ds, portMap ) ) {
delete conn;
}
else {
itemList.append( conn );
}
}
else {
throw( std::runtime_error( "Invalid element type. Data is possibly corrupted." ) );
}
}
return( itemList );
}
#include <iostream>
QList< QGraphicsItem* > SerializationFunctions::load( Editor *editor, QDataStream &ds, QString parentFile,
Scene *scene ) {
QString str;
ds >> str;
if( !str.startsWith( QApplication::applicationName( ) ) ) {
throw( std::runtime_error( "Invalid file format." ) );
}
double version = str.split( " " ).at( 1 ).toDouble( );
QRectF rect;
if( version >= 1.4 ) {
ds >> rect;
}
QList< QGraphicsItem* > items = deserialize( editor, ds, version, parentFile );
if( scene ) {
foreach( QGraphicsItem * item, items ) {
scene->addItem( item );
}
scene->setSceneRect( scene->itemsBoundingRect( ) );
if( !scene->views( ).empty( ) ) {
QGraphicsView *view = scene->views( ).first( );
rect = rect.united( view->rect( ) );
rect.moveCenter( QPointF( 0, 0 ) );
scene->setSceneRect( scene->sceneRect( ).united( rect ) );
view->centerOn( scene->itemsBoundingRect( ).center( ) );
}
}
return( items );
}