]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
ab1891a1cd073859e0467c11e431e61677c50704
[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 bool cpPlugins::Interface::Workspace::
110 CreateFilter( const std::string& filter, const std::string& name )
111 {
112   if( this->m_Plugins == NULL )
113     return( false );
114
115   // Get or create new filter from name
116   if( !( this->m_Graph->HasVertexIndex( name ) ) )
117   {
118     TFilter::Pointer f = this->m_Plugins->CreateObject( filter );
119     if( f.IsNotNull( ) )
120     {
121       f->SetName( name );
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       return( true );
128     }
129     else
130       return( false );
131   }
132   else
133     return( true );
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   dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
215   this->m_Graph->AddEdge(
216     orig_filter, dest_filter,
217     TConnection( output_name, input_name )
218     );
219   return( false );
220 }
221
222 // -------------------------------------------------------------------------
223 bool cpPlugins::Interface::Workspace::
224 Connect( TData* input_object, const std::string& input_name )
225 {
226   auto port = this->m_ExposedInputPorts.find( input_name );
227   if( port != this->m_ExposedInputPorts.end( ) )
228   {
229     TFilter* filter = this->GetFilter( port->second.first );
230     if( filter != NULL )
231     {
232       filter->SetInput( port->second.second, input_object );
233       return( true );
234     }
235     else
236       return( false );
237   }
238   else
239     return( false );
240 }
241
242 // -------------------------------------------------------------------------
243 bool cpPlugins::Interface::Workspace::
244 Reduce( const std::string& name )
245 {
246   return( false );
247 }
248
249 // -------------------------------------------------------------------------
250 void cpPlugins::Interface::Workspace::
251 ExposeInputPort(
252   const std::string& name,
253   const std::string& filter, const std::string& filter_input
254   )
255 {
256   this->m_ExposedInputPorts[ name ] = TExposedPort( filter, filter_input );
257 }
258
259 // -------------------------------------------------------------------------
260 void cpPlugins::Interface::Workspace::
261 ExposeOutputPort(
262   const std::string& name,
263   const std::string& filter, const std::string& filter_output
264   )
265 {
266   this->m_ExposedOutputPorts[ name ] = TExposedPort( filter, filter_output );
267 }
268
269 // -------------------------------------------------------------------------
270 void cpPlugins::Interface::Workspace::
271 HideInputPort( const std::string& name )
272 {
273   auto i = this->m_ExposedInputPorts.find( name );
274   if( i != this->m_ExposedInputPorts.end( ) )
275     this->m_ExposedInputPorts.erase( i );
276 }
277
278 // -------------------------------------------------------------------------
279 void cpPlugins::Interface::Workspace::
280 HideOutputPort( const std::string& name )
281 {
282   auto i = this->m_ExposedOutputPorts.find( name );
283   if( i != this->m_ExposedOutputPorts.end( ) )
284     this->m_ExposedOutputPorts.erase( i );
285 }
286
287 // -------------------------------------------------------------------------
288 bool cpPlugins::Interface::Workspace::
289 RenameExposedInputPort(
290   const std::string& old_name,
291   const std::string& new_name
292   )
293 {
294   auto o = this->m_ExposedInputPorts.find( old_name );
295   auto n = this->m_ExposedInputPorts.find( new_name );
296   if(
297     o != this->m_ExposedInputPorts.end( ) &&
298     n == this->m_ExposedInputPorts.end( )
299     )
300   {
301     this->m_ExposedInputPorts[ new_name ] = o->second;
302     this->m_ExposedInputPorts.erase( o );
303   }
304   else
305     return( false );
306 }
307
308 // -------------------------------------------------------------------------
309 bool cpPlugins::Interface::Workspace::
310 RenameExposedOutputPort(
311   const std::string& old_name,
312   const std::string& new_name
313   )
314 {
315   auto o = this->m_ExposedOutputPorts.find( old_name );
316   auto n = this->m_ExposedOutputPorts.find( new_name );
317   if(
318     o != this->m_ExposedOutputPorts.end( ) &&
319     n == this->m_ExposedOutputPorts.end( )
320     )
321   {
322     this->m_ExposedOutputPorts[ new_name ] = o->second;
323     this->m_ExposedOutputPorts.erase( o );
324   }
325   else
326     return( false );
327 }
328
329 // -------------------------------------------------------------------------
330 const cpPlugins::Interface::Workspace::
331 TExposedPorts& cpPlugins::Interface::Workspace::
332 GetExposedInputPorts( ) const
333 {
334   return( this->m_ExposedInputPorts );
335 }
336
337 // -------------------------------------------------------------------------
338 const cpPlugins::Interface::Workspace::
339 TExposedPorts& cpPlugins::Interface::Workspace::
340 GetExposedOutputPorts( ) const
341 {
342   return( this->m_ExposedOutputPorts );
343 }
344
345 // -------------------------------------------------------------------------
346 std::string cpPlugins::Interface::Workspace::
347 Execute( )
348 {
349   // Find sinks
350   std::set< std::string > sinks = this->m_Graph->GetSinks( );
351
352   // Update sinks
353   std::string err = "";
354   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
355   {
356     std::string lerr = this->Execute( *sIt );
357     if( lerr != "" )
358       err += lerr + std::string( "\n" );
359
360   } // rof
361   return( err );
362 }
363
364 // -------------------------------------------------------------------------
365 std::string cpPlugins::Interface::Workspace::
366 Execute( const std::string& name )
367 {
368   // Get filter
369   TFilter* f = this->GetFilter( name );
370   if( f == NULL )
371     return(
372       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
373       name + std::string( "\" is not a filter." )
374       );
375
376   // Execute and return
377   return( f->Update( ) );
378 }
379
380 // eof - $RCSfile$