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 e_path( argv[ 0 ] );
53 this->m_RunPath = r_path.canonicalPath( ).toStdString( );
54 std::stringstream paths;
56 << e_path.canonicalPath( ).toStdString( )
57 << cpPlugins_SEPARATOR
60 try { this->m_Interface.AddEnvironments( this->m_RunPath ); }
62 try { this->m_Interface.OpenEnvironments( this->m_RunPath ); }
64 try { this->m_Interface.SaveEnvironments( this->m_RunPath ); }
66 this->UpdateEnvironment( );
67 this->m_Workspace.SetInterface( &( this->m_Interface ) );
70 // -------------------------------------------------------------------------
71 cpBaseQtApplication::MainWindow::
76 // -------------------------------------------------------------------------
77 void cpBaseQtApplication::MainWindow::
82 this->m_Interface.LoadEnvironments( );
83 this->m_Interface.GuessPlugins( );
85 catch( std::exception& err )
87 QMessageBox::critical(
88 this, "Error loading required libraries", err.what( )
95 // -------------------------------------------------------------------------
96 void cpBaseQtApplication::MainWindow::
99 cpExtensions::QT::SimpleMPRWidget* mpr,
100 cpBaseQtApplication::Editor* editor
103 this->m_TreeWidget = tree;
104 this->_UpdateLoadedPlugins( );
105 this->m_Editor = editor;
107 if( this->m_Editor != NULL )
109 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
114 SIGNAL( execFilter( const std::string& ) ),
116 SLOT( _ExecFilter( const std::string& ) )
120 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
122 SLOT( _ShowData( const std::string&, const std::string& ) )
126 SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ),
128 SLOT( _HideData( const std::string&, const std::string& ) )
132 SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ),
134 SLOT( _DataProperties( const std::string&, const std::string& ) )
138 if( this->m_MPR != NULL )
139 this->m_Workspace.SetMPRViewer( this->m_MPR );
140 this->_ClearWorkspace( );
143 // -------------------------------------------------------------------------
144 void cpBaseQtApplication::MainWindow::
147 if( this->m_Editor != NULL )
148 this->m_Editor->clear( );
149 if( this->m_MPR != NULL )
150 this->m_MPR->Clear( );
151 this->m_Workspace.Clear( );
154 // -------------------------------------------------------------------------
155 void cpBaseQtApplication::MainWindow::
156 _AddEnvironments( const std::string& environments )
158 try { this->m_Interface.AddEnvironments( environments ); }
160 try { this->m_Interface.SaveEnvironments( this->m_RunPath ); }
162 this->UpdateEnvironment( );
165 // -------------------------------------------------------------------------
166 void cpBaseQtApplication::MainWindow::
167 _LoadPlugins( const std::string& filename )
171 this->m_Interface.LoadFile( filename );
172 this->_UpdateLoadedPlugins( );
174 catch( std::exception& err )
176 QMessageBox::critical(
178 "Error loading plugins path",
185 // -------------------------------------------------------------------------
186 void cpBaseQtApplication::MainWindow::
187 _LoadPluginsFromPath( const std::string& path )
191 this->m_Interface.LoadDirectory( path );
192 this->_UpdateLoadedPlugins( );
194 catch( std::exception& err )
196 QMessageBox::critical(
198 "Error loading plugins path",
205 // -------------------------------------------------------------------------
206 void cpBaseQtApplication::MainWindow::
207 _UpdateLoadedPlugins( )
209 if( this->m_TreeWidget == NULL )
212 auto categories = this->m_Interface.GetCategories( );
213 unsigned int filter_count = 0;
214 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
216 // Create or get category
217 QList< QTreeWidgetItem* > cat_items =
218 this->m_TreeWidget->findItems(
219 cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
221 QTreeWidgetItem* cat = NULL;
222 if( cat_items.size( ) == 0 )
224 cat = new QTreeWidgetItem(
225 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
227 this->m_TreeWidget->addTopLevelItem( cat );
230 cat = cat_items[ 0 ];
233 auto filters = this->m_Interface.GetFilters( *cIt );
234 filter_count += filters.size( );
235 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
237 QList< QTreeWidgetItem* > filter_items =
238 this->m_TreeWidget->findItems(
239 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
241 auto fiIt = filter_items.begin( );
242 auto found_fiIt = filter_items.end( );
243 for( ; fiIt != filter_items.end( ); ++fiIt )
244 if( ( *fiIt )->parent( ) == cat )
248 if( found_fiIt == filter_items.end( ) )
249 QTreeWidgetItem* filter = new QTreeWidgetItem(
250 cat, QStringList( fIt->c_str( ) )
256 this->m_TreeWidget->expandAll( );
259 if( filter_count == 0 )
260 QMessageBox::critical(
262 "Error loading default plugins",
263 "No plugins loaded: remember to load some!!!"
267 // -------------------------------------------------------------------------
268 void cpBaseQtApplication::MainWindow::
271 this->m_Application->setOverrideCursor( Qt::WaitCursor );
272 this->m_Application->installEventFilter( &( this->m_Blocker ) );
275 // -------------------------------------------------------------------------
276 void cpBaseQtApplication::MainWindow::
279 while( this->m_Application->overrideCursor( ) )
280 this->m_Application->restoreOverrideCursor( );
281 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
284 // -------------------------------------------------------------------------
285 void cpBaseQtApplication::MainWindow::
286 _LoadWorkspace( const std::string& filename )
288 this->_ClearWorkspace( );
289 std::string err = this->m_Workspace.LoadWorkspace( filename );
292 QMessageBox::critical(
294 QMessageBox::tr( "Error loading workspace" ),
295 QMessageBox::tr( err.c_str( ) )
301 if( this->m_Editor != NULL )
302 this->m_Editor->setWorkspace( this->m_Workspace );
308 // -------------------------------------------------------------------------
309 void cpBaseQtApplication::MainWindow::
310 _SaveWorkspace( const std::string& filename )
312 std::string err = this->m_Workspace.SaveWorkspace( filename );
314 QMessageBox::critical(
316 QMessageBox::tr( "Error saving workspace" ),
317 QMessageBox::tr( err.c_str( ) )
321 // -------------------------------------------------------------------------
322 void cpBaseQtApplication::MainWindow::
323 _BackgroundProperties( unsigned int i )
325 if( this->m_MPR == NULL )
329 QColorDialog::getColor(
333 QColorDialog::DontUseNativeDialog
335 if( color.isValid( ) )
337 double r = double( color.red( ) ) / double( 255 );
338 double g = double( color.green( ) ) / double( 255 );
339 double b = double( color.blue( ) ) / double( 255 );
342 unsigned int maxId = ( i == 4 )? 3: 4;
343 for( unsigned int j = 0; j < maxId; ++j )
345 auto ren = this->m_MPR->GetRenderer( j );
348 ren->SetBackground( r, g, b );
357 auto ren = this->m_MPR->GetRenderer( i );
360 ren->SetBackground( r, g, b );
370 // -------------------------------------------------------------------------
371 void cpBaseQtApplication::MainWindow::
372 _ShowData( const std::string& filter_name, const std::string& output_name )
374 if( this->m_MPR == NULL )
377 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
381 auto actor = output->GetVTKActor( );
384 this->m_MPR->AddActor(
385 actor, output_name + std::string( "@" ) + filter_name
392 QMessageBox::critical(
394 QMessageBox::tr( "Error showing data" ),
395 QMessageBox::tr( "Unknown VTK conversion." )
401 QMessageBox::critical(
403 QMessageBox::tr( "Error showing data" ),
404 QMessageBox::tr( "Unknown port name." )
408 // -------------------------------------------------------------------------
409 void cpBaseQtApplication::MainWindow::
410 _HideData( const std::string& filter, const std::string& output )
412 std::cout << "MainWindow::HideData" << std::endl;
417 // -------------------------------------------------------------------------
418 void cpBaseQtApplication::MainWindow::
420 const std::string& filter_name, const std::string& output_name
423 if( this->m_MPR == NULL )
426 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
430 auto actors = this->m_MPR->GetActors(
431 output_name + std::string( "@" ) + filter_name
433 auto dlg = new ActorPropertiesQDialog( NULL );
434 for( auto i = actors.begin( ); i != actors.end( ); ++i )
436 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
437 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
438 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
439 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
444 QMessageBox::critical(
446 QMessageBox::tr( "Error showing data" ),
447 QMessageBox::tr( "Unknown port name." )
451 // -------------------------------------------------------------------------
452 void cpBaseQtApplication::MainWindow::
453 _InteractiveLoadPlugins( )
455 QFileDialog dlg( this );
456 dlg.setFileMode( QFileDialog::ExistingFiles );
457 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
459 std::stringstream filter;
460 std::string suffix = std::string( cpPlugins_LIB_EXT );
461 filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
462 dlg.setNameFilter( filter.str( ).c_str( ) );
463 dlg.setDefaultSuffix( suffix.c_str( ) );
464 if( !( dlg.exec( ) ) )
466 QStringList names = dlg.selectedFiles( );
467 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
468 this->_LoadPlugins( qIt->toStdString( ) );
471 // -------------------------------------------------------------------------
472 void cpBaseQtApplication::MainWindow::
473 _InteractiveLoadPluginsFromPath( )
475 QFileDialog d( this );
476 d.setFileMode( QFileDialog::DirectoryOnly );
477 d.setDirectory( this->m_PluginsPath.c_str( ) );
480 this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
483 // -------------------------------------------------------------------------
484 void cpBaseQtApplication::MainWindow::
487 auto paths = this->m_Interface.GetPaths( );
488 auto plugins = this->m_Interface.GetPlugins( );
489 std::stringstream info;
490 info << "----- PATHS -----" << std::endl;
491 for( auto i = paths.begin( ); i != paths.end( ); ++i )
492 info << " " << *i << std::endl;
493 info << std::endl << "----- PLUGINS -----" << std::endl;
494 for( auto i = plugins.begin( ); i != plugins.end( ); ++i )
495 info << " " << *i << std::endl;
496 QMessageBox::information(
498 "Loaded libraries and plugins information",
503 // -------------------------------------------------------------------------
504 void cpBaseQtApplication::MainWindow::
505 _InteractiveLoadWorkspace( )
507 QFileDialog dlg( this );
508 dlg.setFileMode( QFileDialog::ExistingFile );
509 dlg.setDirectory( "." );
511 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
513 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
514 if( !( dlg.exec( ) ) )
516 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
519 // -------------------------------------------------------------------------
520 void cpBaseQtApplication::MainWindow::
521 _InteractiveSaveWorkspace( )
523 QFileDialog dlg( this );
524 dlg.setFileMode( QFileDialog::AnyFile );
525 dlg.setDirectory( "." );
526 dlg.setAcceptMode( QFileDialog::AcceptSave );
528 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
530 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
531 if( !( dlg.exec( ) ) )
533 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
536 // -------------------------------------------------------------------------
537 void cpBaseQtApplication::MainWindow::
538 _InteractiveAddEnviromentPaths( )
540 PathsDialog dlg( this );
541 dlg.addPaths( this->m_Interface.GetPaths( ) );
544 auto paths = dlg.getPaths( );
545 std::stringstream envs;
546 for( auto p = paths.begin( ); p != paths.end( ); ++p )
547 envs << *p << cpPlugins_SEPARATOR;
548 this->_AddEnvironments( envs.str( ) );
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" )