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