#include "App_cpPipelineEditor.h" #include "ui_App_cpPipelineEditor.h" #include #include #include #include #include #include #include // ------------------------------------------------------------------------- #define App_cpPipelineEditor_ConnectAction( ACTION ) \ this->connect( \ this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \ this, SLOT( _Action##ACTION( ) ) \ ) // ------------------------------------------------------------------------- #define App_cpPipelineEditor_ConnectButton( BUTTON ) \ this->connect( \ this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ), \ this, SLOT( _Button##BUTTON( ) ) \ ) // ------------------------------------------------------------------------- App_cpPipelineEditor:: App_cpPipelineEditor( int argc, char* argv[], QWidget* parent ) : QMainWindow( parent ), m_UI( new Ui::App_cpPipelineEditor ), m_Workspace( NULL ), m_PluginsPath( "." ) { this->m_UI->setupUi( this ); // Prepare plugins interface this->m_Plugins = new cpPlugins::Interface::Interface( ); QFileInfo info( argv[ 0 ] ); if( info.exists( ) ) { this->m_PluginsPath = info.canonicalPath( ).toStdString( ); if( !( this->m_Plugins->LoadDefaultConfiguration( this->m_PluginsPath ) ) ) if( this->m_Plugins->LoadFromFolder( this->m_PluginsPath, false ) ) if( !( this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath ) ) ) QMessageBox::critical( this, "Error creating default plugins configuration", "Could not save default plugins configuration" ); this->_UpdateLoadedPlugins( ); } // fi // Create an empty workspace this->m_Workspace = new cpPlugins::Interface::Workspace( ); this->m_Workspace->SetPlugins( this->m_Plugins ); this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace ); this->m_Workspace->SetMPRViewer( this->m_UI->Viewer ); // Connect actions to slots App_cpPipelineEditor_ConnectButton( LoadPluginsFile ); App_cpPipelineEditor_ConnectButton( LoadPluginsPath ); App_cpPipelineEditor_ConnectAction( OpenWorkspace ); App_cpPipelineEditor_ConnectAction( SaveWorkspace ); this->connect( this->m_UI->Canvas->editor( ), SIGNAL( execFilter( const std::string& ) ), this, SLOT( _ExecFilter( const std::string& ) ) ); this->connect( this->m_UI->Canvas->editor( ), SIGNAL( showFilterOutput( const std::string&, const std::string& ) ), this, SLOT( _ShowFilterOutput( const std::string&, const std::string& ) ) ); } // ------------------------------------------------------------------------- App_cpPipelineEditor:: ~App_cpPipelineEditor( ) { delete this->m_UI; if( this->m_Workspace != NULL ) delete this->m_Workspace; delete this->m_Plugins; } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _UpdateLoadedPlugins( ) { auto& classes = this->m_Plugins->GetClasses( ); if( classes.size( ) == 0 ) { QMessageBox::critical( this, "Error loading default plugins", "No plugins loaded: remember to load some!!!" ); return; } // fi auto catIt = classes.begin( ); for( ; catIt != classes.end( ); ++catIt ) { // Create or get category QList< QTreeWidgetItem* > cat_items = this->m_UI->LoadedPlugins->findItems( catIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive ); QTreeWidgetItem* cat = NULL; if( cat_items.size( ) == 0 ) { cat = new QTreeWidgetItem( ( QTreeWidgetItem* )( NULL ), QStringList( catIt->first.c_str( ) ) ); this->m_UI->LoadedPlugins->addTopLevelItem( cat ); } else cat = cat_items[ 0 ]; // Create filters auto fIt = catIt->second.begin( ); for( ; fIt != catIt->second.end( ); ++fIt ) { QList< QTreeWidgetItem* > filter_items = this->m_UI->LoadedPlugins->findItems( fIt->first.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->first.c_str( ) ) ); } // rof } // rof } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _ButtonLoadPluginsFile( ) { QFileDialog dlg( this ); dlg.setFileMode( QFileDialog::ExistingFiles ); dlg.setDirectory( "." ); std::stringstream name_filter; std::string suffix = std::string( PLUGIN_EXT ).substr( 1 ); name_filter << "Plugins file (*" << PLUGIN_EXT << ");;All files (*)"; dlg.setNameFilter( name_filter.str( ).c_str( ) ); dlg.setDefaultSuffix( suffix.c_str( ) ); if( !( dlg.exec( ) ) ) return; // Read QStringList names = dlg.selectedFiles( ); std::stringstream err_str; for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt ) if( !( this->m_Plugins->Load( qIt->toStdString( ) ) ) ) err_str << qIt->toStdString( ) << std::endl; // Show an error message std::string err = err_str.str( ); if( err.size( ) > 0 ) QMessageBox::critical( this, "Error loading plugins", err.c_str( ) ); // Update view this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath ); this->_UpdateLoadedPlugins( ); } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _ButtonLoadPluginsPath( ) { QFileDialog dlg( this ); dlg.setFileMode( QFileDialog::DirectoryOnly ); dlg.setDirectory( "." ); if( !( dlg.exec( ) ) ) return; // Read std::string dir = dlg.selectedFiles( ).begin( )->toStdString( ); if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) ) QMessageBox::critical( this, "Error loading plugins directory", dir.c_str( ) ); // Update view this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath ); this->_UpdateLoadedPlugins( ); } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _ActionOpenWorkspace( ) { QFileDialog dlg( this ); dlg.setFileMode( QFileDialog::ExistingFile ); dlg.setDirectory( "." ); dlg.setNameFilter( QFileDialog::tr( "Workspace file (*.xml);;All files (*)" ) ); dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) ); if( !( dlg.exec( ) ) ) return; std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( ); if( this->m_Workspace != NULL ) delete this->m_Workspace; this->m_Workspace = new cpPlugins::Interface::Workspace( ); this->m_Workspace->SetPlugins( this->m_Plugins ); this->m_Workspace->SetMPRViewer( this->m_UI->Viewer ); std::string err = this->m_Workspace->LoadWorkspace( fname ); if( err != "" ) { delete this->m_Workspace; this->m_Workspace = NULL; QMessageBox::critical( this, QMessageBox::tr( "Error loading workspace" ), QMessageBox::tr( err.c_str( ) ) ); } else this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace ); } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _ActionSaveWorkspace( ) { if( this->m_Workspace == NULL ) return; QFileDialog dlg( this ); dlg.setFileMode( QFileDialog::AnyFile ); dlg.setDirectory( "." ); dlg.setAcceptMode( QFileDialog::AcceptSave ); dlg.setNameFilter( QFileDialog::tr( "Workspace file (*.xml);;All files (*)" ) ); dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) ); if( !( dlg.exec( ) ) ) return; std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( ); std::string err = this->m_Workspace->SaveWorkspace( fname ); if( err != "" ) QMessageBox::critical( this, QMessageBox::tr( "Error saving workspace" ), QMessageBox::tr( err.c_str( ) ) ); } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _ExecFilter( const std::string& filter_name ) { if( this->m_Workspace != NULL ) { // Update filter, if needed std::string err = this->m_Workspace->Execute( filter_name ); if( err != "" ) QMessageBox::critical( this, QMessageBox::tr( "Error executing filter" ), QMessageBox::tr( err.c_str( ) ) ); } // fi } // ------------------------------------------------------------------------- void App_cpPipelineEditor:: _ShowFilterOutput( const std::string& filter_name, const std::string& output_name ) { typedef cpPlugins::Interface::DataObject _TDataObject; // Update filter, if needed this->_ExecFilter( filter_name ); // Get output auto filter = this->m_Workspace->GetFilter( filter_name ); if( filter != NULL ) { auto output = filter->GetOutputData( output_name ); if( output != NULL ) { std::string data_name = output_name + "@" + filter_name; if( this->m_UI->Viewer->AddData( output, data_name, "" ) ) { if( this->m_UI->Viewer->GetNumberOfData( ) > 1 ) this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 ); else this->m_UI->Viewer->SetMainImage( data_name ); this->m_UI->Viewer->ShowData( data_name ); } else QMessageBox::critical( this, QMessageBox::tr( "Error showing data" ), QMessageBox::tr( "No known VTK conversion!" ) ); } // fi } // fi } // eof - $RCSfile$