]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ProcessObject.cxx
17fe21b6c427a1b9d698d4eb6ca14fa56423a64a
[cpPlugins.git] / lib / cpPlugins / Interface / ProcessObject.cxx
1 #include <cpPlugins/Interface/ProcessObject.h>
2 #include <itkProcessObject.h>
3
4 #ifdef cpPlugins_Interface_QT4
5 #include <QApplication>
6 #include <cpPlugins/Interface/ParametersQtDialog.h>
7 #include <cpPlugins/Interface/SimpleMPRWidget.h>
8 #endif // cpPlugins_Interface_QT4
9
10 // -------------------------------------------------------------------------
11 std::set< std::string > cpPlugins::Interface::ProcessObject::
12 GetInputsNames( ) const
13 {
14   std::set< std::string > names;
15   for( auto i = this->m_Inputs.begin( ); i != this->m_Inputs.end( ); ++i )
16     names.insert( i->first );
17   return( names );
18 }
19
20 // -------------------------------------------------------------------------
21 std::set< std::string > cpPlugins::Interface::ProcessObject::
22 GetOutputsNames( ) const
23 {
24   std::set< std::string > names;
25   for( auto i = this->m_Outputs.begin( ); i != this->m_Outputs.end( ); ++i )
26     names.insert( i->first );
27   return( names );
28 }
29
30 // -------------------------------------------------------------------------
31 unsigned int cpPlugins::Interface::ProcessObject::
32 GetNumberOfInputs( ) const
33 {
34   return( this->m_Inputs.size( ) );
35 }
36
37 // -------------------------------------------------------------------------
38 unsigned int cpPlugins::Interface::ProcessObject::
39 GetNumberOfOutputs( ) const
40 {
41   return( this->m_Outputs.size( ) );
42 }
43
44 // -------------------------------------------------------------------------
45 cpPlugins::Interface::
46 ProcessObjectPort& cpPlugins::Interface::ProcessObject::
47 GetOutput( const std::string& id )
48 {
49   static ProcessObjectPort null_port;
50   auto i = this->m_Outputs.find( id );
51   if( i == this->m_Outputs.end( ) )
52   {
53     null_port = NULL;
54     return( null_port );
55   }
56   else
57     return( i->second );
58 }
59
60 // -------------------------------------------------------------------------
61 const cpPlugins::Interface::
62 ProcessObjectPort& cpPlugins::Interface::ProcessObject::
63 GetOutput( const std::string& id ) const
64 {
65   static const ProcessObjectPort null_port;
66   auto i = this->m_Outputs.find( id );
67   if( i == this->m_Outputs.end( ) )
68     return( null_port );
69   else
70     return( i->second );
71 }
72
73 // -------------------------------------------------------------------------
74 bool cpPlugins::Interface::ProcessObject::
75 SetInput( const std::string& id, const ProcessObjectPort& port )
76 {
77   auto i = this->m_Inputs.find( id );
78   if( i != this->m_Inputs.end( ) )
79   {
80     if( i->second.GetPointer( ) != port.GetPointer( ) )
81     {
82       i->second = port;
83       this->Modified( );
84
85     } // fi
86     return( true );
87   }
88   else
89     return( false );
90 }
91
92 // -------------------------------------------------------------------------
93 void cpPlugins::Interface::ProcessObject::
94 DisconnectInputs( )
95 {
96   auto i = this->m_Inputs.begin( );
97   for( ; i != this->m_Inputs.end( ); ++i )
98     i->second = NULL;
99   this->Modified( );
100 }
101
102 // -------------------------------------------------------------------------
103 void cpPlugins::Interface::ProcessObject::
104 DisconnectOutputs( )
105 {
106   auto i = this->m_Outputs.begin( );
107   for( ; i != this->m_Outputs.end( ); ++i )
108     if( i->second.IsValid( ) )
109       i->second->DisconnectPipeline( );
110   this->Modified( );
111 }
112
113 // -------------------------------------------------------------------------
114 void cpPlugins::Interface::ProcessObject::
115 Disconnect( )
116 {
117   this->DisconnectInputs( );
118   this->DisconnectOutputs( );
119 }
120
121 // -------------------------------------------------------------------------
122 itk::ModifiedTimeType cpPlugins::Interface::ProcessObject::
123 GetMTime( ) const
124 {
125   auto params_time = this->m_Parameters->GetMTime( );
126   auto filter_time = this->Superclass::GetMTime( );
127   auto ipobj = this->GetITK< itk::ProcessObject >( );
128   if( ipobj == NULL )
129   {
130     auto vpobj = this->GetVTK< vtkAlgorithm >( );
131     if( vpobj != NULL )
132       filter_time = ( const_cast< vtkAlgorithm* >( vpobj ) )->GetMTime( );
133   }
134   else
135     filter_time = ipobj->GetMTime( );
136   return( ( params_time < filter_time )? filter_time: params_time );
137 }
138
139 // -------------------------------------------------------------------------
140 std::string cpPlugins::Interface::ProcessObject::
141 Update( )
142 {
143   std::string r = "";
144
145   // Force upstream updates
146   _TDataContainer::iterator i = this->m_Inputs.begin( );
147   bool need_to_update = false;
148   for( ; i != this->m_Inputs.end( ) && r == ""; ++i )
149   {
150     if( i->second.IsValid( ) )
151     {
152       Self* src = dynamic_cast< Self* >( i->second->GetSource( ) );
153       if( src != NULL )
154       {
155         need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
156         r = src->Update( );
157
158       } // fi
159     }
160     else
161       r = "cpPlugins::Interface::ProcessObject: No input connected.";
162
163   } // rof
164
165   // Current update
166   if( r == "" )
167   {
168     if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
169     {
170       r = this->_GenerateData( );
171       this->m_LastExecutionTime = this->GetMTime( );
172
173     } // fi
174
175   } // fi
176
177   // Return error description, if any
178   return( r );
179 }
180
181 // -------------------------------------------------------------------------
182 cpPlugins::Interface::ProcessObject::
183 ProcessObject( )
184   : Superclass( ),
185     m_LastExecutionTime( 0 ),
186     m_ParametersDialog( NULL ),
187     m_MPRViewer( NULL )
188 {
189   this->m_Parameters = TParameters::New( );
190   this->m_Parameters->SetProcessObject( this );
191
192 #ifdef cpPlugins_Interface_QT4
193   if( QApplication::instance( ) != NULL )
194   {
195     this->m_ParametersDialog = new ParametersQtDialog( );
196     this->m_ParametersDialog->setParameters( this->m_Parameters );
197
198   } // fi
199 #endif // cpPlugins_Interface_QT4
200 }
201
202 // -------------------------------------------------------------------------
203 cpPlugins::Interface::ProcessObject::
204 ~ProcessObject( )
205 {
206   this->Disconnect( );
207   if( this->m_ParametersDialog != NULL )
208     delete this->m_ParametersDialog;
209 }
210
211 // -------------------------------------------------------------------------
212 void cpPlugins::Interface::ProcessObject::
213 _AddInput( const std::string& name )
214 {
215   typedef typename _TDataContainer::value_type _TValue;
216   auto i = this->m_Inputs.find( name );
217   if( i == this->m_Inputs.end( ) )
218   {
219     i = this->m_Inputs.insert( _TValue( name, NULL ) ).first;
220     this->Modified( );
221
222   } // fi
223 }
224
225 // eof - $RCSfile$