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 );
24 float viewY = float( 0 );
25 filter->QueryFloatAttribute( "ViewX", &viewX );
26 filter->QueryFloatAttribute( "ViewY", &viewY );
27 if( class_value != NULL && name_value != NULL )
29 if( this->CreateFilter( class_value, name_value ) )
31 TFilter* new_filter = this->GetFilter( name_value );
32 new_filter->SetViewCoords( viewX, viewY );
35 TParameters* parameters = new_filter->GetParameters( );
38 TiXmlElement* param = filter->FirstChildElement( "parameter" );
39 while( param != NULL )
41 const char* param_name = param->Attribute( "name" );
42 const char* param_type = param->Attribute( "type" );
43 if( param_name != NULL && param_type != NULL )
45 std::string param_type_str( param_type );
46 const char* value = param->Attribute( "value" );
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 );
88 param = param->NextSiblingElement( "parameter" );
94 << "No valid class \"" << class_value << "\" with name \""
95 << name_value << "\"" << std::endl;
98 err << "Incomplete data." << std::endl;
99 filter = filter->NextSiblingElement( "filter" );
104 TiXmlElement* connection = root->FirstChildElement( "connection" );
105 while( connection != NULL )
107 TiXmlElement* orig = connection->FirstChildElement( "origin" );
108 TiXmlElement* dest = connection->FirstChildElement( "destination" );
109 if( orig != NULL && dest != NULL )
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" );
116 orig_filter != NULL && dest_filter != NULL &&
117 orig_name != NULL && dest_name != NULL
119 this->Connect( orig_filter, dest_filter, orig_name, dest_name );
122 connection = connection->NextSiblingElement( "connection" );
128 return( err.str( ) );
131 // -------------------------------------------------------------------------
132 std::string cpPlugins::Interface::Workspace::
133 SaveWorkspace( const std::string& fname ) const
135 std::stringstream err;
136 TiXmlDocument* doc = new TiXmlDocument( );
137 TiXmlElement* root = new TiXmlElement( "cpPlugins_Workspace" );
140 auto vIt = this->m_Graph->BeginVertices( );
141 for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
143 auto filter = dynamic_cast< TFilter* >( vIt->second.GetPointer( ) );
144 auto data = dynamic_cast< TData* >( vIt->second.GetPointer( ) );
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( ) );
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 )
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" );
195 "value", params->GetString( *nIt, false ).c_str( )
197 e->LinkEndChild( p );
200 root->LinkEndChild( e );
202 else if( data != NULL )
210 auto mIt = this->m_Graph->BeginEdgesRows( );
211 for( ; mIt != this->m_Graph->EndEdgesRows( ); ++mIt )
213 auto rIt = mIt->second.begin( );
214 for( ; rIt != mIt->second.end( ); ++rIt )
216 auto eIt = rIt->second.begin( );
217 for( ; eIt != rIt->second.end( ); ++eIt )
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( ) );
227 conn->LinkEndChild( orig );
228 conn->LinkEndChild( dest );
229 root->LinkEndChild( conn );
237 // Physical write and return
238 doc->LinkEndChild( root );
239 doc->SaveFile( fname.c_str( ) );
241 return( err.str( ) );