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 // -------------------------------------------------------------------------
29 bool PipelineEditor_Blocker::
30 eventFilter( QObject* obj, QEvent* event )
32 return( true ); // -> Block all events
33 /* NOTE: correct implementation:
34 switch( event->type( ) )
36 //list event you want to prevent here ...
37 case QEvent::KeyPress:
38 case QEvent::KeyRelease:
39 case QEvent::MouseButtonRelease:
40 case QEvent::MouseButtonPress:
41 case QEvent::MouseButtonDblClick:
45 return( this->QObject::eventFilter( obj, event ) );
49 // -------------------------------------------------------------------------
51 PipelineEditor( int argc, char* argv[], QApplication* app, QWidget* parent )
52 : QMainWindow( parent ),
53 m_UI( new Ui::PipelineEditor ),
58 this->m_UI->setupUi( this );
60 // Prepare plugins interface
61 QFileInfo info( argv[ 0 ] );
64 if( !( this->m_Interface.LoadConfiguration( cpPlugins_CONFIG_FILE ) ) )
66 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
67 this->_LoadPluginsFromPath( this->m_PluginsPath );
71 this->_UpdateLoadedPlugins( );
76 if( exec_dir.exists( ) )
77 this->_LoadPluginsFromPath( exec_dir.canonicalPath( ).toStdString( ) );
80 this->m_Interface = new cpPlugins::Interface( );
81 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
82 if( !( this->m_Interface->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
83 if( this->m_Interface->LoadFromFolder( this->m_PluginsPath, false ) != "" )
84 if( !( this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
85 QMessageBox::critical(
87 "Error creating default plugins configuration",
88 "Could not save default plugins configuration"
90 this->_UpdateLoadedPlugins( );
95 // Create an empty workspace
96 this->m_Workspace = new cpPlugins::Workspace( );
97 this->m_Workspace->SetInterface( &( this->m_Interface ) );
98 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
99 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
101 // Connect actions to slots
102 PipelineEditor_ConnectButton( LoadPluginsFile );
103 PipelineEditor_ConnectButton( LoadPluginsPath );
104 PipelineEditor_ConnectAction( OpenWorkspace );
105 PipelineEditor_ConnectAction( SaveWorkspace );
107 this->m_UI->Canvas->editor( ),
108 SIGNAL( execFilter( const std::string& ) ),
110 SLOT( _ExecFilter( const std::string& ) )
113 this->m_UI->Canvas->editor( ),
114 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
116 SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
120 // -------------------------------------------------------------------------
124 if( this->m_Workspace != NULL )
125 delete this->m_Workspace;
129 // -------------------------------------------------------------------------
130 void PipelineEditor::
131 _LoadPluginsFromPath( const std::string& path )
133 QDir dir( path.c_str( ) );
134 std::stringstream filters_str;
135 filters_str << "*." << cpPlugins_PLUGIN_EXT;
137 filters << filters_str.str( ).c_str( );
138 auto files = dir.entryList( filters );
139 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
143 this->m_Interface.LoadPluginFile(
144 ( dir.absolutePath( ) + QDir::separator( ) + *fIt ).toStdString( )
147 catch( std::exception& err )
149 // Just ignore un-loadable libraries
153 this->_UpdateLoadedPlugins( );
156 // -------------------------------------------------------------------------
157 void PipelineEditor::
158 _UpdateLoadedPlugins( )
161 auto filters = this->m_Interface.GetFilters( );
162 if( filters.size( ) == 0 )
164 QMessageBox::critical(
166 "Error loading default plugins",
167 "No plugins loaded: remember to load some!!!"
174 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
176 // Create or get category
177 QList< QTreeWidgetItem* > cat_items =
178 this->m_UI->LoadedPlugins->findItems(
179 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
181 QTreeWidgetItem* cat = NULL;
182 if( cat_items.size( ) == 0 )
184 cat = new QTreeWidgetItem(
185 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
187 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
190 cat = cat_items[ 0 ];
193 auto fIt = cIt->second.begin( );
194 for( ; fIt != cIt->second.end( ); ++fIt )
196 QList< QTreeWidgetItem* > filter_items =
197 this->m_UI->LoadedPlugins->findItems(
198 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
200 auto fiIt = filter_items.begin( );
201 auto found_fiIt = filter_items.end( );
202 for( ; fiIt != filter_items.end( ); ++fiIt )
203 if( ( *fiIt )->parent( ) == cat )
207 if( found_fiIt == filter_items.end( ) )
208 QTreeWidgetItem* filter = new QTreeWidgetItem(
209 cat, QStringList( fIt->c_str( ) )
216 this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
219 // -------------------------------------------------------------------------
220 void PipelineEditor::
223 this->m_Application->setOverrideCursor( Qt::WaitCursor );
224 this->m_Application->installEventFilter( &( this->m_Blocker ) );
227 // -------------------------------------------------------------------------
228 void PipelineEditor::
231 while( this->m_Application->overrideCursor( ) )
232 this->m_Application->restoreOverrideCursor( );
233 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
236 // -------------------------------------------------------------------------
237 void PipelineEditor::
238 _ButtonLoadPluginsFile( )
240 QFileDialog dlg( this );
241 dlg.setFileMode( QFileDialog::ExistingFiles );
242 dlg.setDirectory( "." );
244 std::stringstream name_filter;
245 std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
247 << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
248 dlg.setNameFilter( name_filter.str( ).c_str( ) );
249 dlg.setDefaultSuffix( suffix.c_str( ) );
251 if( !( dlg.exec( ) ) )
255 QStringList names = dlg.selectedFiles( );
256 std::stringstream err_str;
257 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
261 this->m_Interface.LoadPluginFile( qIt->toStdString( ) );
263 catch( std::exception& err )
265 err_str << err.what( ) << std::endl;
271 // Show an error message
272 std::string err = err_str.str( );
273 if( err.size( ) > 0 )
274 QMessageBox::critical(
276 "Error loading plugins",
281 // TODO: this->m_Interface.SaveDefaultConfiguration( this->m_PluginsPath );
282 this->_UpdateLoadedPlugins( );
285 // -------------------------------------------------------------------------
286 void PipelineEditor::
287 _ButtonLoadPluginsPath( )
290 QFileDialog dlg( this );
291 dlg.setFileMode( QFileDialog::DirectoryOnly );
292 dlg.setDirectory( "." );
293 if( !( dlg.exec( ) ) )
297 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
298 std::string err = this->m_Interface->LoadFromFolder( dir, false );
300 QMessageBox::critical(
302 "Error loading plugins directory",
307 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
308 this->_UpdateLoadedPlugins( );
312 // -------------------------------------------------------------------------
313 void PipelineEditor::
314 _ActionOpenWorkspace( )
316 QFileDialog dlg( this );
317 dlg.setFileMode( QFileDialog::ExistingFile );
318 dlg.setDirectory( "." );
320 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
322 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
323 if( !( dlg.exec( ) ) )
326 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
327 if( this->m_Workspace != NULL )
328 delete this->m_Workspace;
329 this->m_Workspace = new cpPlugins::Workspace( );
330 this->m_Workspace->SetInterface( &( this->m_Interface ) );
331 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
332 std::string err = this->m_Workspace->LoadWorkspace( fname );
335 delete this->m_Workspace;
336 this->m_Workspace = NULL;
337 QMessageBox::critical(
339 QMessageBox::tr( "Error loading workspace" ),
340 QMessageBox::tr( err.c_str( ) )
344 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
347 // -------------------------------------------------------------------------
348 void PipelineEditor::
349 _ActionSaveWorkspace( )
351 if( this->m_Workspace == NULL )
354 QFileDialog dlg( this );
355 dlg.setFileMode( QFileDialog::AnyFile );
356 dlg.setDirectory( "." );
357 dlg.setAcceptMode( QFileDialog::AcceptSave );
359 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
361 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
362 if( !( dlg.exec( ) ) )
364 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
366 std::string err = this->m_Workspace->SaveWorkspace( fname );
368 QMessageBox::critical(
370 QMessageBox::tr( "Error saving workspace" ),
371 QMessageBox::tr( err.c_str( ) )
376 // -------------------------------------------------------------------------
377 void PipelineEditor::
378 _ExecFilter( const std::string& filter_name )
380 if( this->m_Workspace != NULL )
382 // Update filter, if needed
384 std::string err = this->m_Workspace->Execute( filter_name );
387 QMessageBox::critical(
389 QMessageBox::tr( "Error executing filter" ),
390 QMessageBox::tr( err.c_str( ) )
396 // -------------------------------------------------------------------------
397 void PipelineEditor::
399 const std::string& filter_name, const std::string& output_name
402 typedef cpPlugins::DataObject _TDataObject;
404 // Update filter, if needed
405 this->_ExecFilter( filter_name );
408 auto filter = this->m_Workspace->GetFilter( filter_name );
411 auto output = filter->GetOutputData( output_name );
414 std::string data_name = output_name + "@" + filter_name;
415 auto idata = output->GetVTK< vtkImageData >( );
416 auto mdata = output->GetVTK< vtkPolyData >( );
419 if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
421 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
422 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
424 this->m_UI->Viewer->SetMainImage( data_name );
426 this->m_UI->Viewer->ShowData( data_name );
431 else if( mdata != NULL )
433 if( this->m_UI->Viewer->AddData( mdata, data_name ) )
435 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
437 this->m_UI->Viewer->ShowData( data_name );
443 QMessageBox::critical(
445 QMessageBox::tr( "Error showing data" ),
446 QMessageBox::tr( "No known VTK conversion!" )
450 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
452 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
453 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
455 this->m_UI->Viewer->SetMainImage( data_name );
456 this->m_UI->Viewer->ShowData( data_name );