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