]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/ProcessObject.cxx
e2ea0de61e79640917d122c376c24a2eed027b2f
[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     {
159       Self* src = dynamic_cast< Self* >( i->second->GetSource( ) );
160       if( src != NULL )
161       {
162         need_to_update |= ( this->m_LastExecutionTime < src->GetMTime( ) );
163         r = src->Update( );
164
165       } // fi
166     }
167     else
168       r =
169         "ProcessObject: Required input \"" +
170         i->first + "@" + this->GetClassName( ) +
171         "\" is not valid (=NULL).";
172
173   } // rof
174
175   // Current update
176   if( r == "" )
177   {
178     if( this->m_LastExecutionTime < this->GetMTime( ) || need_to_update )
179     {
180       r = this->_GenerateData( );
181       this->m_LastExecutionTime = this->GetMTime( );
182
183     } // fi
184
185   } // fi
186
187   // Return error description, if any
188   return( r );
189 }
190
191 // -------------------------------------------------------------------------
192 cpPlugins::Interface::ProcessObject::
193 ProcessObject( )
194   : Superclass( ),
195     m_LastExecutionTime( 0 ),
196     m_ParametersDialog( NULL ),
197     m_MPRViewer( NULL )
198 {
199   this->m_Parameters = TParameters::New( );
200   this->m_Parameters->SetProcessObject( this );
201
202 #ifdef cpPlugins_Interface_QT4
203   if( QApplication::instance( ) != NULL )
204   {
205     this->m_ParametersDialog = new ParametersQtDialog( );
206     this->m_ParametersDialog->setParameters( this->m_Parameters );
207
208   } // fi
209 #endif // cpPlugins_Interface_QT4
210 }
211
212 // -------------------------------------------------------------------------
213 cpPlugins::Interface::ProcessObject::
214 ~ProcessObject( )
215 {
216   this->Disconnect( );
217   if( this->m_ParametersDialog != NULL )
218     delete this->m_ParametersDialog;
219 }
220
221 // -------------------------------------------------------------------------
222 void cpPlugins::Interface::ProcessObject::
223 _AddInput( const std::string& name, bool required )
224 {
225   auto i = this->m_Inputs.find( name );
226   if( i == this->m_Inputs.end( ) )
227   {
228     InputProcessObjectPort new_port( required );
229     this->m_Inputs[ name ] = new_port;
230     this->Modified( );
231
232   } // fi
233 }
234
235 // eof - $RCSfile$