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 );
70 this->_UpdateLoadedPlugins( );
75 this->m_Interface = new cpPlugins::Interface( );
76 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
77 if( !( this->m_Interface->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
78 if( this->m_Interface->LoadFromFolder( this->m_PluginsPath, false ) != "" )
79 if( !( this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
80 QMessageBox::critical(
82 "Error creating default plugins configuration",
83 "Could not save default plugins configuration"
85 this->_UpdateLoadedPlugins( );
90 // Create an empty workspace
91 this->m_Workspace = new cpPlugins::Workspace( );
92 this->m_Workspace->SetInterface( &( this->m_Interface ) );
93 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
94 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
96 // Connect actions to slots
97 PipelineEditor_ConnectButton( LoadPluginsFile );
98 PipelineEditor_ConnectButton( LoadPluginsPath );
99 PipelineEditor_ConnectAction( OpenWorkspace );
100 PipelineEditor_ConnectAction( SaveWorkspace );
102 this->m_UI->Canvas->editor( ),
103 SIGNAL( execFilter( const std::string& ) ),
105 SLOT( _ExecFilter( const std::string& ) )
108 this->m_UI->Canvas->editor( ),
109 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
111 SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
115 // -------------------------------------------------------------------------
119 if( this->m_Workspace != NULL )
120 delete this->m_Workspace;
124 // -------------------------------------------------------------------------
125 void PipelineEditor::
126 _LoadPluginsFromPath( const std::string& path )
128 QDir dir( path.c_str( ) );
129 std::stringstream filters_str;
130 filters_str << "*." << cpPlugins_PLUGIN_EXT;
132 filters << filters_str.str( ).c_str( );
133 auto files = dir.entryList( filters );
134 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
138 this->m_Interface.LoadPluginFile( fIt->toStdString( ) );
142 // Just ignore un-loadable libraries
146 this->_UpdateLoadedPlugins( );
149 // -------------------------------------------------------------------------
150 void PipelineEditor::
151 _UpdateLoadedPlugins( )
154 auto filters = this->m_Interface.GetFilters( );
155 if( filters.size( ) == 0 )
157 QMessageBox::critical(
159 "Error loading default plugins",
160 "No plugins loaded: remember to load some!!!"
166 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
168 // Create or get category
169 QList< QTreeWidgetItem* > cat_items =
170 this->m_UI->LoadedPlugins->findItems(
171 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
173 QTreeWidgetItem* cat = NULL;
174 if( cat_items.size( ) == 0 )
176 cat = new QTreeWidgetItem(
177 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
179 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
182 cat = cat_items[ 0 ];
185 auto fIt = cIt->second.begin( );
186 for( ; fIt != cIt->second.end( ); ++fIt )
188 QList< QTreeWidgetItem* > filter_items =
189 this->m_UI->LoadedPlugins->findItems(
190 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
192 auto fiIt = filter_items.begin( );
193 auto found_fiIt = filter_items.end( );
194 for( ; fiIt != filter_items.end( ); ++fiIt )
195 if( ( *fiIt )->parent( ) == cat )
199 if( found_fiIt == filter_items.end( ) )
200 QTreeWidgetItem* filter = new QTreeWidgetItem(
201 cat, QStringList( fIt->c_str( ) )
208 this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
211 // -------------------------------------------------------------------------
212 void PipelineEditor::
215 this->m_Application->setOverrideCursor( Qt::WaitCursor );
216 this->m_Application->installEventFilter( &( this->m_Blocker ) );
219 // -------------------------------------------------------------------------
220 void PipelineEditor::
223 while( this->m_Application->overrideCursor( ) )
224 this->m_Application->restoreOverrideCursor( );
225 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
228 // -------------------------------------------------------------------------
229 void PipelineEditor::
230 _ButtonLoadPluginsFile( )
232 QFileDialog dlg( this );
233 dlg.setFileMode( QFileDialog::ExistingFiles );
234 dlg.setDirectory( "." );
236 std::stringstream name_filter;
237 std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
239 << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
240 dlg.setNameFilter( name_filter.str( ).c_str( ) );
241 dlg.setDefaultSuffix( suffix.c_str( ) );
243 if( !( dlg.exec( ) ) )
247 QStringList names = dlg.selectedFiles( );
248 std::stringstream err_str;
249 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
253 this->m_Interface.LoadPluginFile( qIt->toStdString( ) );
255 catch( std::exception& err )
257 err_str << err.what( ) << std::endl;
263 // Show an error message
264 std::string err = err_str.str( );
265 if( err.size( ) > 0 )
266 QMessageBox::critical(
268 "Error loading plugins",
273 // TODO: this->m_Interface.SaveDefaultConfiguration( this->m_PluginsPath );
274 this->_UpdateLoadedPlugins( );
277 // -------------------------------------------------------------------------
278 void PipelineEditor::
279 _ButtonLoadPluginsPath( )
282 QFileDialog dlg( this );
283 dlg.setFileMode( QFileDialog::DirectoryOnly );
284 dlg.setDirectory( "." );
285 if( !( dlg.exec( ) ) )
289 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
290 std::string err = this->m_Interface->LoadFromFolder( dir, false );
292 QMessageBox::critical(
294 "Error loading plugins directory",
299 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
300 this->_UpdateLoadedPlugins( );
304 // -------------------------------------------------------------------------
305 void PipelineEditor::
306 _ActionOpenWorkspace( )
308 QFileDialog dlg( this );
309 dlg.setFileMode( QFileDialog::ExistingFile );
310 dlg.setDirectory( "." );
312 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
314 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
315 if( !( dlg.exec( ) ) )
318 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
319 if( this->m_Workspace != NULL )
320 delete this->m_Workspace;
321 this->m_Workspace = new cpPlugins::Workspace( );
322 this->m_Workspace->SetInterface( &( this->m_Interface ) );
323 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
324 std::string err = this->m_Workspace->LoadWorkspace( fname );
327 delete this->m_Workspace;
328 this->m_Workspace = NULL;
329 QMessageBox::critical(
331 QMessageBox::tr( "Error loading workspace" ),
332 QMessageBox::tr( err.c_str( ) )
336 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
339 // -------------------------------------------------------------------------
340 void PipelineEditor::
341 _ActionSaveWorkspace( )
343 if( this->m_Workspace == NULL )
346 QFileDialog dlg( this );
347 dlg.setFileMode( QFileDialog::AnyFile );
348 dlg.setDirectory( "." );
349 dlg.setAcceptMode( QFileDialog::AcceptSave );
351 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
353 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
354 if( !( dlg.exec( ) ) )
356 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
358 std::string err = this->m_Workspace->SaveWorkspace( fname );
360 QMessageBox::critical(
362 QMessageBox::tr( "Error saving workspace" ),
363 QMessageBox::tr( err.c_str( ) )
368 // -------------------------------------------------------------------------
369 void PipelineEditor::
370 _ExecFilter( const std::string& filter_name )
372 if( this->m_Workspace != NULL )
374 // Update filter, if needed
376 std::string err = this->m_Workspace->Execute( filter_name );
379 QMessageBox::critical(
381 QMessageBox::tr( "Error executing filter" ),
382 QMessageBox::tr( err.c_str( ) )
388 // -------------------------------------------------------------------------
389 void PipelineEditor::
391 const std::string& filter_name, const std::string& output_name
394 typedef cpPlugins::DataObject _TDataObject;
396 // Update filter, if needed
397 this->_ExecFilter( filter_name );
400 auto filter = this->m_Workspace->GetFilter( filter_name );
403 auto output = filter->GetOutputData( output_name );
406 std::string data_name = output_name + "@" + filter_name;
407 auto idata = output->GetVTK< vtkImageData >( );
408 auto mdata = output->GetVTK< vtkPolyData >( );
411 if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
413 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
414 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
416 this->m_UI->Viewer->SetMainImage( data_name );
418 this->m_UI->Viewer->ShowData( data_name );
423 else if( mdata != NULL )
427 QMessageBox::critical(
429 QMessageBox::tr( "Error showing data" ),
430 QMessageBox::tr( "No known VTK conversion!" )
434 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
436 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
437 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
439 this->m_UI->Viewer->SetMainImage( data_name );
440 this->m_UI->Viewer->ShowData( data_name );