1 #include "App_cpPipelineEditor.h"
2 #include "ui_App_cpPipelineEditor.h"
4 #include <cpPipelineEditor/Editor.h>
9 #include <vtkImageData.h>
10 #include <vtkPolyData.h>
12 #include <cpPlugins/Interface/Workspace.h>
13 #include <cpPlugins/Interface/DataObject.h>
15 // -------------------------------------------------------------------------
16 #define App_cpPipelineEditor_ConnectAction( ACTION ) \
18 this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
19 this, SLOT( _Action##ACTION( ) ) \
22 // -------------------------------------------------------------------------
23 #define App_cpPipelineEditor_ConnectButton( BUTTON ) \
25 this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ), \
26 this, SLOT( _Button##BUTTON( ) ) \
29 // -------------------------------------------------------------------------
30 App_cpPipelineEditor::
31 App_cpPipelineEditor( int argc, char* argv[], QWidget* parent )
32 : QMainWindow( parent ),
33 m_UI( new Ui::App_cpPipelineEditor ),
36 this->m_UI->setupUi( this );
38 // Prepare plugins interface
39 this->m_Plugins = new cpPlugins::Interface::Interface( );
40 QFileInfo info( argv[ 0 ] );
43 std::string path = info.canonicalPath( ).toStdString( );
44 if( !( this->m_Plugins->LoadDefaultConfiguration( path ) ) )
45 if( this->m_Plugins->LoadFromFolder( path, false ) )
46 if( !( this->m_Plugins->SaveDefaultConfiguration( path ) ) )
47 QMessageBox::critical(
49 "Error creating default plugins configuration",
50 "Could not save default plugins configuration"
52 this->_UpdateLoadedPlugins( );
56 // Create an empty workspace
57 this->m_Workspace = new cpPlugins::Interface::Workspace( );
58 this->m_Workspace->SetPlugins( this->m_Plugins );
59 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
60 this->m_Workspace->AddInteractor( this->m_UI->Viewer->GetInteractor( 0 ) );
61 this->m_Workspace->AddInteractor( this->m_UI->Viewer->GetInteractor( 1 ) );
62 this->m_Workspace->AddInteractor( this->m_UI->Viewer->GetInteractor( 2 ) );
63 this->m_Workspace->AddInteractor( this->m_UI->Viewer->GetInteractor( 3 ) );
65 // Connect actions to slots
66 App_cpPipelineEditor_ConnectButton( LoadPluginsFile );
67 App_cpPipelineEditor_ConnectButton( LoadPluginsPath );
68 App_cpPipelineEditor_ConnectAction( OpenWorkspace );
69 App_cpPipelineEditor_ConnectAction( SaveWorkspace );
71 this->m_UI->Canvas->editor( ),
72 SIGNAL( execFilter( const std::string& ) ),
74 SLOT( _ExecFilter( const std::string& ) )
77 this->m_UI->Canvas->editor( ),
78 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
80 SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
84 // -------------------------------------------------------------------------
85 App_cpPipelineEditor::
86 ~App_cpPipelineEditor( )
89 if( this->m_Workspace != NULL )
90 delete this->m_Workspace;
91 delete this->m_Plugins;
94 // -------------------------------------------------------------------------
95 void App_cpPipelineEditor::
96 _UpdateLoadedPlugins( )
98 auto& classes = this->m_Plugins->GetClasses( );
100 if( classes.size( ) == 0 )
102 QMessageBox::critical(
104 "Error loading default plugins",
105 "No plugins loaded: remember to load some!!!"
111 auto catIt = classes.begin( );
112 for( ; catIt != classes.end( ); ++catIt )
114 // Create or get category
115 QList< QTreeWidgetItem* > cat_items =
116 this->m_UI->LoadedPlugins->findItems(
117 catIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
119 QTreeWidgetItem* cat = NULL;
120 if( cat_items.size( ) == 0 )
122 cat = new QTreeWidgetItem(
123 ( QTreeWidgetItem* )( NULL ), QStringList( catIt->first.c_str( ) )
125 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
128 cat = cat_items[ 0 ];
131 auto fIt = catIt->second.begin( );
132 for( ; fIt != catIt->second.end( ); ++fIt )
134 QList< QTreeWidgetItem* > filter_items =
135 this->m_UI->LoadedPlugins->findItems(
136 fIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
138 auto fiIt = filter_items.begin( );
139 auto found_fiIt = filter_items.end( );
140 for( ; fiIt != filter_items.end( ); ++fiIt )
141 if( ( *fiIt )->parent( ) == cat )
145 if( found_fiIt == filter_items.end( ) )
146 QTreeWidgetItem* filter = new QTreeWidgetItem(
147 cat, QStringList( fIt->first.c_str( ) )
154 // -------------------------------------------------------------------------
155 void App_cpPipelineEditor::
156 _ButtonLoadPluginsFile( )
158 QFileDialog dlg( this );
159 dlg.setFileMode( QFileDialog::ExistingFiles );
160 dlg.setDirectory( "." );
162 dlg.setNameFilter( "Plugins file (*.dll);;All files (*)" );
163 dlg.setDefaultSuffix( "dll" );
165 dlg.setNameFilter( "Plugins file (*.so);;All files (*)" );
166 dlg.setDefaultSuffix( "so" );
168 if( !( dlg.exec( ) ) )
172 QStringList names = dlg.selectedFiles( );
173 std::stringstream err_str;
174 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
175 if( !( this->m_Plugins->Load( qIt->toStdString( ) ) ) )
176 err_str << qIt->toStdString( ) << std::endl;
178 // Show an error message
179 std::string err = err_str.str( );
180 if( err.size( ) > 0 )
181 QMessageBox::critical(
183 "Error loading plugins",
188 this->_UpdateLoadedPlugins( );
191 // -------------------------------------------------------------------------
192 void App_cpPipelineEditor::
193 _ButtonLoadPluginsPath( )
195 QFileDialog dlg( this );
196 dlg.setFileMode( QFileDialog::DirectoryOnly );
197 dlg.setDirectory( "." );
198 if( !( dlg.exec( ) ) )
202 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
203 if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) )
204 QMessageBox::critical(
206 "Error loading plugins directory",
211 this->_UpdateLoadedPlugins( );
214 // -------------------------------------------------------------------------
215 void App_cpPipelineEditor::
216 _ActionOpenWorkspace( )
218 QFileDialog dlg( this );
219 dlg.setFileMode( QFileDialog::ExistingFile );
220 dlg.setDirectory( "." );
222 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
224 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
225 if( !( dlg.exec( ) ) )
227 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
229 if( this->m_Workspace != NULL )
230 delete this->m_Workspace;
231 this->m_Workspace = new cpPlugins::Interface::Workspace( );
232 this->m_Workspace->SetPlugins( this->m_Plugins );
233 std::string err = this->m_Workspace->LoadWorkspace( fname );
236 delete this->m_Workspace;
237 this->m_Workspace = NULL;
238 QMessageBox::critical(
240 QMessageBox::tr( "Error loading workspace" ),
241 QMessageBox::tr( err.c_str( ) )
245 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
248 // -------------------------------------------------------------------------
249 void App_cpPipelineEditor::
250 _ActionSaveWorkspace( )
252 if( this->m_Workspace == NULL )
255 QFileDialog dlg( this );
256 dlg.setFileMode( QFileDialog::AnyFile );
257 dlg.setDirectory( "." );
258 dlg.setAcceptMode( QFileDialog::AcceptSave );
260 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
262 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
263 if( !( dlg.exec( ) ) )
265 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
267 std::string err = this->m_Workspace->SaveWorkspace( fname );
269 QMessageBox::critical(
271 QMessageBox::tr( "Error saving workspace" ),
272 QMessageBox::tr( err.c_str( ) )
277 // -------------------------------------------------------------------------
278 void App_cpPipelineEditor::
279 _ExecFilter( const std::string& filter_name )
281 if( this->m_Workspace != NULL )
283 // Update filter, if needed
284 std::string err = this->m_Workspace->Execute( filter_name );
286 QMessageBox::critical(
288 QMessageBox::tr( "Error executing filter" ),
289 QMessageBox::tr( err.c_str( ) )
295 // -------------------------------------------------------------------------
296 void App_cpPipelineEditor::
298 const std::string& filter_name, const std::string& output_name
301 typedef cpPlugins::Interface::DataObject _TDataObject;
303 // Update filter, if needed
304 this->_ExecFilter( filter_name );
307 auto filter = this->m_Workspace->GetFilter( filter_name );
310 auto output = filter->GetOutput< _TDataObject >( output_name );
313 std::string data_name = output_name + "@" + filter_name;
314 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
316 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
317 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
319 this->m_UI->Viewer->SetMainImage( data_name );
320 this->m_UI->Viewer->ShowData( data_name );
323 QMessageBox::critical(
325 QMessageBox::tr( "Error showing data" ),
326 QMessageBox::tr( "No known VTK conversion!" )