]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/BaseMPRWindow.cxx
Dicom series reader plugin added.
[cpPlugins.git] / lib / cpPlugins / Interface / BaseMPRWindow.cxx
1 #include <cpPlugins/Interface/BaseMPRWindow.h>
2
3 #ifdef cpPlugins_Interface_QT4
4
5 #ifdef _WIN32
6 #  define PLUGIN_PREFIX ""
7 #  define PLUGIN_EXT "dll"
8 #  define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
9 #else // Linux
10 #  define PLUGIN_PREFIX "lib"
11 #  define PLUGIN_EXT "so"
12 #  define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
13 #endif // _WIN32
14
15 #include <fstream>
16 #include <sstream>
17
18 #include <QFileDialog>
19 #include <QMessageBox>
20
21 // -------------------------------------------------------------------------
22 cpPlugins::Interface::BaseMPRWindow::
23 BaseMPRWindow( QWidget* parent )
24   : cpExtensions::QT::QuadSplitter( parent ),
25     m_LastLoadedPlugin( "." )
26 {
27   // Configure splitter
28   this->m_XVTK = new QVTKWidget( this );
29   this->m_YVTK = new QVTKWidget( this );
30   this->m_ZVTK = new QVTKWidget( this );
31   this->m_WVTK = new QVTKWidget( this );
32   this->addWidgets( this->m_YVTK, this->m_XVTK, this->m_ZVTK, this->m_WVTK );
33
34   // Create and associate vtk renderers
35   this->m_MPRObjects = vtkSmartPointer< TMPRObjects >::New( );
36   this->m_MPRObjects->SetRenderWindows(
37     this->m_XVTK->GetRenderWindow( ),
38     this->m_YVTK->GetRenderWindow( ),
39     this->m_ZVTK->GetRenderWindow( ),
40     this->m_WVTK->GetRenderWindow( )
41     );
42 }
43
44 // -------------------------------------------------------------------------
45 cpPlugins::Interface::BaseMPRWindow::
46 ~BaseMPRWindow( )
47 {
48   if( this->m_WVTK != NULL )
49     delete this->m_WVTK;
50   if( this->m_ZVTK != NULL )
51     delete this->m_ZVTK;
52   if( this->m_YVTK != NULL )
53     delete this->m_YVTK;
54   if( this->m_XVTK != NULL )
55     delete this->m_XVTK;
56 }
57
58 // -------------------------------------------------------------------------
59 void cpPlugins::Interface::BaseMPRWindow::
60 DialogLoadPlugins( )
61 {
62   // Show dialog and check if it was accepted
63   QFileDialog dialog( this );
64   dialog.setFileMode( QFileDialog::ExistingFile );
65   dialog.setDirectory( this->m_LastLoadedPlugin.c_str( ) );
66   dialog.setNameFilter( tr( PLUGIN_REGEX ) );
67   dialog.setDefaultSuffix( tr( PLUGIN_EXT ) );
68   if( !( dialog.exec( ) ) )
69     return;
70
71   std::string fname = dialog.selectedFiles( ).at( 0 ).toStdString( );
72   if( !( this->LoadPlugins( fname ) ) )
73     QMessageBox::critical(
74       this, tr( "Ignoring plugin" ), tr( fname.c_str( ) )
75       );
76 }
77
78 // -------------------------------------------------------------------------
79 void cpPlugins::Interface::BaseMPRWindow::
80 AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
81 {
82   std::map< std::string, std::set< std::string > >::const_iterator i;
83   std::set< std::string >::const_iterator j;
84
85   menu->clear( );
86   for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
87   {
88     QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
89     for( j = i->second.begin( ); j != i->second.end( ); ++j )
90     {
91       QAction* a = newMenu->addAction( j->c_str( ) );
92       QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
93
94     } // rof
95
96   } // rof
97 }
98
99 // -------------------------------------------------------------------------
100 bool cpPlugins::Interface::BaseMPRWindow::
101 LoadPlugins( const std::string& fname )
102 {
103   this->Block( );
104
105   // Is it already loaded?
106   if( this->m_LoadedPlugins.find( fname ) != this->m_LoadedPlugins.end( ) )
107   {
108     this->Unblock( );
109     return( true );
110
111   } // fi
112
113   // Was it succesfully loaded?
114   if( !( this->m_Interface.Load( fname ) ) )
115   {
116     this->Unblock( );
117     return( false );
118
119   } // fi
120
121   // Update a simple track
122   this->m_LoadedPlugins.insert( fname );
123   this->m_LastLoadedPlugin = fname;
124   this->_UpdatePlugins( );
125
126   this->Unblock( );
127   return( true );
128 }
129
130 // -------------------------------------------------------------------------
131 void cpPlugins::Interface::BaseMPRWindow::
132 LoadPlugins( )
133 {
134   // Load file into a char buffer
135   std::ifstream in(
136     "Plugins.cfg", std::ios::in | std::ios::binary | std::ios::ate
137     );
138   if( !in.is_open( ) )
139     return;
140   std::streampos size = in.tellg( );
141   char* buffer = new char[ size ];
142   in.seekg( 0, std::ios::beg );
143   in.read( buffer, size );
144   in.close( );
145
146   // Read stream
147   std::stringstream in_stream( buffer );
148   while( in_stream )
149   {
150     char line[ 2048 ];
151     in_stream.getline( line, 2048 );
152     this->LoadPlugins( line );
153
154   } // elihw
155
156   // Finish
157   delete buffer;
158 }
159
160 // -------------------------------------------------------------------------
161 std::string cpPlugins::Interface::BaseMPRWindow::
162 LoadImage( )
163 {
164   std::string msg = "";
165   std::string ret = "";
166   if( this->m_ImageReader.IsNull( ) )
167     msg = "No valid image reader. Please load a valid plugin file.";
168
169   if( this->m_ImageReader->ExecConfigurationDialog( this ) )
170   {
171     this->Block( );
172     msg = this->m_ImageReader->Update( );
173     if( msg == "" )
174     {
175       TImage::Pointer image =
176         this->m_ImageReader->GetOutput< TImage >( "Output" );
177       if( this->m_Images.size( ) == 0 )
178         ret = image->GetName( );
179       else
180         ret = "Segmentation";
181       this->m_Images[ ret ] = image;
182       this->m_ImageReader->DisconnectOutputs( );
183       this->m_ImageReader->GetParameters( )->ClearStringList( "FileNames" );
184       vtkImageData* vtk_id = image->GetVTK< vtkImageData >( );
185       if( vtk_id != NULL )
186       {
187         this->m_MPRObjects->AddImage( vtk_id );
188         /*
189           MementoState(m_state, this->m_Image);  
190           this->m_state++;
191         */
192       }
193       else
194         msg = "Read image does not have a valid VTK converter.";
195     }
196     else
197       ret = "";
198
199   } // fi
200
201   // Show errors and return
202   this->Unblock( );
203   if( msg != "" )
204     QMessageBox::critical(
205       this, tr( "Error reading image." ), tr( msg.c_str( ) )
206       );
207   return( ret );
208 }
209
210 // -------------------------------------------------------------------------
211 std::string cpPlugins::Interface::BaseMPRWindow::
212 LoadDicomSeries( )
213 {
214   std::string msg = "";
215   std::string ret = "";
216   if( this->m_DicomSeriesReader.IsNull( ) )
217     msg = "No valid DICOM series reader. Please load a valid plugin file.";
218
219   if( this->m_DicomSeriesReader->ExecConfigurationDialog( this ) )
220   {
221     this->Block( );
222     msg = this->m_DicomSeriesReader->Update( );
223     if( msg == "" )
224     {
225       TImage::Pointer image =
226         this->m_DicomSeriesReader->GetOutput< TImage >( "Output" );
227       if( this->m_Images.size( ) == 0 )
228         ret = image->GetName( );
229       else
230         ret = "Segmentation";
231       this->m_Images[ ret ] = image;
232       this->m_DicomSeriesReader->DisconnectOutputs( );
233       this->m_DicomSeriesReader->GetParameters( )->
234         ClearStringList( "FileNames" );
235       vtkImageData* vtk_id = image->GetVTK< vtkImageData >( );
236       if( vtk_id != NULL )
237       {
238         this->m_MPRObjects->AddImage( vtk_id );
239         /*
240           MementoState(m_state, this->m_Image);  
241           this->m_state++;
242         */
243       }
244       else
245         msg = "Read dicom series does not have a valid VTK converter.";
246     }
247     else
248       ret = "";
249
250   } // fi
251
252   // Show errors and return
253   this->Unblock( );
254   if( msg != "" )
255     QMessageBox::critical(
256       this, tr( "Error reading dicom series." ), tr( msg.c_str( ) )
257       );
258   return( ret );
259 }
260
261 // -------------------------------------------------------------------------
262 std::string cpPlugins::Interface::BaseMPRWindow::
263 LoadMesh( )
264 {
265   return( "" );
266 }
267
268 // -------------------------------------------------------------------------
269 void cpPlugins::Interface::BaseMPRWindow::
270 ExecuteFilter(
271   const std::string& name,
272   const std::string& input_id,
273   const std::string& output_id
274   )
275 {
276   TProcessObject::Pointer filter =
277     this->m_Interface.CreateProcessObject( name );
278   std::string category = filter->GetClassCategory( );
279   if( category == "ImageToBinaryImageFilter" )
280   {
281     TImages::iterator iIt = this->m_Images.find( input_id );
282     if( iIt != this->m_Images.end( ) )
283     {
284       filter->SetInput( "Input", this->m_Images[ input_id ] );
285       bool dlg_ok = filter->ExecConfigurationDialog( NULL );
286       if( !dlg_ok )
287         return;
288
289       std::string err = filter->Update( );
290       std::cout << "ERR: " << err << std::endl;
291       if( err == "" )
292       {
293         this->m_Images[ "Segmentation" ] =
294           filter->GetOutput< TImage >( "Output" );
295         filter->DisconnectOutputs( );
296
297         vtkImageData* vtk_id =
298           this->m_Images[ "Segmentation" ]->GetVTK< vtkImageData >( );
299         if( vtk_id != NULL )
300           this->m_MPRObjects->AddImage( vtk_id );
301       }
302       else
303         QMessageBox::critical(
304           this, tr( "Error with plugin" ), tr( err.c_str( ) )
305           );
306
307     } // fi
308
309   } // fi
310 }
311
312 // -------------------------------------------------------------------------
313 void cpPlugins::Interface::BaseMPRWindow::
314 AddImage( const std::string& name, TImage* image )
315 {
316   this->m_Images[ name ] = image;
317   vtkImageData* vtk_id =
318     this->m_Images[ name ]->GetVTK< vtkImageData >( );
319   if( vtk_id != NULL )
320     this->m_MPRObjects->AddImage( vtk_id );
321 }
322
323 // -------------------------------------------------------------------------
324 void cpPlugins::Interface::BaseMPRWindow::
325 ClearAll( )
326 {
327   this->m_MPRObjects->ClearAll( );
328   this->m_Images.clear( );
329   this->m_Meshes.clear( );
330 }
331
332 // -------------------------------------------------------------------------
333 void cpPlugins::Interface::BaseMPRWindow::
334 _UpdatePlugins( )
335 {
336   typedef TInterface::TClasses _C;
337
338   this->m_Filters.clear( );
339   _C& classes = this->m_Interface.GetClasses( );
340   for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
341   {
342     TProcessObject::Pointer o =
343       this->m_Interface.CreateProcessObject( i->first );
344     std::string name = o->GetClassName( );
345     std::string category = o->GetClassCategory( );
346     if( category == "ImageReader" )
347       this->m_ImageReader = o;
348     else if( category == "ImageWriter" )
349       this->m_ImageWriter = o;
350     else if( category == "MeshReader" )
351       this->m_MeshReader = o;
352     else if( category == "MeshWriter" )
353       this->m_MeshWriter = o;
354     else if( category == "DicomSeriesReader" )
355       this->m_DicomSeriesReader = o;
356     else
357       this->m_Filters[ category ].insert( name );
358
359     /*
360       if( category == "ImageReader" )
361       this->m_ImageReaderClass = name;
362       else if( category == "ImageWriter" )
363       this->m_ImageWriterClass = name;
364       else if( category == "MeshReader" )
365       this->m_MeshReaderClass = name;
366       else if( category == "MeshWriter" )
367       this->m_MeshWriterClass = name;
368       else if( category == "MeshToMeshFilter" )
369       {
370       if( name.find_last_of( "Cutter" ) != std::string::npos )
371       this->m_MeshCutterClass = name;
372       }
373       else if( category == "ImageToImageFilter" )
374       {
375       QAction* action =
376       this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
377       QObject::connect(
378       action, SIGNAL( triggered( ) ),
379       this, SLOT( _triggered_actionImageToImage( ) )
380       );
381       }
382       else if( category == "ImageToMeshFilter" )
383       {
384       QAction* action =
385       this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
386       QObject::connect(
387       action, SIGNAL( triggered( ) ),
388       this, SLOT( _triggered_actionImageToMesh( ) )
389       );
390
391       } // fi
392     */
393
394   } // rof
395 }
396
397 #endif // cpPlugins_Interface_QT4
398
399 // eof - $RCSfile$