]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
...
[cpPlugins.git] / lib / cpPlugins / Interface / Workspace.cxx
1 #include <cpPlugins/Interface/Workspace.h>
2 #include <cpPlugins/Pipeline/Widget.h>
3
4 // -------------------------------------------------------------------------
5 void cpPlugins::Interface::Workspace::
6 Clear( )
7 {
8   this->m_Filters.clear( );
9 }
10
11 // -------------------------------------------------------------------------
12 std::vector< std::string > cpPlugins::Interface::Workspace::
13 GetFiltersNames( ) const
14 {
15   std::vector< std::string > n;
16   for( auto i : this->m_Filters )
17     n.push_back( i.first );
18   return( n );
19 }
20
21 // -------------------------------------------------------------------------
22 cpPlugins::Interface::Workspace::
23 TFilter* cpPlugins::Interface::Workspace::
24 GetFilter( const std::string& name )
25 {
26   auto i = this->m_Filters.find( name );
27   if( i != this->m_Filters.end( ) )
28     return( i->second.GetPointer( ) );
29   else
30     return( NULL );
31 }
32
33 // -------------------------------------------------------------------------
34 const cpPlugins::Interface::Workspace::
35 TFilter* cpPlugins::Interface::Workspace::
36 GetFilter( const std::string& name ) const
37 {
38   auto i = this->m_Filters.find( name );
39   if( i != this->m_Filters.end( ) )
40     return( i->second.GetPointer( ) );
41   else
42     return( NULL );
43 }
44
45 // -------------------------------------------------------------------------
46 cpPlugins::Interface::Workspace::
47 TWidget* cpPlugins::Interface::Workspace::
48 GetWidget( const std::string& name )
49 {
50   TFilter* process = this->GetFilter( name );
51   return( dynamic_cast< TWidget* >( process ) );
52 }
53
54 // -------------------------------------------------------------------------
55 const cpPlugins::Interface::Workspace::
56 TWidget* cpPlugins::Interface::Workspace::
57 GetWidget( const std::string& name ) const
58 {
59   const TFilter* process = this->GetFilter( name );
60   return( dynamic_cast< const TWidget* >( process ) );
61 }
62
63 // -------------------------------------------------------------------------
64 bool cpPlugins::Interface::Workspace::
65 HasFilter( const std::string& name ) const
66 {
67   return( this->m_Filters.find( name ) != this->m_Filters.end( ) );
68 }
69
70 // -------------------------------------------------------------------------
71 bool cpPlugins::Interface::Workspace::
72 HasWidget( const std::string& name ) const
73 {
74   const TWidget* wdg = this->GetWidget( name );
75   return( wdg != NULL );
76 }
77
78 // -------------------------------------------------------------------------
79 cpPlugins::Interface::Workspace::
80 TFilter* cpPlugins::Interface::Workspace::
81 CreateFilter(
82   const std::string& category, const std::string& filter,
83   const std::string& name
84   )
85 {
86   typedef cpPlugins::Pipeline::Widget _TWidget;
87
88   TFilter::Pointer o = this->m_Loader.CreateFilter( category, filter );
89   if( o.IsNotNull( ) )
90   {
91     // Choose a name
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 );
97
98     // Interactors
99     for(
100       auto i = this->m_Interactors.begin( );
101       i != this->m_Interactors.end( );
102       ++i
103       )
104       o->AddInteractor( *i );
105
106     // Finish association
107     this->m_Filters[ real_name ] = o;
108
109   } // fi
110   return( o.GetPointer( ) );
111 }
112
113 // -------------------------------------------------------------------------
114 cpPlugins::Interface::Workspace::
115 TFilter* cpPlugins::Interface::Workspace::
116 CreateFilter( const std::string& category, const std::string& filter )
117 {
118   return( this->CreateFilter( category, filter, filter ) );
119 }
120
121 // -------------------------------------------------------------------------
122 bool cpPlugins::Interface::Workspace::
123 RenameFilter( const std::string& old_name, const std::string& new_name )
124 {
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( ) )
128   {
129     // Rename filter
130     o->second->SetName( new_name );
131     this->m_Filters[ new_name ] = o->second;
132     this->m_Filters.erase( o );
133
134     // Rename exposed ports
135     /* TODO
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;
144     */
145
146     return( true );
147   }
148   else
149     return( false );
150 }
151
152 // -------------------------------------------------------------------------
153 bool cpPlugins::Interface::Workspace::
154 RemoveFilter( const std::string& name )
155 {
156   auto i = this->m_Filters.find( name );
157   if( i != this->m_Filters.end( ) )
158   {
159     i->second->Disconnect( );
160     this->m_Filters.erase( i );
161     return( true );
162   }
163   else
164     return( false );
165 }
166
167 // -------------------------------------------------------------------------
168 void cpPlugins::Interface::Workspace::
169 SetPrintExecution( bool b )
170 {
171   this->m_PrintExecution = b;
172   for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
173     i->second->SetPrintExecution( b );
174 }
175
176 // -------------------------------------------------------------------------
177 void cpPlugins::Interface::Workspace::
178 PrintExecutionOn( )
179 {
180   this->SetPrintExecution( true );
181 }
182
183 // -------------------------------------------------------------------------
184 void cpPlugins::Interface::Workspace::
185 PrintExecutionOff( )
186 {
187   this->SetPrintExecution( true );
188 }
189
190 // -------------------------------------------------------------------------
191 void cpPlugins::Interface::Workspace::
192 AddInteractor( vtkRenderWindowInteractor* iren )
193 {
194   if( iren != NULL )
195   {
196     this->m_Interactors.insert( iren );
197     for( auto f : this->m_Filters )
198       f.second->AddInteractor( iren );
199
200   } // fi
201 }
202
203 // -------------------------------------------------------------------------
204 bool cpPlugins::Interface::Workspace::
205 Connect(
206   const std::string& origin_filter,
207   const std::string& origin_output,
208   const std::string& destination_filter,
209   const std::string& destination_input
210   )
211 {
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 )
216     return( false );
217   if( !( destination->HasInput( destination_input ) ) )
218     return( false );
219   if( !( origin->HasOutput( origin_output ) ) )
220     return( false );
221
222   // Check if there is room for a new connection
223   bool ok = true;
224   if( destination->IsInputMultiple( destination_input ) )
225   {
226     for(
227       unsigned int i = 0;
228       i < destination->GetInputSize( destination_input );
229       ++i
230       )
231       if(
232         destination->GetInput( destination_input, i )->GetSource( ) == origin
233         )
234         ok = false;
235   }
236   else
237     ok = ( destination->GetInput( destination_input ) == NULL );
238   if( ok )
239     destination->AddInput(
240       destination_input,
241       origin->GetOutput( origin_output )
242       );
243   return( ok );
244 }
245
246 // -------------------------------------------------------------------------
247 bool cpPlugins::Interface::Workspace::
248 Connect(
249   TDataObject* input,
250   const std::string& destination_filter,
251   const std::string& destination_input
252   )
253 {
254   // Get filters and check pertinence
255   if( input == NULL )
256     return( false );
257   TFilter* destination = this->GetFilter( destination_filter );
258   if( destination == NULL )
259     return( false );
260   if( !( destination->HasInput( destination_input ) ) )
261     return( false );
262
263   // Check if there is room for a new connection
264   bool ok = true;
265   if( destination->IsInputMultiple( destination_input ) )
266   {
267     for(
268       unsigned int i = 0;
269       i < destination->GetInputSize( destination_input );
270       ++i
271       )
272       if(
273         destination->GetInput( destination_input, i )->GetSource( ) ==
274         input->GetSource( )
275         )
276         ok = false;
277   }
278   else
279     ok = ( destination->GetInput( destination_input ) == NULL );
280   if( ok )
281     destination->AddInput(
282       destination_input,
283       input
284       );
285   return( ok );
286 }
287
288 // -------------------------------------------------------------------------
289 bool cpPlugins::Interface::Workspace::
290 Disconnect(
291   const std::string& origin_filter,
292   const std::string& origin_output,
293   const std::string& destination_filter,
294   const std::string& destination_input
295   )
296 {
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 )
301     return( false );
302   if( !( destination->HasInput( destination_input ) ) )
303     return( false );
304   if( !( origin->HasOutput( origin_output ) ) )
305     return( false );
306
307   // Check if there is room for a new connection
308   bool ok = false;
309   unsigned int del_id = 0;
310   for(
311     unsigned int i = 0;
312     i < destination->GetInputSize( destination_input );
313     ++i
314     )
315     if(
316       destination->GetInput( destination_input, i )->GetSource( ) == origin
317       )
318     {
319       ok = true;
320       del_id = i;
321
322     } // fi
323   if( ok )
324     destination->DisconnectInput( destination_input, del_id );
325   return( ok );
326 }
327
328 // -------------------------------------------------------------------------
329 /*
330 const cpPlugins::Interface::Workspace::
331 TExposedPorts& cpPlugins::Interface::Workspace::
332 GetExposedInputs( ) const
333 {
334   return( this->m_ExposedInputs );
335 }
336
337 // -------------------------------------------------------------------------
338 const cpPlugins::Interface::Workspace::
339 TExposedPorts& cpPlugins::Interface::Workspace::
340 GetExposedOutputs( ) const
341 {
342   return( this->m_ExposedOutputs );
343 }
344
345 // -------------------------------------------------------------------------
346 cpPlugins::Pipeline::DataObject* cpPlugins::Interface::Workspace::
347 GetExposedOutput( const std::string& name )
348 {
349   auto i = this->m_ExposedOutputs.find( name );
350   if( i != this->m_ExposedOutputs.end( ) )
351   {
352     auto f = this->GetFilter( i->second.first );
353     if( f != NULL )
354       return( f->GetOutput( i->second.second ) );
355     else
356       return( NULL );
357   }
358   else
359     return( NULL );
360 }
361
362 // -------------------------------------------------------------------------
363 const cpPlugins::Pipeline::DataObject* cpPlugins::Interface::Workspace::
364 GetExposedOutput( const std::string& name ) const
365 {
366   auto i = this->m_ExposedOutputs.find( name );
367   if( i != this->m_ExposedOutputs.end( ) )
368   {
369     auto f = this->GetFilter( i->second.first );
370     if( f != NULL )
371       return( f->GetOutput( i->second.second ) );
372     else
373       return( NULL );
374   }
375   else
376     return( NULL );
377 }
378
379 // -------------------------------------------------------------------------
380 bool cpPlugins::Interface::Workspace::
381 ExposeInput(
382   const std::string& name,
383   const std::string& filter, const std::string& filter_input
384   )
385 {
386   auto i = this->m_ExposedInputs.find( name );
387   if( i == this->m_ExposedInputs.end( ) )
388   {
389     this->m_ExposedInputs[ name ] =
390       std::pair< std::string, std::string >( filter, filter_input );
391     return( true );
392   }
393   else
394     return( false );
395 }
396
397 // -------------------------------------------------------------------------
398 bool cpPlugins::Interface::Workspace::
399 ExposeOutput(
400   const std::string& name,
401   const std::string& filter, const std::string& filter_output
402   )
403 {
404   auto i = this->m_ExposedOutputs.find( name );
405   if( i == this->m_ExposedOutputs.end( ) )
406   {
407     this->m_ExposedOutputs[ name ] =
408       std::pair< std::string, std::string >( filter, filter_output );
409     return( true );
410   }
411   else
412     return( false );
413 }
414
415 // -------------------------------------------------------------------------
416 void cpPlugins::Interface::Workspace::
417 HideInput( const std::string& name )
418 {
419   auto i = this->m_ExposedInputs.find( name );
420   if( i != this->m_ExposedInputs.end( ) )
421     this->m_ExposedInputs.erase( i );
422 }
423
424 // -------------------------------------------------------------------------
425 void cpPlugins::Interface::Workspace::
426 HideOutput( const std::string& name )
427 {
428   auto i = this->m_ExposedOutputs.find( name );
429   if( i != this->m_ExposedOutputs.end( ) )
430     this->m_ExposedOutputs.erase( i );
431 }
432
433 // -------------------------------------------------------------------------
434 bool cpPlugins::Interface::Workspace::
435 RenameExposedInput(
436   const std::string& old_name, const std::string& new_name
437   )
438 {
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( ) )
442   {
443     this->m_ExposedInputs[ new_name ] = o->second;
444     this->m_ExposedInputs.erase( o );
445     return( true );
446   }
447   else
448     return( false );
449 }
450
451 // -------------------------------------------------------------------------
452 bool cpPlugins::Interface::Workspace::
453 RenameExposedOutput(
454   const std::string& old_name, const std::string& new_name
455   )
456 {
457   auto o = this->m_ExposedOutputs.find( old_name );
458   auto n = this->m_ExposedOutputs.find( new_name );
459   if(
460     o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
461     )
462   {
463     this->m_ExposedOutputs[ new_name ] = o->second;
464     this->m_ExposedOutputs.erase( o );
465     return( true );
466   }
467   else
468     return( false );
469 }
470
471 // -------------------------------------------------------------------------
472 std::vector< std::pair< std::string, std::string > >
473 cpPlugins::Interface::Workspace::
474 GetConnections(
475   const std::string& origin, const std::string& destination
476   ) const
477 {
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 )
482   {
483     auto outs = orig->GetOutputsNames( );
484     auto ins = dest->GetInputsNames( );
485     for( auto o = outs.begin( ); o != outs.end( ); ++o )
486     {
487       for( auto i = ins.begin( ); i != ins.end( ); ++i )
488       {
489         unsigned int nInputs = dest->GetInputSize( *i );
490         for( unsigned j = 0; j < nInputs; ++j )
491         {
492           auto od = orig->GetOutput( *o );
493           auto id = dest->GetInput( *i, j );
494           if( od != NULL && od == id )
495             conns.push_back(
496               std::pair< std::string, std::string >( *o, *i )
497               );
498
499         } // rof
500
501       } // rof
502
503     } // rof
504
505   } // fi
506   return( conns );
507 }
508
509 // -------------------------------------------------------------------------
510 void cpPlugins::Interface::Workspace::
511 Connect(
512   const std::string& orig_filter, const std::string& dest_filter,
513   const std::string& output_name, const std::string& input_name
514   )
515 {
516   auto o = this->GetFilter( orig_filter );
517   auto d = this->GetFilter( dest_filter );
518   if( o != NULL && d != NULL )
519   {
520     try
521     {
522       d->AddInput( input_name, o->GetOutput( output_name ) );
523     }
524     catch( std::exception& err )
525     {
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( "\": " ) +
532         err.what( )
533         );
534
535     } // yrt
536
537   } // fi
538 }
539
540 // -------------------------------------------------------------------------
541 void cpPlugins::Interface::Workspace::
542 Connect(
543   cpPlugins::Pipeline::DataObject* output,
544   const std::string& dest_filter, const std::string& input_name
545   )
546 {
547   auto d = this->GetFilter( dest_filter );
548   if( d != NULL )
549     d->AddInput( input_name, output );
550 }
551
552 // -------------------------------------------------------------------------
553 void cpPlugins::Interface::Workspace::
554 Connect(
555   cpPlugins::Pipeline::DataObject* output,
556   const std::string& exposed_input_name
557   )
558 {
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 );
562 }
563
564 // -------------------------------------------------------------------------
565 void cpPlugins::Interface::Workspace::
566 Disconnect(
567   const std::string& orig_filter, const std::string& dest_filter,
568   const std::string& output_name, const std::string& input_name
569   )
570 {
571   auto orig = this->GetFilter( orig_filter );
572   auto dest = this->GetFilter( dest_filter );
573   if( orig != NULL && dest != NULL )
574   {
575     auto out = orig->GetOutput( output_name );
576     auto in = dest->GetInput( input_name );
577     if( out != NULL && out == in )
578       dest->SetInput(
579         input_name, ( cpPlugins::Pipeline::DataObject* )( NULL )
580         );
581
582   } // fi
583 }
584
585 // -------------------------------------------------------------------------
586 void cpPlugins::Interface::Workspace::
587 Disconnect(
588   const std::string& dest_filter, const std::string& input_name
589   )
590 {
591   throw std::logic_error( "Disconnect 1" );
592 }
593
594 // -------------------------------------------------------------------------
595 void cpPlugins::Interface::Workspace::
596 Disconnect( const std::string& dest_filter )
597 {
598   throw std::logic_error( "Disconnect 2" );
599 }
600 */
601
602 // -------------------------------------------------------------------------
603 void cpPlugins::Interface::Workspace::
604 Update( )
605 {
606   for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
607     f->second->Update( );
608 }
609
610 // -------------------------------------------------------------------------
611 void cpPlugins::Interface::Workspace::
612 Update( const std::string& name )
613 {
614   auto filter = this->GetFilter( name );
615   if( filter != NULL )
616     filter->Update( );
617 }
618
619 // -------------------------------------------------------------------------
620 cpPlugins::Interface::Workspace::
621 Workspace( )
622   : Superclass( ),
623     m_PrintExecution( false )
624 {
625 }
626
627 // -------------------------------------------------------------------------
628 cpPlugins::Interface::Workspace::
629 ~Workspace( )
630 {
631   /* TODO
632      this->m_ExposedOutputs.clear( );
633      this->m_ExposedInputs.clear( );
634   */
635   this->m_Filters.clear( );
636 }
637
638 // eof - $RCSfile$