]> 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_Interface( 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 TInterface* cpPlugins::Interface::Workspace::
20 GetInterface( )
21 {
22   return( this->m_Interface );
23 }
24
25 // -------------------------------------------------------------------------
26 void cpPlugins::Interface::Workspace::
27 SetInterface( TInterface* i )
28 {
29   if( this->m_Interface != i )
30     this->m_Interface = i;
31 }
32
33 /* TODO
34    bool cpPlugins::Interface::Workspace::
35    LoadPluginsPath( const std::string& path, bool r )
36    {
37    // Load all plugins from given folder
38    std::list< std::string > files =
39    this->m_Interface.LoadFromFolder( path, r );
40    
41    // Update a simple track
42    bool ret = false;
43    if( files.size( ) > 0 )
44    {
45    for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
46    {
47    this->m_LoadedPlugins.insert( *fIt );
48    this->m_LastLoadedPlugin = *fIt;
49    
50    } // rof
51    this->_UpdateLoadedPluginsInformation( );
52    ret = true;
53    
54    } // fi
55    return( ret );
56    }
57
58    // -------------------------------------------------------------------------
59    bool cpPlugins::Interface::Workspace::
60    LoadPlugins( const std::string& fname )
61    {
62    // Is it already loaded?
63    bool ret = true;
64    if( this->m_LoadedPlugins.find( fname ) == this->m_LoadedPlugins.end( ) )
65    {
66    // Was it succesfully loaded?
67    ret = this->m_Interface.Load( fname );
68    
69    // Update a simple track
70    if( ret )
71    {
72    this->m_LoadedPlugins.insert( fname );
73    this->m_LastLoadedPlugin = fname;
74    this->_UpdateLoadedPluginsInformation( );
75    
76    } // fi
77    
78    } // fi
79    return( ret );
80    }
81    
82    // -------------------------------------------------------------------------
83    const cpPlugins::Interface::Workspace::
84    TStringContainer& cpPlugins::Interface::Workspace::
85    GetLoadedPlugins( ) const
86    {
87    return( this->m_LoadedPlugins );
88    }
89    
90    // -------------------------------------------------------------------------
91    void cpPlugins::Interface::Workspace::
92    GetLoadedPluginCategories( TStringContainer& categories ) const
93    {
94    categories.clear( );
95    auto fIt = this->m_LoadedFilters.begin( );
96    for( ; fIt != this->m_LoadedFilters.end( ); ++fIt )
97    categories.insert( fIt->first );
98    }
99    
100    // -------------------------------------------------------------------------
101    void cpPlugins::Interface::Workspace::
102    GetLoadedPluginFilters( TStringContainer& filters ) const
103    {
104    filters.clear( );
105    auto pIt = this->m_LoadedFilters.begin( );
106    for( ; pIt != this->m_LoadedFilters.end( ); ++pIt )
107    for( auto fIt = pIt->second.begin( ); fIt != pIt->second.end( ); ++fIt )
108    filters.insert( *fIt );
109    }
110    
111    // -------------------------------------------------------------------------
112    const cpPlugins::Interface::Workspace::
113    TStringContainer& cpPlugins::Interface::Workspace::
114    GetLoadedPluginFilters( const std::string& category ) const
115    {
116    static const TStringContainer EMPTY;
117    auto pIt = this->m_LoadedFilters.find( category );
118    if( pIt != this->m_LoadedFilters.end( ) )
119    return( pIt->second );
120    else
121    return( EMPTY );
122    }
123 */
124
125 // -------------------------------------------------------------------------
126 void cpPlugins::Interface::Workspace::
127 Clear( )
128 {
129   if( this->m_Graph.IsNotNull( ) )
130     this->m_Graph->Clear( );
131 }
132
133 // -------------------------------------------------------------------------
134 cpPlugins::Interface::Workspace::
135 TGraph* cpPlugins::Interface::Workspace::
136 GetGraph( )
137 {
138   return( this->m_Graph );
139 }
140
141 // -------------------------------------------------------------------------
142 const cpPlugins::Interface::Workspace::
143 TGraph* cpPlugins::Interface::Workspace::
144 GetGraph( ) const
145 {
146   return( this->m_Graph );
147 }
148
149 // -------------------------------------------------------------------------
150 bool cpPlugins::Interface::Workspace::
151 CreateFilter( const std::string& filter, const std::string& name )
152 {
153   if( this->m_Interface == NULL )
154     return( false );
155
156   // Get or create new filter from name
157   if( !( this->m_Graph->HasVertexIndex( name ) ) )
158   {
159     TFilter::Pointer f = this->m_Interface->CreateObject( filter );
160     if( f.IsNotNull( ) )
161     {
162       f->SetName( name );
163       TObject::Pointer o = f.GetPointer( );
164       this->m_Graph->SetVertex( name, o );
165       return( true );
166     }
167     else
168       return( false );
169   }
170   else
171     return( true );
172 }
173
174 // -------------------------------------------------------------------------
175 bool cpPlugins::Interface::Workspace::
176 Connect(
177   const std::string& orig_filter, const std::string& dest_filter,
178   const std::string& output_name, const std::string& input_name
179   )
180 {
181   // Get filters
182   TFilter* orig =
183     dynamic_cast< TFilter* >(
184       this->m_Graph->GetVertex( orig_filter ).GetPointer( )
185       );
186   TFilter* dest =
187     dynamic_cast< TFilter* >(
188       this->m_Graph->GetVertex( dest_filter ).GetPointer( )
189       );
190   if( orig == NULL || dest == NULL )
191     return( false );
192
193   // Real connection
194   dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
195   this->m_Graph->AddConnection(
196     orig_filter, dest_filter,
197     TConnection( output_name, input_name )
198     );
199   return( false );
200 }
201
202 // -------------------------------------------------------------------------
203 bool cpPlugins::Interface::Workspace::
204 Connect( const std::string& i, const std::string& o )
205 {
206   auto ii = this->m_InputPorts.find( i );
207   auto oi = this->m_OutputPorts.find( o );
208   if( ii != this->m_InputPorts.end( ) && oi != this->m_OutputPorts.end( ) )
209     return(
210       this->Connect(
211         oi->second.first,
212         ii->second.first,
213         oi->second.second,
214         ii->second.second
215         )
216       );
217   else
218     return( false );
219 }
220
221 // -------------------------------------------------------------------------
222 cpPlugins::Interface::Workspace::
223 TParameters* cpPlugins::Interface::Workspace::
224 GetParameters( const std::string& name )
225 {
226   TFilter* f =
227     dynamic_cast< TFilter* >(
228       this->m_Graph->GetVertex( name ).GetPointer( )
229       );
230   if( f != NULL )
231     return( f->GetParameters( ) );
232   else
233     return( NULL );
234 }
235
236 // -------------------------------------------------------------------------
237 const cpPlugins::Interface::Workspace::
238 TParameters* cpPlugins::Interface::Workspace::
239 GetParameters( const std::string& name ) const
240 {
241   const TFilter* f =
242     dynamic_cast< const TFilter* >(
243       this->m_Graph->GetVertex( name ).GetPointer( )
244       );
245   if( f != NULL )
246     return( f->GetParameters( ) );
247   else
248     return( NULL );
249 }
250
251 // -------------------------------------------------------------------------
252 cpPlugins::Interface::Workspace::
253 TFilter* cpPlugins::Interface::Workspace::
254 GetFilter( const std::string& name )
255 {
256   TFilter* f =
257     dynamic_cast< TFilter* >(
258       this->m_Graph->GetVertex( name ).GetPointer( )
259       );
260   return( f );
261 }
262
263 // -------------------------------------------------------------------------
264 const cpPlugins::Interface::Workspace::
265 TFilter* cpPlugins::Interface::Workspace::
266 GetFilter( const std::string& name ) const
267 {
268   const TFilter* f =
269     dynamic_cast< const TFilter* >(
270       this->m_Graph->GetVertex( name ).GetPointer( )
271       );
272   return( f );
273 }
274
275 // -------------------------------------------------------------------------
276 bool cpPlugins::Interface::Workspace::
277 HasFilter( const std::string& name ) const
278 {
279   if( this->m_Graph->HasVertexIndex( name ) )
280     return( this->GetFilter( name ) != NULL );
281   else
282     return( false );
283 }
284
285 // -------------------------------------------------------------------------
286 bool cpPlugins::Interface::Workspace::
287 Reduce( const std::string& name )
288 {
289   return( false );
290 }
291
292 // -------------------------------------------------------------------------
293 void cpPlugins::Interface::Workspace::
294 AddInputPort(
295   const std::string& name,
296   const std::string& filter, const std::string& filter_input
297   )
298 {
299   this->m_InputPorts[ name ] = TGlobalPort( filter, filter_input );
300 }
301
302 // -------------------------------------------------------------------------
303 void cpPlugins::Interface::Workspace::
304 AddOutputPort(
305   const std::string& name,
306   const std::string& filter, const std::string& filter_output
307   )
308 {
309   this->m_OutputPorts[ name ] = TGlobalPort( filter, filter_output );
310 }
311
312 // -------------------------------------------------------------------------
313 void cpPlugins::Interface::Workspace::
314 ClearInputPorts( )
315 {
316   this->m_InputPorts.clear( );
317 }
318
319 // -------------------------------------------------------------------------
320 void cpPlugins::Interface::Workspace::
321 ClearOutputPorts( )
322 {
323   this->m_OutputPorts.clear( );
324 }
325
326 // -------------------------------------------------------------------------
327 std::string cpPlugins::Interface::Workspace::
328 Execute( )
329 {
330   // Find sinks
331   std::set< std::string > sinks = this->m_Graph->GetSinks( );
332
333   // Update sinks
334   std::string err = "";
335   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
336   {
337     std::string lerr = this->Execute( *sIt, NULL );
338     if( lerr != "" )
339       err += lerr + std::string( "\n" );
340
341   } // rof
342   return( err );
343 }
344
345 // -------------------------------------------------------------------------
346 std::string cpPlugins::Interface::Workspace::
347 Execute( const std::string& name, QWidget* p )
348 {
349   // Get filter
350   TFilter* f =
351     dynamic_cast< TFilter* >(
352       this->m_Graph->GetVertex( name ).GetPointer( )
353       );
354   if( f == NULL )
355     return(
356       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
357       name + std::string( "\" is not a filter." )
358       );
359
360   // Execute and return
361   if( p != NULL )
362   {
363     auto diag_res = f->ExecConfigurationDialog( p );
364     if( diag_res == TFilter::DialogResult_NoModal )
365       return( f->Update( ) );
366     else
367       return( "" );
368   }
369   else
370     return( f->Update( ) );
371 }
372
373 // -------------------------------------------------------------------------
374 void cpPlugins::Interface::Workspace::
375 _UpdateLoadedPluginsInformation( )
376 {
377   /* TODO
378      if( this->m_Interface != NULL )
379      {
380      this->m_LoadedFilters.clear( );
381      const TInterface::TClasses& cls = this->m_Interface->GetClasses( );
382      for( auto i = cls.begin( ); i != cls.end( ); ++i )
383      {
384      TFilter::Pointer o = this->m_Interface->CreateObject( i->first );
385      std::string name = o->GetClassName( );
386      std::string category = o->GetClassCategory( );
387      this->m_LoadedFilters[ category ].insert( name );
388
389      } // rof
390
391      } // fi
392   */
393 }
394
395 // eof - $RCSfile$