]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
Pipeline editor added.
[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 cpPlugins::Interface::Workspace::
110 TGraph* cpPlugins::Interface::Workspace::
111 GetGraph( )
112 {
113   return( this->m_Graph );
114 }
115
116 // -------------------------------------------------------------------------
117 const cpPlugins::Interface::Workspace::
118 TGraph* cpPlugins::Interface::Workspace::
119 GetGraph( ) const
120 {
121   return( this->m_Graph );
122 }
123
124 // -------------------------------------------------------------------------
125 bool cpPlugins::Interface::Workspace::
126 CreateFilter( const std::string& filter, const std::string& name )
127 {
128   // Get or create new filter from name
129   if( !( this->m_Graph->HasVertexIndex( name ) ) )
130   {
131     TFilter::Pointer f = this->m_Interface.CreateObject( filter );
132     if( f.IsNotNull( ) )
133     {
134       f->SetName( name );
135       TObject::Pointer o = f.GetPointer( );
136       this->m_Graph->InsertVertex( name, o );
137       return( true );
138     }
139     else
140       return( false );
141   }
142   else
143     return( true );
144 }
145
146 // -------------------------------------------------------------------------
147 bool cpPlugins::Interface::Workspace::
148 Connect(
149   const std::string& orig_filter, const std::string& dest_filter,
150   const std::string& output_name, const std::string& input_name
151   )
152 {
153   // Get filters
154   TFilter* orig =
155     dynamic_cast< TFilter* >(
156       this->m_Graph->GetVertex( orig_filter ).GetPointer( )
157       );
158   TFilter* dest =
159     dynamic_cast< TFilter* >(
160       this->m_Graph->GetVertex( dest_filter ).GetPointer( )
161       );
162   if( orig == NULL || dest == NULL )
163     return( false );
164
165   // Real connection
166   dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
167   this->m_Graph->AddConnection(
168     orig_filter, dest_filter,
169     TConnection( output_name, input_name )
170     );
171   return( false );
172 }
173
174 // -------------------------------------------------------------------------
175 cpPlugins::Interface::Workspace::
176 TParameters* cpPlugins::Interface::Workspace::
177 GetParameters( const std::string& name )
178 {
179   TFilter* f =
180     dynamic_cast< TFilter* >(
181       this->m_Graph->GetVertex( name ).GetPointer( )
182       );
183   if( f != NULL )
184     return( f->GetParameters( ) );
185   else
186     return( NULL );
187 }
188
189 // -------------------------------------------------------------------------
190 const cpPlugins::Interface::Workspace::
191 TParameters* cpPlugins::Interface::Workspace::
192 GetParameters( const std::string& name ) const
193 {
194   const TFilter* f =
195     dynamic_cast< const TFilter* >(
196       this->m_Graph->GetVertex( name ).GetPointer( )
197       );
198   if( f != NULL )
199     return( f->GetParameters( ) );
200   else
201     return( NULL );
202 }
203
204 // -------------------------------------------------------------------------
205 bool cpPlugins::Interface::Workspace::
206 Reduce( const std::string& name )
207 {
208   return( false );
209 }
210
211 // -------------------------------------------------------------------------
212 std::string cpPlugins::Interface::Workspace::
213 Execute( )
214 {
215   // Find sinks
216   std::set< std::string > sinks = this->m_Graph->GetSinks( );
217
218   // Update sinks
219   std::string err = "";
220   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
221   {
222     std::string lerr = this->Execute( *sIt );
223     if( lerr != "" )
224       err += lerr + std::string( "\n" );
225
226   } // rof
227   return( err );
228 }
229
230 // -------------------------------------------------------------------------
231 std::string cpPlugins::Interface::Workspace::
232 Execute( const std::string& name )
233 {
234   // Get filter
235   TFilter* f =
236     dynamic_cast< TFilter* >(
237       this->m_Graph->GetVertex( name ).GetPointer( )
238       );
239   if( f == NULL )
240     return(
241       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
242       name + std::string( "\" is not a filter." )
243       );
244
245   // Execute and return
246   return( f->Update( ) );
247 }
248
249 // -------------------------------------------------------------------------
250 void cpPlugins::Interface::Workspace::
251 _UpdateLoadedPluginsInformation( )
252 {
253   this->m_LoadedFilters.clear( );
254   const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
255   for( auto i = cls.begin( ); i != cls.end( ); ++i )
256   {
257     TFilter::Pointer o = this->m_Interface.CreateObject( i->first );
258     std::string name = o->GetClassName( );
259     std::string category = o->GetClassCategory( );
260     this->m_LoadedFilters[ category ].insert( name );
261
262   } // rof
263 }
264
265 // eof - $RCSfile$