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