X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2FcpBaseQtApplication%2FMainWindow.cxx;h=f8ac71ad118762a584da6459736ac0587d0a94db;hb=1f5c3516bf4240bcdd91979ac303298ef1f75fc3;hp=1a425579b44d19b51f0bb2dbdabcc580e0d6a665;hpb=201c5026430f9bcc33f9db6a39f5d03db096c860;p=cpPlugins.git diff --git a/lib/cpBaseQtApplication/MainWindow.cxx b/lib/cpBaseQtApplication/MainWindow.cxx index 1a42557..f8ac71a 100644 --- a/lib/cpBaseQtApplication/MainWindow.cxx +++ b/lib/cpBaseQtApplication/MainWindow.cxx @@ -1,16 +1,29 @@ #include -#include -#include -#include -#include #include -#include #include #include #include #include -#include + +#include +#include + +#include + +#include +#include +#include +#include + +/* TODO + #include + #include + #include + #include + #include + #include +*/ // ------------------------------------------------------------------------- bool cpBaseQtApplication::MainWindow::_TBlocker:: @@ -35,22 +48,34 @@ eventFilter( QObject* obj, QEvent* event ) // ------------------------------------------------------------------------- cpBaseQtApplication::MainWindow:: -MainWindow( - int argc, char* argv[], - QApplication* app, - QWidget* parent - ) +MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent ) : Superclass( parent ), m_Application( app ), - m_PluginsPath( "." ), - m_TreeWidget( NULL ), + m_Navigator( NULL ), m_Editor( NULL ), m_MPR( NULL ) { - QFileInfo i( argv[ 0 ] ); - if( i.exists( ) ) - this->m_Interface.UpdateEnvironments( i.canonicalPath( ).toStdString( ) ); - this->m_Workspace.SetInterface( &( this->m_Interface ) ); + // Use some evident paths + QFileInfo e_path( argv[ 0 ] ); + QDir r_path( "." ); + std::set< std::string > paths; + paths.insert( e_path.canonicalPath( ).toStdString( ) ); + paths.insert( r_path.canonicalPath( ).toStdString( ) ); + std::stringstream env; + for( auto p = paths.begin( ); p != paths.end( ); ++p ) + env << *p << cpPlugins_ENV_SEPARATOR; + + // Some variables + this->m_RunPath = r_path.canonicalPath( ).toStdString( ); + + // Get the plugins interface + this->m_Plugins = TPlugins::New( ); + try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { } + this->updateEnvironment( ); + try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { } + + // Create local workspace + this->m_Workspace = TWorkspace::New( ); } // ------------------------------------------------------------------------- @@ -61,12 +86,12 @@ cpBaseQtApplication::MainWindow:: // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -UpdateEnvironment( ) +updateEnvironment( ) { try { - this->m_Interface.LoadEnvironment( ); - this->m_Interface.GuessPlugins( ); + this->m_Plugins->LoadEnvironments( ); + this->m_Plugins->GuessPlugins( ); } catch( std::exception& err ) { @@ -80,472 +105,362 @@ UpdateEnvironment( ) // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_Configure( - QTreeWidget* tree, - cpExtensions::QT::SimpleMPRWidget* mpr, - cpBaseQtApplication::Editor* editor - ) +updateFilter( const std::string& name ) { - this->m_TreeWidget = tree; - this->_UpdateLoadedPlugins( ); - this->m_Editor = editor; - this->m_MPR = mpr; - if( this->m_Editor != NULL ) - { - this->m_Editor->setWorkspace( &( this->m_Workspace ) ); - - // Slots <-> signals - this->connect( - this->m_Editor, - SIGNAL( execFilter( const std::string& ) ), - this, - SLOT( _ExecFilter( const std::string& ) ) - ); - this->connect( - this->m_Editor, - SIGNAL( showFilterOutput( const std::string&, const std::string& ) ), - this, - SLOT( _ShowData( const std::string&, const std::string& ) ) - ); - this->connect( - this->m_Editor, - SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ), - this, - SLOT( _HideData( const std::string&, const std::string& ) ) - ); - this->connect( - this->m_Editor, - SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ), - this, - SLOT( _DataProperties( const std::string&, const std::string& ) ) - ); - - } // fi - if( this->m_MPR != NULL ) - this->m_Workspace.SetMPRViewer( this->m_MPR ); - this->_ClearWorkspace( ); + auto filter = this->m_Workspace->GetFilter( name ); + if( filter != NULL ) + cpBaseQtApplication_Execute( filter->Update( ) ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_ClearWorkspace( ) +showData( const std::string& name, const std::string& port ) { - if( this->m_Editor != NULL ) - this->m_Editor->clear( ); - if( this->m_MPR != NULL ) - this->m_MPR->Clear( ); - this->m_Workspace.Clear( ); + this->updateFilter( name ); + auto filter = this->m_Workspace->GetFilter( name ); + if( filter != NULL ) + this->showData( + filter->GetOutput( port ), port + std::string( "@" ) + name + ); + else + QMessageBox::critical( + this, + QMessageBox::tr( "Error showing data" ), + QMessageBox::tr( "Unknown filter." ) + ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_LoadPlugins( const std::string& filename ) +showData( cpPlugins::BaseObjects::DataObject* data, const std::string& name ) { - try - { - this->m_Interface.LoadFile( filename ); - this->_UpdateLoadedPlugins( ); - } - catch( std::exception& err ) - { + if( this->m_MPR == NULL || data == NULL ) + return; + + // Associate visual data + this->_block( ); + bool success = this->m_MPR->Add( data->GetVTK< vtkDataSet >( ), name ); + this->_unBlock( ); + + // Show data or show an error + if( success ) + this->m_MPR->Render( ); + else QMessageBox::critical( this, - "Error loading plugins path", - err.what( ) + QMessageBox::tr( "Error showing data" ), + QMessageBox::tr( "Unknown conversion to a \"vtkProp\" object." ) ); +} - } // yrt +// ------------------------------------------------------------------------- +/* TODO + void cpBaseQtApplication::MainWindow:: + hideData( const std::string& name, const std::string& port ) + { + this->hideData( port + std::string( "@" ) + name ); + } + + // ------------------------------------------------------------------------- + void cpBaseQtApplication::MainWindow:: + hideData( const std::string& name ) + { + } +*/ + +// ------------------------------------------------------------------------- +void cpBaseQtApplication::MainWindow:: +dataProperties( const std::string& name, const std::string& port ) +{ + this->dataProperties( port + std::string( "@" ) + name ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_LoadPluginsFromPath( const std::string& path ) +dataProperties( const std::string& name ) { - try - { - this->m_Interface.LoadDirectory( path ); - this->_UpdateLoadedPlugins( ); - } - catch( std::exception& err ) + if( this->m_MPR == NULL ) + return; + + auto props = this->m_MPR->GetViewProps( name ); + if( props.size( ) > 0 ) { - QMessageBox::critical( - this, - "Error loading plugins path", - err.what( ) - ); + this->_block( ); + auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL ); + dlg->setProps( props ); + dlg->addRenderWindow( this->m_MPR->GetXRenderWindow( ) ); + dlg->addRenderWindow( this->m_MPR->GetYRenderWindow( ) ); + dlg->addRenderWindow( this->m_MPR->GetZRenderWindow( ) ); + dlg->addRenderWindow( this->m_MPR->GetWRenderWindow( ) ); + this->_unBlock( ); + dlg->exec( ); - } // yrt + } // fi } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_UpdateLoadedPlugins( ) +_block( ) { - if( this->m_TreeWidget == NULL ) - return; - this->_Block( ); - auto categories = this->m_Interface.GetCategories( ); - unsigned int filter_count = 0; - for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt ) + if( this->m_Application != NULL ) { - // Create or get category - QList< QTreeWidgetItem* > cat_items = - this->m_TreeWidget->findItems( - cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive - ); - QTreeWidgetItem* cat = NULL; - if( cat_items.size( ) == 0 ) - { - cat = new QTreeWidgetItem( - ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) ) - ); - this->m_TreeWidget->addTopLevelItem( cat ); - } - else - cat = cat_items[ 0 ]; - - // Create filters - auto filters = this->m_Interface.GetFilters( *cIt ); - filter_count += filters.size( ); - for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt ) - { - QList< QTreeWidgetItem* > filter_items = - this->m_TreeWidget->findItems( - fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive - ); - auto fiIt = filter_items.begin( ); - auto found_fiIt = filter_items.end( ); - for( ; fiIt != filter_items.end( ); ++fiIt ) - if( ( *fiIt )->parent( ) == cat ) - found_fiIt = fiIt; - - // Add filter - if( found_fiIt == filter_items.end( ) ) - QTreeWidgetItem* filter = new QTreeWidgetItem( - cat, QStringList( fIt->c_str( ) ) - ); - - } // rof - - } // rof - this->m_TreeWidget->expandAll( ); - this->_UnBlock( ); - - if( filter_count == 0 ) - QMessageBox::critical( - this, - "Error loading default plugins", - "No plugins loaded: remember to load some!!!" - ); + this->m_Application->setOverrideCursor( Qt::WaitCursor ); + this->m_Application->installEventFilter( &( this->m_Blocker ) ); + + } // fi } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_ShowPlugins( ) +_unBlock( ) { - auto paths = this->m_Interface.GetPaths( ); - auto plugins = this->m_Interface.GetPlugins( ); - std::stringstream info; - info << "----- PATHS -----" << std::endl; - for( auto i = paths.begin( ); i != paths.end( ); ++i ) - info << " " << *i << std::endl; - info << std::endl << "----- PLUGINS -----" << std::endl; - for( auto i = plugins.begin( ); i != plugins.end( ); ++i ) - info << " " << *i << std::endl; - QMessageBox::information( - this, - "Loaded libraries and plugins information", - info.str( ).c_str( ) - ); + if( this->m_Application != NULL ) + { + while( this->m_Application->overrideCursor( ) ) + this->m_Application->restoreOverrideCursor( ); + this->m_Application->removeEventFilter( &( this->m_Blocker ) ); + + } // fi } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_Block( ) +_configure( PluginsNavigator* nav, TMPR* mpr, Editor* edt ) { - this->m_Application->setOverrideCursor( Qt::WaitCursor ); - this->m_Application->installEventFilter( &( this->m_Blocker ) ); + this->m_Navigator = nav; + this->m_MPR = mpr; + this->m_Editor = edt; + this->_updatePlugins( ); + if( this->m_Editor != NULL ) + this->m_Editor->setWorkspace( this->m_Workspace ); + + // Associate interactors + if( this->m_MPR != NULL ) + { + this->m_Workspace->AddInteractor( this->m_MPR->GetXInteractor( ) ); + this->m_Workspace->AddInteractor( this->m_MPR->GetYInteractor( ) ); + this->m_Workspace->AddInteractor( this->m_MPR->GetZInteractor( ) ); + this->m_Workspace->AddInteractor( this->m_MPR->GetWInteractor( ) ); + + } // fi } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_UnBlock( ) +_updatePlugins( ) { - while( this->m_Application->overrideCursor( ) ) - this->m_Application->restoreOverrideCursor( ); - this->m_Application->removeEventFilter( &( this->m_Blocker ) ); + if( this->m_Navigator == NULL ) + return; + this->_block( ); + this->m_Navigator->Update( ); + this->_unBlock( ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_LoadWorkspace( const std::string& filename ) +_loadWorkspace( const std::string& filename ) { - this->_ClearWorkspace( ); - std::string err = this->m_Workspace.LoadWorkspace( filename ); - if( err != "" ) + try + { + this->m_Workspace->Load( filename ); + } + catch( std::exception& err ) { QMessageBox::critical( this, QMessageBox::tr( "Error loading workspace" ), - QMessageBox::tr( err.c_str( ) ) + QMessageBox::tr( err.what( ) ) ); - } - else - { - /* TODO - if( this->m_Editor != NULL ) - this->m_Editor->setWorkspace( this->m_Workspace ); - */ - } // fi + } // yrt + if( this->m_Editor != NULL ) + this->m_Editor->redrawWorkspace( ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_SaveWorkspace( const std::string& filename ) +_loadWorkspace( ) { - std::string err = this->m_Workspace.SaveWorkspace( filename ); - if( err != "" ) - QMessageBox::critical( - this, - QMessageBox::tr( "Error saving workspace" ), - QMessageBox::tr( err.c_str( ) ) - ); + QFileDialog dlg( this ); + dlg.setFileMode( QFileDialog::ExistingFile ); + dlg.setDirectory( "." ); + dlg.setNameFilter( + QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" ) + ); + dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) ); + if( !( dlg.exec( ) ) ) + return; + this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_BackgroundProperties( unsigned int i ) +_saveWorkspace( const std::string& filename ) { - if( this->m_MPR == NULL ) - return; - - QColor color = - QColorDialog::getColor( - QColor( 0, 0, 0 ), + try + { + this->m_Workspace->Save( filename ); + } + catch( std::exception& err ) + { + QMessageBox::critical( this, - "Select Color", - QColorDialog::DontUseNativeDialog + QMessageBox::tr( "Error saving workspace" ), + QMessageBox::tr( err.what( ) ) ); - if( color.isValid( ) ) - { - double r = double( color.red( ) ) / double( 255 ); - double g = double( color.green( ) ) / double( 255 ); - double b = double( color.blue( ) ) / double( 255 ); - if( i >= 4 ) - { - unsigned int maxId = ( i == 4 )? 3: 4; - for( unsigned int j = 0; j < maxId; ++j ) - { - auto ren = this->m_MPR->GetRenderer( j ); - if( ren != NULL ) - { - ren->SetBackground( r, g, b ); - ren->Render( ); - - } // fi - - } // rof - } - else - { - auto ren = this->m_MPR->GetRenderer( i ); - if( ren != NULL ) - { - ren->SetBackground( r, g, b ); - ren->Render( ); - - } // fi - - } // fi - } // fi + } // yrt } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_ShowData( const std::string& filter_name, const std::string& output_name ) +_saveWorkspace( ) { - if( this->m_MPR == NULL ) + QFileDialog dlg( this ); + dlg.setFileMode( QFileDialog::AnyFile ); + dlg.setDirectory( "." ); + dlg.setAcceptMode( QFileDialog::AcceptSave ); + dlg.setNameFilter( + QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" ) + ); + dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) ); + if( !( dlg.exec( ) ) ) return; + this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) ); +} - auto output = this->m_Workspace.GetOutput( filter_name, output_name ); - if( output != NULL ) - { - this->_Block( ); - auto actor = output->GetVTKActor( ); - if( actor != NULL ) - { - this->m_MPR->AddActor( - actor, output_name + std::string( "@" ) + filter_name - ); - this->_UnBlock( ); - } - else - { - this->_UnBlock( ); - QMessageBox::critical( - this, - QMessageBox::tr( "Error showing data" ), - QMessageBox::tr( "Unknown VTK conversion." ) - ); - - } // fi - } - else - QMessageBox::critical( - this, - QMessageBox::tr( "Error showing data" ), - QMessageBox::tr( "Unknown port name." ) - ); +// ------------------------------------------------------------------------- +void cpBaseQtApplication::MainWindow:: +_showPlugins( ) +{ + std::stringstream info; + this->m_Plugins->Print( info ); + QMessageBox::information( + this, + "Loaded libraries and plugins information", + info.str( ).c_str( ) + ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_HideData( const std::string& filter, const std::string& output ) +_addEnvironmentPaths( const std::string& envs ) { - std::cout << "MainWindow::HideData" << std::endl; - /* TODO - */ + try { this->m_Plugins->AddEnvironments( envs ); } catch( ... ) { } + try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { } + this->updateEnvironment( ); + this->_updatePlugins( ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_DataProperties( - const std::string& filter_name, const std::string& output_name - ) +_addEnvironmentPaths( ) { - if( this->m_MPR == NULL ) - return; + PathsDialog dlg( this ); + dlg.addPaths( this->m_Plugins->GetPaths( ) ); + if( dlg.exec( ) ) + { + auto paths = dlg.getPaths( ); + std::stringstream envs; + for( auto p = paths.begin( ); p != paths.end( ); ++p ) + envs << *p << cpPlugins_ENV_SEPARATOR; + this->_addEnvironmentPaths( envs.str( ) ); - auto output = this->m_Workspace.GetOutput( filter_name, output_name ); - if( output != NULL ) + } // fi +} + +// ------------------------------------------------------------------------- +void cpBaseQtApplication::MainWindow:: +_loadPlugins( const std::string& filename ) +{ + try { - this->_Block( ); - auto actors = this->m_MPR->GetActors( - output_name + std::string( "@" ) + filter_name - ); - auto dlg = new ActorPropertiesQDialog( NULL ); - for( auto i = actors.begin( ); i != actors.end( ); ++i ) - dlg->addActor( *i ); - dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) ); - dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) ); - dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) ); - dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) ); - this->_UnBlock( ); - dlg->exec( ); + this->m_Plugins->LoadFile( filename ); + this->_updatePlugins( ); } - else + catch( std::exception& err ) + { QMessageBox::critical( this, - QMessageBox::tr( "Error showing data" ), - QMessageBox::tr( "Unknown port name." ) + "Error loading plugins path", + err.what( ) ); + + } // yrt } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_InteractiveLoadPlugins( ) +_loadPlugins( ) { QFileDialog dlg( this ); dlg.setFileMode( QFileDialog::ExistingFiles ); - dlg.setDirectory( this->m_PluginsPath.c_str( ) ); std::stringstream filter; std::string suffix = std::string( cpPlugins_LIB_EXT ); - filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)"; + filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)"; dlg.setNameFilter( filter.str( ).c_str( ) ); dlg.setDefaultSuffix( suffix.c_str( ) ); if( !( dlg.exec( ) ) ) return; QStringList names = dlg.selectedFiles( ); for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt ) - this->_LoadPlugins( qIt->toStdString( ) ); + this->_loadPlugins( qIt->toStdString( ) ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_InteractiveLoadPluginsFromPath( ) +_loadPluginsFromPath( const std::string& path ) { - QFileDialog d( this ); - d.setFileMode( QFileDialog::DirectoryOnly ); - d.setDirectory( this->m_PluginsPath.c_str( ) ); - if( !( d.exec( ) ) ) - return; - this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) ); -} + try + { + this->m_Plugins->LoadDirectory( path ); + this->_updatePlugins( ); + } + catch( std::exception& err ) + { + QMessageBox::critical( + this, + "Error loading plugins path", + err.what( ) + ); -// ------------------------------------------------------------------------- -void cpBaseQtApplication::MainWindow:: -_InteractiveLoadWorkspace( ) -{ - QFileDialog dlg( this ); - dlg.setFileMode( QFileDialog::ExistingFile ); - dlg.setDirectory( "." ); - dlg.setNameFilter( - QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" ) - ); - dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) ); - if( !( dlg.exec( ) ) ) - return; - this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) ); + } // yrt } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_InteractiveSaveWorkspace( ) +_loadPluginsFromPath( ) { - QFileDialog dlg( this ); - dlg.setFileMode( QFileDialog::AnyFile ); - dlg.setDirectory( "." ); - dlg.setAcceptMode( QFileDialog::AcceptSave ); - dlg.setNameFilter( - QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" ) - ); - dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) ); - if( !( dlg.exec( ) ) ) + QFileDialog d( this ); + d.setFileMode( QFileDialog::DirectoryOnly ); + if( !( d.exec( ) ) ) return; - this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) ); + this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) ); } // ------------------------------------------------------------------------- void cpBaseQtApplication::MainWindow:: -_ExecFilter( const std::string& filter_name ) +_actorsProperties( ) { - this->_Block( ); - try - { - this->m_Workspace.Execute( filter_name ); - this->_UnBlock( ); - } - catch( itk::ExceptionObject& err1 ) - { - this->_UnBlock( ); - QMessageBox::critical( - this, - QMessageBox::tr( "Error executing filter" ), - QMessageBox::tr( err1.GetDescription( ) ) - ); - } - catch( std::exception& err2 ) - { - this->_UnBlock( ); - QMessageBox::critical( - this, - QMessageBox::tr( "Error executing filter" ), - QMessageBox::tr( err2.what( ) ) - ); - } - catch( ... ) + if( this->m_MPR != NULL ) { - this->_UnBlock( ); - QMessageBox::critical( - this, - QMessageBox::tr( "Error executing filter" ), - QMessageBox::tr( "Unknown error" ) - ); + auto dlg = new cpExtensions::QT::ConfigurationChooser( this ); + dlg->setData( this->m_MPR ); + dlg->exec( ); - } // yrt + } // fi } +// ------------------------------------------------------------------------- +/* TODO + void cpBaseQtApplication::MainWindow:: + _ClearWorkspace( ) + { + if( this->m_Editor != NULL ) + this->m_Editor->clear( ); + this->m_Workspace->Clear( ); + } +*/ + // eof - $RCSfile$