]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Workspace.cxx
b3f6af70a41e280e55d49211b6499563748e120d
[cpPlugins.git] / lib / cpPlugins / Workspace.cxx
1 #include <cpPlugins/Workspace.h>
2 #include <cpPlugins/BaseWidget.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <vtkRenderWindowInteractor.h>
5
6 // -------------------------------------------------------------------------
7 cpPlugins::Workspace::
8 Workspace( )
9   : m_Interface( NULL ),
10     m_PrintExecution( false ),
11     m_MPRViewer( NULL )
12 {
13   this->m_Graph = TGraph::New( );
14 }
15
16 // -------------------------------------------------------------------------
17 cpPlugins::Workspace::
18 ~Workspace( )
19 {
20   this->m_Graph->Clear( );
21 }
22
23 // -------------------------------------------------------------------------
24 cpPlugins::Interface* cpPlugins::Workspace::
25 GetInterface( )
26 {
27   return( this->m_Interface );
28 }
29
30 // -------------------------------------------------------------------------
31 const cpPlugins::Interface* cpPlugins::Workspace::
32 GetInterface( ) const
33 {
34   return( this->m_Interface );
35 }
36
37 // -------------------------------------------------------------------------
38 void cpPlugins::Workspace::
39 SetInterface( cpPlugins::Interface* i )
40 {
41   if( this->m_Interface != i )
42     this->m_Interface = i;
43 }
44
45 // -------------------------------------------------------------------------
46 cpPlugins::Workspace::
47 TGraph* cpPlugins::Workspace::
48 GetGraph( )
49 {
50   return( this->m_Graph );
51 }
52
53 // -------------------------------------------------------------------------
54 const cpPlugins::Workspace::
55 TGraph* cpPlugins::Workspace::
56 GetGraph( ) const
57 {
58   return( this->m_Graph );
59 }
60
61 // -------------------------------------------------------------------------
62 void cpPlugins::Workspace::
63 Clear( )
64 {
65   if( this->m_Graph.IsNotNull( ) )
66     this->m_Graph->Clear( );
67 }
68
69 // -------------------------------------------------------------------------
70 void cpPlugins::Workspace::
71 ClearConnections( )
72 {
73 }
74
75 // -------------------------------------------------------------------------
76 cpPlugins::ProcessObject* cpPlugins::Workspace::
77 GetFilter( const std::string& name )
78 {
79   ProcessObject* f =
80     dynamic_cast< ProcessObject* >(
81       this->m_Graph->GetVertex( name ).GetPointer( )
82       );
83   return( f );
84 }
85
86 // -------------------------------------------------------------------------
87 const cpPlugins::ProcessObject* cpPlugins::Workspace::
88 GetFilter( const std::string& name ) const
89 {
90   const ProcessObject* f =
91     dynamic_cast< const ProcessObject* >(
92       this->m_Graph->GetVertex( name ).GetPointer( )
93       );
94   return( f );
95 }
96
97 // -------------------------------------------------------------------------
98 cpPlugins::DataObject* cpPlugins::Workspace::
99 GetOutput( const std::string& filter, const std::string& output )
100 {
101   auto f = this->GetFilter( filter );
102   if( f != NULL )
103     return( f->GetOutput( output ) );
104   else
105     return( NULL );
106 }
107
108 // -------------------------------------------------------------------------
109 const cpPlugins::DataObject* cpPlugins::Workspace::
110 GetOutput( const std::string& filter, const std::string& output ) const
111 {
112   auto f = this->GetFilter( filter );
113   if( f != NULL )
114     return( f->GetOutput( output ) );
115   else
116     return( NULL );
117 }
118
119 // -------------------------------------------------------------------------
120 bool cpPlugins::Workspace::
121 HasFilter( const std::string& name ) const
122 {
123   if( this->m_Graph->HasVertexIndex( name ) )
124     return( this->GetFilter( name ) != NULL );
125   else
126     return( false );
127 }
128
129 // -------------------------------------------------------------------------
130 cpPlugins::ProcessObject* cpPlugins::Workspace::
131 CreateFilter(
132   const std::string& category,
133   const std::string& filter,
134   const std::string& name
135   )
136 {
137   if( this->m_Interface == NULL )
138     return( NULL );
139
140   // Get or create new filter from name
141   if( !( this->m_Graph->HasVertexIndex( name ) ) )
142   {
143     ProcessObject::Pointer f =
144       this->m_Interface->CreateProcessObject( category, filter );
145     if( f.IsNotNull( ) )
146     {
147       if( f->IsInteractive( ) )
148       {
149         std::vector< void* > interactive_objects;
150         interactive_objects.push_back( this->m_SingleInteractor );
151         interactive_objects.push_back( this->m_MPRViewer );
152         f->SetInteractionObjects( interactive_objects );
153
154       } // fi
155       f->SetPrintExecution( this->m_PrintExecution );
156       Object::Pointer o = f.GetPointer( );
157       this->m_Graph->SetVertex( name, o );
158       f->SetName( name );
159
160     } // fi
161     return( f.GetPointer( ) );
162   }
163   else
164     return( this->GetFilter( name ) );
165 }
166
167 // -------------------------------------------------------------------------
168 bool cpPlugins::Workspace::
169 RenameFilter( const std::string& old_name, const std::string& new_name )
170 {
171   return( this->m_Graph->RenameVertex( old_name, new_name ) );
172 }
173
174 // -------------------------------------------------------------------------
175 bool cpPlugins::Workspace::
176 RemoveFilter( const std::string& name )
177 {
178   auto filter = this->GetFilter( name );
179   if( filter != NULL )
180   {
181     auto vIt = this->m_Graph->BeginVertices( );
182     for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
183     {
184       if( vIt->first != name )
185       {
186         if( this->m_Graph->HasEdge( name, vIt->first ) )
187         {
188           auto edges = this->m_Graph->GetEdges( name, vIt->first );
189           auto other = this->GetFilter( vIt->first );
190           for( auto eIt = edges.begin( ); eIt != edges.end( ); ++eIt )
191             other->SetInput( eIt->first, ( DataObject* )( NULL ) );
192
193         } // fi
194
195         if( this->m_Graph->HasEdge( vIt->first, name ) )
196         {
197           auto edges = this->m_Graph->GetEdges( vIt->first, name );
198           for( auto eIt = edges.begin( ); eIt != edges.end( ); ++eIt )
199             filter->SetInput( eIt->first, ( DataObject* )( NULL ) );
200
201         } // fi
202
203       } // fi
204
205     } // rof
206     this->m_Graph->RemoveVertex( name );
207     return( true );
208   }
209   else
210     return( false );
211 }
212
213 // -------------------------------------------------------------------------
214 void cpPlugins::Workspace::
215 SetParameter( const std::string& name, const std::string& value )
216 {
217   std::vector< std::string > tokens;
218   cpPlugins::TokenizeString( tokens, name, "@" );
219
220   if( this->HasFilter( tokens[ 1 ] ) )
221   {
222     auto filter = this->GetFilter( tokens[ 1 ] );
223     filter->GetParameters( )->SetString( tokens[ 0 ], value );
224
225   } // fi
226 }
227
228 // -------------------------------------------------------------------------
229 void cpPlugins::Workspace::
230 SetPrintExecution( bool b )
231 {
232   this->m_PrintExecution = b;
233   auto vIt = this->m_Graph->BeginVertices( );
234   for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
235   {
236     auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
237     if( f != NULL )
238       f->SetPrintExecution( b );
239
240   } // rof
241 }
242
243 // -------------------------------------------------------------------------
244 void cpPlugins::Workspace::
245 PrintExecutionOn( )
246 {
247   this->SetPrintExecution( true );
248 }
249
250 // -------------------------------------------------------------------------
251 void cpPlugins::Workspace::
252 PrintExecutionOff( )
253 {
254   this->SetPrintExecution( false );
255 }
256
257 // -------------------------------------------------------------------------
258 vtkRenderWindowInteractor* cpPlugins::Workspace::
259 GetSingleInteractor( )
260 {
261   return( this->m_SingleInteractor );
262 }
263
264 // -------------------------------------------------------------------------
265 const vtkRenderWindowInteractor* cpPlugins::Workspace::
266 GetSingleInteractor( ) const
267 {
268   return( this->m_SingleInteractor );
269 }
270
271 // -------------------------------------------------------------------------
272 void cpPlugins::Workspace::
273 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
274 {
275   this->m_SingleInteractor = interactor;
276 }
277
278 // -------------------------------------------------------------------------
279 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
280 GetMPRViewer( )
281 {
282   return( this->m_MPRViewer );
283 }
284
285 // -------------------------------------------------------------------------
286 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
287 GetMPRViewer( ) const
288 {
289   return( this->m_MPRViewer );
290 }
291
292 // -------------------------------------------------------------------------
293 void cpPlugins::Workspace::
294 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
295 {
296   this->m_MPRViewer = wdg;
297 }
298
299 // -------------------------------------------------------------------------
300 bool cpPlugins::Workspace::
301 Connect(
302   const std::string& orig_filter, const std::string& dest_filter,
303   const std::string& output_name, const std::string& input_name
304   )
305 {
306   // Get filters
307   ProcessObject* orig = this->GetFilter( orig_filter );
308   ProcessObject* dest = this->GetFilter( dest_filter );
309   if( orig == NULL || dest == NULL )
310     return( false );
311
312   // Real connection
313   if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
314   {
315     this->m_Graph->AddEdge(
316       orig_filter, dest_filter,
317       TConnection( output_name, input_name )
318       );
319     return( true );
320   }
321   else
322     return( false );
323 }
324
325 // -------------------------------------------------------------------------
326 bool cpPlugins::Workspace::
327 Connect( const OutputPort& port, const std::string& exposed_port )
328 {
329   auto i = this->m_ExposedInputPorts.find( exposed_port );
330   if( i != this->m_ExposedInputPorts.end( ) )
331   {
332     ProcessObject* filter = this->GetFilter( i->second.first );
333     if( filter != NULL )
334       return( filter->SetInputPort( i->second.second, port ) );
335     else
336       return( false );
337   }
338   else
339     return( false );
340 }
341
342 // -------------------------------------------------------------------------
343 bool cpPlugins::Workspace::
344 Disconnect(
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   // Get filters
350   ProcessObject* orig = this->GetFilter( orig_filter );
351   ProcessObject* dest = this->GetFilter( dest_filter );
352   if( orig == NULL || dest == NULL )
353     return( false );
354
355   // Real disconnection
356   if( dest->SetInput( input_name, ( DataObject* )( NULL ) ) )
357   {
358     this->m_Graph->RemoveEdge(
359       orig_filter, dest_filter,
360       TConnection( output_name, input_name )
361       );
362     return( true );
363   }
364   else
365     return( false );
366 }
367
368 // -------------------------------------------------------------------------
369 bool cpPlugins::Workspace::
370 Reduce( const std::string& name )
371 {
372   return( false );
373 }
374
375 // -------------------------------------------------------------------------
376 void cpPlugins::Workspace::
377 ExposeInputPort(
378   const std::string& name,
379   const std::string& filter, const std::string& filter_input
380   )
381 {
382   this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
383 }
384
385 // -------------------------------------------------------------------------
386 void cpPlugins::Workspace::
387 ExposeOutputPort(
388   const std::string& name,
389   const std::string& filter, const std::string& filter_output
390   )
391 {
392   this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
393 }
394
395 // -------------------------------------------------------------------------
396 void cpPlugins::Workspace::
397 HideInputPort( const std::string& name )
398 {
399   auto i = this->m_ExposedInputPorts.find( name );
400   if( i != this->m_ExposedInputPorts.end( ) )
401     this->m_ExposedInputPorts.erase( i );
402 }
403
404 // -------------------------------------------------------------------------
405 void cpPlugins::Workspace::
406 HideOutputPort( const std::string& name )
407 {
408   auto i = this->m_ExposedOutputPorts.find( name );
409   if( i != this->m_ExposedOutputPorts.end( ) )
410     this->m_ExposedOutputPorts.erase( i );
411 }
412
413 // -------------------------------------------------------------------------
414 bool cpPlugins::Workspace::
415 RenameExposedInputPort(
416   const std::string& old_name,
417   const std::string& new_name
418   )
419 {
420   auto o = this->m_ExposedInputPorts.find( old_name );
421   auto n = this->m_ExposedInputPorts.find( new_name );
422   if(
423     o != this->m_ExposedInputPorts.end( ) &&
424     n == this->m_ExposedInputPorts.end( )
425     )
426   {
427     this->m_ExposedInputPorts[ new_name ] = o->second;
428     this->m_ExposedInputPorts.erase( o );
429     return( true );
430   }
431   else
432     return( false );
433 }
434
435 // -------------------------------------------------------------------------
436 bool cpPlugins::Workspace::
437 RenameExposedOutputPort(
438   const std::string& old_name,
439   const std::string& new_name
440   )
441 {
442   auto o = this->m_ExposedOutputPorts.find( old_name );
443   auto n = this->m_ExposedOutputPorts.find( new_name );
444   if(
445     o != this->m_ExposedOutputPorts.end( ) &&
446     n == this->m_ExposedOutputPorts.end( )
447     )
448   {
449     this->m_ExposedOutputPorts[ new_name ] = o->second;
450     this->m_ExposedOutputPorts.erase( o );
451     return( true );
452   }
453   else
454     return( false );
455 }
456
457 // -------------------------------------------------------------------------
458 const cpPlugins::Workspace::
459 TExposedPorts& cpPlugins::Workspace::
460 GetExposedInputPorts( ) const
461 {
462   return( this->m_ExposedInputPorts );
463 }
464
465 // -------------------------------------------------------------------------
466 const cpPlugins::Workspace::
467 TExposedPorts& cpPlugins::Workspace::
468 GetExposedOutputPorts( ) const
469 {
470   return( this->m_ExposedOutputPorts );
471 }
472
473 // -------------------------------------------------------------------------
474 cpPlugins::
475 OutputPort& cpPlugins::Workspace::
476 GetExposedOutput( const std::string& name )
477 {
478   static OutputPort null_port;
479
480   auto i = this->m_ExposedOutputPorts.find( name );
481   if( i != this->m_ExposedOutputPorts.end( ) )
482   {
483     ProcessObject* filter = this->GetFilter( i->second.first );
484     if( filter != NULL )
485       return( filter->GetOutputPort( i->second.second ) );
486
487   } // fi
488   return( null_port );
489 }
490
491 // -------------------------------------------------------------------------
492 const cpPlugins::
493 OutputPort& cpPlugins::Workspace::
494 GetExposedOutput( const std::string& name ) const
495 {
496   static const OutputPort null_port;
497
498   auto i = this->m_ExposedOutputPorts.find( name );
499   if( i != this->m_ExposedOutputPorts.end( ) )
500   {
501     const ProcessObject* filter = this->GetFilter( i->second.first );
502     if( filter != NULL )
503       return( filter->GetOutputPort( i->second.second ) );
504
505   } // fi
506   return( null_port );
507 }
508
509 // -------------------------------------------------------------------------
510 void cpPlugins::Workspace::
511 Execute( )
512 {
513   // Find sinks
514   std::set< std::string > sinks = this->m_Graph->GetSinks( );
515
516   // Update sinks
517   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
518     this->Execute( *sIt );
519 }
520
521 // -------------------------------------------------------------------------
522 void cpPlugins::Workspace::
523 Execute( const std::string& name )
524 {
525   // Get filter
526   ProcessObject* f = this->GetFilter( name );
527   if( f == NULL )
528   {
529     itkGenericExceptionMacro(
530       "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."
531       );
532
533   } // fi
534
535   // Execute
536   f->Update( );
537 }
538
539 // eof - $RCSfile$