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