]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
Now it's broken :-(
[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 /* TODO
281 cpPlugins::Interface::Workspace::
282 TData* cpPlugins::Interface::Workspace::
283 GetOutput( const std::string& name )
284 {
285   auto port = this->m_ExposedOutputPorts.find( name );
286   if( port != this->m_ExposedOutputPorts.end( ) )
287   {
288     TFilter* f = this->GetFilter( port->second.first );
289     if( f != NULL )
290       return( f->GetOutput< TData >( port->second.second ) );
291     else
292       return( NULL );
293   }
294   else
295     return( NULL );
296 }
297
298 // -------------------------------------------------------------------------
299 const cpPlugins::Interface::Workspace::
300 TData* cpPlugins::Interface::Workspace::
301 GetOutput( const std::string& name ) const
302 {
303   auto port = this->m_ExposedOutputPorts.find( name );
304   if( port != this->m_ExposedOutputPorts.end( ) )
305   {
306     const TFilter* f = this->GetFilter( port->second.first );
307     if( f != NULL )
308       return( f->GetOutput< TData >( port->second.second ) );
309     else
310       return( NULL );
311   }
312   else
313     return( NULL );
314 }
315
316 // -------------------------------------------------------------------------
317 void cpPlugins::Interface::Workspace::
318 ClearInputPorts( )
319 {
320   this->m_ExposedInputPorts.clear( );
321 }
322
323 // -------------------------------------------------------------------------
324 void cpPlugins::Interface::Workspace::
325 ClearOutputPorts( )
326 {
327   this->m_ExposedOutputPorts.clear( );
328 }
329 */
330
331 // -------------------------------------------------------------------------
332 std::string cpPlugins::Interface::Workspace::
333 Execute( QWidget* p )
334 {
335   // Find sinks
336   std::set< std::string > sinks = this->m_Graph->GetSinks( );
337
338   // Update sinks
339   std::string err = "";
340   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
341   {
342     std::string lerr = this->Execute( *sIt, p );
343     if( lerr != "" )
344       err += lerr + std::string( "\n" );
345
346   } // rof
347   return( err );
348 }
349
350 // -------------------------------------------------------------------------
351 std::string cpPlugins::Interface::Workspace::
352 Execute( const std::string& name, QWidget* p )
353 {
354   // Get filter
355   TFilter* f = this->GetFilter( name );
356   if( f == NULL )
357     return(
358       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
359       name + std::string( "\" is not a filter." )
360       );
361
362   // Execute and return
363   if( p != NULL )
364   {
365     auto diag_res = f->ExecConfigurationDialog( p );
366     if( diag_res == TFilter::DialogResult_NoModal )
367       return( f->Update( ) );
368     else
369       return( "" );
370   }
371   else
372     return( f->Update( ) );
373 }
374
375 // eof - $RCSfile$