]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
bdc67e000d0c351e8a7e68ad39276fdc9e50ea4f
[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 Disconnect(
238   const std::string& origin_filter,
239   const std::string& origin_output,
240   const std::string& destination_filter,
241   const std::string& destination_input
242   )
243 {
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 )
248     return( false );
249   if( !( destination->HasInput( destination_input ) ) )
250     return( false );
251   if( !( origin->HasOutput( origin_output ) ) )
252     return( false );
253
254   // Check if there is room for a new connection
255   bool ok = false;
256   unsigned int del_id = 0;
257   for(
258     unsigned int i = 0;
259     i < destination->GetInputSize( destination_input );
260     ++i
261     )
262     if(
263       destination->GetInput( destination_input, i )->GetSource( ) == origin
264       )
265     {
266       ok = true;
267       del_id = i;
268
269     } // fi
270   if( ok )
271     destination->DisconnectInput( destination_input, del_id );
272   return( ok );
273 }
274
275 // -------------------------------------------------------------------------
276 /*
277 const cpPlugins::Interface::Workspace::
278 TExposedPorts& cpPlugins::Interface::Workspace::
279 GetExposedInputs( ) const
280 {
281   return( this->m_ExposedInputs );
282 }
283
284 // -------------------------------------------------------------------------
285 const cpPlugins::Interface::Workspace::
286 TExposedPorts& cpPlugins::Interface::Workspace::
287 GetExposedOutputs( ) const
288 {
289   return( this->m_ExposedOutputs );
290 }
291
292 // -------------------------------------------------------------------------
293 cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
294 GetExposedOutput( const std::string& name )
295 {
296   auto i = this->m_ExposedOutputs.find( name );
297   if( i != this->m_ExposedOutputs.end( ) )
298   {
299     auto f = this->GetFilter( i->second.first );
300     if( f != NULL )
301       return( f->GetOutput( i->second.second ) );
302     else
303       return( NULL );
304   }
305   else
306     return( NULL );
307 }
308
309 // -------------------------------------------------------------------------
310 const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
311 GetExposedOutput( const std::string& name ) const
312 {
313   auto i = this->m_ExposedOutputs.find( name );
314   if( i != this->m_ExposedOutputs.end( ) )
315   {
316     auto f = this->GetFilter( i->second.first );
317     if( f != NULL )
318       return( f->GetOutput( i->second.second ) );
319     else
320       return( NULL );
321   }
322   else
323     return( NULL );
324 }
325
326 // -------------------------------------------------------------------------
327 bool cpPlugins::Interface::Workspace::
328 ExposeInput(
329   const std::string& name,
330   const std::string& filter, const std::string& filter_input
331   )
332 {
333   auto i = this->m_ExposedInputs.find( name );
334   if( i == this->m_ExposedInputs.end( ) )
335   {
336     this->m_ExposedInputs[ name ] =
337       std::pair< std::string, std::string >( filter, filter_input );
338     return( true );
339   }
340   else
341     return( false );
342 }
343
344 // -------------------------------------------------------------------------
345 bool cpPlugins::Interface::Workspace::
346 ExposeOutput(
347   const std::string& name,
348   const std::string& filter, const std::string& filter_output
349   )
350 {
351   auto i = this->m_ExposedOutputs.find( name );
352   if( i == this->m_ExposedOutputs.end( ) )
353   {
354     this->m_ExposedOutputs[ name ] =
355       std::pair< std::string, std::string >( filter, filter_output );
356     return( true );
357   }
358   else
359     return( false );
360 }
361
362 // -------------------------------------------------------------------------
363 void cpPlugins::Interface::Workspace::
364 HideInput( const std::string& name )
365 {
366   auto i = this->m_ExposedInputs.find( name );
367   if( i != this->m_ExposedInputs.end( ) )
368     this->m_ExposedInputs.erase( i );
369 }
370
371 // -------------------------------------------------------------------------
372 void cpPlugins::Interface::Workspace::
373 HideOutput( const std::string& name )
374 {
375   auto i = this->m_ExposedOutputs.find( name );
376   if( i != this->m_ExposedOutputs.end( ) )
377     this->m_ExposedOutputs.erase( i );
378 }
379
380 // -------------------------------------------------------------------------
381 bool cpPlugins::Interface::Workspace::
382 RenameExposedInput(
383   const std::string& old_name, const std::string& new_name
384   )
385 {
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( ) )
389   {
390     this->m_ExposedInputs[ new_name ] = o->second;
391     this->m_ExposedInputs.erase( o );
392     return( true );
393   }
394   else
395     return( false );
396 }
397
398 // -------------------------------------------------------------------------
399 bool cpPlugins::Interface::Workspace::
400 RenameExposedOutput(
401   const std::string& old_name, const std::string& new_name
402   )
403 {
404   auto o = this->m_ExposedOutputs.find( old_name );
405   auto n = this->m_ExposedOutputs.find( new_name );
406   if(
407     o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
408     )
409   {
410     this->m_ExposedOutputs[ new_name ] = o->second;
411     this->m_ExposedOutputs.erase( o );
412     return( true );
413   }
414   else
415     return( false );
416 }
417
418 // -------------------------------------------------------------------------
419 std::vector< std::pair< std::string, std::string > >
420 cpPlugins::Interface::Workspace::
421 GetConnections(
422   const std::string& origin, const std::string& destination
423   ) const
424 {
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 )
429   {
430     auto outs = orig->GetOutputsNames( );
431     auto ins = dest->GetInputsNames( );
432     for( auto o = outs.begin( ); o != outs.end( ); ++o )
433     {
434       for( auto i = ins.begin( ); i != ins.end( ); ++i )
435       {
436         unsigned int nInputs = dest->GetInputSize( *i );
437         for( unsigned j = 0; j < nInputs; ++j )
438         {
439           auto od = orig->GetOutput( *o );
440           auto id = dest->GetInput( *i, j );
441           if( od != NULL && od == id )
442             conns.push_back(
443               std::pair< std::string, std::string >( *o, *i )
444               );
445
446         } // rof
447
448       } // rof
449
450     } // rof
451
452   } // fi
453   return( conns );
454 }
455
456 // -------------------------------------------------------------------------
457 void cpPlugins::Interface::Workspace::
458 Connect(
459   const std::string& orig_filter, const std::string& dest_filter,
460   const std::string& output_name, const std::string& input_name
461   )
462 {
463   auto o = this->GetFilter( orig_filter );
464   auto d = this->GetFilter( dest_filter );
465   if( o != NULL && d != NULL )
466   {
467     try
468     {
469       d->AddInput( input_name, o->GetOutput( output_name ) );
470     }
471     catch( std::exception& err )
472     {
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( "\": " ) +
479         err.what( )
480         );
481
482     } // yrt
483
484   } // fi
485 }
486
487 // -------------------------------------------------------------------------
488 void cpPlugins::Interface::Workspace::
489 Connect(
490   cpPlugins::BaseObjects::DataObject* output,
491   const std::string& dest_filter, const std::string& input_name
492   )
493 {
494   auto d = this->GetFilter( dest_filter );
495   if( d != NULL )
496     d->AddInput( input_name, output );
497 }
498
499 // -------------------------------------------------------------------------
500 void cpPlugins::Interface::Workspace::
501 Connect(
502   cpPlugins::BaseObjects::DataObject* output,
503   const std::string& exposed_input_name
504   )
505 {
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 );
509 }
510
511 // -------------------------------------------------------------------------
512 void cpPlugins::Interface::Workspace::
513 Disconnect(
514   const std::string& orig_filter, const std::string& dest_filter,
515   const std::string& output_name, const std::string& input_name
516   )
517 {
518   auto orig = this->GetFilter( orig_filter );
519   auto dest = this->GetFilter( dest_filter );
520   if( orig != NULL && dest != NULL )
521   {
522     auto out = orig->GetOutput( output_name );
523     auto in = dest->GetInput( input_name );
524     if( out != NULL && out == in )
525       dest->SetInput(
526         input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
527         );
528
529   } // fi
530 }
531
532 // -------------------------------------------------------------------------
533 void cpPlugins::Interface::Workspace::
534 Disconnect(
535   const std::string& dest_filter, const std::string& input_name
536   )
537 {
538   throw std::logic_error( "Disconnect 1" );
539 }
540
541 // -------------------------------------------------------------------------
542 void cpPlugins::Interface::Workspace::
543 Disconnect( const std::string& dest_filter )
544 {
545   throw std::logic_error( "Disconnect 2" );
546 }
547 */
548
549 // -------------------------------------------------------------------------
550 void cpPlugins::Interface::Workspace::
551 Update( )
552 {
553   for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
554     f->second->Update( );
555 }
556
557 // -------------------------------------------------------------------------
558 void cpPlugins::Interface::Workspace::
559 Update( const std::string& name )
560 {
561   auto filter = this->GetFilter( name );
562   if( filter != NULL )
563     filter->Update( );
564 }
565
566 // -------------------------------------------------------------------------
567 cpPlugins::Interface::Workspace::
568 Workspace( )
569   : Superclass( ),
570     m_PrintExecution( false )
571 {
572   this->m_Plugins = TPlugins::New( );
573 }
574
575 // -------------------------------------------------------------------------
576 cpPlugins::Interface::Workspace::
577 ~Workspace( )
578 {
579   /* TODO
580      this->m_ExposedOutputs.clear( );
581      this->m_ExposedInputs.clear( );
582   */
583   this->m_Filters.clear( );
584 }
585
586 // eof - $RCSfile$