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::
82 const std::string& category, const std::string& filter,
83 const std::string& name
86 typedef cpPlugins::Pipeline::Widget _TWidget;
88 TFilter::Pointer o = this->m_Loader.CreateFilter( category, filter );
92 std::string real_name = name;
93 while( this->GetFilter( real_name ) != NULL )
94 real_name += std::string( "_" );
95 o->SetPrintExecution( this->m_PrintExecution );
96 o->SetName( real_name );
100 auto i = this->m_Interactors.begin( );
101 i != this->m_Interactors.end( );
104 o->AddInteractor( *i );
106 // Finish association
107 this->m_Filters[ real_name ] = o;
110 return( o.GetPointer( ) );
113 // -------------------------------------------------------------------------
114 cpPlugins::Interface::Workspace::
115 TFilter* cpPlugins::Interface::Workspace::
116 CreateFilter( const std::string& category, const std::string& filter )
118 return( this->CreateFilter( category, filter, filter ) );
121 // -------------------------------------------------------------------------
122 bool cpPlugins::Interface::Workspace::
123 RenameFilter( const std::string& old_name, const std::string& new_name )
125 auto o = this->m_Filters.find( old_name );
126 auto n = this->m_Filters.find( new_name );
127 if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) )
130 o->second->SetName( new_name );
131 this->m_Filters[ new_name ] = o->second;
132 this->m_Filters.erase( o );
134 // Rename exposed ports
136 auto e = this->m_ExposedInputs.begin( );
137 for( ; e != this->m_ExposedInputs.end( ); ++e )
138 if( e->second.first == old_name )
139 e->second.first = new_name;
140 e = this->m_ExposedOutputs.begin( );
141 for( ; e != this->m_ExposedOutputs.end( ); ++e )
142 if( e->second.first == old_name )
143 e->second.first = new_name;
152 // -------------------------------------------------------------------------
153 bool cpPlugins::Interface::Workspace::
154 RemoveFilter( const std::string& name )
156 auto i = this->m_Filters.find( name );
157 if( i != this->m_Filters.end( ) )
159 i->second->Disconnect( );
160 this->m_Filters.erase( i );
167 // -------------------------------------------------------------------------
168 void cpPlugins::Interface::Workspace::
169 SetPrintExecution( bool b )
171 this->m_PrintExecution = b;
172 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
173 i->second->SetPrintExecution( b );
176 // -------------------------------------------------------------------------
177 void cpPlugins::Interface::Workspace::
180 this->SetPrintExecution( true );
183 // -------------------------------------------------------------------------
184 void cpPlugins::Interface::Workspace::
187 this->SetPrintExecution( true );
190 // -------------------------------------------------------------------------
191 void cpPlugins::Interface::Workspace::
192 AddInteractor( vtkRenderWindowInteractor* iren )
196 this->m_Interactors.insert( iren );
197 for( auto f : this->m_Filters )
198 f.second->AddInteractor( iren );
203 // -------------------------------------------------------------------------
204 bool cpPlugins::Interface::Workspace::
206 const std::string& origin_filter,
207 const std::string& origin_output,
208 const std::string& destination_filter,
209 const std::string& destination_input
212 // Get filters and check pertinence
213 TFilter* origin = this->GetFilter( origin_filter );
214 TFilter* destination = this->GetFilter( destination_filter );
215 if( origin == NULL || destination == NULL )
217 if( !( destination->HasInput( destination_input ) ) )
219 if( !( origin->HasOutput( origin_output ) ) )
222 // Check if there is room for a new connection
224 if( destination->IsInputMultiple( destination_input ) )
228 i < destination->GetInputSize( destination_input );
232 destination->GetInput( destination_input, i )->GetSource( ) == origin
237 ok = ( destination->GetInput( destination_input ) == NULL );
239 destination->AddInput(
241 origin->GetOutput( origin_output )
246 // -------------------------------------------------------------------------
247 bool cpPlugins::Interface::Workspace::
250 const std::string& destination_filter,
251 const std::string& destination_input
254 // Get filters and check pertinence
257 TFilter* destination = this->GetFilter( destination_filter );
258 if( destination == NULL )
260 if( !( destination->HasInput( destination_input ) ) )
263 // Check if there is room for a new connection
265 if( destination->IsInputMultiple( destination_input ) )
269 i < destination->GetInputSize( destination_input );
273 destination->GetInput( destination_input, i )->GetSource( ) ==
279 ok = ( destination->GetInput( destination_input ) == NULL );
281 destination->AddInput(
288 // -------------------------------------------------------------------------
289 bool cpPlugins::Interface::Workspace::
291 const std::string& origin_filter,
292 const std::string& origin_output,
293 const std::string& destination_filter,
294 const std::string& destination_input
297 // Get filters and check pertinence
298 TFilter* origin = this->GetFilter( origin_filter );
299 TFilter* destination = this->GetFilter( destination_filter );
300 if( origin == NULL || destination == NULL )
302 if( !( destination->HasInput( destination_input ) ) )
304 if( !( origin->HasOutput( origin_output ) ) )
307 // Check if there is room for a new connection
309 unsigned int del_id = 0;
312 i < destination->GetInputSize( destination_input );
316 destination->GetInput( destination_input, i )->GetSource( ) == origin
324 destination->DisconnectInput( destination_input, del_id );
328 // -------------------------------------------------------------------------
330 const cpPlugins::Interface::Workspace::
331 TExposedPorts& cpPlugins::Interface::Workspace::
332 GetExposedInputs( ) const
334 return( this->m_ExposedInputs );
337 // -------------------------------------------------------------------------
338 const cpPlugins::Interface::Workspace::
339 TExposedPorts& cpPlugins::Interface::Workspace::
340 GetExposedOutputs( ) const
342 return( this->m_ExposedOutputs );
345 // -------------------------------------------------------------------------
346 cpPlugins::Pipeline::DataObject* cpPlugins::Interface::Workspace::
347 GetExposedOutput( const std::string& name )
349 auto i = this->m_ExposedOutputs.find( name );
350 if( i != this->m_ExposedOutputs.end( ) )
352 auto f = this->GetFilter( i->second.first );
354 return( f->GetOutput( i->second.second ) );
362 // -------------------------------------------------------------------------
363 const cpPlugins::Pipeline::DataObject* cpPlugins::Interface::Workspace::
364 GetExposedOutput( const std::string& name ) const
366 auto i = this->m_ExposedOutputs.find( name );
367 if( i != this->m_ExposedOutputs.end( ) )
369 auto f = this->GetFilter( i->second.first );
371 return( f->GetOutput( i->second.second ) );
379 // -------------------------------------------------------------------------
380 bool cpPlugins::Interface::Workspace::
382 const std::string& name,
383 const std::string& filter, const std::string& filter_input
386 auto i = this->m_ExposedInputs.find( name );
387 if( i == this->m_ExposedInputs.end( ) )
389 this->m_ExposedInputs[ name ] =
390 std::pair< std::string, std::string >( filter, filter_input );
397 // -------------------------------------------------------------------------
398 bool cpPlugins::Interface::Workspace::
400 const std::string& name,
401 const std::string& filter, const std::string& filter_output
404 auto i = this->m_ExposedOutputs.find( name );
405 if( i == this->m_ExposedOutputs.end( ) )
407 this->m_ExposedOutputs[ name ] =
408 std::pair< std::string, std::string >( filter, filter_output );
415 // -------------------------------------------------------------------------
416 void cpPlugins::Interface::Workspace::
417 HideInput( const std::string& name )
419 auto i = this->m_ExposedInputs.find( name );
420 if( i != this->m_ExposedInputs.end( ) )
421 this->m_ExposedInputs.erase( i );
424 // -------------------------------------------------------------------------
425 void cpPlugins::Interface::Workspace::
426 HideOutput( const std::string& name )
428 auto i = this->m_ExposedOutputs.find( name );
429 if( i != this->m_ExposedOutputs.end( ) )
430 this->m_ExposedOutputs.erase( i );
433 // -------------------------------------------------------------------------
434 bool cpPlugins::Interface::Workspace::
436 const std::string& old_name, const std::string& new_name
439 auto o = this->m_ExposedInputs.find( old_name );
440 auto n = this->m_ExposedInputs.find( new_name );
441 if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
443 this->m_ExposedInputs[ new_name ] = o->second;
444 this->m_ExposedInputs.erase( o );
451 // -------------------------------------------------------------------------
452 bool cpPlugins::Interface::Workspace::
454 const std::string& old_name, const std::string& new_name
457 auto o = this->m_ExposedOutputs.find( old_name );
458 auto n = this->m_ExposedOutputs.find( new_name );
460 o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
463 this->m_ExposedOutputs[ new_name ] = o->second;
464 this->m_ExposedOutputs.erase( o );
471 // -------------------------------------------------------------------------
472 std::vector< std::pair< std::string, std::string > >
473 cpPlugins::Interface::Workspace::
475 const std::string& origin, const std::string& destination
478 std::vector< std::pair< std::string, std::string > > conns;
479 auto orig = this->GetFilter( origin );
480 auto dest = this->GetFilter( destination );
481 if( orig != NULL && dest != NULL )
483 auto outs = orig->GetOutputsNames( );
484 auto ins = dest->GetInputsNames( );
485 for( auto o = outs.begin( ); o != outs.end( ); ++o )
487 for( auto i = ins.begin( ); i != ins.end( ); ++i )
489 unsigned int nInputs = dest->GetInputSize( *i );
490 for( unsigned j = 0; j < nInputs; ++j )
492 auto od = orig->GetOutput( *o );
493 auto id = dest->GetInput( *i, j );
494 if( od != NULL && od == id )
496 std::pair< std::string, std::string >( *o, *i )
509 // -------------------------------------------------------------------------
510 void cpPlugins::Interface::Workspace::
512 const std::string& orig_filter, const std::string& dest_filter,
513 const std::string& output_name, const std::string& input_name
516 auto o = this->GetFilter( orig_filter );
517 auto d = this->GetFilter( dest_filter );
518 if( o != NULL && d != NULL )
522 d->AddInput( input_name, o->GetOutput( output_name ) );
524 catch( std::exception& err )
526 throw std::logic_error(
527 std::string( "Error connecting \"" ) +
528 output_name + std::string( "@" ) + orig_filter +
529 std::string( "\" with \"" ) +
530 input_name + std::string( "@" ) + dest_filter +
531 std::string( "\": " ) +
540 // -------------------------------------------------------------------------
541 void cpPlugins::Interface::Workspace::
543 cpPlugins::Pipeline::DataObject* output,
544 const std::string& dest_filter, const std::string& input_name
547 auto d = this->GetFilter( dest_filter );
549 d->AddInput( input_name, output );
552 // -------------------------------------------------------------------------
553 void cpPlugins::Interface::Workspace::
555 cpPlugins::Pipeline::DataObject* output,
556 const std::string& exposed_input_name
559 auto i = this->m_ExposedInputs.find( exposed_input_name );
560 if( i != this->m_ExposedInputs.end( ) )
561 this->Connect( output, i->second.first, i->second.second );
564 // -------------------------------------------------------------------------
565 void cpPlugins::Interface::Workspace::
567 const std::string& orig_filter, const std::string& dest_filter,
568 const std::string& output_name, const std::string& input_name
571 auto orig = this->GetFilter( orig_filter );
572 auto dest = this->GetFilter( dest_filter );
573 if( orig != NULL && dest != NULL )
575 auto out = orig->GetOutput( output_name );
576 auto in = dest->GetInput( input_name );
577 if( out != NULL && out == in )
579 input_name, ( cpPlugins::Pipeline::DataObject* )( NULL )
585 // -------------------------------------------------------------------------
586 void cpPlugins::Interface::Workspace::
588 const std::string& dest_filter, const std::string& input_name
591 throw std::logic_error( "Disconnect 1" );
594 // -------------------------------------------------------------------------
595 void cpPlugins::Interface::Workspace::
596 Disconnect( const std::string& dest_filter )
598 throw std::logic_error( "Disconnect 2" );
602 // -------------------------------------------------------------------------
603 void cpPlugins::Interface::Workspace::
606 for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
607 f->second->Update( );
610 // -------------------------------------------------------------------------
611 void cpPlugins::Interface::Workspace::
612 Update( const std::string& name )
614 auto filter = this->GetFilter( name );
619 // -------------------------------------------------------------------------
620 cpPlugins::Interface::Workspace::
623 m_PrintExecution( false )
627 // -------------------------------------------------------------------------
628 cpPlugins::Interface::Workspace::
632 this->m_ExposedOutputs.clear( );
633 this->m_ExposedInputs.clear( );
635 this->m_Filters.clear( );