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