]> 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   // Get or create new filter from name
137   if( !( this->m_Graph->HasVertexIndex( name ) ) )
138   {
139     TFilter::Pointer f = this->m_Interface.CreateObject( filter );
140     if( f.IsNotNull( ) )
141     {
142       f->SetName( name );
143       TObject::Pointer o = f.GetPointer( );
144       this->m_Graph->InsertVertex( name, o );
145       return( true );
146     }
147     else
148       return( false );
149   }
150   else
151     return( true );
152 }
153
154 // -------------------------------------------------------------------------
155 bool cpPlugins::Interface::Workspace::
156 Connect(
157   const std::string& orig_filter, const std::string& dest_filter,
158   const std::string& output_name, const std::string& input_name
159   )
160 {
161   // Get filters
162   TFilter* orig =
163     dynamic_cast< TFilter* >(
164       this->m_Graph->GetVertex( orig_filter ).GetPointer( )
165       );
166   TFilter* dest =
167     dynamic_cast< TFilter* >(
168       this->m_Graph->GetVertex( dest_filter ).GetPointer( )
169       );
170   if( orig == NULL || dest == NULL )
171     return( false );
172
173   // Real connection
174   dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
175   this->m_Graph->AddConnection(
176     orig_filter, dest_filter,
177     TConnection( output_name, input_name )
178     );
179   return( false );
180 }
181
182 // -------------------------------------------------------------------------
183 cpPlugins::Interface::Workspace::
184 TParameters* cpPlugins::Interface::Workspace::
185 GetParameters( const std::string& name )
186 {
187   TFilter* f =
188     dynamic_cast< TFilter* >(
189       this->m_Graph->GetVertex( name ).GetPointer( )
190       );
191   if( f != NULL )
192     return( f->GetParameters( ) );
193   else
194     return( NULL );
195 }
196
197 // -------------------------------------------------------------------------
198 const cpPlugins::Interface::Workspace::
199 TParameters* cpPlugins::Interface::Workspace::
200 GetParameters( const std::string& name ) const
201 {
202   const TFilter* f =
203     dynamic_cast< const TFilter* >(
204       this->m_Graph->GetVertex( name ).GetPointer( )
205       );
206   if( f != NULL )
207     return( f->GetParameters( ) );
208   else
209     return( NULL );
210 }
211
212 // -------------------------------------------------------------------------
213 cpPlugins::Interface::Workspace::
214 TFilter* cpPlugins::Interface::Workspace::
215 GetFilter( const std::string& name )
216 {
217   TFilter* f =
218     dynamic_cast< TFilter* >(
219       this->m_Graph->GetVertex( name ).GetPointer( )
220       );
221   return( f );
222 }
223
224 // -------------------------------------------------------------------------
225 const cpPlugins::Interface::Workspace::
226 TFilter* cpPlugins::Interface::Workspace::
227 GetFilter( const std::string& name ) const
228 {
229   const TFilter* f =
230     dynamic_cast< const TFilter* >(
231       this->m_Graph->GetVertex( name ).GetPointer( )
232       );
233   return( f );
234 }
235
236 // -------------------------------------------------------------------------
237 bool cpPlugins::Interface::Workspace::
238 HasFilter( const std::string& name ) const
239 {
240   if( this->m_Graph->HasVertexIndex( name ) )
241     return( this->GetFilter( name ) != NULL );
242   else
243     return( false );
244 }
245
246 // -------------------------------------------------------------------------
247 bool cpPlugins::Interface::Workspace::
248 Reduce( const std::string& name )
249 {
250   return( false );
251 }
252
253 // -------------------------------------------------------------------------
254 std::string cpPlugins::Interface::Workspace::
255 Execute( )
256 {
257   // Find sinks
258   std::set< std::string > sinks = this->m_Graph->GetSinks( );
259
260   // Update sinks
261   std::string err = "";
262   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
263   {
264     std::string lerr = this->Execute( *sIt, NULL );
265     if( lerr != "" )
266       err += lerr + std::string( "\n" );
267
268   } // rof
269   return( err );
270 }
271
272 // -------------------------------------------------------------------------
273 std::string cpPlugins::Interface::Workspace::
274 Execute( const std::string& name, QWidget* p )
275 {
276   // Get filter
277   TFilter* f =
278     dynamic_cast< TFilter* >(
279       this->m_Graph->GetVertex( name ).GetPointer( )
280       );
281   if( f == NULL )
282     return(
283       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
284       name + std::string( "\" is not a filter." )
285       );
286
287   // Execute and return
288   if( p != NULL )
289   {
290     auto diag_res = f->ExecConfigurationDialog( p );
291     if( diag_res == TFilter::DialogResult_NoModal )
292       return( f->Update( ) );
293     else
294       return( "" );
295   }
296   else
297     return( f->Update( ) );
298 }
299
300 // -------------------------------------------------------------------------
301 void cpPlugins::Interface::Workspace::
302 _UpdateLoadedPluginsInformation( )
303 {
304   this->m_LoadedFilters.clear( );
305   const TInterface::TClasses& cls = this->m_Interface.GetClasses( );
306   for( auto i = cls.begin( ); i != cls.end( ); ++i )
307   {
308     TFilter::Pointer o = this->m_Interface.CreateObject( i->first );
309     std::string name = o->GetClassName( );
310     std::string category = o->GetClassCategory( );
311     this->m_LoadedFilters[ category ].insert( name );
312
313   } // rof
314 }
315
316 // eof - $RCSfile$