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