]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Workspace.cxx
More on graph editor
[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   std::cout << name << " " << filter << " " << filter_input << std::endl;
197
198   this->m_InputPorts[ name ] = TGlobalPort( filter, filter_input );
199 }
200
201 // -------------------------------------------------------------------------
202 void cpPlugins::Interface::Workspace::
203 AddOutputPort(
204   const std::string& name,
205   const std::string& filter, const std::string& filter_output
206   )
207 {
208   this->m_OutputPorts[ name ] = TGlobalPort( filter, filter_output );
209 }
210
211 // -------------------------------------------------------------------------
212 cpPlugins::Interface::Workspace::
213 TData* cpPlugins::Interface::Workspace::
214 GetOutput( const std::string& name )
215 {
216   auto port = this->m_OutputPorts.find( name );
217   if( port != this->m_OutputPorts.end( ) )
218   {
219     TFilter* f = this->GetFilter( port->second.first );
220     if( f != NULL )
221       return( f->GetOutput< TData >( port->second.second ) );
222     else
223       return( NULL );
224   }
225   else
226     return( NULL );
227 }
228
229 // -------------------------------------------------------------------------
230 const cpPlugins::Interface::Workspace::
231 TData* cpPlugins::Interface::Workspace::
232 GetOutput( const std::string& name ) const
233 {
234   auto port = this->m_OutputPorts.find( name );
235   if( port != this->m_OutputPorts.end( ) )
236   {
237     const TFilter* f = this->GetFilter( port->second.first );
238     if( f != NULL )
239       return( f->GetOutput< TData >( port->second.second ) );
240     else
241       return( NULL );
242   }
243   else
244     return( NULL );
245 }
246
247 // -------------------------------------------------------------------------
248 void cpPlugins::Interface::Workspace::
249 ClearInputPorts( )
250 {
251   this->m_InputPorts.clear( );
252 }
253
254 // -------------------------------------------------------------------------
255 void cpPlugins::Interface::Workspace::
256 ClearOutputPorts( )
257 {
258   this->m_OutputPorts.clear( );
259 }
260
261 // -------------------------------------------------------------------------
262 std::string cpPlugins::Interface::Workspace::
263 Execute( )
264 {
265   // Find sinks
266   std::set< std::string > sinks = this->m_Graph->GetSinks( );
267
268   // Update sinks
269   std::string err = "";
270   for( auto sIt = sinks.begin( ); sIt != sinks.end( ); ++sIt )
271   {
272     std::string lerr = this->Execute( *sIt, NULL );
273     if( lerr != "" )
274       err += lerr + std::string( "\n" );
275
276   } // rof
277   return( err );
278 }
279
280 // -------------------------------------------------------------------------
281 std::string cpPlugins::Interface::Workspace::
282 Execute( const std::string& name, QWidget* p )
283 {
284   // Get filter
285   TFilter* f = this->GetFilter( name );
286   if( f == NULL )
287     return(
288       std::string( "cpPlugins::Interface::Workspace: Vertex \"" ) +
289       name + std::string( "\" is not a filter." )
290       );
291
292   // Execute and return
293   if( p != NULL )
294   {
295     auto diag_res = f->ExecConfigurationDialog( p );
296     if( diag_res == TFilter::DialogResult_NoModal )
297       return( f->Update( ) );
298     else
299       return( "" );
300   }
301   else
302     return( f->Update( ) );
303 }
304
305 // eof - $RCSfile$