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 ] );
53 this->m_ExecutionPath = i.canonicalPath( ).toStdString( );
56 this->m_Interface.OpenEnvironments( this->m_ExecutionPath );
57 this->m_Interface.UpdateEnvironments( this->m_ExecutionPath );
62 this->m_ExecutionPath = "";
63 this->m_Workspace.SetInterface( &( this->m_Interface ) );
66 // -------------------------------------------------------------------------
67 cpBaseQtApplication::MainWindow::
72 // -------------------------------------------------------------------------
73 void cpBaseQtApplication::MainWindow::
78 this->m_Interface.LoadEnvironment( );
79 this->m_Interface.GuessPlugins( );
81 catch( std::exception& err )
83 QMessageBox::critical(
84 this, "Error loading required libraries", err.what( )
91 // -------------------------------------------------------------------------
92 void cpBaseQtApplication::MainWindow::
95 cpExtensions::QT::SimpleMPRWidget* mpr,
96 cpBaseQtApplication::Editor* editor
99 this->m_TreeWidget = tree;
100 this->_UpdateLoadedPlugins( );
101 this->m_Editor = editor;
103 if( this->m_Editor != NULL )
105 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
110 SIGNAL( execFilter( const std::string& ) ),
112 SLOT( _ExecFilter( const std::string& ) )
116 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
118 SLOT( _ShowData( const std::string&, const std::string& ) )
122 SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ),
124 SLOT( _HideData( const std::string&, const std::string& ) )
128 SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ),
130 SLOT( _DataProperties( const std::string&, const std::string& ) )
134 if( this->m_MPR != NULL )
135 this->m_Workspace.SetMPRViewer( this->m_MPR );
136 this->_ClearWorkspace( );
139 // -------------------------------------------------------------------------
140 void cpBaseQtApplication::MainWindow::
143 if( this->m_Editor != NULL )
144 this->m_Editor->clear( );
145 if( this->m_MPR != NULL )
146 this->m_MPR->Clear( );
147 this->m_Workspace.Clear( );
150 // -------------------------------------------------------------------------
151 void cpBaseQtApplication::MainWindow::
152 _LoadPlugins( const std::string& filename )
156 this->m_Interface.LoadFile( filename );
157 this->_UpdateLoadedPlugins( );
159 catch( std::exception& err )
161 QMessageBox::critical(
163 "Error loading plugins path",
170 // -------------------------------------------------------------------------
171 void cpBaseQtApplication::MainWindow::
172 _LoadPluginsFromPath( const std::string& path )
176 this->m_Interface.LoadDirectory( path );
177 this->_UpdateLoadedPlugins( );
179 catch( std::exception& err )
181 QMessageBox::critical(
183 "Error loading plugins path",
190 // -------------------------------------------------------------------------
191 void cpBaseQtApplication::MainWindow::
192 _UpdateLoadedPlugins( )
194 if( this->m_TreeWidget == NULL )
197 auto categories = this->m_Interface.GetCategories( );
198 unsigned int filter_count = 0;
199 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
201 // Create or get category
202 QList< QTreeWidgetItem* > cat_items =
203 this->m_TreeWidget->findItems(
204 cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
206 QTreeWidgetItem* cat = NULL;
207 if( cat_items.size( ) == 0 )
209 cat = new QTreeWidgetItem(
210 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
212 this->m_TreeWidget->addTopLevelItem( cat );
215 cat = cat_items[ 0 ];
218 auto filters = this->m_Interface.GetFilters( *cIt );
219 filter_count += filters.size( );
220 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
222 QList< QTreeWidgetItem* > filter_items =
223 this->m_TreeWidget->findItems(
224 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
226 auto fiIt = filter_items.begin( );
227 auto found_fiIt = filter_items.end( );
228 for( ; fiIt != filter_items.end( ); ++fiIt )
229 if( ( *fiIt )->parent( ) == cat )
233 if( found_fiIt == filter_items.end( ) )
234 QTreeWidgetItem* filter = new QTreeWidgetItem(
235 cat, QStringList( fIt->c_str( ) )
241 this->m_TreeWidget->expandAll( );
244 if( filter_count == 0 )
245 QMessageBox::critical(
247 "Error loading default plugins",
248 "No plugins loaded: remember to load some!!!"
252 // -------------------------------------------------------------------------
253 void cpBaseQtApplication::MainWindow::
256 auto paths = this->m_Interface.GetPaths( );
257 auto plugins = this->m_Interface.GetPlugins( );
258 std::stringstream info;
259 info << "----- PATHS -----" << std::endl;
260 for( auto i = paths.begin( ); i != paths.end( ); ++i )
261 info << " " << *i << std::endl;
262 info << std::endl << "----- PLUGINS -----" << std::endl;
263 for( auto i = plugins.begin( ); i != plugins.end( ); ++i )
264 info << " " << *i << std::endl;
265 QMessageBox::information(
267 "Loaded libraries and plugins information",
272 // -------------------------------------------------------------------------
273 void cpBaseQtApplication::MainWindow::
276 this->m_Application->setOverrideCursor( Qt::WaitCursor );
277 this->m_Application->installEventFilter( &( this->m_Blocker ) );
280 // -------------------------------------------------------------------------
281 void cpBaseQtApplication::MainWindow::
284 while( this->m_Application->overrideCursor( ) )
285 this->m_Application->restoreOverrideCursor( );
286 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
289 // -------------------------------------------------------------------------
290 void cpBaseQtApplication::MainWindow::
291 _LoadWorkspace( const std::string& filename )
293 this->_ClearWorkspace( );
294 std::string err = this->m_Workspace.LoadWorkspace( filename );
297 QMessageBox::critical(
299 QMessageBox::tr( "Error loading workspace" ),
300 QMessageBox::tr( err.c_str( ) )
306 if( this->m_Editor != NULL )
307 this->m_Editor->setWorkspace( this->m_Workspace );
313 // -------------------------------------------------------------------------
314 void cpBaseQtApplication::MainWindow::
315 _SaveWorkspace( const std::string& filename )
317 std::string err = this->m_Workspace.SaveWorkspace( filename );
319 QMessageBox::critical(
321 QMessageBox::tr( "Error saving workspace" ),
322 QMessageBox::tr( err.c_str( ) )
326 // -------------------------------------------------------------------------
327 void cpBaseQtApplication::MainWindow::
328 _BackgroundProperties( unsigned int i )
330 if( this->m_MPR == NULL )
334 QColorDialog::getColor(
338 QColorDialog::DontUseNativeDialog
340 if( color.isValid( ) )
342 double r = double( color.red( ) ) / double( 255 );
343 double g = double( color.green( ) ) / double( 255 );
344 double b = double( color.blue( ) ) / double( 255 );
347 unsigned int maxId = ( i == 4 )? 3: 4;
348 for( unsigned int j = 0; j < maxId; ++j )
350 auto ren = this->m_MPR->GetRenderer( j );
353 ren->SetBackground( r, g, b );
362 auto ren = this->m_MPR->GetRenderer( i );
365 ren->SetBackground( r, g, b );
375 // -------------------------------------------------------------------------
376 void cpBaseQtApplication::MainWindow::
377 _ShowData( const std::string& filter_name, const std::string& output_name )
379 if( this->m_MPR == NULL )
382 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
386 auto actor = output->GetVTKActor( );
389 this->m_MPR->AddActor(
390 actor, output_name + std::string( "@" ) + filter_name
397 QMessageBox::critical(
399 QMessageBox::tr( "Error showing data" ),
400 QMessageBox::tr( "Unknown VTK conversion." )
406 QMessageBox::critical(
408 QMessageBox::tr( "Error showing data" ),
409 QMessageBox::tr( "Unknown port name." )
413 // -------------------------------------------------------------------------
414 void cpBaseQtApplication::MainWindow::
415 _HideData( const std::string& filter, const std::string& output )
417 std::cout << "MainWindow::HideData" << std::endl;
422 // -------------------------------------------------------------------------
423 void cpBaseQtApplication::MainWindow::
425 const std::string& filter_name, const std::string& output_name
428 if( this->m_MPR == NULL )
431 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
435 auto actors = this->m_MPR->GetActors(
436 output_name + std::string( "@" ) + filter_name
438 auto dlg = new ActorPropertiesQDialog( NULL );
439 for( auto i = actors.begin( ); i != actors.end( ); ++i )
441 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
442 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
443 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
444 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
449 QMessageBox::critical(
451 QMessageBox::tr( "Error showing data" ),
452 QMessageBox::tr( "Unknown port name." )
456 // -------------------------------------------------------------------------
457 void cpBaseQtApplication::MainWindow::
458 _InteractiveLoadPlugins( )
460 QFileDialog dlg( this );
461 dlg.setFileMode( QFileDialog::ExistingFiles );
462 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
464 std::stringstream filter;
465 std::string suffix = std::string( cpPlugins_LIB_EXT );
466 filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
467 dlg.setNameFilter( filter.str( ).c_str( ) );
468 dlg.setDefaultSuffix( suffix.c_str( ) );
469 if( !( dlg.exec( ) ) )
471 QStringList names = dlg.selectedFiles( );
472 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
473 this->_LoadPlugins( qIt->toStdString( ) );
476 // -------------------------------------------------------------------------
477 void cpBaseQtApplication::MainWindow::
478 _InteractiveLoadPluginsFromPath( )
480 QFileDialog d( this );
481 d.setFileMode( QFileDialog::DirectoryOnly );
482 d.setDirectory( this->m_PluginsPath.c_str( ) );
485 this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
488 // -------------------------------------------------------------------------
489 void cpBaseQtApplication::MainWindow::
490 _InteractiveLoadWorkspace( )
492 QFileDialog dlg( this );
493 dlg.setFileMode( QFileDialog::ExistingFile );
494 dlg.setDirectory( "." );
496 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
498 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
499 if( !( dlg.exec( ) ) )
501 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
504 // -------------------------------------------------------------------------
505 void cpBaseQtApplication::MainWindow::
506 _InteractiveSaveWorkspace( )
508 QFileDialog dlg( this );
509 dlg.setFileMode( QFileDialog::AnyFile );
510 dlg.setDirectory( "." );
511 dlg.setAcceptMode( QFileDialog::AcceptSave );
513 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
515 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
516 if( !( dlg.exec( ) ) )
518 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
521 // -------------------------------------------------------------------------
522 void cpBaseQtApplication::MainWindow::
523 _ExecFilter( const std::string& filter_name )
528 this->m_Workspace.Execute( filter_name );
531 catch( itk::ExceptionObject& err1 )
534 QMessageBox::critical(
536 QMessageBox::tr( "Error executing filter" ),
537 QMessageBox::tr( err1.GetDescription( ) )
540 catch( std::exception& err2 )
543 QMessageBox::critical(
545 QMessageBox::tr( "Error executing filter" ),
546 QMessageBox::tr( err2.what( ) )
552 QMessageBox::critical(
554 QMessageBox::tr( "Error executing filter" ),
555 QMessageBox::tr( "Unknown error" )