]> 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 void cpPlugins::Interface::Workspace::
6 Clear( )
7 {
8   this->m_Filters.clear( );
9   this->m_ExposedInputs.clear( );
10   this->m_ExposedOutputs.clear( );
11 }
12
13 // -------------------------------------------------------------------------
14 std::vector< std::string > cpPlugins::Interface::Workspace::
15 GetFiltersNames( ) const
16 {
17   std::vector< std::string > n;
18   for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
19     n.push_back( i->first );
20   return( n );
21 }
22
23 // -------------------------------------------------------------------------
24 cpPlugins::Interface::Workspace::
25 TProcess* cpPlugins::Interface::Workspace::
26 GetFilter( const std::string& name )
27 {
28   auto i = this->m_Filters.find( name );
29   if( i != this->m_Filters.end( ) )
30     return( i->second.GetPointer( ) );
31   else
32     return( NULL );
33 }
34
35 // -------------------------------------------------------------------------
36 const cpPlugins::Interface::Workspace::
37 TProcess* cpPlugins::Interface::Workspace::
38 GetFilter( const std::string& name ) const
39 {
40   auto i = this->m_Filters.find( name );
41   if( i != this->m_Filters.end( ) )
42     return( i->second.GetPointer( ) );
43   else
44     return( NULL );
45 }
46
47 // -------------------------------------------------------------------------
48 cpPlugins::Interface::Workspace::
49 TWidget* cpPlugins::Interface::Workspace::
50 GetWidget( const std::string& name )
51 {
52   TProcess* process = this->GetFilter( name );
53   return( dynamic_cast< TWidget* >( process ) );
54 }
55
56 // -------------------------------------------------------------------------
57 const cpPlugins::Interface::Workspace::
58 TWidget* cpPlugins::Interface::Workspace::
59 GetWidget( const std::string& name ) const
60 {
61   const TProcess* process = this->GetFilter( name );
62   return( dynamic_cast< const TWidget* >( process ) );
63 }
64
65 // -------------------------------------------------------------------------
66 bool cpPlugins::Interface::Workspace::
67 HasFilter( const std::string& name ) const
68 {
69   return( this->m_Filters.find( name ) != this->m_Filters.end( ) );
70 }
71
72 // -------------------------------------------------------------------------
73 bool cpPlugins::Interface::Workspace::
74 HasWidget( const std::string& name ) const
75 {
76   const TWidget* wdg = this->GetWidget( name );
77   return( wdg != NULL );
78 }
79
80 // -------------------------------------------------------------------------
81 cpPlugins::Interface::Workspace::
82 TProcess* cpPlugins::Interface::Workspace::
83 CreateFilter(
84   const std::string& category,
85   const std::string& filter,
86   const std::string& name
87   )
88 {
89   typedef cpPlugins::BaseObjects::Widget _TWidget;
90
91   TInterface::Pointer interface = TInterface::New( );
92   TProcess::Pointer o = this->GetFilter( name );
93   if( o.IsNull( ) )
94   {
95     o = interface->CreateProcessObject( category, filter );
96     if( o.IsNotNull( ) )
97     {
98       o->SetPrintExecution( this->m_PrintExecution );
99       o->SetName( name );
100
101       // Interactors
102       for(
103         auto i = this->m_Interactors.begin( );
104         i != this->m_Interactors.end( );
105         ++i
106         )
107         o->AddInteractor( *i );
108
109       // Finish association
110       this->m_Filters[ name ] = o;
111
112     } // fi
113
114   } // fi
115   return( o.GetPointer( ) );
116 }
117
118 // -------------------------------------------------------------------------
119 bool cpPlugins::Interface::Workspace::
120 RenameFilter( const std::string& old_name, const std::string& new_name )
121 {
122   auto o = this->m_Filters.find( old_name );
123   auto n = this->m_Filters.find( new_name );
124   if( o != this->m_Filters.end( ) && n == this->m_Filters.end( ) )
125   {
126     // Rename filter
127     o->second->SetName( new_name );
128     this->m_Filters[ new_name ] = o->second;
129     this->m_Filters.erase( o );
130
131     // Rename exposed ports
132     auto e = this->m_ExposedInputs.begin( );
133     for( ; e != this->m_ExposedInputs.end( ); ++e )
134       if( e->second.first == old_name )
135         e->second.first = new_name;
136     e = this->m_ExposedOutputs.begin( );
137     for( ; e != this->m_ExposedOutputs.end( ); ++e )
138       if( e->second.first == old_name )
139         e->second.first = new_name;
140
141     return( true );
142   }
143   else
144     return( false );
145 }
146
147 // -------------------------------------------------------------------------
148 bool cpPlugins::Interface::Workspace::
149 RemoveFilter( const std::string& name )
150 {
151   auto i = this->m_Filters.find( name );
152   if( i != this->m_Filters.end( ) )
153   {
154     i->second->Disconnect( );
155     this->m_Filters.erase( i );
156     return( true );
157   }
158   else
159     return( false );
160 }
161
162 // -------------------------------------------------------------------------
163 void cpPlugins::Interface::Workspace::
164 SetPrintExecution( bool b )
165 {
166   this->m_PrintExecution = b;
167   for( auto i = this->m_Filters.begin( ); i != this->m_Filters.end( ); ++i )
168     i->second->SetPrintExecution( b );
169 }
170
171 // -------------------------------------------------------------------------
172 void cpPlugins::Interface::Workspace::
173 PrintExecutionOn( )
174 {
175   this->SetPrintExecution( true );
176 }
177
178 // -------------------------------------------------------------------------
179 void cpPlugins::Interface::Workspace::
180 PrintExecutionOff( )
181 {
182   this->SetPrintExecution( true );
183 }
184
185 // -------------------------------------------------------------------------
186 void cpPlugins::Interface::Workspace::
187 AddInteractor( vtkRenderWindowInteractor* iren )
188 {
189   if( iren != NULL )
190     this->m_Interactors.insert( iren );
191 }
192
193 // -------------------------------------------------------------------------
194 const cpPlugins::Interface::Workspace::
195 TExposedPorts& cpPlugins::Interface::Workspace::
196 GetExposedInputs( ) const
197 {
198   return( this->m_ExposedInputs );
199 }
200
201 // -------------------------------------------------------------------------
202 const cpPlugins::Interface::Workspace::
203 TExposedPorts& cpPlugins::Interface::Workspace::
204 GetExposedOutputs( ) const
205 {
206   return( this->m_ExposedOutputs );
207 }
208
209 // -------------------------------------------------------------------------
210 cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
211 GetExposedOutput( const std::string& name )
212 {
213   auto i = this->m_ExposedOutputs.find( name );
214   if( i != this->m_ExposedOutputs.end( ) )
215   {
216     auto f = this->GetFilter( i->second.first );
217     if( f != NULL )
218       return( f->GetOutput( i->second.second ) );
219     else
220       return( NULL );
221   }
222   else
223     return( NULL );
224 }
225
226 // -------------------------------------------------------------------------
227 const cpPlugins::BaseObjects::DataObject* cpPlugins::Interface::Workspace::
228 GetExposedOutput( const std::string& name ) const
229 {
230   auto i = this->m_ExposedOutputs.find( name );
231   if( i != this->m_ExposedOutputs.end( ) )
232   {
233     auto f = this->GetFilter( i->second.first );
234     if( f != NULL )
235       return( f->GetOutput( i->second.second ) );
236     else
237       return( NULL );
238   }
239   else
240     return( NULL );
241 }
242
243 // -------------------------------------------------------------------------
244 bool cpPlugins::Interface::Workspace::
245 ExposeInput(
246   const std::string& name,
247   const std::string& filter, const std::string& filter_input
248   )
249 {
250   auto i = this->m_ExposedInputs.find( name );
251   if( i == this->m_ExposedInputs.end( ) )
252   {
253     this->m_ExposedInputs[ name ] =
254       std::pair< std::string, std::string >( filter, filter_input );
255     return( true );
256   }
257   else
258     return( false );
259 }
260
261 // -------------------------------------------------------------------------
262 bool cpPlugins::Interface::Workspace::
263 ExposeOutput(
264   const std::string& name,
265   const std::string& filter, const std::string& filter_output
266   )
267 {
268   auto i = this->m_ExposedOutputs.find( name );
269   if( i == this->m_ExposedOutputs.end( ) )
270   {
271     this->m_ExposedOutputs[ name ] =
272       std::pair< std::string, std::string >( filter, filter_output );
273     return( true );
274   }
275   else
276     return( false );
277 }
278
279 // -------------------------------------------------------------------------
280 void cpPlugins::Interface::Workspace::
281 HideInput( const std::string& name )
282 {
283   auto i = this->m_ExposedInputs.find( name );
284   if( i != this->m_ExposedInputs.end( ) )
285     this->m_ExposedInputs.erase( i );
286 }
287
288 // -------------------------------------------------------------------------
289 void cpPlugins::Interface::Workspace::
290 HideOutput( const std::string& name )
291 {
292   auto i = this->m_ExposedOutputs.find( name );
293   if( i != this->m_ExposedOutputs.end( ) )
294     this->m_ExposedOutputs.erase( i );
295 }
296
297 // -------------------------------------------------------------------------
298 bool cpPlugins::Interface::Workspace::
299 RenameExposedInput(
300   const std::string& old_name, const std::string& new_name
301   )
302 {
303   auto o = this->m_ExposedInputs.find( old_name );
304   auto n = this->m_ExposedInputs.find( new_name );
305   if( o != this->m_ExposedInputs.end( ) && n == this->m_ExposedInputs.end( ) )
306   {
307     this->m_ExposedInputs[ new_name ] = o->second;
308     this->m_ExposedInputs.erase( o );
309     return( true );
310   }
311   else
312     return( false );
313 }
314
315 // -------------------------------------------------------------------------
316 bool cpPlugins::Interface::Workspace::
317 RenameExposedOutput(
318   const std::string& old_name, const std::string& new_name
319   )
320 {
321   auto o = this->m_ExposedOutputs.find( old_name );
322   auto n = this->m_ExposedOutputs.find( new_name );
323   if(
324     o != this->m_ExposedOutputs.end( ) && n == this->m_ExposedOutputs.end( )
325     )
326   {
327     this->m_ExposedOutputs[ new_name ] = o->second;
328     this->m_ExposedOutputs.erase( o );
329     return( true );
330   }
331   else
332     return( false );
333 }
334
335 // -------------------------------------------------------------------------
336 std::vector< std::pair< std::string, std::string > >
337 cpPlugins::Interface::Workspace::
338 GetConnections(
339   const std::string& origin, const std::string& destination
340   ) const
341 {
342   std::vector< std::pair< std::string, std::string > > conns;
343   auto orig = this->GetFilter( origin );
344   auto dest = this->GetFilter( destination );
345   if( orig != NULL && dest != NULL )
346   {
347     auto outs = orig->GetOutputsNames( );
348     auto ins = dest->GetInputsNames( );
349     for( auto o = outs.begin( ); o != outs.end( ); ++o )
350     {
351       for( auto i = ins.begin( ); i != ins.end( ); ++i )
352       {
353         unsigned int nInputs = dest->GetInputSize( *i );
354         for( unsigned j = 0; j < nInputs; ++j )
355         {
356           auto od = orig->GetOutput( *o );
357           auto id = dest->GetInput( *i, j );
358           if( od != NULL && od == id )
359             conns.push_back(
360               std::pair< std::string, std::string >( *o, *i )
361               );
362
363         } // rof
364
365       } // rof
366
367     } // rof
368
369   } // fi
370   return( conns );
371 }
372
373 // -------------------------------------------------------------------------
374 void cpPlugins::Interface::Workspace::
375 Connect(
376   const std::string& orig_filter, const std::string& dest_filter,
377   const std::string& output_name, const std::string& input_name
378   )
379 {
380   auto o = this->GetFilter( orig_filter );
381   auto d = this->GetFilter( dest_filter );
382   if( o != NULL && d != NULL )
383   {
384     try
385     {
386       d->AddInput( input_name, o->GetOutput( output_name ) );
387     }
388     catch( std::exception& err )
389     {
390       throw std::logic_error(
391         std::string( "Error connecting \"" ) +
392         output_name + std::string( "@" ) + orig_filter +
393         std::string( "\" with \"" ) +
394         input_name + std::string( "@" ) + dest_filter +
395         std::string( "\": " ) +
396         err.what( )
397         );
398
399     } // yrt
400
401   } // fi
402 }
403
404 // -------------------------------------------------------------------------
405 void cpPlugins::Interface::Workspace::
406 Connect(
407   cpPlugins::BaseObjects::DataObject* output,
408   const std::string& dest_filter, const std::string& input_name
409   )
410 {
411   auto d = this->GetFilter( dest_filter );
412   if( d != NULL )
413     d->AddInput( input_name, output );
414 }
415
416 // -------------------------------------------------------------------------
417 void cpPlugins::Interface::Workspace::
418 Connect(
419   cpPlugins::BaseObjects::DataObject* output,
420   const std::string& exposed_input_name
421   )
422 {
423   auto i = this->m_ExposedInputs.find( exposed_input_name );
424   if( i != this->m_ExposedInputs.end( ) )
425     this->Connect( output, i->second.first, i->second.second );
426 }
427
428 // -------------------------------------------------------------------------
429 void cpPlugins::Interface::Workspace::
430 Disconnect(
431   const std::string& orig_filter, const std::string& dest_filter,
432   const std::string& output_name, const std::string& input_name
433   )
434 {
435   auto orig = this->GetFilter( orig_filter );
436   auto dest = this->GetFilter( dest_filter );
437   if( orig != NULL && dest != NULL )
438   {
439     auto out = orig->GetOutput( output_name );
440     auto in = dest->GetInput( input_name );
441     if( out != NULL && out == in )
442       dest->SetInput(
443         input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
444         );
445
446   } // fi
447 }
448
449 // -------------------------------------------------------------------------
450 void cpPlugins::Interface::Workspace::
451 Disconnect(
452   const std::string& dest_filter, const std::string& input_name
453   )
454 {
455   throw std::logic_error( "Disconnect 1" );
456 }
457
458 // -------------------------------------------------------------------------
459 void cpPlugins::Interface::Workspace::
460 Disconnect( const std::string& dest_filter )
461 {
462   throw std::logic_error( "Disconnect 2" );
463 }
464
465 // -------------------------------------------------------------------------
466 void cpPlugins::Interface::Workspace::
467 Execute( )
468 {
469   for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
470     f->second->Update( );
471 }
472
473 // -------------------------------------------------------------------------
474 void cpPlugins::Interface::Workspace::
475 Execute( const std::string& name )
476 {
477   auto filter = this->GetFilter( name );
478   if( filter != NULL )
479     filter->Update( );
480 }
481
482 // -------------------------------------------------------------------------
483 cpPlugins::Interface::Workspace::
484 Workspace( )
485   : Superclass( ),
486     m_PrintExecution( false )
487 {
488 }
489
490 // -------------------------------------------------------------------------
491 cpPlugins::Interface::Workspace::
492 ~Workspace( )
493 {
494   this->m_ExposedOutputs.clear( );
495   this->m_ExposedInputs.clear( );
496   this->m_Filters.clear( );
497 }
498
499 // eof - $RCSfile$