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 cpPlugins::Interface::Workspace::
49 TWidget* cpPlugins::Interface::Workspace::
50 GetWidget( const std::string& name )
52 TProcess* process = this->GetFilter( name );
53 return( dynamic_cast< TWidget* >( process ) );
56 // -------------------------------------------------------------------------
57 const cpPlugins::Interface::Workspace::
58 TWidget* cpPlugins::Interface::Workspace::
59 GetWidget( const std::string& name ) const
61 const TProcess* process = this->GetFilter( name );
62 return( dynamic_cast< const TWidget* >( process ) );
65 // -------------------------------------------------------------------------
66 bool cpPlugins::Interface::Workspace::
67 HasFilter( const std::string& name ) const
69 return( this->m_Filters.find( name ) != this->m_Filters.end( ) );
72 // -------------------------------------------------------------------------
73 bool cpPlugins::Interface::Workspace::
74 HasWidget( const std::string& name ) const
76 const TWidget* wdg = this->GetWidget( name );
77 return( wdg != NULL );
80 // -------------------------------------------------------------------------
81 cpPlugins::Interface::Workspace::
82 TProcess* cpPlugins::Interface::Workspace::
84 const std::string& category,
85 const std::string& filter,
86 const std::string& name
89 typedef cpPlugins::BaseObjects::Widget _TWidget;
91 TInterface::Pointer interface = TInterface::New( );
92 TProcess::Pointer o = this->GetFilter( name );
95 o = interface->CreateProcessObject( category, filter );
98 o->SetPrintExecution( this->m_PrintExecution );
103 auto i = this->m_Interactors.begin( );
104 i != this->m_Interactors.end( );
107 o->AddInteractor( *i );
109 // Finish association
110 this->m_Filters[ name ] = o;
115 return( o.GetPointer( ) );
118 // -------------------------------------------------------------------------
119 bool cpPlugins::Interface::Workspace::
120 RenameFilter( const std::string& old_name, const std::string& new_name )
122 auto o = this->m_Filters.find( old_name );
123 auto n = this->m_Filters.find( new_name );
124 if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) )
127 o->second->SetName( new_name );
128 this->m_Filters[ new_name ] = o->second;
129 this->m_Filters.erase( o );
131 // Rename exposed ports
132 auto e = this->m_ExposedInputs.begin( );
133 for( ; e != this->m_ExposedInputs.end( ); ++e )
134 if( e->second.first == old_name )
135 e->second.first = new_name;
136 e = this->m_ExposedOutputs.begin( );
137 for( ; e != this->m_ExposedOutputs.end( ); ++e )
138 if( e->second.first == old_name )
139 e->second.first = new_name;
147 // -------------------------------------------------------------------------
148 bool cpPlugins::Interface::Workspace::
149 RemoveFilter( const std::string& name )
151 auto i = this->m_Filters.find( name );
152 if( i != this->m_Filters.end( ) )
154 i->second->Disconnect( );
155 this->m_Filters.erase( i );
162 // -------------------------------------------------------------------------
163 void cpPlugins::Interface::Workspace::
164 SetPrintExecution( bool b )
166 this->m_PrintExecution = b;
167 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
168 i->second->SetPrintExecution( b );
171 // -------------------------------------------------------------------------
172 void cpPlugins::Interface::Workspace::
175 this->SetPrintExecution( true );
178 // -------------------------------------------------------------------------
179 void cpPlugins::Interface::Workspace::
182 this->SetPrintExecution( true );
185 // -------------------------------------------------------------------------
186 void cpPlugins::Interface::Workspace::
187 AddInteractor( vtkRenderWindowInteractor* iren )
190 this->m_Interactors.insert( iren );
193 // -------------------------------------------------------------------------
194 const cpPlugins::Interface::Workspace::
195 TExposedPorts& cpPlugins::Interface::Workspace::
196 GetExposedInputs( ) const
198 return( this->m_ExposedInputs );
201 // -------------------------------------------------------------------------
202 const cpPlugins::Interface::Workspace::
203 TExposedPorts& cpPlugins::Interface::Workspace::
204 GetExposedOutputs( ) const
206 return( this->m_ExposedOutputs );
209 // -------------------------------------------------------------------------
210 cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
211 GetExposedOutput( const std::string& name )
213 auto i = this->m_ExposedOutputs.find( name );
214 if( i != this->m_ExposedOutputs.end( ) )
216 auto f = this->GetFilter( i->second.first );
218 return( f->GetOutput( i->second.second ) );
226 // -------------------------------------------------------------------------
227 const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
228 GetExposedOutput( const std::string& name ) const
230 auto i = this->m_ExposedOutputs.find( name );
231 if( i != this->m_ExposedOutputs.end( ) )
233 auto f = this->GetFilter( i->second.first );
235 return( f->GetOutput( i->second.second ) );
243 // -------------------------------------------------------------------------
244 bool cpPlugins::Interface::Workspace::
246 const std::string& name,
247 const std::string& filter, const std::string& filter_input
250 auto i = this->m_ExposedInputs.find( name );
251 if( i == this->m_ExposedInputs.end( ) )
253 this->m_ExposedInputs[ name ] =
254 std::pair< std::string, std::string >( filter, filter_input );
261 // -------------------------------------------------------------------------
262 bool cpPlugins::Interface::Workspace::
264 const std::string& name,
265 const std::string& filter, const std::string& filter_output
268 auto i = this->m_ExposedOutputs.find( name );
269 if( i == this->m_ExposedOutputs.end( ) )
271 this->m_ExposedOutputs[ name ] =
272 std::pair< std::string, std::string >( filter, filter_output );
279 // -------------------------------------------------------------------------
280 void cpPlugins::Interface::Workspace::
281 HideInput( const std::string& name )
283 auto i = this->m_ExposedInputs.find( name );
284 if( i != this->m_ExposedInputs.end( ) )
285 this->m_ExposedInputs.erase( i );
288 // -------------------------------------------------------------------------
289 void cpPlugins::Interface::Workspace::
290 HideOutput( const std::string& name )
292 auto i = this->m_ExposedOutputs.find( name );
293 if( i != this->m_ExposedOutputs.end( ) )
294 this->m_ExposedOutputs.erase( i );
297 // -------------------------------------------------------------------------
298 bool cpPlugins::Interface::Workspace::
300 const std::string& old_name, const std::string& new_name
303 auto o = this->m_ExposedInputs.find( old_name );
304 auto n = this->m_ExposedInputs.find( new_name );
305 if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
307 this->m_ExposedInputs[ new_name ] = o->second;
308 this->m_ExposedInputs.erase( o );
315 // -------------------------------------------------------------------------
316 bool cpPlugins::Interface::Workspace::
318 const std::string& old_name, const std::string& new_name
321 auto o = this->m_ExposedOutputs.find( old_name );
322 auto n = this->m_ExposedOutputs.find( new_name );
324 o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
327 this->m_ExposedOutputs[ new_name ] = o->second;
328 this->m_ExposedOutputs.erase( o );
335 // -------------------------------------------------------------------------
336 std::vector< std::pair< std::string, std::string > >
337 cpPlugins::Interface::Workspace::
339 const std::string& origin, const std::string& destination
342 std::vector< std::pair< std::string, std::string > > conns;
343 auto orig = this->GetFilter( origin );
344 auto dest = this->GetFilter( destination );
345 if( orig != NULL && dest != NULL )
347 auto outs = orig->GetOutputsNames( );
348 auto ins = dest->GetInputsNames( );
349 for( auto o = outs.begin( ); o != outs.end( ); ++o )
351 for( auto i = ins.begin( ); i != ins.end( ); ++i )
353 auto od = orig->GetOutput( *o );
354 auto id = dest->GetInput( *i );
355 if( od != NULL && od == id )
357 std::pair< std::string, std::string >( *o, *i )
368 // -------------------------------------------------------------------------
369 void cpPlugins::Interface::Workspace::
371 const std::string& orig_filter, const std::string& dest_filter,
372 const std::string& output_name, const std::string& input_name
375 auto o = this->GetFilter( orig_filter );
376 auto d = this->GetFilter( dest_filter );
377 if( o != NULL && d != NULL )
381 d->AddInput( input_name, o->GetOutput( output_name ) );
383 catch( std::exception& err )
385 throw std::logic_error(
386 std::string( "Error connecting \"" ) +
387 output_name + std::string( "@" ) + orig_filter +
388 std::string( "\" with \"" ) +
389 input_name + std::string( "@" ) + dest_filter +
390 std::string( "\": " ) +
399 // -------------------------------------------------------------------------
400 void cpPlugins::Interface::Workspace::
402 cpPlugins::BaseObjects::DataObject* output,
403 const std::string& dest_filter, const std::string& input_name
406 auto d = this->GetFilter( dest_filter );
408 d->AddInput( input_name, output );
411 // -------------------------------------------------------------------------
412 void cpPlugins::Interface::Workspace::
414 cpPlugins::BaseObjects::DataObject* output,
415 const std::string& exposed_input_name
418 auto i = this->m_ExposedInputs.find( exposed_input_name );
419 if( i != this->m_ExposedInputs.end( ) )
420 this->Connect( output, i->second.first, i->second.second );
423 // -------------------------------------------------------------------------
424 void cpPlugins::Interface::Workspace::
426 const std::string& orig_filter, const std::string& dest_filter,
427 const std::string& output_name, const std::string& input_name
430 auto orig = this->GetFilter( orig_filter );
431 auto dest = this->GetFilter( dest_filter );
432 if( orig != NULL && dest != NULL )
434 auto out = orig->GetOutput( output_name );
435 auto in = dest->GetInput( input_name );
436 if( out != NULL && out == in )
438 input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
444 // -------------------------------------------------------------------------
445 void cpPlugins::Interface::Workspace::
447 const std::string& dest_filter, const std::string& input_name
450 throw std::logic_error( "Disconnect 1" );
453 // -------------------------------------------------------------------------
454 void cpPlugins::Interface::Workspace::
455 Disconnect( const std::string& dest_filter )
457 throw std::logic_error( "Disconnect 2" );
460 // -------------------------------------------------------------------------
461 void cpPlugins::Interface::Workspace::
464 for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
465 f->second->Update( );
468 // -------------------------------------------------------------------------
469 void cpPlugins::Interface::Workspace::
470 Execute( const std::string& name )
472 auto filter = this->GetFilter( name );
477 // -------------------------------------------------------------------------
478 cpPlugins::Interface::Workspace::
481 m_PrintExecution( false )
485 // -------------------------------------------------------------------------
486 cpPlugins::Interface::Workspace::
489 this->m_ExposedOutputs.clear( );
490 this->m_ExposedInputs.clear( );
491 this->m_Filters.clear( );