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