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