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