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