1 #include <cpPlugins/Interface/Workspace.h>
2 #include <cpPlugins/BaseObjects/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::BaseObjects::Widget _TWidget;
85 TFilter::Pointer o = this->m_Plugins->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::
238 const std::string& origin_filter,
239 const std::string& origin_output,
240 const std::string& destination_filter,
241 const std::string& destination_input
244 // Get filters and check pertinence
245 TFilter* origin = this->GetFilter( origin_filter );
246 TFilter* destination = this->GetFilter( destination_filter );
247 if( origin == NULL || destination == NULL )
249 if( !( destination->HasInput( destination_input ) ) )
251 if( !( origin->HasOutput( origin_output ) ) )
254 // Check if there is room for a new connection
256 unsigned int del_id = 0;
259 i < destination->GetInputSize( destination_input );
263 destination->GetInput( destination_input, i )->GetSource( ) == origin
271 destination->DisconnectInput( destination_input, del_id );
275 // -------------------------------------------------------------------------
277 const cpPlugins::Interface::Workspace::
278 TExposedPorts& cpPlugins::Interface::Workspace::
279 GetExposedInputs( ) const
281 return( this->m_ExposedInputs );
284 // -------------------------------------------------------------------------
285 const cpPlugins::Interface::Workspace::
286 TExposedPorts& cpPlugins::Interface::Workspace::
287 GetExposedOutputs( ) const
289 return( this->m_ExposedOutputs );
292 // -------------------------------------------------------------------------
293 cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
294 GetExposedOutput( const std::string& name )
296 auto i = this->m_ExposedOutputs.find( name );
297 if( i != this->m_ExposedOutputs.end( ) )
299 auto f = this->GetFilter( i->second.first );
301 return( f->GetOutput( i->second.second ) );
309 // -------------------------------------------------------------------------
310 const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
311 GetExposedOutput( const std::string& name ) const
313 auto i = this->m_ExposedOutputs.find( name );
314 if( i != this->m_ExposedOutputs.end( ) )
316 auto f = this->GetFilter( i->second.first );
318 return( f->GetOutput( i->second.second ) );
326 // -------------------------------------------------------------------------
327 bool cpPlugins::Interface::Workspace::
329 const std::string& name,
330 const std::string& filter, const std::string& filter_input
333 auto i = this->m_ExposedInputs.find( name );
334 if( i == this->m_ExposedInputs.end( ) )
336 this->m_ExposedInputs[ name ] =
337 std::pair< std::string, std::string >( filter, filter_input );
344 // -------------------------------------------------------------------------
345 bool cpPlugins::Interface::Workspace::
347 const std::string& name,
348 const std::string& filter, const std::string& filter_output
351 auto i = this->m_ExposedOutputs.find( name );
352 if( i == this->m_ExposedOutputs.end( ) )
354 this->m_ExposedOutputs[ name ] =
355 std::pair< std::string, std::string >( filter, filter_output );
362 // -------------------------------------------------------------------------
363 void cpPlugins::Interface::Workspace::
364 HideInput( const std::string& name )
366 auto i = this->m_ExposedInputs.find( name );
367 if( i != this->m_ExposedInputs.end( ) )
368 this->m_ExposedInputs.erase( i );
371 // -------------------------------------------------------------------------
372 void cpPlugins::Interface::Workspace::
373 HideOutput( const std::string& name )
375 auto i = this->m_ExposedOutputs.find( name );
376 if( i != this->m_ExposedOutputs.end( ) )
377 this->m_ExposedOutputs.erase( i );
380 // -------------------------------------------------------------------------
381 bool cpPlugins::Interface::Workspace::
383 const std::string& old_name, const std::string& new_name
386 auto o = this->m_ExposedInputs.find( old_name );
387 auto n = this->m_ExposedInputs.find( new_name );
388 if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
390 this->m_ExposedInputs[ new_name ] = o->second;
391 this->m_ExposedInputs.erase( o );
398 // -------------------------------------------------------------------------
399 bool cpPlugins::Interface::Workspace::
401 const std::string& old_name, const std::string& new_name
404 auto o = this->m_ExposedOutputs.find( old_name );
405 auto n = this->m_ExposedOutputs.find( new_name );
407 o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
410 this->m_ExposedOutputs[ new_name ] = o->second;
411 this->m_ExposedOutputs.erase( o );
418 // -------------------------------------------------------------------------
419 std::vector< std::pair< std::string, std::string > >
420 cpPlugins::Interface::Workspace::
422 const std::string& origin, const std::string& destination
425 std::vector< std::pair< std::string, std::string > > conns;
426 auto orig = this->GetFilter( origin );
427 auto dest = this->GetFilter( destination );
428 if( orig != NULL && dest != NULL )
430 auto outs = orig->GetOutputsNames( );
431 auto ins = dest->GetInputsNames( );
432 for( auto o = outs.begin( ); o != outs.end( ); ++o )
434 for( auto i = ins.begin( ); i != ins.end( ); ++i )
436 unsigned int nInputs = dest->GetInputSize( *i );
437 for( unsigned j = 0; j < nInputs; ++j )
439 auto od = orig->GetOutput( *o );
440 auto id = dest->GetInput( *i, j );
441 if( od != NULL && od == id )
443 std::pair< std::string, std::string >( *o, *i )
456 // -------------------------------------------------------------------------
457 void cpPlugins::Interface::Workspace::
459 const std::string& orig_filter, const std::string& dest_filter,
460 const std::string& output_name, const std::string& input_name
463 auto o = this->GetFilter( orig_filter );
464 auto d = this->GetFilter( dest_filter );
465 if( o != NULL && d != NULL )
469 d->AddInput( input_name, o->GetOutput( output_name ) );
471 catch( std::exception& err )
473 throw std::logic_error(
474 std::string( "Error connecting \"" ) +
475 output_name + std::string( "@" ) + orig_filter +
476 std::string( "\" with \"" ) +
477 input_name + std::string( "@" ) + dest_filter +
478 std::string( "\": " ) +
487 // -------------------------------------------------------------------------
488 void cpPlugins::Interface::Workspace::
490 cpPlugins::BaseObjects::DataObject* output,
491 const std::string& dest_filter, const std::string& input_name
494 auto d = this->GetFilter( dest_filter );
496 d->AddInput( input_name, output );
499 // -------------------------------------------------------------------------
500 void cpPlugins::Interface::Workspace::
502 cpPlugins::BaseObjects::DataObject* output,
503 const std::string& exposed_input_name
506 auto i = this->m_ExposedInputs.find( exposed_input_name );
507 if( i != this->m_ExposedInputs.end( ) )
508 this->Connect( output, i->second.first, i->second.second );
511 // -------------------------------------------------------------------------
512 void cpPlugins::Interface::Workspace::
514 const std::string& orig_filter, const std::string& dest_filter,
515 const std::string& output_name, const std::string& input_name
518 auto orig = this->GetFilter( orig_filter );
519 auto dest = this->GetFilter( dest_filter );
520 if( orig != NULL && dest != NULL )
522 auto out = orig->GetOutput( output_name );
523 auto in = dest->GetInput( input_name );
524 if( out != NULL && out == in )
526 input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
532 // -------------------------------------------------------------------------
533 void cpPlugins::Interface::Workspace::
535 const std::string& dest_filter, const std::string& input_name
538 throw std::logic_error( "Disconnect 1" );
541 // -------------------------------------------------------------------------
542 void cpPlugins::Interface::Workspace::
543 Disconnect( const std::string& dest_filter )
545 throw std::logic_error( "Disconnect 2" );
549 // -------------------------------------------------------------------------
550 void cpPlugins::Interface::Workspace::
553 for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
554 f->second->Update( );
557 // -------------------------------------------------------------------------
558 void cpPlugins::Interface::Workspace::
559 Update( const std::string& name )
561 auto filter = this->GetFilter( name );
566 // -------------------------------------------------------------------------
567 cpPlugins::Interface::Workspace::
570 m_PrintExecution( false )
572 this->m_Plugins = TPlugins::New( );
575 // -------------------------------------------------------------------------
576 cpPlugins::Interface::Workspace::
580 this->m_ExposedOutputs.clear( );
581 this->m_ExposedInputs.clear( );
583 this->m_Filters.clear( );