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