]> 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 // -------------------------------------------------------------------------
34 void cpPlugins::Interface::Workspace::
35 Clear( )
36 {
37   if( this->m_Graph.IsNotNull( ) )
38     this->m_Graph->Clear( );
39 }
40
41 // -------------------------------------------------------------------------
42 cpPlugins::Interface::Workspace::
43 TGraph* cpPlugins::Interface::Workspace::
44 GetGraph( )
45 {
46   return( this->m_Graph );
47 }
48
49 // -------------------------------------------------------------------------
50 const cpPlugins::Interface::Workspace::
51 TGraph* cpPlugins::Interface::Workspace::
52 GetGraph( ) const
53 {
54   return( this->m_Graph );
55 }
56
57 // -------------------------------------------------------------------------
58 bool cpPlugins::Interface::Workspace::
59 CreateFilter( const std::string& filter, const std::string& name )
60 {
61   if( this->m_Interface == NULL )
62     return( false );
63
64   // Get or create new filter from name
65   if( !( this->m_Graph->HasVertexIndex( name ) ) )
66   {
67     TFilter::Pointer f = this->m_Interface->CreateObject( filter );
68     if( f.IsNotNull( ) )
69     {
70       f->SetName( name );
71       TObject::Pointer o = f.GetPointer( );
72       this->m_Graph->SetVertex( name, o );
73       return( true );
74     }
75     else
76       return( false );
77   }
78   else
79     return( true );
80 }
81
82 // -------------------------------------------------------------------------
83 bool cpPlugins::Interface::Workspace::
84 Connect(
85   const std::string& orig_filter, const std::string& dest_filter,
86   const std::string& output_name, const std::string& input_name
87   )
88 {
89   // Get filters
90   TFilter* orig = this->GetFilter( orig_filter );
91   TFilter* dest = this->GetFilter( dest_filter );
92   if( orig == NULL || dest == NULL )
93     return( false );
94
95   // Real connection
96   dest->SetInput( input_name, orig->GetOutput< TData >( output_name ) );
97   this->m_Graph->AddConnection(
98     orig_filter, dest_filter,
99     TConnection( output_name, input_name )
100     );
101   return( false );
102 }
103
104 // -------------------------------------------------------------------------
105 bool cpPlugins::Interface::Workspace::
106 Connect( TData* input_object, const std::string& input_name )
107 {
108   auto port = this->m_InputPorts.find( input_name );
109   if( port != this->m_InputPorts.end( ) )
110   {
111     TFilter* filter = this->GetFilter( port->second.first );
112     if( filter != NULL )
113     {
114       filter->SetInput( port->second.second, input_object );
115       return( true );
116     }
117     else
118       return( false );
119   }
120   else
121     return( false );
122 }
123
124 // -------------------------------------------------------------------------
125 cpPlugins::Interface::Workspace::
126 TParameters* cpPlugins::Interface::Workspace::
127 GetParameters( const std::string& name )
128 {
129   TFilter* f = this->GetFilter( name );
130   if( f != NULL )
131     return( f->GetParameters( ) );
132   else
133     return( NULL );
134 }
135
136 // -------------------------------------------------------------------------
137 const cpPlugins::Interface::Workspace::
138 TParameters* cpPlugins::Interface::Workspace::
139 GetParameters( const std::string& name ) const
140 {
141   const TFilter* f = this->GetFilter( name );
142   if( f != NULL )
143     return( f->GetParameters( ) );
144   else
145     return( NULL );
146 }
147
148 // -------------------------------------------------------------------------
149 cpPlugins::Interface::Workspace::
150 TFilter* cpPlugins::Interface::Workspace::
151 GetFilter( const std::string& name )
152 {
153   TFilter* f =
154     dynamic_cast< TFilter* >(
155       this->m_Graph->GetVertex( name ).GetPointer( )
156       );
157   return( f );
158 }
159
160 // -------------------------------------------------------------------------
161 const cpPlugins::Interface::Workspace::
162 TFilter* cpPlugins::Interface::Workspace::
163 GetFilter( const std::string& name ) const
164 {
165   const TFilter* f =
166     dynamic_cast< const TFilter* >(
167       this->m_Graph->GetVertex( name ).GetPointer( )
168       );
169   return( f );
170 }
171
172 // -------------------------------------------------------------------------
173 bool cpPlugins::Interface::Workspace::
174 HasFilter( const std::string& name ) const
175 {
176   if( this->m_Graph->HasVertexIndex( name ) )
177     return( this->GetFilter( name ) != NULL );
178   else
179     return( false );
180 }
181
182 // -------------------------------------------------------------------------
183 bool cpPlugins::Interface::Workspace::
184 Reduce( const std::string& name )
185 {
186   return( false );
187 }
188
189 // -------------------------------------------------------------------------
190 void cpPlugins::Interface::Workspace::
191 AddInputPort(
192   const std::string& name,
193   const std::string& filter, const std::string& filter_input
194   )
195 {
196   this->m_InputPorts[ name ] = TGlobalPort( filter, filter_input );
197 }
198
199 // -------------------------------------------------------------------------
200 void cpPlugins::Interface::Workspace::
201 AddOutputPort(
202   const std::string& name,
203   const std::string& filter, const std::string& filter_output
204   )
205 {
206   this->m_OutputPorts[ name ] = TGlobalPort( filter, filter_output );
207 }
208
209 // -------------------------------------------------------------------------
210 cpPlugins::Interface::Workspace::
211 TData* cpPlugins::Interface::Workspace::
212 GetOutput( const std::string& name )
213 {
214   auto port = this->m_OutputPorts.find( name );
215   if( port != this->m_OutputPorts.end( ) )
216   {
217     TFilter* f = this->GetFilter( port->second.first );
218     if( f != NULL )
219       return( f->GetOutput< TData >( port->second.second ) );
220     else
221       return( NULL );
222   }
223   else
224     return( NULL );
225 }
226
227 // -------------------------------------------------------------------------
228 const cpPlugins::Interface::Workspace::
229 TData* cpPlugins::Interface::Workspace::
230 GetOutput( const std::string& name ) const
231 {
232   auto port = this->m_OutputPorts.find( name );
233   if( port != this->m_OutputPorts.end( ) )
234   {
235     const TFilter* f = this->GetFilter( port->second.first );
236     if( f != NULL )
237       return( f->GetOutput< TData >( port->second.second ) );
238     else
239       return( NULL );
240   }
241   else
242     return( NULL );
243 }
244
245 // -------------------------------------------------------------------------
246 void cpPlugins::Interface::Workspace::
247 ClearInputPorts( )
248 {
249   this->m_InputPorts.clear( );
250 }
251
252 // -------------------------------------------------------------------------
253 void cpPlugins::Interface::Workspace::
254 ClearOutputPorts( )
255 {
256   this->m_OutputPorts.clear( );
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 = this->GetFilter( name );
284   if( f == NULL )
285     return(
286       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
287       name + std::string( "\" is not a filter." )
288       );
289
290   // Execute and return
291   if( p != NULL )
292   {
293     auto diag_res = f->ExecConfigurationDialog( p );
294     if( diag_res == TFilter::DialogResult_NoModal )
295       return( f->Update( ) );
296     else
297       return( "" );
298   }
299   else
300     return( f->Update( ) );
301 }
302
303 // eof - $RCSfile$