1 #include <cpPlugins/Interface/Workspace.h>
2 #include <cpPlugins/BaseObjects/Widget.h>
4 // -------------------------------------------------------------------------
5 cpPlugins::Interface::Workspace::
6 Pointer cpPlugins::Interface::Workspace::
9 Pointer smartPtr = new Self;
10 smartPtr->UnRegister( );
14 // -------------------------------------------------------------------------
15 itk::LightObject::Pointer cpPlugins::Interface::Workspace::
16 CreateAnother( ) const
18 itk::LightObject::Pointer smartPtr;
19 smartPtr = Self::New( ).GetPointer( );
23 // -------------------------------------------------------------------------
24 cpPlugins::Interface::Workspace::
25 Pointer cpPlugins::Interface::Workspace::
29 dynamic_cast< Self* >( this->InternalClone( ).GetPointer( ) );
33 // -------------------------------------------------------------------------
34 void cpPlugins::Interface::Workspace::
37 this->m_Filters.clear( );
40 // -------------------------------------------------------------------------
41 std::vector< std::string > cpPlugins::Interface::Workspace::
42 GetFiltersNames( ) const
44 std::vector< std::string > n;
45 for( auto i : this->m_Filters )
46 n.push_back( i.first );
50 // -------------------------------------------------------------------------
51 cpPlugins::Interface::Workspace::
52 TFilter* cpPlugins::Interface::Workspace::
53 GetFilter( const std::string& name )
55 auto i = this->m_Filters.find( name );
56 if( i != this->m_Filters.end( ) )
57 return( i->second.GetPointer( ) );
62 // -------------------------------------------------------------------------
63 const cpPlugins::Interface::Workspace::
64 TFilter* cpPlugins::Interface::Workspace::
65 GetFilter( const std::string& name ) const
67 auto i = this->m_Filters.find( name );
68 if( i != this->m_Filters.end( ) )
69 return( i->second.GetPointer( ) );
74 // -------------------------------------------------------------------------
75 cpPlugins::Interface::Workspace::
76 TWidget* cpPlugins::Interface::Workspace::
77 GetWidget( const std::string& name )
79 TFilter* process = this->GetFilter( name );
80 return( dynamic_cast< TWidget* >( process ) );
83 // -------------------------------------------------------------------------
84 const cpPlugins::Interface::Workspace::
85 TWidget* cpPlugins::Interface::Workspace::
86 GetWidget( const std::string& name ) const
88 const TFilter* process = this->GetFilter( name );
89 return( dynamic_cast< const TWidget* >( process ) );
92 // -------------------------------------------------------------------------
93 bool cpPlugins::Interface::Workspace::
94 HasFilter( const std::string& name ) const
96 return( this->m_Filters.find( name ) != this->m_Filters.end( ) );
99 // -------------------------------------------------------------------------
100 bool cpPlugins::Interface::Workspace::
101 HasWidget( const std::string& name ) const
103 const TWidget* wdg = this->GetWidget( name );
104 return( wdg != NULL );
107 // -------------------------------------------------------------------------
108 cpPlugins::Interface::Workspace::
109 TFilter* cpPlugins::Interface::Workspace::
110 CreateFilter( const std::string& category, const std::string& filter )
112 typedef cpPlugins::BaseObjects::Widget _TWidget;
114 TFilter::Pointer o = this->m_Plugins->CreateFilter( category, filter );
118 std::string name = filter;
119 while( this->GetFilter( name ) != NULL )
120 name += std::string( "_" );
121 o->SetPrintExecution( this->m_PrintExecution );
126 auto i = this->m_Interactors.begin( );
127 i != this->m_Interactors.end( );
130 o->AddInteractor( *i );
132 // Finish association
133 this->m_Filters[ name ] = o;
136 return( o.GetPointer( ) );
139 // -------------------------------------------------------------------------
140 bool cpPlugins::Interface::Workspace::
141 RenameFilter( const std::string& old_name, const std::string& new_name )
143 auto o = this->m_Filters.find( old_name );
144 auto n = this->m_Filters.find( new_name );
145 if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) )
148 o->second->SetName( new_name );
149 this->m_Filters[ new_name ] = o->second;
150 this->m_Filters.erase( o );
152 // Rename exposed ports
154 auto e = this->m_ExposedInputs.begin( );
155 for( ; e != this->m_ExposedInputs.end( ); ++e )
156 if( e->second.first == old_name )
157 e->second.first = new_name;
158 e = this->m_ExposedOutputs.begin( );
159 for( ; e != this->m_ExposedOutputs.end( ); ++e )
160 if( e->second.first == old_name )
161 e->second.first = new_name;
170 // -------------------------------------------------------------------------
171 bool cpPlugins::Interface::Workspace::
172 RemoveFilter( const std::string& name )
174 auto i = this->m_Filters.find( name );
175 if( i != this->m_Filters.end( ) )
177 i->second->Disconnect( );
178 this->m_Filters.erase( i );
185 // -------------------------------------------------------------------------
186 void cpPlugins::Interface::Workspace::
187 SetPrintExecution( bool b )
189 this->m_PrintExecution = b;
190 for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
191 i->second->SetPrintExecution( b );
194 // -------------------------------------------------------------------------
195 void cpPlugins::Interface::Workspace::
198 this->SetPrintExecution( true );
201 // -------------------------------------------------------------------------
202 void cpPlugins::Interface::Workspace::
205 this->SetPrintExecution( true );
208 // -------------------------------------------------------------------------
209 void cpPlugins::Interface::Workspace::
210 AddInteractor( vtkRenderWindowInteractor* iren )
214 this->m_Interactors.insert( iren );
215 for( auto f : this->m_Filters )
216 f.second->AddInteractor( iren );
221 // -------------------------------------------------------------------------
222 bool cpPlugins::Interface::Workspace::
224 const std::string& origin_filter,
225 const std::string& origin_output,
226 const std::string& destination_filter,
227 const std::string& destination_input
230 // Get filters and check pertinence
231 TFilter* origin = this->GetFilter( origin_filter );
232 TFilter* destination = this->GetFilter( destination_filter );
233 if( origin == NULL || destination == NULL )
235 if( !( destination->HasInput( destination_input ) ) )
237 if( !( origin->HasOutput( origin_output ) ) )
240 // Check if there is room for a new connection
242 if( destination->IsInputMultiple( destination_input ) )
246 i < destination->GetInputSize( destination_input );
250 destination->GetInput( destination_input, i )->GetSource( ) == origin
255 ok = ( destination->GetInput( destination_input ) == NULL );
257 destination->AddInput(
259 origin->GetOutput( origin_output )
264 // -------------------------------------------------------------------------
265 bool cpPlugins::Interface::Workspace::
268 const std::string& destination_filter,
269 const std::string& destination_input
272 // Get filters and check pertinence
275 TFilter* destination = this->GetFilter( destination_filter );
276 if( destination == NULL )
278 if( !( destination->HasInput( destination_input ) ) )
281 // Check if there is room for a new connection
283 if( destination->IsInputMultiple( destination_input ) )
287 i < destination->GetInputSize( destination_input );
291 destination->GetInput( destination_input, i )->GetSource( ) ==
297 ok = ( destination->GetInput( destination_input ) == NULL );
299 destination->AddInput(
306 // -------------------------------------------------------------------------
307 bool cpPlugins::Interface::Workspace::
309 const std::string& origin_filter,
310 const std::string& origin_output,
311 const std::string& destination_filter,
312 const std::string& destination_input
315 // Get filters and check pertinence
316 TFilter* origin = this->GetFilter( origin_filter );
317 TFilter* destination = this->GetFilter( destination_filter );
318 if( origin == NULL || destination == NULL )
320 if( !( destination->HasInput( destination_input ) ) )
322 if( !( origin->HasOutput( origin_output ) ) )
325 // Check if there is room for a new connection
327 unsigned int del_id = 0;
330 i < destination->GetInputSize( destination_input );
334 destination->GetInput( destination_input, i )->GetSource( ) == origin
342 destination->DisconnectInput( destination_input, del_id );
346 // -------------------------------------------------------------------------
348 const cpPlugins::Interface::Workspace::
349 TExposedPorts& cpPlugins::Interface::Workspace::
350 GetExposedInputs( ) const
352 return( this->m_ExposedInputs );
355 // -------------------------------------------------------------------------
356 const cpPlugins::Interface::Workspace::
357 TExposedPorts& cpPlugins::Interface::Workspace::
358 GetExposedOutputs( ) const
360 return( this->m_ExposedOutputs );
363 // -------------------------------------------------------------------------
364 cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
365 GetExposedOutput( const std::string& name )
367 auto i = this->m_ExposedOutputs.find( name );
368 if( i != this->m_ExposedOutputs.end( ) )
370 auto f = this->GetFilter( i->second.first );
372 return( f->GetOutput( i->second.second ) );
380 // -------------------------------------------------------------------------
381 const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
382 GetExposedOutput( const std::string& name ) const
384 auto i = this->m_ExposedOutputs.find( name );
385 if( i != this->m_ExposedOutputs.end( ) )
387 auto f = this->GetFilter( i->second.first );
389 return( f->GetOutput( i->second.second ) );
397 // -------------------------------------------------------------------------
398 bool cpPlugins::Interface::Workspace::
400 const std::string& name,
401 const std::string& filter, const std::string& filter_input
404 auto i = this->m_ExposedInputs.find( name );
405 if( i == this->m_ExposedInputs.end( ) )
407 this->m_ExposedInputs[ name ] =
408 std::pair< std::string, std::string >( filter, filter_input );
415 // -------------------------------------------------------------------------
416 bool cpPlugins::Interface::Workspace::
418 const std::string& name,
419 const std::string& filter, const std::string& filter_output
422 auto i = this->m_ExposedOutputs.find( name );
423 if( i == this->m_ExposedOutputs.end( ) )
425 this->m_ExposedOutputs[ name ] =
426 std::pair< std::string, std::string >( filter, filter_output );
433 // -------------------------------------------------------------------------
434 void cpPlugins::Interface::Workspace::
435 HideInput( const std::string& name )
437 auto i = this->m_ExposedInputs.find( name );
438 if( i != this->m_ExposedInputs.end( ) )
439 this->m_ExposedInputs.erase( i );
442 // -------------------------------------------------------------------------
443 void cpPlugins::Interface::Workspace::
444 HideOutput( const std::string& name )
446 auto i = this->m_ExposedOutputs.find( name );
447 if( i != this->m_ExposedOutputs.end( ) )
448 this->m_ExposedOutputs.erase( i );
451 // -------------------------------------------------------------------------
452 bool cpPlugins::Interface::Workspace::
454 const std::string& old_name, const std::string& new_name
457 auto o = this->m_ExposedInputs.find( old_name );
458 auto n = this->m_ExposedInputs.find( new_name );
459 if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
461 this->m_ExposedInputs[ new_name ] = o->second;
462 this->m_ExposedInputs.erase( o );
469 // -------------------------------------------------------------------------
470 bool cpPlugins::Interface::Workspace::
472 const std::string& old_name, const std::string& new_name
475 auto o = this->m_ExposedOutputs.find( old_name );
476 auto n = this->m_ExposedOutputs.find( new_name );
478 o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
481 this->m_ExposedOutputs[ new_name ] = o->second;
482 this->m_ExposedOutputs.erase( o );
489 // -------------------------------------------------------------------------
490 std::vector< std::pair< std::string, std::string > >
491 cpPlugins::Interface::Workspace::
493 const std::string& origin, const std::string& destination
496 std::vector< std::pair< std::string, std::string > > conns;
497 auto orig = this->GetFilter( origin );
498 auto dest = this->GetFilter( destination );
499 if( orig != NULL && dest != NULL )
501 auto outs = orig->GetOutputsNames( );
502 auto ins = dest->GetInputsNames( );
503 for( auto o = outs.begin( ); o != outs.end( ); ++o )
505 for( auto i = ins.begin( ); i != ins.end( ); ++i )
507 unsigned int nInputs = dest->GetInputSize( *i );
508 for( unsigned j = 0; j < nInputs; ++j )
510 auto od = orig->GetOutput( *o );
511 auto id = dest->GetInput( *i, j );
512 if( od != NULL && od == id )
514 std::pair< std::string, std::string >( *o, *i )
527 // -------------------------------------------------------------------------
528 void cpPlugins::Interface::Workspace::
530 const std::string& orig_filter, const std::string& dest_filter,
531 const std::string& output_name, const std::string& input_name
534 auto o = this->GetFilter( orig_filter );
535 auto d = this->GetFilter( dest_filter );
536 if( o != NULL && d != NULL )
540 d->AddInput( input_name, o->GetOutput( output_name ) );
542 catch( std::exception& err )
544 throw std::logic_error(
545 std::string( "Error connecting \"" ) +
546 output_name + std::string( "@" ) + orig_filter +
547 std::string( "\" with \"" ) +
548 input_name + std::string( "@" ) + dest_filter +
549 std::string( "\": " ) +
558 // -------------------------------------------------------------------------
559 void cpPlugins::Interface::Workspace::
561 cpPlugins::BaseObjects::DataObject* output,
562 const std::string& dest_filter, const std::string& input_name
565 auto d = this->GetFilter( dest_filter );
567 d->AddInput( input_name, output );
570 // -------------------------------------------------------------------------
571 void cpPlugins::Interface::Workspace::
573 cpPlugins::BaseObjects::DataObject* output,
574 const std::string& exposed_input_name
577 auto i = this->m_ExposedInputs.find( exposed_input_name );
578 if( i != this->m_ExposedInputs.end( ) )
579 this->Connect( output, i->second.first, i->second.second );
582 // -------------------------------------------------------------------------
583 void cpPlugins::Interface::Workspace::
585 const std::string& orig_filter, const std::string& dest_filter,
586 const std::string& output_name, const std::string& input_name
589 auto orig = this->GetFilter( orig_filter );
590 auto dest = this->GetFilter( dest_filter );
591 if( orig != NULL && dest != NULL )
593 auto out = orig->GetOutput( output_name );
594 auto in = dest->GetInput( input_name );
595 if( out != NULL && out == in )
597 input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
603 // -------------------------------------------------------------------------
604 void cpPlugins::Interface::Workspace::
606 const std::string& dest_filter, const std::string& input_name
609 throw std::logic_error( "Disconnect 1" );
612 // -------------------------------------------------------------------------
613 void cpPlugins::Interface::Workspace::
614 Disconnect( const std::string& dest_filter )
616 throw std::logic_error( "Disconnect 2" );
620 // -------------------------------------------------------------------------
621 void cpPlugins::Interface::Workspace::
624 for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
625 f->second->Update( );
628 // -------------------------------------------------------------------------
629 void cpPlugins::Interface::Workspace::
630 Update( const std::string& name )
632 auto filter = this->GetFilter( name );
637 // -------------------------------------------------------------------------
638 cpPlugins::Interface::Workspace::
641 m_PrintExecution( false )
643 this->m_Plugins = TPlugins::New( );
646 // -------------------------------------------------------------------------
647 cpPlugins::Interface::Workspace::
651 this->m_ExposedOutputs.clear( );
652 this->m_ExposedInputs.clear( );
654 this->m_Filters.clear( );