1 #include <cpPlugins/Interface/Workspace.h>
2 #include <third_party/tinyxml/tinyxml.h>
4 // -------------------------------------------------------------------------
5 std::string cpPlugins::Interface::Workspace::
6 LoadWorkspace( const std::string& fname )
8 TiXmlDocument* doc = new TiXmlDocument( fname.c_str( ) );
10 TiXmlElement* root = doc->RootElement( );
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;
18 TiXmlElement* filter = root->FirstChildElement( "filter" );
19 while( filter != NULL )
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 )
28 if( this->CreateFilter( class_value, name_value ) )
30 TFilter* new_filter = this->GetFilter( name_value );
31 new_filter->SetViewCoords( viewX, viewY );
34 TParameters* parameters = new_filter->GetParameters( );
37 TiXmlElement* param = filter->FirstChildElement( "parameter" );
38 while( param != NULL )
40 const char* param_name = param->Attribute( "name" );
41 const char* param_type = param->Attribute( "type" );
42 if( param_name != NULL && param_type != NULL )
44 std::string param_type_str( param_type );
45 const char* value = param->Attribute( "value" );
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 );
87 param = param->NextSiblingElement( "parameter" );
93 << "No valid class \"" << class_value << "\" with name \""
94 << name_value << "\"" << std::endl;
97 err << "Incomplete data." << std::endl;
98 filter = filter->NextSiblingElement( "filter" );
103 TiXmlElement* connection = root->FirstChildElement( "connection" );
104 while( connection != NULL )
106 TiXmlElement* orig = connection->FirstChildElement( "origin" );
107 TiXmlElement* dest = connection->FirstChildElement( "destination" );
108 if( orig != NULL && dest != NULL )
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" );
115 orig_filter != NULL && dest_filter != NULL &&
116 orig_name != NULL && dest_name != NULL
118 this->Connect( orig_filter, dest_filter, orig_name, dest_name );
121 connection = connection->NextSiblingElement( "connection" );
127 return( err.str( ) );
130 // -------------------------------------------------------------------------
131 std::string cpPlugins::Interface::Workspace::
132 SaveWorkspace( const std::string& fname ) const
134 std::stringstream err;
135 TiXmlDocument* doc = new TiXmlDocument( );
136 TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
139 auto vIt = this->m_Graph->BeginVertices( );
140 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
142 auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
143 auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
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( ) );
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 )
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" );
194 "value", params->GetString( *nIt, false ).c_str( )
196 e->LinkEndChild( p );
199 root->LinkEndChild( e );
201 else if( data != NULL )
209 auto mIt = this->m_Graph->BeginEdgesRows( );
210 for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt )
212 auto rIt = mIt->second.begin( );
213 for( ; rIt != mIt->second.end( ); ++rIt )
215 auto eIt = rIt->second.begin( );
216 for( ; eIt != rIt->second.end( ); ++eIt )
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( ) );
226 conn->LinkEndChild( orig );
227 conn->LinkEndChild( dest );
228 root->LinkEndChild( conn );
236 // Physical write and return
237 doc->LinkEndChild( root );
238 doc->SaveFile( fname.c_str( ) );
240 return( err.str( ) );