1 #include <cpPlugins/Interface/BaseMPRWindow.h>
3 #ifdef cpPlugins_Interface_QT4
6 # define PLUGIN_PREFIX ""
7 # define PLUGIN_EXT "dll"
8 # define PLUGIN_REGEX "Plugins file (*.dll);;All files (*)"
10 # define PLUGIN_PREFIX "lib"
11 # define PLUGIN_EXT "so"
12 # define PLUGIN_REGEX "Plugins file (*.so);;All files (*)"
18 #include <QFileDialog>
19 #include <QMessageBox>
21 // -------------------------------------------------------------------------
22 cpPlugins::Interface::BaseMPRWindow::
23 BaseMPRWindow( QWidget* parent )
24 : cpExtensions::QT::QuadSplitter( parent ),
25 m_LastLoadedPlugin( "." )
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_XVTK, this->m_YVTK, this->m_ZVTK, this->m_WVTK );
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( )
44 // -------------------------------------------------------------------------
45 cpPlugins::Interface::BaseMPRWindow::
48 if( this->m_WVTK != NULL )
50 if( this->m_ZVTK != NULL )
52 if( this->m_YVTK != NULL )
54 if( this->m_XVTK != NULL )
58 // -------------------------------------------------------------------------
59 void cpPlugins::Interface::BaseMPRWindow::
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( ) ) )
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( ) )
78 // -------------------------------------------------------------------------
79 void cpPlugins::Interface::BaseMPRWindow::
80 AssociatePluginsToMenu( QMenu* menu, QObject* obj, const char* slot )
82 std::map< std::string, std::set< std::string > >::const_iterator i;
83 std::set< std::string >::const_iterator j;
86 for( i = this->m_Filters.begin( ); i != this->m_Filters.end( ); i++ )
88 QMenu* newMenu = menu->addMenu( i->first.c_str( ) );
89 for( j = i->second.begin( ); j != i->second.end( ); ++j )
91 QAction* a = newMenu->addAction( j->c_str( ) );
92 QObject::connect( a, SIGNAL( triggered( ) ), obj, slot );
99 // -------------------------------------------------------------------------
100 bool cpPlugins::Interface::BaseMPRWindow::
101 LoadPlugins( const std::string& fname )
105 // Is it already loaded?
106 if( this->m_LoadedPlugins.find( fname ) != this->m_LoadedPlugins.end( ) )
113 // Was it succesfully loaded?
114 if( !( this->m_Interface.Load( fname ) ) )
121 // Update a simple track
122 this->m_LoadedPlugins.insert( fname );
123 this->m_LastLoadedPlugin = fname;
124 this->_UpdatePlugins( );
130 // -------------------------------------------------------------------------
131 void cpPlugins::Interface::BaseMPRWindow::
134 // Load file into a char buffer
136 "Plugins.cfg", std::ios::in | std::ios::binary | std::ios::ate
140 std::streampos size = in.tellg( );
141 char* buffer = new char[ size ];
142 in.seekg( 0, std::ios::beg );
143 in.read( buffer, size );
147 std::stringstream in_stream( buffer );
151 in_stream.getline( line, 2048 );
152 this->LoadPlugins( line );
160 // -------------------------------------------------------------------------
161 std::string cpPlugins::Interface::BaseMPRWindow::
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.";
169 if( this->m_ImageReader->ExecConfigurationDialog( this ) )
172 msg = this->m_ImageReader->Update( );
175 TImage::Pointer image =
176 this->m_ImageReader->GetOutput< TImage >( "Output" );
177 if( this->m_Images.size( ) == 0 )
178 ret = image->GetName( );
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 >( );
187 this->m_MPRObjects->AddImage( vtk_id );
189 MementoState(m_state, this->m_Image);
194 msg = "Read image does not have a valid VTK converter.";
201 // Show errors and return
204 QMessageBox::critical(
205 this, tr( "Error reading image." ), tr( msg.c_str( ) )
210 // -------------------------------------------------------------------------
211 std::string cpPlugins::Interface::BaseMPRWindow::
217 // -------------------------------------------------------------------------
218 void cpPlugins::Interface::BaseMPRWindow::
220 const std::string& name,
221 const std::string& input_id,
222 const std::string& output_id
225 TProcessObject::Pointer filter =
226 this->m_Interface.CreateProcessObject( name );
227 std::string category = filter->GetClassCategory( );
228 if( category == "ImageToBinaryImageFilter" )
230 TImages::iterator iIt = this->m_Images.find( input_id );
231 if( iIt != this->m_Images.end( ) )
233 filter->SetInput( "Input", this->m_Images[ input_id ] );
234 bool dlg_ok = filter->ExecConfigurationDialog( NULL );
238 std::string err = filter->Update( );
239 std::cout << "ERR: " << err << std::endl;
242 this->m_Images[ "Segmentation" ] =
243 filter->GetOutput< TImage >( "Output" );
244 filter->DisconnectOutputs( );
246 vtkImageData* vtk_id =
247 this->m_Images[ "Segmentation" ]->GetVTK< vtkImageData >( );
249 this->m_MPRObjects->AddImage( vtk_id );
252 QMessageBox::critical(
253 this, tr( "Error with plugin" ), tr( err.c_str( ) )
261 // -------------------------------------------------------------------------
262 void cpPlugins::Interface::BaseMPRWindow::
263 AddImage( const std::string& name, TImage* image )
265 this->m_Images[ name ] = image;
266 vtkImageData* vtk_id =
267 this->m_Images[ name ]->GetVTK< vtkImageData >( );
269 this->m_MPRObjects->AddImage( vtk_id );
272 // -------------------------------------------------------------------------
273 void cpPlugins::Interface::BaseMPRWindow::
276 this->m_MPRObjects->ClearAll( );
277 this->m_Images.clear( );
278 this->m_Meshes.clear( );
281 // -------------------------------------------------------------------------
282 void cpPlugins::Interface::BaseMPRWindow::
285 typedef TInterface::TClasses _C;
287 this->m_Filters.clear( );
288 _C& classes = this->m_Interface.GetClasses( );
289 for( _C::const_iterator i = classes.begin( ); i != classes.end( ); ++i )
291 TProcessObject::Pointer o =
292 this->m_Interface.CreateProcessObject( i->first );
293 std::string name = o->GetClassName( );
294 std::string category = o->GetClassCategory( );
295 if( category == "ImageReader" )
296 this->m_ImageReader = o;
297 else if( category == "ImageWriter" )
298 this->m_ImageWriter = o;
299 else if( category == "MeshReader" )
300 this->m_MeshReader = o;
301 else if( category == "MeshWriter" )
302 this->m_MeshWriter = o;
304 this->m_Filters[ category ].insert( name );
307 if( category == "ImageReader" )
308 this->m_ImageReaderClass = name;
309 else if( category == "ImageWriter" )
310 this->m_ImageWriterClass = name;
311 else if( category == "MeshReader" )
312 this->m_MeshReaderClass = name;
313 else if( category == "MeshWriter" )
314 this->m_MeshWriterClass = name;
315 else if( category == "MeshToMeshFilter" )
317 if( name.find_last_of( "Cutter" ) != std::string::npos )
318 this->m_MeshCutterClass = name;
320 else if( category == "ImageToImageFilter" )
323 this->m_UI->MenuImageToImage->addAction( QString( name.c_str( ) ) );
325 action, SIGNAL( triggered( ) ),
326 this, SLOT( _triggered_actionImageToImage( ) )
329 else if( category == "ImageToMeshFilter" )
332 this->m_UI->MenuImageToMesh->addAction( QString( name.c_str( ) ) );
334 action, SIGNAL( triggered( ) ),
335 this, SLOT( _triggered_actionImageToMesh( ) )
344 #endif // cpPlugins_Interface_QT4