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