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