]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/WorkspaceIO.cxx
ce81b8639ff7089b188e6953ce1bb255dcabf8cf
[cpPlugins.git] / lib / cpPlugins / Interface / WorkspaceIO.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
3
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
7 {
8   TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
9   doc->LoadFile( );
10   TiXmlElement* root = doc->RootElement( );
11   if( root == NULL )
12     return( "cpPlugins::Interface::Workspace: No valid file" );
13   if( std::string( root->Value( ) ) != "cpPlugins_Workspace" )
14     return( "cpPlugins::Interface::Workspace: No valid workspace" );
15   std::stringstream err;
16
17   // Read filters
18   TiXmlElement* filter = root->FirstChildElement( "filter" );
19   while( filter != NULL )
20   {
21     const char* class_value = filter->Attribute( "class" );
22     const char* name_value = filter->Attribute( "name" );
23     float viewX = float( 0 );
24     float viewY = float( 0 );
25     filter->QueryFloatAttribute( "ViewX", &viewX );
26     filter->QueryFloatAttribute( "ViewY", &viewY );
27     if( class_value != NULL && name_value != NULL )
28     {
29       if( this->CreateFilter( class_value, name_value ) )
30       {
31         TFilter* new_filter = this->GetFilter( name_value );
32         new_filter->SetViewCoords( viewX, viewY );
33
34         // Read parameters
35         TParameters* parameters = new_filter->GetParameters( );
36         parameters->Clear( );
37
38         TiXmlElement* param = filter->FirstChildElement( "parameter" );
39         while( param != NULL )
40         {
41           const char* param_name = param->Attribute( "name" );
42           const char* param_type = param->Attribute( "type" );
43           if( param_name != NULL && param_type != NULL )
44           {
45             std::string param_type_str( param_type );
46             const char* value = param->Attribute( "value" );
47             if( value != NULL )
48             {
49               if( param_type_str == "String" )
50                 parameters->ConfigureAsString( param_name );
51               else if( param_type_str == "Bool" )
52                 parameters->ConfigureAsBool( param_name );
53               else if( param_type_str == "Int" )
54                 parameters->ConfigureAsInt( param_name );
55               else if( param_type_str == "Uint" )
56                 parameters->ConfigureAsUint( param_name );
57               else if( param_type_str == "Real" )
58                 parameters->ConfigureAsReal( param_name );
59               else if( param_type_str == "Index" )
60                 parameters->ConfigureAsIndex( param_name );
61               else if( param_type_str == "Point" )
62                 parameters->ConfigureAsPoint( param_name );
63               else if( param_type_str == "Vector" )
64                 parameters->ConfigureAsVector( param_name );
65               else if( param_type_str == "StringList" )
66                 parameters->ConfigureAsStringList( param_name );
67               else if( param_type_str == "BoolList" )
68                 parameters->ConfigureAsBoolList( param_name );
69               else if( param_type_str == "IntList" )
70                 parameters->ConfigureAsIntList( param_name );
71               else if( param_type_str == "UintList" )
72                 parameters->ConfigureAsUintList( param_name );
73               else if( param_type_str == "RealList" )
74                 parameters->ConfigureAsRealList( param_name );
75               else if( param_type_str == "IndexList" )
76                 parameters->ConfigureAsIndexList( param_name );
77               else if( param_type_str == "PointList" )
78                 parameters->ConfigureAsPointList( param_name );
79               else if( param_type_str == "VectorList" )
80                 parameters->ConfigureAsVectorList( param_name );
81               else if( param_type_str == "Choices" )
82                 parameters->ConfigureAsChoices( param_name );
83               parameters->SetString( param_name, value, false );
84
85             } // fi
86
87           } // fi
88           param = param->NextSiblingElement( "parameter" );
89
90         } // elihw
91       }
92       else
93         err
94           << "No valid class \"" << class_value << "\" with name \""
95           << name_value << "\"" << std::endl;
96     }
97     else
98       err << "Incomplete data." << std::endl;
99     filter = filter->NextSiblingElement( "filter" );
100
101   } // elihw
102
103   // Read connections
104   TiXmlElement* connection = root->FirstChildElement( "connection" );
105   while( connection != NULL )
106   {
107     TiXmlElement* orig = connection->FirstChildElement( "origin" );
108     TiXmlElement* dest = connection->FirstChildElement( "destination" );
109     if( orig != NULL && dest != NULL )
110     {
111       const char* orig_filter = orig->Attribute( "filter" );
112       const char* dest_filter = dest->Attribute( "filter" );
113       const char* orig_name = orig->Attribute( "name" );
114       const char* dest_name = dest->Attribute( "name" );
115       if(
116         orig_filter != NULL && dest_filter != NULL &&
117         orig_name != NULL && dest_name != NULL
118         )
119         this->Connect( orig_filter, dest_filter, orig_name, dest_name );
120
121     } // fi
122     connection = connection->NextSiblingElement( "connection" );
123
124   } // elihw
125
126   // Finish and return
127   delete doc;
128   return( err.str( ) );
129 }
130
131 // -------------------------------------------------------------------------
132 std::string cpPlugins::Interface::Workspace::
133 SaveWorkspace( const std::string& fname ) const
134 {
135   std::stringstream err;
136   TiXmlDocument* doc = new TiXmlDocument( );
137   TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
138
139   // Save vertices
140   auto vIt = this->m_Graph->BeginVertices( );
141   for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
142   {
143     auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
144     auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
145     if( filter != NULL )
146     {
147       TiXmlElement* e = new TiXmlElement( "filter" );
148       e->SetAttribute( "class", filter->GetClassName( ) );
149       e->SetAttribute( "name", filter->GetName( ) );
150       e->SetAttribute( "ViewX", filter->GetViewX( ) );
151       e->SetAttribute( "ViewY", filter->GetViewY( ) );
152
153       const TParameters* params = filter->GetParameters( );
154       std::vector< std::string > names;
155       params->GetNames( names );
156       for( auto nIt = names.begin( ); nIt != names.end( ); ++nIt )
157       {
158         TiXmlElement* p = new TiXmlElement( "parameter" );
159         p->SetAttribute( "name", nIt->c_str( ) );
160         if( params->HasString( *nIt ) )
161           p->SetAttribute( "type", "String" );
162         else if( params->HasBool( *nIt ) ) 
163           p->SetAttribute( "type", "Bool" );
164         else if( params->HasInt( *nIt ) )
165           p->SetAttribute( "type", "Int" );
166         else if( params->HasUint( *nIt ) )
167           p->SetAttribute( "type", "Uint" );
168         else if( params->HasReal( *nIt ) )
169           p->SetAttribute( "type", "Real" );
170         else if( params->HasIndex( *nIt ) )
171           p->SetAttribute( "type", "Index" );
172         else if( params->HasPoint( *nIt ) )
173           p->SetAttribute( "type", "Point" );
174         else if( params->HasVector( *nIt ) )
175           p->SetAttribute( "type", "Vector" );
176         else if( params->HasStringList( *nIt ) )
177           p->SetAttribute( "type", "StringList" );
178         else if( params->HasBoolList( *nIt ) )
179           p->SetAttribute( "type", "BoolList" );
180         else if( params->HasIntList( *nIt ) )
181           p->SetAttribute( "type", "IntList" );
182         else if( params->HasUintList( *nIt ) )
183           p->SetAttribute( "type", "UintList" );
184         else if( params->HasRealList( *nIt ) )
185           p->SetAttribute( "type", "RealList" );
186         else if( params->HasIndexList( *nIt ) )
187           p->SetAttribute( "type", "IndexList" );
188         else if( params->HasPointList( *nIt ) )
189           p->SetAttribute( "type", "PointList" );
190         else if( params->HasVectorList( *nIt ) )
191           p->SetAttribute( "type", "VectorList" );
192         else if( params->HasChoices( *nIt ) )
193           p->SetAttribute( "type", "Choices" );
194         p->SetAttribute(
195           "value", params->GetString( *nIt, false ).c_str( )
196           );
197         e->LinkEndChild( p );
198
199       } // rof
200       root->LinkEndChild( e );
201     }
202     else if( data != NULL )
203     {
204       // TODO
205     } // fi
206
207   } // rof
208
209   // Save connections
210   auto mIt = this->m_Graph->BeginEdgesRows( );
211   for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt )
212   {
213     auto rIt = mIt->second.begin( );
214     for( ; rIt != mIt->second.end( ); ++rIt )
215     {
216       auto eIt = rIt->second.begin( );
217       for( ; eIt != rIt->second.end( ); ++eIt )
218       {
219         TiXmlElement* conn = new TiXmlElement( "connection" );
220         TiXmlElement* orig = new TiXmlElement( "origin" );
221         TiXmlElement* dest = new TiXmlElement( "destination" );
222         orig->SetAttribute( "filter", mIt->first.c_str( ) );
223         orig->SetAttribute( "name", eIt->first.c_str( ) );
224         dest->SetAttribute( "filter", rIt->first.c_str( ) );
225         dest->SetAttribute( "name", eIt->second.c_str( ) );
226
227         conn->LinkEndChild( orig );
228         conn->LinkEndChild( dest );
229         root->LinkEndChild( conn );
230
231       } // rof
232
233     } // rof
234
235   } // rof
236
237   // Physical write and return
238   doc->LinkEndChild( root );
239   doc->SaveFile( fname.c_str( ) );
240   delete doc;
241   return( err.str( ) );
242 }
243
244 // eof - $RCSfile$