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