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