1 #include <cpPlugins/Interface/Workspace.h>
2 #include <cpPlugins/BaseObjects/Widget.h>
4 // -------------------------------------------------------------------------
5 void cpPlugins::Interface::Workspace::
8 this->m_Filters.clear( );
9 this->m_ExposedInputs.clear( );
10 this->m_ExposedOutputs.clear( );
13 // -------------------------------------------------------------------------
14 std::vector< std::string > cpPlugins::Interface::Workspace::
15 GetFiltersNames( ) const
17 std::vector< std::string > n;
18 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
19 n.push_back( i->first );
23 // -------------------------------------------------------------------------
24 cpPlugins::Interface::Workspace::
25 TProcess* cpPlugins::Interface::Workspace::
26 GetFilter( const std::string& name )
28 auto i = this->m_Filters.find( name );
29 if( i != this->m_Filters.end( ) )
30 return( i->second.GetPointer( ) );
35 // -------------------------------------------------------------------------
36 const cpPlugins::Interface::Workspace::
37 TProcess* cpPlugins::Interface::Workspace::
38 GetFilter( const std::string& name ) const
40 auto i = this->m_Filters.find( name );
41 if( i != this->m_Filters.end( ) )
42 return( i->second.GetPointer( ) );
47 // -------------------------------------------------------------------------
48 bool cpPlugins::Interface::Workspace::
49 HasFilter( const std::string& name ) const
51 return( this->m_Filters.find( name ) != this->m_Filters.end( ) );
54 // -------------------------------------------------------------------------
55 cpPlugins::Interface::Workspace::
56 TProcess* cpPlugins::Interface::Workspace::
58 const std::string& category,
59 const std::string& filter,
60 const std::string& name
63 typedef cpPlugins::BaseObjects::Widget _TWidget;
65 TInterface::Pointer interface = TInterface::New( );
66 TProcess::Pointer o = this->GetFilter( name );
69 o = interface->CreateProcessObject( category, filter );
72 o->SetPrintExecution( this->m_PrintExecution );
77 auto i = this->m_Interactors.begin( );
78 i != this->m_Interactors.end( );
81 o->AddInteractor( *i );
84 this->m_Filters[ name ] = o;
89 return( o.GetPointer( ) );
92 // -------------------------------------------------------------------------
93 bool cpPlugins::Interface::Workspace::
94 RenameFilter( const std::string& old_name, const std::string& new_name )
96 auto o = this->m_Filters.find( old_name );
97 auto n = this->m_Filters.find( new_name );
98 if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) )
101 o->second->SetName( new_name );
102 this->m_Filters[ new_name ] = o->second;
103 this->m_Filters.erase( o );
105 // Rename exposed ports
106 auto e = this->m_ExposedInputs.begin( );
107 for( ; e != this->m_ExposedInputs.end( ); ++e )
108 if( e->second.first == old_name )
109 e->second.first = new_name;
110 e = this->m_ExposedOutputs.begin( );
111 for( ; e != this->m_ExposedOutputs.end( ); ++e )
112 if( e->second.first == old_name )
113 e->second.first = new_name;
121 // -------------------------------------------------------------------------
122 bool cpPlugins::Interface::Workspace::
123 RemoveFilter( const std::string& name )
125 auto i = this->m_Filters.find( name );
126 if( i != this->m_Filters.end( ) )
128 i->second->Disconnect( );
129 this->m_Filters.erase( i );
136 // -------------------------------------------------------------------------
137 void cpPlugins::Interface::Workspace::
138 SetPrintExecution( bool b )
140 this->m_PrintExecution = b;
141 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
142 i->second->SetPrintExecution( b );
145 // -------------------------------------------------------------------------
146 void cpPlugins::Interface::Workspace::
149 this->SetPrintExecution( true );
152 // -------------------------------------------------------------------------
153 void cpPlugins::Interface::Workspace::
156 this->SetPrintExecution( true );
159 // -------------------------------------------------------------------------
160 void cpPlugins::Interface::Workspace::
161 AddInteractor( vtkRenderWindowInteractor* iren )
164 this->m_Interactors.insert( iren );
167 // -------------------------------------------------------------------------
168 const cpPlugins::Interface::Workspace::
169 TExposedPorts& cpPlugins::Interface::Workspace::
170 GetExposedInputs( ) const
172 return( this->m_ExposedInputs );
175 // -------------------------------------------------------------------------
176 const cpPlugins::Interface::Workspace::
177 TExposedPorts& cpPlugins::Interface::Workspace::
178 GetExposedOutputs( ) const
180 return( this->m_ExposedOutputs );
183 // -------------------------------------------------------------------------
184 cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
185 GetExposedOutput( const std::string& name )
187 auto i = this->m_ExposedOutputs.find( name );
188 if( i != this->m_ExposedOutputs.end( ) )
190 auto f = this->GetFilter( i->second.first );
192 return( f->GetOutput( i->second.second ) );
200 // -------------------------------------------------------------------------
201 const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
202 GetExposedOutput( const std::string& name ) const
204 auto i = this->m_ExposedOutputs.find( name );
205 if( i != this->m_ExposedOutputs.end( ) )
207 auto f = this->GetFilter( i->second.first );
209 return( f->GetOutput( i->second.second ) );
217 // -------------------------------------------------------------------------
218 bool cpPlugins::Interface::Workspace::
220 const std::string& name,
221 const std::string& filter, const std::string& filter_input
224 auto i = this->m_ExposedInputs.find( name );
225 if( i == this->m_ExposedInputs.end( ) )
227 this->m_ExposedInputs[ name ] =
228 std::pair< std::string, std::string >( filter, filter_input );
235 // -------------------------------------------------------------------------
236 bool cpPlugins::Interface::Workspace::
238 const std::string& name,
239 const std::string& filter, const std::string& filter_output
242 auto i = this->m_ExposedOutputs.find( name );
243 if( i == this->m_ExposedOutputs.end( ) )
245 this->m_ExposedOutputs[ name ] =
246 std::pair< std::string, std::string >( filter, filter_output );
253 // -------------------------------------------------------------------------
254 void cpPlugins::Interface::Workspace::
255 HideInput( const std::string& name )
257 auto i = this->m_ExposedInputs.find( name );
258 if( i != this->m_ExposedInputs.end( ) )
259 this->m_ExposedInputs.erase( i );
262 // -------------------------------------------------------------------------
263 void cpPlugins::Interface::Workspace::
264 HideOutput( const std::string& name )
266 auto i = this->m_ExposedOutputs.find( name );
267 if( i != this->m_ExposedOutputs.end( ) )
268 this->m_ExposedOutputs.erase( i );
271 // -------------------------------------------------------------------------
272 bool cpPlugins::Interface::Workspace::
274 const std::string& old_name, const std::string& new_name
277 auto o = this->m_ExposedInputs.find( old_name );
278 auto n = this->m_ExposedInputs.find( new_name );
279 if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
281 this->m_ExposedInputs[ new_name ] = o->second;
282 this->m_ExposedInputs.erase( o );
289 // -------------------------------------------------------------------------
290 bool cpPlugins::Interface::Workspace::
292 const std::string& old_name, const std::string& new_name
295 auto o = this->m_ExposedOutputs.find( old_name );
296 auto n = this->m_ExposedOutputs.find( new_name );
298 o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
301 this->m_ExposedOutputs[ new_name ] = o->second;
302 this->m_ExposedOutputs.erase( o );
309 // -------------------------------------------------------------------------
310 std::vector< std::pair< std::string, std::string > >
311 cpPlugins::Interface::Workspace::
313 const std::string& origin, const std::string& destination
316 std::vector< std::pair< std::string, std::string > > conns;
317 auto orig = this->GetFilter( origin );
318 auto dest = this->GetFilter( destination );
319 if( orig != NULL && dest != NULL )
321 auto outs = orig->GetOutputsNames( );
322 auto ins = dest->GetInputsNames( );
323 for( auto o = outs.begin( ); o != outs.end( ); ++o )
325 for( auto i = ins.begin( ); i != ins.end( ); ++i )
327 auto od = orig->GetOutput( *o );
328 auto id = dest->GetInput( *i );
329 if( od != NULL && od == id )
331 std::pair< std::string, std::string >( *o, *i )
342 // -------------------------------------------------------------------------
343 void cpPlugins::Interface::Workspace::
345 const std::string& orig_filter, const std::string& dest_filter,
346 const std::string& output_name, const std::string& input_name
349 auto o = this->GetFilter( orig_filter );
350 auto d = this->GetFilter( dest_filter );
351 if( o != NULL && d != NULL )
355 d->AddInput( input_name, o->GetOutput( output_name ) );
357 catch( std::exception& err )
359 throw std::logic_error(
360 std::string( "Error connecting \"" ) +
361 output_name + std::string( "@" ) + orig_filter +
362 std::string( "\" with \"" ) +
363 input_name + std::string( "@" ) + dest_filter +
364 std::string( "\": " ) +
373 // -------------------------------------------------------------------------
374 void cpPlugins::Interface::Workspace::
376 cpPlugins::BaseObjects::DataObject* output,
377 const std::string& dest_filter, const std::string& input_name
380 auto d = this->GetFilter( dest_filter );
382 d->AddInput( input_name, output );
385 // -------------------------------------------------------------------------
386 void cpPlugins::Interface::Workspace::
388 cpPlugins::BaseObjects::DataObject* output,
389 const std::string& exposed_input_name
392 auto i = this->m_ExposedInputs.find( exposed_input_name );
393 if( i != this->m_ExposedInputs.end( ) )
394 this->Connect( output, i->second.first, i->second.second );
397 // -------------------------------------------------------------------------
398 void cpPlugins::Interface::Workspace::
400 const std::string& orig_filter, const std::string& dest_filter,
401 const std::string& output_name, const std::string& input_name
404 auto orig = this->GetFilter( orig_filter );
405 auto dest = this->GetFilter( dest_filter );
406 if( orig != NULL && dest != NULL )
408 auto out = orig->GetOutput( output_name );
409 auto in = dest->GetInput( input_name );
410 if( out != NULL && out == in )
412 input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
418 // -------------------------------------------------------------------------
419 void cpPlugins::Interface::Workspace::
421 const std::string& dest_filter, const std::string& input_name
424 throw std::logic_error( "Disconnect 1" );
427 // -------------------------------------------------------------------------
428 void cpPlugins::Interface::Workspace::
429 Disconnect( const std::string& dest_filter )
431 throw std::logic_error( "Disconnect 2" );
434 // -------------------------------------------------------------------------
435 void cpPlugins::Interface::Workspace::
438 for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
439 f->second->Update( );
442 // -------------------------------------------------------------------------
443 void cpPlugins::Interface::Workspace::
444 Execute( const std::string& name )
446 auto filter = this->GetFilter( name );
451 // -------------------------------------------------------------------------
452 cpPlugins::Interface::Workspace::
455 m_PrintExecution( false )
459 // -------------------------------------------------------------------------
460 cpPlugins::Interface::Workspace::
463 this->m_ExposedOutputs.clear( );
464 this->m_ExposedInputs.clear( );
465 this->m_Filters.clear( );