]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ProcessObject.cxx
...
[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     i->second = port;
81     this->Modified( );
82     return( true );
83   }
84   else
85     return( false );
86 }
87
88 // -------------------------------------------------------------------------
89 void cpPlugins::Interface::ProcessObject::
90 DisconnectInputs( )
91 {
92   auto i = this->m_Inputs.begin( );
93   for( ; i != this->m_Inputs.end( ); ++i )
94     i->second = NULL;
95   this->Modified( );
96 }
97
98 // -------------------------------------------------------------------------
99 void cpPlugins::Interface::ProcessObject::
100 DisconnectOutputs( )
101 {
102   auto i = this->m_Outputs.begin( );
103   for( ; i != this->m_Outputs.end( ); ++i )
104     if( i->second.IsValid( ) )
105       i->second->DisconnectPipeline( );
106   this->Modified( );
107 }
108
109 // -------------------------------------------------------------------------
110 void cpPlugins::Interface::ProcessObject::
111 Disconnect( )
112 {
113   this->DisconnectInputs( );
114   this->DisconnectOutputs( );
115 }
116
117 // -------------------------------------------------------------------------
118 itk::ModifiedTimeType cpPlugins::Interface::ProcessObject::
119 GetMTime( ) const
120 {
121   auto params_time = this->m_Parameters->GetMTime( );
122   auto filter_time = this->Superclass::GetMTime( );
123   auto ipobj = this->GetITK< itk::ProcessObject >( );
124   if( ipobj == NULL )
125   {
126     auto vpobj = this->GetVTK< vtkAlgorithm >( );
127     if( vpobj != NULL )
128       filter_time = ( const_cast< vtkAlgorithm* >( vpobj ) )->GetMTime( );
129   }
130   else
131     filter_time = ipobj->GetMTime( );
132   return( ( params_time < filter_time )? filter_time: params_time );
133 }
134
135 // -------------------------------------------------------------------------
136 std::string cpPlugins::Interface::ProcessObject::
137 Update( )
138 {
139   static 
140   std::string r = "";
141
142   // Force upstream updates
143   _TDataContainer::iterator i = this->m_Inputs.begin( );
144   bool need_to_update = false;
145   for( ; i != this->m_Inputs.end( ) && r == ""; ++i )
146   {
147     if( i->second.IsValid( ) )
148     {
149       Self* src = dynamic_cast< Self* >( i->second->GetSource( ) );
150       if( src != NULL )
151       {
152         need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
153         r = src->Update( );
154
155       } // fi
156     }
157     else
158       r = "cpPlugins::Interface::ProcessObject: No input connected.";
159
160   } // rof
161
162   // Current update
163   if( r == "" )
164   {
165     if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
166     {
167       r = this->_GenerateData( );
168       this->m_LastExecutionTime = this->GetMTime( );
169
170     } // fi
171
172   } // fi
173
174   // Return error description, if any
175   return( r );
176 }
177
178 // -------------------------------------------------------------------------
179 cpPlugins::Interface::ProcessObject::
180 ProcessObject( )
181   : Superclass( ),
182     m_LastExecutionTime( 0 ),
183     m_ParametersDialog( NULL ),
184     m_MPRViewer( NULL )
185 {
186   this->m_Parameters = TParameters::New( );
187   this->m_Parameters->SetProcessObject( this );
188
189 #ifdef cpPlugins_Interface_QT4
190   if( QApplication::instance( ) != NULL )
191   {
192     this->m_ParametersDialog = new ParametersQtDialog( );
193     this->m_ParametersDialog->setParameters( this->m_Parameters );
194
195   } // fi
196 #endif // cpPlugins_Interface_QT4
197 }
198
199 // -------------------------------------------------------------------------
200 cpPlugins::Interface::ProcessObject::
201 ~ProcessObject( )
202 {
203   this->Disconnect( );
204   if( this->m_ParametersDialog != NULL )
205     delete this->m_ParametersDialog;
206 }
207
208 // -------------------------------------------------------------------------
209 void cpPlugins::Interface::ProcessObject::
210 _AddInput( const std::string& name )
211 {
212   typedef typename _TDataContainer::value_type _TValue;
213   auto i = this->m_Inputs.find( name );
214   if( i == this->m_Inputs.end( ) )
215     i = this->m_Inputs.insert( _TValue( name, NULL ) ).first;
216 }
217
218 // eof - $RCSfile$