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