1 #include <cpBaseQtApplication/MainWindow.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
5 #include <cpBaseQtApplication/Editor.h>
6 #include <cpBaseQtApplication/PathsDialog.h>
7 #include <vtkRenderer.h>
8 #include <QApplication>
9 #include <QColorDialog>
11 #include <QFileDialog>
13 #include <QMessageBox>
14 #include <QTreeWidget>
16 // -------------------------------------------------------------------------
17 bool cpBaseQtApplication::MainWindow::_TBlocker::
18 eventFilter( QObject* obj, QEvent* event )
20 return( true ); // -> Block all events
21 /* NOTE: this should be the correct implementation:
22 switch( event->type( ) )
24 //list event you want to prevent here ...
25 case QEvent::KeyPress:
26 case QEvent::KeyRelease:
27 case QEvent::MouseButtonRelease:
28 case QEvent::MouseButtonPress:
29 case QEvent::MouseButtonDblClick:
33 return( this->QObject::eventFilter( obj, event ) );
37 // -------------------------------------------------------------------------
38 cpBaseQtApplication::MainWindow::
40 int argc, char* argv[],
44 : Superclass( parent ),
51 QFileInfo i( argv[ 0 ] );
54 this->m_ExecutionPath = i.canonicalPath( ).toStdString( );
55 try { this->m_Interface.AddEnvironments( this->m_ExecutionPath ); }
57 try { this->m_Interface.OpenEnvironments( this->m_ExecutionPath ); }
59 try { this->m_Interface.SaveEnvironments( this->m_ExecutionPath ); }
63 this->m_ExecutionPath = "";
64 this->UpdateEnvironment( );
65 this->m_Workspace.SetInterface( &( this->m_Interface ) );
68 // -------------------------------------------------------------------------
69 cpBaseQtApplication::MainWindow::
74 // -------------------------------------------------------------------------
75 void cpBaseQtApplication::MainWindow::
80 this->m_Interface.LoadEnvironments( );
81 this->m_Interface.GuessPlugins( );
83 catch( std::exception& err )
85 QMessageBox::critical(
86 this, "Error loading required libraries", err.what( )
93 // -------------------------------------------------------------------------
94 void cpBaseQtApplication::MainWindow::
97 cpExtensions::QT::SimpleMPRWidget* mpr,
98 cpBaseQtApplication::Editor* editor
101 this->m_TreeWidget = tree;
102 this->_UpdateLoadedPlugins( );
103 this->m_Editor = editor;
105 if( this->m_Editor != NULL )
107 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
112 SIGNAL( execFilter( const std::string& ) ),
114 SLOT( _ExecFilter( const std::string& ) )
118 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
120 SLOT( _ShowData( const std::string&, const std::string& ) )
124 SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ),
126 SLOT( _HideData( const std::string&, const std::string& ) )
130 SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ),
132 SLOT( _DataProperties( const std::string&, const std::string& ) )
136 if( this->m_MPR != NULL )
137 this->m_Workspace.SetMPRViewer( this->m_MPR );
138 this->_ClearWorkspace( );
141 // -------------------------------------------------------------------------
142 void cpBaseQtApplication::MainWindow::
145 if( this->m_Editor != NULL )
146 this->m_Editor->clear( );
147 if( this->m_MPR != NULL )
148 this->m_MPR->Clear( );
149 this->m_Workspace.Clear( );
152 // -------------------------------------------------------------------------
153 void cpBaseQtApplication::MainWindow::
154 _AddEnvironments( const std::string& environments )
156 try { this->m_Interface.AddEnvironments( environments ); }
158 try { this->m_Interface.SaveEnvironments( this->m_ExecutionPath ); }
160 this->UpdateEnvironment( );
163 // -------------------------------------------------------------------------
164 void cpBaseQtApplication::MainWindow::
165 _LoadPlugins( const std::string& filename )
169 this->m_Interface.LoadFile( filename );
170 this->_UpdateLoadedPlugins( );
172 catch( std::exception& err )
174 QMessageBox::critical(
176 "Error loading plugins path",
183 // -------------------------------------------------------------------------
184 void cpBaseQtApplication::MainWindow::
185 _LoadPluginsFromPath( const std::string& path )
189 this->m_Interface.LoadDirectory( path );
190 this->_UpdateLoadedPlugins( );
192 catch( std::exception& err )
194 QMessageBox::critical(
196 "Error loading plugins path",
203 // -------------------------------------------------------------------------
204 void cpBaseQtApplication::MainWindow::
205 _UpdateLoadedPlugins( )
207 if( this->m_TreeWidget == NULL )
210 auto categories = this->m_Interface.GetCategories( );
211 unsigned int filter_count = 0;
212 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
214 // Create or get category
215 QList< QTreeWidgetItem* > cat_items =
216 this->m_TreeWidget->findItems(
217 cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
219 QTreeWidgetItem* cat = NULL;
220 if( cat_items.size( ) == 0 )
222 cat = new QTreeWidgetItem(
223 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
225 this->m_TreeWidget->addTopLevelItem( cat );
228 cat = cat_items[ 0 ];
231 auto filters = this->m_Interface.GetFilters( *cIt );
232 filter_count += filters.size( );
233 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
235 QList< QTreeWidgetItem* > filter_items =
236 this->m_TreeWidget->findItems(
237 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
239 auto fiIt = filter_items.begin( );
240 auto found_fiIt = filter_items.end( );
241 for( ; fiIt != filter_items.end( ); ++fiIt )
242 if( ( *fiIt )->parent( ) == cat )
246 if( found_fiIt == filter_items.end( ) )
247 QTreeWidgetItem* filter = new QTreeWidgetItem(
248 cat, QStringList( fIt->c_str( ) )
254 this->m_TreeWidget->expandAll( );
257 if( filter_count == 0 )
258 QMessageBox::critical(
260 "Error loading default plugins",
261 "No plugins loaded: remember to load some!!!"
265 // -------------------------------------------------------------------------
266 void cpBaseQtApplication::MainWindow::
269 this->m_Application->setOverrideCursor( Qt::WaitCursor );
270 this->m_Application->installEventFilter( &( this->m_Blocker ) );
273 // -------------------------------------------------------------------------
274 void cpBaseQtApplication::MainWindow::
277 while( this->m_Application->overrideCursor( ) )
278 this->m_Application->restoreOverrideCursor( );
279 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
282 // -------------------------------------------------------------------------
283 void cpBaseQtApplication::MainWindow::
284 _LoadWorkspace( const std::string& filename )
286 this->_ClearWorkspace( );
287 std::string err = this->m_Workspace.LoadWorkspace( filename );
290 QMessageBox::critical(
292 QMessageBox::tr( "Error loading workspace" ),
293 QMessageBox::tr( err.c_str( ) )
299 if( this->m_Editor != NULL )
300 this->m_Editor->setWorkspace( this->m_Workspace );
306 // -------------------------------------------------------------------------
307 void cpBaseQtApplication::MainWindow::
308 _SaveWorkspace( const std::string& filename )
310 std::string err = this->m_Workspace.SaveWorkspace( filename );
312 QMessageBox::critical(
314 QMessageBox::tr( "Error saving workspace" ),
315 QMessageBox::tr( err.c_str( ) )
319 // -------------------------------------------------------------------------
320 void cpBaseQtApplication::MainWindow::
321 _BackgroundProperties( unsigned int i )
323 if( this->m_MPR == NULL )
327 QColorDialog::getColor(
331 QColorDialog::DontUseNativeDialog
333 if( color.isValid( ) )
335 double r = double( color.red( ) ) / double( 255 );
336 double g = double( color.green( ) ) / double( 255 );
337 double b = double( color.blue( ) ) / double( 255 );
340 unsigned int maxId = ( i == 4 )? 3: 4;
341 for( unsigned int j = 0; j < maxId; ++j )
343 auto ren = this->m_MPR->GetRenderer( j );
346 ren->SetBackground( r, g, b );
355 auto ren = this->m_MPR->GetRenderer( i );
358 ren->SetBackground( r, g, b );
368 // -------------------------------------------------------------------------
369 void cpBaseQtApplication::MainWindow::
370 _ShowData( const std::string& filter_name, const std::string& output_name )
372 if( this->m_MPR == NULL )
375 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
379 auto actor = output->GetVTKActor( );
382 this->m_MPR->AddActor(
383 actor, output_name + std::string( "@" ) + filter_name
390 QMessageBox::critical(
392 QMessageBox::tr( "Error showing data" ),
393 QMessageBox::tr( "Unknown VTK conversion." )
399 QMessageBox::critical(
401 QMessageBox::tr( "Error showing data" ),
402 QMessageBox::tr( "Unknown port name." )
406 // -------------------------------------------------------------------------
407 void cpBaseQtApplication::MainWindow::
408 _HideData( const std::string& filter, const std::string& output )
410 std::cout << "MainWindow::HideData" << std::endl;
415 // -------------------------------------------------------------------------
416 void cpBaseQtApplication::MainWindow::
418 const std::string& filter_name, const std::string& output_name
421 if( this->m_MPR == NULL )
424 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
428 auto actors = this->m_MPR->GetActors(
429 output_name + std::string( "@" ) + filter_name
431 auto dlg = new ActorPropertiesQDialog( NULL );
432 for( auto i = actors.begin( ); i != actors.end( ); ++i )
434 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
435 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
436 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
437 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
442 QMessageBox::critical(
444 QMessageBox::tr( "Error showing data" ),
445 QMessageBox::tr( "Unknown port name." )
449 // -------------------------------------------------------------------------
450 void cpBaseQtApplication::MainWindow::
451 _InteractiveLoadPlugins( )
453 QFileDialog dlg( this );
454 dlg.setFileMode( QFileDialog::ExistingFiles );
455 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
457 std::stringstream filter;
458 std::string suffix = std::string( cpPlugins_LIB_EXT );
459 filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
460 dlg.setNameFilter( filter.str( ).c_str( ) );
461 dlg.setDefaultSuffix( suffix.c_str( ) );
462 if( !( dlg.exec( ) ) )
464 QStringList names = dlg.selectedFiles( );
465 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
466 this->_LoadPlugins( qIt->toStdString( ) );
469 // -------------------------------------------------------------------------
470 void cpBaseQtApplication::MainWindow::
471 _InteractiveLoadPluginsFromPath( )
473 QFileDialog d( this );
474 d.setFileMode( QFileDialog::DirectoryOnly );
475 d.setDirectory( this->m_PluginsPath.c_str( ) );
478 this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
481 // -------------------------------------------------------------------------
482 void cpBaseQtApplication::MainWindow::
485 auto paths = this->m_Interface.GetPaths( );
486 auto plugins = this->m_Interface.GetPlugins( );
487 std::stringstream info;
488 info << "----- PATHS -----" << std::endl;
489 for( auto i = paths.begin( ); i != paths.end( ); ++i )
490 info << " " << *i << std::endl;
491 info << std::endl << "----- PLUGINS -----" << std::endl;
492 for( auto i = plugins.begin( ); i != plugins.end( ); ++i )
493 info << " " << *i << std::endl;
494 QMessageBox::information(
496 "Loaded libraries and plugins information",
501 // -------------------------------------------------------------------------
502 void cpBaseQtApplication::MainWindow::
503 _InteractiveLoadWorkspace( )
505 QFileDialog dlg( this );
506 dlg.setFileMode( QFileDialog::ExistingFile );
507 dlg.setDirectory( "." );
509 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
511 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
512 if( !( dlg.exec( ) ) )
514 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
517 // -------------------------------------------------------------------------
518 void cpBaseQtApplication::MainWindow::
519 _InteractiveSaveWorkspace( )
521 QFileDialog dlg( this );
522 dlg.setFileMode( QFileDialog::AnyFile );
523 dlg.setDirectory( "." );
524 dlg.setAcceptMode( QFileDialog::AcceptSave );
526 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
528 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
529 if( !( dlg.exec( ) ) )
531 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
534 // -------------------------------------------------------------------------
535 void cpBaseQtApplication::MainWindow::
536 _InteractiveAddEnviromentPaths( )
538 PathsDialog dlg( this );
539 dlg.addPaths( this->m_Interface.GetPaths( ) );
542 auto paths = dlg.getPaths( );
543 for( auto p = paths.begin( ); p != paths.end( ); ++p )
545 try { this->m_Interface.AddEnvironments( *p ); }
549 this->UpdateEnvironment( );
554 // -------------------------------------------------------------------------
555 void cpBaseQtApplication::MainWindow::
556 _ExecFilter( const std::string& filter_name )
561 this->m_Workspace.Execute( filter_name );
564 catch( itk::ExceptionObject& err1 )
567 QMessageBox::critical(
569 QMessageBox::tr( "Error executing filter" ),
570 QMessageBox::tr( err1.GetDescription( ) )
573 catch( std::exception& err2 )
576 QMessageBox::critical(
578 QMessageBox::tr( "Error executing filter" ),
579 QMessageBox::tr( err2.what( ) )
585 QMessageBox::critical(
587 QMessageBox::tr( "Error executing filter" ),
588 QMessageBox::tr( "Unknown error" )