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