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