]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
90c26d5415ca430af81d910a7187f0274eb08cb2
[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         auto od = orig->GetOutput( *o );
354         auto id = dest->GetInput( *i );
355         if( od != NULL && od == id )
356           conns.push_back(
357             std::pair< std::string, std::string >( *o, *i )
358             );
359
360       } // rof
361
362     } // rof
363
364   } // fi
365   return( conns );
366 }
367
368 // -------------------------------------------------------------------------
369 void cpPlugins::Interface::Workspace::
370 Connect(
371   const std::string& orig_filter, const std::string& dest_filter,
372   const std::string& output_name, const std::string& input_name
373   )
374 {
375   auto o = this->GetFilter( orig_filter );
376   auto d = this->GetFilter( dest_filter );
377   if( o != NULL && d != NULL )
378   {
379     try
380     {
381       d->AddInput( input_name, o->GetOutput( output_name ) );
382     }
383     catch( std::exception& err )
384     {
385       throw std::logic_error(
386         std::string( "Error connecting \"" ) +
387         output_name + std::string( "@" ) + orig_filter +
388         std::string( "\" with \"" ) +
389         input_name + std::string( "@" ) + dest_filter +
390         std::string( "\": " ) +
391         err.what( )
392         );
393
394     } // yrt
395
396   } // fi
397 }
398
399 // -------------------------------------------------------------------------
400 void cpPlugins::Interface::Workspace::
401 Connect(
402   cpPlugins::BaseObjects::DataObject* output,
403   const std::string& dest_filter, const std::string& input_name
404   )
405 {
406   auto d = this->GetFilter( dest_filter );
407   if( d != NULL )
408     d->AddInput( input_name, output );
409 }
410
411 // -------------------------------------------------------------------------
412 void cpPlugins::Interface::Workspace::
413 Connect(
414   cpPlugins::BaseObjects::DataObject* output,
415   const std::string& exposed_input_name
416   )
417 {
418   auto i = this->m_ExposedInputs.find( exposed_input_name );
419   if( i != this->m_ExposedInputs.end( ) )
420     this->Connect( output, i->second.first, i->second.second );
421 }
422
423 // -------------------------------------------------------------------------
424 void cpPlugins::Interface::Workspace::
425 Disconnect(
426   const std::string& orig_filter, const std::string& dest_filter,
427   const std::string& output_name, const std::string& input_name
428   )
429 {
430   auto orig = this->GetFilter( orig_filter );
431   auto dest = this->GetFilter( dest_filter );
432   if( orig != NULL && dest != NULL )
433   {
434     auto out = orig->GetOutput( output_name );
435     auto in = dest->GetInput( input_name );
436     if( out != NULL && out == in )
437       dest->SetInput(
438         input_name, ( cpPlugins::BaseObjects::DataObject* )( NULL )
439         );
440
441   } // fi
442 }
443
444 // -------------------------------------------------------------------------
445 void cpPlugins::Interface::Workspace::
446 Disconnect(
447   const std::string& dest_filter, const std::string& input_name
448   )
449 {
450   throw std::logic_error( "Disconnect 1" );
451 }
452
453 // -------------------------------------------------------------------------
454 void cpPlugins::Interface::Workspace::
455 Disconnect( const std::string& dest_filter )
456 {
457   throw std::logic_error( "Disconnect 2" );
458 }
459
460 // -------------------------------------------------------------------------
461 void cpPlugins::Interface::Workspace::
462 Execute( )
463 {
464   for( auto f = this->m_Filters.begin( ); f != this->m_Filters.end( ); ++f )
465     f->second->Update( );
466 }
467
468 // -------------------------------------------------------------------------
469 void cpPlugins::Interface::Workspace::
470 Execute( const std::string& name )
471 {
472   auto filter = this->GetFilter( name );
473   if( filter != NULL )
474     filter->Update( );
475 }
476
477 // -------------------------------------------------------------------------
478 cpPlugins::Interface::Workspace::
479 Workspace( )
480   : Superclass( ),
481     m_PrintExecution( false )
482 {
483 }
484
485 // -------------------------------------------------------------------------
486 cpPlugins::Interface::Workspace::
487 ~Workspace( )
488 {
489   this->m_ExposedOutputs.clear( );
490   this->m_ExposedInputs.clear( );
491   this->m_Filters.clear( );
492 }
493
494 // eof - $RCSfile$