#include #include #include #include #include #include #include #include #include #include #include #include // ------------------------------------------------------------------------- bool cpPipelineEditor::BaseQtMainWindow::_TBlocker:: eventFilter( QObject* obj, QEvent* event ) { return( true ); // -> Block all events /* NOTE: this should be the correct implementation: switch( event->type( ) ) { //list event you want to prevent here ... case QEvent::KeyPress: case QEvent::KeyRelease: case QEvent::MouseButtonRelease: case QEvent::MouseButtonPress: case QEvent::MouseButtonDblClick: //... return( true ); } // hctiws return( this->QObject::eventFilter( obj, event ) ); */ } // ------------------------------------------------------------------------- cpPipelineEditor::BaseQtMainWindow:: BaseQtMainWindow( int argc, char* argv[], QApplication* app, QWidget* parent ) : Superclass( parent ), m_Application( app ), m_PluginsPath( "." ), m_Interface( NULL ), m_Workspace( NULL ), m_TreeWidget( NULL ), m_Editor( NULL ), m_MPR( NULL ) { this->m_ApplicationPath = "."; QFileInfo info( argv[ 0 ] ); if( info.exists( ) ) this->m_ApplicationPath = info.canonicalPath( ).toStdString( ); } // ------------------------------------------------------------------------- cpPipelineEditor::BaseQtMainWindow:: ~BaseQtMainWindow( ) { if( this->m_Workspace != NULL ) delete this->m_Workspace; delete this->m_Interface; } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _Configure( QTreeWidget* tree, cpExtensions::QT::SimpleMPRWidget* mpr, cpPipelineEditor::Editor* editor ) { if( this->m_Interface != NULL ) { delete this->m_Interface; this->m_Interface = NULL; } // fi try { this->m_Interface = new cpPlugins::Interface( ); this->m_Interface->GuessAccesiblePlugins( ); } catch( std::exception& err ) { if( this->m_Interface != NULL ) delete this->m_Interface; this->m_Interface = NULL; QMessageBox::critical( this, "Error creating plugins interface", err.what( ) ); std::exit( 1 ); } // yrt // Try to load plugins from executable dir this->_LoadPluginsFromPath( this->m_ApplicationPath ); // Finish configuration this->m_TreeWidget = tree; if( this->m_TreeWidget != NULL ) this->_UpdateLoadedPlugins( ); this->m_Editor = editor; this->m_MPR = mpr; this->_CreateWorkspace( ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _CreateWorkspace( ) { if( this->m_Workspace != NULL ) delete this->m_Workspace; this->m_Workspace = new cpPlugins::Workspace( ); this->m_Workspace->SetInterface( this->m_Interface ); if( this->m_Editor != NULL ) this->m_Editor->setWorkspace( this->m_Workspace ); if( this->m_MPR != NULL ) this->m_Workspace->SetMPRViewer( this->m_MPR ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _LoadPlugins( const std::string& filename ) { try { this->m_Interface->LoadPluginFile( filename ); this->_UpdateLoadedPlugins( ); } catch( std::exception& err ) { QMessageBox::critical( this, "Error loading plugins path", err.what( ) ); } // yrt } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _LoadPluginsFromPath( const std::string& path ) { try { this->m_Interface->LoadPluginDir( path ); this->_UpdateLoadedPlugins( ); } catch( std::exception& err ) { QMessageBox::critical( this, "Error loading plugins path", err.what( ) ); } // yrt } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _UpdateLoadedPlugins( ) { this->_Block( ); auto filters = this->m_Interface->GetFilters( ); if( filters.size( ) == 0 ) { this->_UnBlock( ); QMessageBox::critical( this, "Error loading default plugins", "No plugins loaded: remember to load some!!!" ); return; } // fi if( this->m_TreeWidget != NULL ) { for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt ) { // Create or get category QList< QTreeWidgetItem* > cat_items = this->m_TreeWidget->findItems( cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive ); QTreeWidgetItem* cat = NULL; if( cat_items.size( ) == 0 ) { cat = new QTreeWidgetItem( ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) ) ); this->m_TreeWidget->addTopLevelItem( cat ); } else cat = cat_items[ 0 ]; // Create filters auto fIt = cIt->second.begin( ); for( ; fIt != cIt->second.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 } // fi this->_UnBlock( ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _Block( ) { this->m_Application->setOverrideCursor( Qt::WaitCursor ); this->m_Application->installEventFilter( &( this->m_Blocker ) ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _UnBlock( ) { while( this->m_Application->overrideCursor( ) ) this->m_Application->restoreOverrideCursor( ); this->m_Application->removeEventFilter( &( this->m_Blocker ) ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _LoadWorkspace( const std::string& filename ) { this->_CreateWorkspace( ); std::string err = this->m_Workspace->LoadWorkspace( filename ); if( err != "" ) { QMessageBox::critical( this, QMessageBox::tr( "Error loading workspace" ), QMessageBox::tr( err.c_str( ) ) ); } else { if( this->m_Editor != NULL ) this->m_Editor->setWorkspace( this->m_Workspace ); } // fi } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _SaveWorkspace( const std::string& filename ) { if( this->m_Workspace != NULL ) { std::string err = this->m_Workspace->SaveWorkspace( filename ); if( err != "" ) QMessageBox::critical( this, QMessageBox::tr( "Error saving workspace" ), QMessageBox::tr( err.c_str( ) ) ); } // fi } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _ShowData( const std::string& filter_name, const std::string& output_name ) { if( this->m_MPR == NULL || this->m_Workspace == NULL ) return; 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 cpPipelineEditor::BaseQtMainWindow:: _HideData( const std::string& filter, const std::string& output ) { std::cout << "BaseQtMainWindow::HideData" << std::endl; /* TODO */ } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _DataProperties( const std::string& filter_name, const std::string& output_name ) { if( this->m_MPR == NULL || this->m_Workspace == NULL ) return; auto output = this->m_Workspace->GetOutput( filter_name, output_name ); if( output != NULL ) { this->_Block( ); auto actors = this->m_MPR->GetActors( output_name + std::string( "@" ) + filter_name ); auto dlg = new cpPlugins::ActorPropertiesQtDialog( 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( ); } else QMessageBox::critical( this, QMessageBox::tr( "Error showing data" ), QMessageBox::tr( "Unknown port name." ) ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _BackgroundProperties( unsigned int i ) { if( this->m_MPR == NULL ) return; QColor color = QColorDialog::getColor( QColor( 0, 0, 0 ), this, "Select Color", QColorDialog::DontUseNativeDialog ); 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 } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _InteractiveLoadPlugins( ) { 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 (*)"; 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( ) ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _InteractiveLoadPluginsFromPath( ) { QFileDialog d( this ); d.setFileMode( QFileDialog::DirectoryOnly ); d.setDirectory( this->m_PluginsPath.c_str( ) ); if( !( d.exec( ) ) ) return; this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _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( ) ); } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _InteractiveSaveWorkspace( ) { if( this->m_Workspace != 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( ) ); } // fi } // ------------------------------------------------------------------------- void cpPipelineEditor::BaseQtMainWindow:: _ExecFilter( const std::string& filter_name ) { if( this->m_Workspace == NULL ) return; 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( ... ) { this->_UnBlock( ); QMessageBox::critical( this, QMessageBox::tr( "Error executing filter" ), QMessageBox::tr( "Unknown error" ) ); } // yrt } // eof - $RCSfile$