]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Workspace.cxx
dd4ade8e7237fb4e3ad6482808a46ba27d66c29e
[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 void cpPlugins::Workspace::
176 RemoveFilter( const std::string& name )
177 {
178 }
179
180 // -------------------------------------------------------------------------
181 void cpPlugins::Workspace::
182 SetParameter( const std::string& name, const std::string& value )
183 {
184   std::vector< std::string > tokens;
185   cpPlugins::TokenizeString( tokens, name, "@" );
186
187   if( this->HasFilter( tokens[ 1 ] ) )
188   {
189     auto filter = this->GetFilter( tokens[ 1 ] );
190     filter->GetParameters( )->SetString( tokens[ 0 ], value );
191
192   } // fi
193 }
194
195 // -------------------------------------------------------------------------
196 void cpPlugins::Workspace::
197 SetPrintExecution( bool b )
198 {
199   this->m_PrintExecution = b;
200   auto vIt = this->m_Graph->BeginVertices( );
201   for( ; vIt != this->m_Graph->EndVertices( ); ++vIt )
202   {
203     auto f = dynamic_cast< ProcessObject* >( vIt->second.GetPointer( ) );
204     if( f != NULL )
205       f->SetPrintExecution( b );
206
207   } // rof
208 }
209
210 // -------------------------------------------------------------------------
211 void cpPlugins::Workspace::
212 PrintExecutionOn( )
213 {
214   this->SetPrintExecution( true );
215 }
216
217 // -------------------------------------------------------------------------
218 void cpPlugins::Workspace::
219 PrintExecutionOff( )
220 {
221   this->SetPrintExecution( false );
222 }
223
224 // -------------------------------------------------------------------------
225 vtkRenderWindowInteractor* cpPlugins::Workspace::
226 GetSingleInteractor( )
227 {
228   return( this->m_SingleInteractor );
229 }
230
231 // -------------------------------------------------------------------------
232 const vtkRenderWindowInteractor* cpPlugins::Workspace::
233 GetSingleInteractor( ) const
234 {
235   return( this->m_SingleInteractor );
236 }
237
238 // -------------------------------------------------------------------------
239 void cpPlugins::Workspace::
240 SetSingleInteractor( vtkRenderWindowInteractor* interactor )
241 {
242   this->m_SingleInteractor = interactor;
243 }
244
245 // -------------------------------------------------------------------------
246 cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
247 GetMPRViewer( )
248 {
249   return( this->m_MPRViewer );
250 }
251
252 // -------------------------------------------------------------------------
253 const cpPlugins::Workspace::TMPRWidget* cpPlugins::Workspace::
254 GetMPRViewer( ) const
255 {
256   return( this->m_MPRViewer );
257 }
258
259 // -------------------------------------------------------------------------
260 void cpPlugins::Workspace::
261 SetMPRViewer( cpPlugins::Workspace::TMPRWidget* wdg )
262 {
263   this->m_MPRViewer = wdg;
264 }
265
266 // -------------------------------------------------------------------------
267 bool cpPlugins::Workspace::
268 Connect(
269   const std::string& orig_filter, const std::string& dest_filter,
270   const std::string& output_name, const std::string& input_name
271   )
272 {
273   // Get filters
274   ProcessObject* orig = this->GetFilter( orig_filter );
275   ProcessObject* dest = this->GetFilter( dest_filter );
276   if( orig == NULL || dest == NULL )
277     return( false );
278
279   // Real connection
280   if( dest->SetInputPort( input_name, orig->GetOutputPort( output_name ) ) )
281   {
282     this->m_Graph->AddEdge(
283       orig_filter, dest_filter,
284       TConnection( output_name, input_name )
285       );
286     return( true );
287   }
288   else
289     return( false );
290 }
291
292 // -------------------------------------------------------------------------
293 bool cpPlugins::Workspace::
294 Connect( const OutputPort& port, const std::string& exposed_port )
295 {
296   auto i = this->m_ExposedInputPorts.find( exposed_port );
297   if( i != this->m_ExposedInputPorts.end( ) )
298   {
299     ProcessObject* filter = this->GetFilter( i->second.first );
300     if( filter != NULL )
301       return( filter->SetInputPort( i->second.second, port ) );
302     else
303       return( false );
304   }
305   else
306     return( false );
307 }
308
309 // -------------------------------------------------------------------------
310 bool cpPlugins::Workspace::
311 Reduce( const std::string& name )
312 {
313   return( false );
314 }
315
316 // -------------------------------------------------------------------------
317 void cpPlugins::Workspace::
318 ExposeInputPort(
319   const std::string& name,
320   const std::string& filter, const std::string& filter_input
321   )
322 {
323   this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
324 }
325
326 // -------------------------------------------------------------------------
327 void cpPlugins::Workspace::
328 ExposeOutputPort(
329   const std::string& name,
330   const std::string& filter, const std::string& filter_output
331   )
332 {
333   this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
334 }
335
336 // -------------------------------------------------------------------------
337 void cpPlugins::Workspace::
338 HideInputPort( const std::string& name )
339 {
340   auto i = this->m_ExposedInputPorts.find( name );
341   if( i != this->m_ExposedInputPorts.end( ) )
342     this->m_ExposedInputPorts.erase( i );
343 }
344
345 // -------------------------------------------------------------------------
346 void cpPlugins::Workspace::
347 HideOutputPort( const std::string& name )
348 {
349   auto i = this->m_ExposedOutputPorts.find( name );
350   if( i != this->m_ExposedOutputPorts.end( ) )
351     this->m_ExposedOutputPorts.erase( i );
352 }
353
354 // -------------------------------------------------------------------------
355 bool cpPlugins::Workspace::
356 RenameExposedInputPort(
357   const std::string& old_name,
358   const std::string& new_name
359   )
360 {
361   auto o = this->m_ExposedInputPorts.find( old_name );
362   auto n = this->m_ExposedInputPorts.find( new_name );
363   if(
364     o != this->m_ExposedInputPorts.end( ) &&
365     n == this->m_ExposedInputPorts.end( )
366     )
367   {
368     this->m_ExposedInputPorts[ new_name ] = o->second;
369     this->m_ExposedInputPorts.erase( o );
370     return( true );
371   }
372   else
373     return( false );
374 }
375
376 // -------------------------------------------------------------------------
377 bool cpPlugins::Workspace::
378 RenameExposedOutputPort(
379   const std::string& old_name,
380   const std::string& new_name
381   )
382 {
383   auto o = this->m_ExposedOutputPorts.find( old_name );
384   auto n = this->m_ExposedOutputPorts.find( new_name );
385   if(
386     o != this->m_ExposedOutputPorts.end( ) &&
387     n == this->m_ExposedOutputPorts.end( )
388     )
389   {
390     this->m_ExposedOutputPorts[ new_name ] = o->second;
391     this->m_ExposedOutputPorts.erase( o );
392     return( true );
393   }
394   else
395     return( false );
396 }
397
398 // -------------------------------------------------------------------------
399 const cpPlugins::Workspace::
400 TExposedPorts& cpPlugins::Workspace::
401 GetExposedInputPorts( ) const
402 {
403   return( this->m_ExposedInputPorts );
404 }
405
406 // -------------------------------------------------------------------------
407 const cpPlugins::Workspace::
408 TExposedPorts& cpPlugins::Workspace::
409 GetExposedOutputPorts( ) const
410 {
411   return( this->m_ExposedOutputPorts );
412 }
413
414 // -------------------------------------------------------------------------
415 cpPlugins::
416 OutputPort& cpPlugins::Workspace::
417 GetExposedOutput( const std::string& name )
418 {
419   static OutputPort null_port;
420
421   auto i = this->m_ExposedOutputPorts.find( name );
422   if( i != this->m_ExposedOutputPorts.end( ) )
423   {
424     ProcessObject* filter = this->GetFilter( i->second.first );
425     if( filter != NULL )
426       return( filter->GetOutputPort( i->second.second ) );
427
428   } // fi
429   return( null_port );
430 }
431
432 // -------------------------------------------------------------------------
433 const cpPlugins::
434 OutputPort& cpPlugins::Workspace::
435 GetExposedOutput( const std::string& name ) const
436 {
437   static const OutputPort null_port;
438
439   auto i = this->m_ExposedOutputPorts.find( name );
440   if( i != this->m_ExposedOutputPorts.end( ) )
441   {
442     const ProcessObject* filter = this->GetFilter( i->second.first );
443     if( filter != NULL )
444       return( filter->GetOutputPort( i->second.second ) );
445
446   } // fi
447   return( null_port );
448 }
449
450 // -------------------------------------------------------------------------
451 void cpPlugins::Workspace::
452 Execute( )
453 {
454   // Find sinks
455   std::set< std::string > sinks = this->m_Graph->GetSinks( );
456
457   // Update sinks
458   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
459     this->Execute( *sIt );
460 }
461
462 // -------------------------------------------------------------------------
463 void cpPlugins::Workspace::
464 Execute( const std::string& name )
465 {
466   // Get filter
467   ProcessObject* f = this->GetFilter( name );
468   if( f == NULL )
469   {
470     itkGenericExceptionMacro( 
471       "cpPlugins::Workspace: Vertex \"" << name << "\" is not a filter."
472       );
473
474   } // fi
475
476   // Execute
477   f->Update( );
478 }
479
480 // eof - $RCSfile$