]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
8a588afc4377279f49712bcf47aa3649ce004d85
[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 cpPlugins::Interface::Workspace::
204 TParameters* cpPlugins::Interface::Workspace::
205 GetParameters( const std::string& name )
206 {
207   TFilter* f =
208     dynamic_cast< TFilter* >(
209       this->m_Graph->GetVertex( name ).GetPointer( )
210       );
211   if( f != NULL )
212     return( f->GetParameters( ) );
213   else
214     return( NULL );
215 }
216
217 // -------------------------------------------------------------------------
218 const cpPlugins::Interface::Workspace::
219 TParameters* cpPlugins::Interface::Workspace::
220 GetParameters( const std::string& name ) const
221 {
222   const TFilter* f =
223     dynamic_cast< const TFilter* >(
224       this->m_Graph->GetVertex( name ).GetPointer( )
225       );
226   if( f != NULL )
227     return( f->GetParameters( ) );
228   else
229     return( NULL );
230 }
231
232 // -------------------------------------------------------------------------
233 cpPlugins::Interface::Workspace::
234 TFilter* cpPlugins::Interface::Workspace::
235 GetFilter( const std::string& name )
236 {
237   TFilter* f =
238     dynamic_cast< TFilter* >(
239       this->m_Graph->GetVertex( name ).GetPointer( )
240       );
241   return( f );
242 }
243
244 // -------------------------------------------------------------------------
245 const cpPlugins::Interface::Workspace::
246 TFilter* cpPlugins::Interface::Workspace::
247 GetFilter( const std::string& name ) const
248 {
249   const TFilter* f =
250     dynamic_cast< const TFilter* >(
251       this->m_Graph->GetVertex( name ).GetPointer( )
252       );
253   return( f );
254 }
255
256 // -------------------------------------------------------------------------
257 bool cpPlugins::Interface::Workspace::
258 HasFilter( const std::string& name ) const
259 {
260   if( this->m_Graph->HasVertexIndex( name ) )
261     return( this->GetFilter( name ) != NULL );
262   else
263     return( false );
264 }
265
266 // -------------------------------------------------------------------------
267 bool cpPlugins::Interface::Workspace::
268 Reduce( const std::string& name )
269 {
270   return( false );
271 }
272
273 // -------------------------------------------------------------------------
274 std::string cpPlugins::Interface::Workspace::
275 Execute( )
276 {
277   // Find sinks
278   std::set< std::string > sinks = this->m_Graph->GetSinks( );
279
280   // Update sinks
281   std::string err = "";
282   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
283   {
284     std::string lerr = this->Execute( *sIt, NULL );
285     if( lerr != "" )
286       err += lerr + std::string( "\n" );
287
288   } // rof
289   return( err );
290 }
291
292 // -------------------------------------------------------------------------
293 std::string cpPlugins::Interface::Workspace::
294 Execute( const std::string& name, QWidget* p )
295 {
296   // Get filter
297   TFilter* f =
298     dynamic_cast< TFilter* >(
299       this->m_Graph->GetVertex( name ).GetPointer( )
300       );
301   if( f == NULL )
302     return(
303       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
304       name + std::string( "\" is not a filter." )
305       );
306
307   // Execute and return
308   if( p != NULL )
309   {
310     auto diag_res = f->ExecConfigurationDialog( p );
311     if( diag_res == TFilter::DialogResult_NoModal )
312       return( f->Update( ) );
313     else
314       return( "" );
315   }
316   else
317     return( f->Update( ) );
318 }
319
320 // -------------------------------------------------------------------------
321 void cpPlugins::Interface::Workspace::
322 _UpdateLoadedPluginsInformation( )
323 {
324   /* TODO
325      if( this->m_Interface != NULL )
326      {
327      this->m_LoadedFilters.clear( );
328      const TInterface::TClasses& cls = this->m_Interface->GetClasses( );
329      for( auto i = cls.begin( ); i != cls.end( ); ++i )
330      {
331      TFilter::Pointer o = this->m_Interface->CreateObject( i->first );
332      std::string name = o->GetClassName( );
333      std::string category = o->GetClassCategory( );
334      this->m_LoadedFilters[ category ].insert( name );
335
336      } // rof
337
338      } // fi
339   */
340 }
341
342 // eof - $RCSfile$