1 #include "PipelineEditor.h"
2 #include "ui_PipelineEditor.h"
4 #include <cpPipelineEditor/Editor.h>
9 #include <vtkImageData.h>
10 #include <vtkPolyData.h>
12 #include <cpPlugins/DataObject.h>
14 // -------------------------------------------------------------------------
15 #define PipelineEditor_ConnectAction( ACTION ) \
17 this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
18 this, SLOT( _Action##ACTION( ) ) \
21 // -------------------------------------------------------------------------
22 #define PipelineEditor_ConnectButton( BUTTON ) \
24 this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ), \
25 this, SLOT( _Button##BUTTON( ) ) \
28 // -------------------------------------------------------------------------
30 PipelineEditor( int argc, char* argv[], QWidget* parent )
31 : QMainWindow( parent ),
32 m_UI( new Ui::PipelineEditor ),
36 this->m_UI->setupUi( this );
38 // Prepare plugins interface
39 QFileInfo info( argv[ 0 ] );
42 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
43 this->_LoadPluginsFromPath( this->m_PluginsPath );
48 this->m_Interface = new cpPlugins::Interface( );
49 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
50 if( !( this->m_Interface->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
51 if( this->m_Interface->LoadFromFolder( this->m_PluginsPath, false ) != "" )
52 if( !( this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
53 QMessageBox::critical(
55 "Error creating default plugins configuration",
56 "Could not save default plugins configuration"
58 this->_UpdateLoadedPlugins( );
63 // Create an empty workspace
64 this->m_Workspace = new cpPlugins::Workspace( );
65 this->m_Workspace->SetInterface( &( this->m_Interface ) );
66 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
67 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
69 // Connect actions to slots
70 PipelineEditor_ConnectButton( LoadPluginsFile );
71 PipelineEditor_ConnectButton( LoadPluginsPath );
72 PipelineEditor_ConnectAction( OpenWorkspace );
73 PipelineEditor_ConnectAction( SaveWorkspace );
75 this->m_UI->Canvas->editor( ),
76 SIGNAL( execFilter( const std::string& ) ),
78 SLOT( _ExecFilter( const std::string& ) )
81 this->m_UI->Canvas->editor( ),
82 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
84 SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
88 // -------------------------------------------------------------------------
92 if( this->m_Workspace != NULL )
93 delete this->m_Workspace;
97 // -------------------------------------------------------------------------
99 _LoadPluginsFromPath( const std::string& path )
101 QDir dir( path.c_str( ) );
102 std::stringstream filters_str;
103 filters_str << "*." << cpPlugins_PLUGIN_EXT;
105 filters << filters_str.str( ).c_str( );
106 auto files = dir.entryList( filters );
107 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
111 this->m_Interface.LoadPluginFile( fIt->toStdString( ) );
115 // Just ignore un-loadable libraries
119 this->_UpdateLoadedPlugins( );
122 // -------------------------------------------------------------------------
123 void PipelineEditor::
124 _UpdateLoadedPlugins( )
126 auto filters = this->m_Interface.GetFilters( );
127 if( filters.size( ) == 0 )
129 QMessageBox::critical(
131 "Error loading default plugins",
132 "No plugins loaded: remember to load some!!!"
138 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
140 // Create or get category
141 QList< QTreeWidgetItem* > cat_items =
142 this->m_UI->LoadedPlugins->findItems(
143 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
145 QTreeWidgetItem* cat = NULL;
146 if( cat_items.size( ) == 0 )
148 cat = new QTreeWidgetItem(
149 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
151 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
154 cat = cat_items[ 0 ];
157 auto fIt = cIt->second.begin( );
158 for( ; fIt != cIt->second.end( ); ++fIt )
160 QList< QTreeWidgetItem* > filter_items =
161 this->m_UI->LoadedPlugins->findItems(
162 fIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
164 auto fiIt = filter_items.begin( );
165 auto found_fiIt = filter_items.end( );
166 for( ; fiIt != filter_items.end( ); ++fiIt )
167 if( ( *fiIt )->parent( ) == cat )
171 if( found_fiIt == filter_items.end( ) )
172 QTreeWidgetItem* filter = new QTreeWidgetItem(
173 cat, QStringList( fIt->first.c_str( ) )
181 // -------------------------------------------------------------------------
182 void PipelineEditor::
183 _ButtonLoadPluginsFile( )
186 QFileDialog dlg( this );
187 dlg.setFileMode( QFileDialog::ExistingFiles );
188 dlg.setDirectory( "." );
190 std::stringstream name_filter;
191 std::string suffix = std::string( cpPlugins_PLUGIN_EXT ).substr( 1 );
193 name_filter << "Plugins file (*" << cpPlugins_PLUGIN_EXT << ");;All files (*)";
194 dlg.setNameFilter( name_filter.str( ).c_str( ) );
195 dlg.setDefaultSuffix( suffix.c_str( ) );
197 if( !( dlg.exec( ) ) )
201 QStringList names = dlg.selectedFiles( );
202 std::stringstream err_str;
203 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
204 err_str << this->m_Interface->Load( qIt->toStdString( ) );
206 // Show an error message
207 std::string err = err_str.str( );
208 if( err.size( ) > 0 )
209 QMessageBox::critical(
211 "Error loading plugins",
216 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
217 this->_UpdateLoadedPlugins( );
221 // -------------------------------------------------------------------------
222 void PipelineEditor::
223 _ButtonLoadPluginsPath( )
226 QFileDialog dlg( this );
227 dlg.setFileMode( QFileDialog::DirectoryOnly );
228 dlg.setDirectory( "." );
229 if( !( dlg.exec( ) ) )
233 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
234 std::string err = this->m_Interface->LoadFromFolder( dir, false );
236 QMessageBox::critical(
238 "Error loading plugins directory",
243 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
244 this->_UpdateLoadedPlugins( );
248 // -------------------------------------------------------------------------
249 void PipelineEditor::
250 _ActionOpenWorkspace( )
252 QFileDialog dlg( this );
253 dlg.setFileMode( QFileDialog::ExistingFile );
254 dlg.setDirectory( "." );
256 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
258 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
259 if( !( dlg.exec( ) ) )
261 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
263 if( this->m_Workspace != NULL )
264 delete this->m_Workspace;
265 this->m_Workspace = new cpPlugins::Workspace( );
266 this->m_Workspace->SetInterface( &( this->m_Interface ) );
267 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
268 std::string err = this->m_Workspace->LoadWorkspace( fname );
271 delete this->m_Workspace;
272 this->m_Workspace = NULL;
273 QMessageBox::critical(
275 QMessageBox::tr( "Error loading workspace" ),
276 QMessageBox::tr( err.c_str( ) )
280 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
283 // -------------------------------------------------------------------------
284 void PipelineEditor::
285 _ActionSaveWorkspace( )
287 if( this->m_Workspace == NULL )
290 QFileDialog dlg( this );
291 dlg.setFileMode( QFileDialog::AnyFile );
292 dlg.setDirectory( "." );
293 dlg.setAcceptMode( QFileDialog::AcceptSave );
295 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
297 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
298 if( !( dlg.exec( ) ) )
300 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
302 std::string err = this->m_Workspace->SaveWorkspace( fname );
304 QMessageBox::critical(
306 QMessageBox::tr( "Error saving workspace" ),
307 QMessageBox::tr( err.c_str( ) )
312 // -------------------------------------------------------------------------
313 void PipelineEditor::
314 _ExecFilter( const std::string& filter_name )
316 if( this->m_Workspace != NULL )
318 // Update filter, if needed
319 std::string err = this->m_Workspace->Execute( filter_name );
321 QMessageBox::critical(
323 QMessageBox::tr( "Error executing filter" ),
324 QMessageBox::tr( err.c_str( ) )
330 // -------------------------------------------------------------------------
331 void PipelineEditor::
333 const std::string& filter_name, const std::string& output_name
336 typedef cpPlugins::DataObject _TDataObject;
338 // Update filter, if needed
339 this->_ExecFilter( filter_name );
342 auto filter = this->m_Workspace->GetFilter( filter_name );
345 auto output = filter->GetOutputData( output_name );
348 std::string data_name = output_name + "@" + filter_name;
349 auto idata = output->GetVTK< vtkImageData >( );
350 auto mdata = output->GetVTK< vtkPolyData >( );
353 if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
355 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
356 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
358 this->m_UI->Viewer->SetMainImage( data_name );
359 this->m_UI->Viewer->ShowData( data_name );
363 else if( mdata != NULL )
367 QMessageBox::critical(
369 QMessageBox::tr( "Error showing data" ),
370 QMessageBox::tr( "No known VTK conversion!" )
374 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
376 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
377 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
379 this->m_UI->Viewer->SetMainImage( data_name );
380 this->m_UI->Viewer->ShowData( data_name );