1 #include <cpPlugins/Interface/Workspace.h>
2 #include <cpPlugins/Pipeline/Widget.h>
4 // -------------------------------------------------------------------------
5 void cpPlugins::Interface::Workspace::
8 this->m_Filters.clear( );
11 // -------------------------------------------------------------------------
12 std::vector< std::string > cpPlugins::Interface::Workspace::
13 GetFiltersNames( ) const
15 std::vector< std::string > n;
16 for( auto i : this->m_Filters )
17 n.push_back( i.first );
21 // -------------------------------------------------------------------------
22 cpPlugins::Interface::Workspace::
23 TFilter* cpPlugins::Interface::Workspace::
24 GetFilter( const std::string& name )
26 auto i = this->m_Filters.find( name );
27 if( i != this->m_Filters.end( ) )
28 return( i->second.GetPointer( ) );
33 // -------------------------------------------------------------------------
34 const cpPlugins::Interface::Workspace::
35 TFilter* cpPlugins::Interface::Workspace::
36 GetFilter( const std::string& name ) const
38 auto i = this->m_Filters.find( name );
39 if( i != this->m_Filters.end( ) )
40 return( i->second.GetPointer( ) );
45 // -------------------------------------------------------------------------
46 cpPlugins::Interface::Workspace::
47 TWidget* cpPlugins::Interface::Workspace::
48 GetWidget( const std::string& name )
50 TFilter* process = this->GetFilter( name );
51 return( dynamic_cast< TWidget* >( process ) );
54 // -------------------------------------------------------------------------
55 const cpPlugins::Interface::Workspace::
56 TWidget* cpPlugins::Interface::Workspace::
57 GetWidget( const std::string& name ) const
59 const TFilter* process = this->GetFilter( name );
60 return( dynamic_cast< const TWidget* >( process ) );
63 // -------------------------------------------------------------------------
64 bool cpPlugins::Interface::Workspace::
65 HasFilter( const std::string& name ) const
67 return( this->m_Filters.find( name ) != this->m_Filters.end( ) );
70 // -------------------------------------------------------------------------
71 bool cpPlugins::Interface::Workspace::
72 HasWidget( const std::string& name ) const
74 const TWidget* wdg = this->GetWidget( name );
75 return( wdg != NULL );
78 // -------------------------------------------------------------------------
79 cpPlugins::Interface::Workspace::
80 TFilter* cpPlugins::Interface::Workspace::
81 CreateFilter( const std::string& category, const std::string& filter )
83 typedef cpPlugins::Pipeline::Widget _TWidget;
85 TFilter::Pointer o = this->m_Loader.CreateFilter( category, filter );
89 std::string name = filter;
90 while( this->GetFilter( name ) != NULL )
91 name += std::string( "_" );
92 o->SetPrintExecution( this->m_PrintExecution );
97 auto i = this->m_Interactors.begin( );
98 i != this->m_Interactors.end( );
101 o->AddInteractor( *i );
103 // Finish association
104 this->m_Filters[ name ] = o;
107 return( o.GetPointer( ) );
110 // -------------------------------------------------------------------------
111 bool cpPlugins::Interface::Workspace::
112 RenameFilter( const std::string& old_name, const std::string& new_name )
114 auto o = this->m_Filters.find( old_name );
115 auto n = this->m_Filters.find( new_name );
116 if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) )
119 o->second->SetName( new_name );
120 this->m_Filters[ new_name ] = o->second;
121 this->m_Filters.erase( o );
123 // Rename exposed ports
125 auto e = this->m_ExposedInputs.begin( );
126 for( ; e != this->m_ExposedInputs.end( ); ++e )
127 if( e->second.first == old_name )
128 e->second.first = new_name;
129 e = this->m_ExposedOutputs.begin( );
130 for( ; e != this->m_ExposedOutputs.end( ); ++e )
131 if( e->second.first == old_name )
132 e->second.first = new_name;
141 // -------------------------------------------------------------------------
142 bool cpPlugins::Interface::Workspace::
143 RemoveFilter( const std::string& name )
145 auto i = this->m_Filters.find( name );
146 if( i != this->m_Filters.end( ) )
148 i->second->Disconnect( );
149 this->m_Filters.erase( i );
156 // -------------------------------------------------------------------------
157 void cpPlugins::Interface::Workspace::
158 SetPrintExecution( bool b )
160 this->m_PrintExecution = b;
161 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
162 i->second->SetPrintExecution( b );
165 // -------------------------------------------------------------------------
166 void cpPlugins::Interface::Workspace::
169 this->SetPrintExecution( true );
172 // -------------------------------------------------------------------------
173 void cpPlugins::Interface::Workspace::
176 this->SetPrintExecution( true );
179 // -------------------------------------------------------------------------
180 void cpPlugins::Interface::Workspace::
181 AddInteractor( vtkRenderWindowInteractor* iren )
185 this->m_Interactors.insert( iren );
186 for( auto f : this->m_Filters )
187 f.second->AddInteractor( iren );
192 // -------------------------------------------------------------------------
193 bool cpPlugins::Interface::Workspace::
195 const std::string& origin_filter,
196 const std::string& origin_output,
197 const std::string& destination_filter,
198 const std::string& destination_input
201 // Get filters and check pertinence
202 TFilter* origin = this->GetFilter( origin_filter );
203 TFilter* destination = this->GetFilter( destination_filter );
204 if( origin == NULL || destination == NULL )
206 if( !( destination->HasInput( destination_input ) ) )
208 if( !( origin->HasOutput( origin_output ) ) )
211 // Check if there is room for a new connection
213 if( destination->IsInputMultiple( destination_input ) )
217 i < destination->GetInputSize( destination_input );
221 destination->GetInput( destination_input, i )->GetSource( ) == origin
226 ok = ( destination->GetInput( destination_input ) == NULL );
228 destination->AddInput(
230 origin->GetOutput( origin_output )
235 // -------------------------------------------------------------------------
236 bool cpPlugins::Interface::Workspace::
239 const std::string& destination_filter,
240 const std::string& destination_input
243 // Get filters and check pertinence
246 TFilter* destination = this->GetFilter( destination_filter );
247 if( destination == NULL )
249 if( !( destination->HasInput( destination_input ) ) )
252 // Check if there is room for a new connection
254 if( destination->IsInputMultiple( destination_input ) )
258 i < destination->GetInputSize( destination_input );
262 destination->GetInput( destination_input, i )->GetSource( ) ==
268 ok = ( destination->GetInput( destination_input ) == NULL );
270 destination->AddInput(
277 // -------------------------------------------------------------------------
278 bool cpPlugins::Interface::Workspace::
280 const std::string& origin_filter,
281 const std::string& origin_output,
282 const std::string& destination_filter,
283 const std::string& destination_input
286 // Get filters and check pertinence
287 TFilter* origin = this->GetFilter( origin_filter );
288 TFilter* destination = this->GetFilter( destination_filter );
289 if( origin == NULL || destination == NULL )
291 if( !( destination->HasInput( destination_input ) ) )
293 if( !( origin->HasOutput( origin_output ) ) )
296 // Check if there is room for a new connection
298 unsigned int del_id = 0;
301 i < destination->GetInputSize( destination_input );
305 destination->GetInput( destination_input, i )->GetSource( ) == origin
313 destination->DisconnectInput( destination_input, del_id );
317 // -------------------------------------------------------------------------
319 const cpPlugins::Interface::Workspace::
320 TExposedPorts& cpPlugins::Interface::Workspace::
321 GetExposedInputs( ) const
323 return( this->m_ExposedInputs );
326 // -------------------------------------------------------------------------
327 const cpPlugins::Interface::Workspace::
328 TExposedPorts& cpPlugins::Interface::Workspace::
329 GetExposedOutputs( ) const
331 return( this->m_ExposedOutputs );
334 // -------------------------------------------------------------------------
335 cpPlugins::Pipeline::DataObject* cpPlugins::Interface::Workspace::
336 GetExposedOutput( const std::string& name )
338 auto i = this->m_ExposedOutputs.find( name );
339 if( i != this->m_ExposedOutputs.end( ) )
341 auto f = this->GetFilter( i->second.first );
343 return( f->GetOutput( i->second.second ) );
351 // -------------------------------------------------------------------------
352 const cpPlugins::Pipeline::DataObject* cpPlugins::Interface::Workspace::
353 GetExposedOutput( const std::string& name ) const
355 auto i = this->m_ExposedOutputs.find( name );
356 if( i != this->m_ExposedOutputs.end( ) )
358 auto f = this->GetFilter( i->second.first );
360 return( f->GetOutput( i->second.second ) );
368 // -------------------------------------------------------------------------
369 bool cpPlugins::Interface::Workspace::
371 const std::string& name,
372 const std::string& filter, const std::string& filter_input
375 auto i = this->m_ExposedInputs.find( name );
376 if( i == this->m_ExposedInputs.end( ) )
378 this->m_ExposedInputs[ name ] =
379 std::pair< std::string, std::string >( filter, filter_input );
386 // -------------------------------------------------------------------------
387 bool cpPlugins::Interface::Workspace::
389 const std::string& name,
390 const std::string& filter, const std::string& filter_output
393 auto i = this->m_ExposedOutputs.find( name );
394 if( i == this->m_ExposedOutputs.end( ) )
396 this->m_ExposedOutputs[ name ] =
397 std::pair< std::string, std::string >( filter, filter_output );
404 // -------------------------------------------------------------------------
405 void cpPlugins::Interface::Workspace::
406 HideInput( const std::string& name )
408 auto i = this->m_ExposedInputs.find( name );
409 if( i != this->m_ExposedInputs.end( ) )
410 this->m_ExposedInputs.erase( i );
413 // -------------------------------------------------------------------------
414 void cpPlugins::Interface::Workspace::
415 HideOutput( const std::string& name )
417 auto i = this->m_ExposedOutputs.find( name );
418 if( i != this->m_ExposedOutputs.end( ) )
419 this->m_ExposedOutputs.erase( i );
422 // -------------------------------------------------------------------------
423 bool cpPlugins::Interface::Workspace::
425 const std::string& old_name, const std::string& new_name
428 auto o = this->m_ExposedInputs.find( old_name );
429 auto n = this->m_ExposedInputs.find( new_name );
430 if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
432 this->m_ExposedInputs[ new_name ] = o->second;
433 this->m_ExposedInputs.erase( o );
440 // -------------------------------------------------------------------------
441 bool cpPlugins::Interface::Workspace::
443 const std::string& old_name, const std::string& new_name
446 auto o = this->m_ExposedOutputs.find( old_name );
447 auto n = this->m_ExposedOutputs.find( new_name );
449 o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
452 this->m_ExposedOutputs[ new_name ] = o->second;
453 this->m_ExposedOutputs.erase( o );
460 // -------------------------------------------------------------------------
461 std::vector< std::pair< std::string, std::string > >
462 cpPlugins::Interface::Workspace::
464 const std::string& origin, const std::string& destination
467 std::vector< std::pair< std::string, std::string > > conns;
468 auto orig = this->GetFilter( origin );
469 auto dest = this->GetFilter( destination );
470 if( orig != NULL && dest != NULL )
472 auto outs = orig->GetOutputsNames( );
473 auto ins = dest->GetInputsNames( );
474 for( auto o = outs.begin( ); o != outs.end( ); ++o )
476 for( auto i = ins.begin( ); i != ins.end( ); ++i )
478 unsigned int nInputs = dest->GetInputSize( *i );
479 for( unsigned j = 0; j < nInputs; ++j )
481 auto od = orig->GetOutput( *o );
482 auto id = dest->GetInput( *i, j );
483 if( od != NULL && od == id )
485 std::pair< std::string, std::string >( *o, *i )
498 // -------------------------------------------------------------------------
499 void cpPlugins::Interface::Workspace::
501 const std::string& orig_filter, const std::string& dest_filter,
502 const std::string& output_name, const std::string& input_name
505 auto o = this->GetFilter( orig_filter );
506 auto d = this->GetFilter( dest_filter );
507 if( o != NULL && d != NULL )
511 d->AddInput( input_name, o->GetOutput( output_name ) );
513 catch( std::exception& err )
515 throw std::logic_error(
516 std::string( "Error connecting \"" ) +
517 output_name + std::string( "@" ) + orig_filter +
518 std::string( "\" with \"" ) +
519 input_name + std::string( "@" ) + dest_filter +
520 std::string( "\": " ) +
529 // -------------------------------------------------------------------------
530 void cpPlugins::Interface::Workspace::
532 cpPlugins::Pipeline::DataObject* output,
533 const std::string& dest_filter, const std::string& input_name
536 auto d = this->GetFilter( dest_filter );
538 d->AddInput( input_name, output );
541 // -------------------------------------------------------------------------
542 void cpPlugins::Interface::Workspace::
544 cpPlugins::Pipeline::DataObject* output,
545 const std::string& exposed_input_name
548 auto i = this->m_ExposedInputs.find( exposed_input_name );
549 if( i != this->m_ExposedInputs.end( ) )
550 this->Connect( output, i->second.first, i->second.second );
553 // -------------------------------------------------------------------------
554 void cpPlugins::Interface::Workspace::
556 const std::string& orig_filter, const std::string& dest_filter,
557 const std::string& output_name, const std::string& input_name
560 auto orig = this->GetFilter( orig_filter );
561 auto dest = this->GetFilter( dest_filter );
562 if( orig != NULL && dest != NULL )
564 auto out = orig->GetOutput( output_name );
565 auto in = dest->GetInput( input_name );
566 if( out != NULL && out == in )
568 input_name, ( cpPlugins::Pipeline::DataObject* )( NULL )
574 // -------------------------------------------------------------------------
575 void cpPlugins::Interface::Workspace::
577 const std::string& dest_filter, const std::string& input_name
580 throw std::logic_error( "Disconnect 1" );
583 // -------------------------------------------------------------------------
584 void cpPlugins::Interface::Workspace::
585 Disconnect( const std::string& dest_filter )
587 throw std::logic_error( "Disconnect 2" );
591 // -------------------------------------------------------------------------
592 void cpPlugins::Interface::Workspace::
595 for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
596 f->second->Update( );
599 // -------------------------------------------------------------------------
600 void cpPlugins::Interface::Workspace::
601 Update( const std::string& name )
603 auto filter = this->GetFilter( name );
608 // -------------------------------------------------------------------------
609 cpPlugins::Interface::Workspace::
612 m_PrintExecution( false )
616 // -------------------------------------------------------------------------
617 cpPlugins::Interface::Workspace::
621 this->m_ExposedOutputs.clear( );
622 this->m_ExposedInputs.clear( );
624 this->m_Filters.clear( );