1 #include <cpBaseQtApplication/MainWindow.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
5 #include <cpBaseQtApplication/Editor.h>
6 #include <vtkRenderer.h>
7 #include <QApplication>
8 #include <QColorDialog>
10 #include <QFileDialog>
12 #include <QMessageBox>
13 #include <QTreeWidget>
15 // -------------------------------------------------------------------------
16 bool cpBaseQtApplication::MainWindow::_TBlocker::
17 eventFilter( QObject* obj, QEvent* event )
19 return( true ); // -> Block all events
20 /* NOTE: this should be the correct implementation:
21 switch( event->type( ) )
23 //list event you want to prevent here ...
24 case QEvent::KeyPress:
25 case QEvent::KeyRelease:
26 case QEvent::MouseButtonRelease:
27 case QEvent::MouseButtonPress:
28 case QEvent::MouseButtonDblClick:
32 return( this->QObject::eventFilter( obj, event ) );
36 // -------------------------------------------------------------------------
37 cpBaseQtApplication::MainWindow::
39 int argc, char* argv[],
43 : Superclass( parent ),
50 QFileInfo i( argv[ 0 ] );
52 this->m_Interface.UpdateEnvironments( i.canonicalPath( ).toStdString( ) );
53 this->m_Workspace.SetInterface( &( this->m_Interface ) );
56 // -------------------------------------------------------------------------
57 cpBaseQtApplication::MainWindow::
62 // -------------------------------------------------------------------------
63 void cpBaseQtApplication::MainWindow::
68 this->m_Interface.LoadEnvironment( );
69 this->m_Interface.GuessPlugins( );
71 catch( std::exception& err )
73 QMessageBox::critical(
74 this, "Error loading required libraries", err.what( )
81 // -------------------------------------------------------------------------
82 void cpBaseQtApplication::MainWindow::
85 cpExtensions::QT::SimpleMPRWidget* mpr,
86 cpBaseQtApplication::Editor* editor
89 this->m_TreeWidget = tree;
90 this->_UpdateLoadedPlugins( );
91 this->m_Editor = editor;
93 if( this->m_Editor != NULL )
95 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
100 SIGNAL( execFilter( const std::string& ) ),
102 SLOT( _ExecFilter( const std::string& ) )
106 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
108 SLOT( _ShowData( const std::string&, const std::string& ) )
112 SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ),
114 SLOT( _HideData( const std::string&, const std::string& ) )
118 SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ),
120 SLOT( _DataProperties( const std::string&, const std::string& ) )
124 if( this->m_MPR != NULL )
125 this->m_Workspace.SetMPRViewer( this->m_MPR );
126 this->_ClearWorkspace( );
129 // -------------------------------------------------------------------------
130 void cpBaseQtApplication::MainWindow::
133 if( this->m_Editor != NULL )
134 this->m_Editor->clear( );
135 if( this->m_MPR != NULL )
136 this->m_MPR->Clear( );
137 this->m_Workspace.Clear( );
140 // -------------------------------------------------------------------------
141 void cpBaseQtApplication::MainWindow::
142 _LoadPlugins( const std::string& filename )
146 this->m_Interface.LoadFile( filename );
147 this->_UpdateLoadedPlugins( );
149 catch( std::exception& err )
151 QMessageBox::critical(
153 "Error loading plugins path",
160 // -------------------------------------------------------------------------
161 void cpBaseQtApplication::MainWindow::
162 _LoadPluginsFromPath( const std::string& path )
166 this->m_Interface.LoadDirectory( path );
167 this->_UpdateLoadedPlugins( );
169 catch( std::exception& err )
171 QMessageBox::critical(
173 "Error loading plugins path",
180 // -------------------------------------------------------------------------
181 void cpBaseQtApplication::MainWindow::
182 _UpdateLoadedPlugins( )
184 if( this->m_TreeWidget == NULL )
187 auto categories = this->m_Interface.GetCategories( );
188 unsigned int filter_count = 0;
189 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
191 // Create or get category
192 QList< QTreeWidgetItem* > cat_items =
193 this->m_TreeWidget->findItems(
194 cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
196 QTreeWidgetItem* cat = NULL;
197 if( cat_items.size( ) == 0 )
199 cat = new QTreeWidgetItem(
200 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
202 this->m_TreeWidget->addTopLevelItem( cat );
205 cat = cat_items[ 0 ];
208 auto filters = this->m_Interface.GetFilters( *cIt );
209 filter_count += filters.size( );
210 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
212 QList< QTreeWidgetItem* > filter_items =
213 this->m_TreeWidget->findItems(
214 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
216 auto fiIt = filter_items.begin( );
217 auto found_fiIt = filter_items.end( );
218 for( ; fiIt != filter_items.end( ); ++fiIt )
219 if( ( *fiIt )->parent( ) == cat )
223 if( found_fiIt == filter_items.end( ) )
224 QTreeWidgetItem* filter = new QTreeWidgetItem(
225 cat, QStringList( fIt->c_str( ) )
231 this->m_TreeWidget->expandAll( );
234 if( filter_count == 0 )
235 QMessageBox::critical(
237 "Error loading default plugins",
238 "No plugins loaded: remember to load some!!!"
242 // -------------------------------------------------------------------------
243 void cpBaseQtApplication::MainWindow::
246 auto paths = this->m_Interface.GetPaths( );
247 auto plugins = this->m_Interface.GetPlugins( );
248 std::stringstream info;
249 info << "----- PATHS -----" << std::endl;
250 for( auto i = paths.begin( ); i != paths.end( ); ++i )
251 info << " " << *i << std::endl;
252 info << std::endl << "----- PLUGINS -----" << std::endl;
253 for( auto i = plugins.begin( ); i != plugins.end( ); ++i )
254 info << " " << *i << std::endl;
255 QMessageBox::information(
257 "Loaded libraries and plugins information",
262 // -------------------------------------------------------------------------
263 void cpBaseQtApplication::MainWindow::
266 this->m_Application->setOverrideCursor( Qt::WaitCursor );
267 this->m_Application->installEventFilter( &( this->m_Blocker ) );
270 // -------------------------------------------------------------------------
271 void cpBaseQtApplication::MainWindow::
274 while( this->m_Application->overrideCursor( ) )
275 this->m_Application->restoreOverrideCursor( );
276 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
279 // -------------------------------------------------------------------------
280 void cpBaseQtApplication::MainWindow::
281 _LoadWorkspace( const std::string& filename )
283 this->_ClearWorkspace( );
284 std::string err = this->m_Workspace.LoadWorkspace( filename );
287 QMessageBox::critical(
289 QMessageBox::tr( "Error loading workspace" ),
290 QMessageBox::tr( err.c_str( ) )
296 if( this->m_Editor != NULL )
297 this->m_Editor->setWorkspace( this->m_Workspace );
303 // -------------------------------------------------------------------------
304 void cpBaseQtApplication::MainWindow::
305 _SaveWorkspace( const std::string& filename )
307 std::string err = this->m_Workspace.SaveWorkspace( filename );
309 QMessageBox::critical(
311 QMessageBox::tr( "Error saving workspace" ),
312 QMessageBox::tr( err.c_str( ) )
316 // -------------------------------------------------------------------------
317 void cpBaseQtApplication::MainWindow::
318 _BackgroundProperties( unsigned int i )
320 if( this->m_MPR == NULL )
324 QColorDialog::getColor(
328 QColorDialog::DontUseNativeDialog
330 if( color.isValid( ) )
332 double r = double( color.red( ) ) / double( 255 );
333 double g = double( color.green( ) ) / double( 255 );
334 double b = double( color.blue( ) ) / double( 255 );
337 unsigned int maxId = ( i == 4 )? 3: 4;
338 for( unsigned int j = 0; j < maxId; ++j )
340 auto ren = this->m_MPR->GetRenderer( j );
343 ren->SetBackground( r, g, b );
352 auto ren = this->m_MPR->GetRenderer( i );
355 ren->SetBackground( r, g, b );
365 // -------------------------------------------------------------------------
366 void cpBaseQtApplication::MainWindow::
367 _ShowData( const std::string& filter_name, const std::string& output_name )
369 if( this->m_MPR == NULL )
372 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
376 auto actor = output->GetVTKActor( );
379 this->m_MPR->AddActor(
380 actor, output_name + std::string( "@" ) + filter_name
387 QMessageBox::critical(
389 QMessageBox::tr( "Error showing data" ),
390 QMessageBox::tr( "Unknown VTK conversion." )
396 QMessageBox::critical(
398 QMessageBox::tr( "Error showing data" ),
399 QMessageBox::tr( "Unknown port name." )
403 // -------------------------------------------------------------------------
404 void cpBaseQtApplication::MainWindow::
405 _HideData( const std::string& filter, const std::string& output )
407 std::cout << "MainWindow::HideData" << std::endl;
412 // -------------------------------------------------------------------------
413 void cpBaseQtApplication::MainWindow::
415 const std::string& filter_name, const std::string& output_name
418 if( this->m_MPR == NULL )
421 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
425 auto actors = this->m_MPR->GetActors(
426 output_name + std::string( "@" ) + filter_name
428 auto dlg = new ActorPropertiesQDialog( NULL );
429 for( auto i = actors.begin( ); i != actors.end( ); ++i )
431 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
432 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
433 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
434 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
439 QMessageBox::critical(
441 QMessageBox::tr( "Error showing data" ),
442 QMessageBox::tr( "Unknown port name." )
446 // -------------------------------------------------------------------------
447 void cpBaseQtApplication::MainWindow::
448 _InteractiveLoadPlugins( )
450 QFileDialog dlg( this );
451 dlg.setFileMode( QFileDialog::ExistingFiles );
452 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
454 std::stringstream filter;
455 std::string suffix = std::string( cpPlugins_LIB_EXT );
456 filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
457 dlg.setNameFilter( filter.str( ).c_str( ) );
458 dlg.setDefaultSuffix( suffix.c_str( ) );
459 if( !( dlg.exec( ) ) )
461 QStringList names = dlg.selectedFiles( );
462 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
463 this->_LoadPlugins( qIt->toStdString( ) );
466 // -------------------------------------------------------------------------
467 void cpBaseQtApplication::MainWindow::
468 _InteractiveLoadPluginsFromPath( )
470 QFileDialog d( this );
471 d.setFileMode( QFileDialog::DirectoryOnly );
472 d.setDirectory( this->m_PluginsPath.c_str( ) );
475 this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
478 // -------------------------------------------------------------------------
479 void cpBaseQtApplication::MainWindow::
480 _InteractiveLoadWorkspace( )
482 QFileDialog dlg( this );
483 dlg.setFileMode( QFileDialog::ExistingFile );
484 dlg.setDirectory( "." );
486 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
488 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
489 if( !( dlg.exec( ) ) )
491 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
494 // -------------------------------------------------------------------------
495 void cpBaseQtApplication::MainWindow::
496 _InteractiveSaveWorkspace( )
498 QFileDialog dlg( this );
499 dlg.setFileMode( QFileDialog::AnyFile );
500 dlg.setDirectory( "." );
501 dlg.setAcceptMode( QFileDialog::AcceptSave );
503 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
505 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
506 if( !( dlg.exec( ) ) )
508 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
511 // -------------------------------------------------------------------------
512 void cpBaseQtApplication::MainWindow::
513 _ExecFilter( const std::string& filter_name )
518 this->m_Workspace.Execute( filter_name );
521 catch( itk::ExceptionObject& err1 )
524 QMessageBox::critical(
526 QMessageBox::tr( "Error executing filter" ),
527 QMessageBox::tr( err1.GetDescription( ) )
530 catch( std::exception& err2 )
533 QMessageBox::critical(
535 QMessageBox::tr( "Error executing filter" ),
536 QMessageBox::tr( err2.what( ) )
542 QMessageBox::critical(
544 QMessageBox::tr( "Error executing filter" ),
545 QMessageBox::tr( "Unknown error" )