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 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
65 this->_LoadPluginsFromPath( this->m_PluginsPath );
70 this->m_Interface = new cpPlugins::Interface( );
71 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
72 if( !( this->m_Interface->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
73 if( this->m_Interface->LoadFromFolder( this->m_PluginsPath, false ) != "" )
74 if( !( this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
75 QMessageBox::critical(
77 "Error creating default plugins configuration",
78 "Could not save default plugins configuration"
80 this->_UpdateLoadedPlugins( );
85 // Create an empty workspace
86 this->m_Workspace = new cpPlugins::Workspace( );
87 this->m_Workspace->SetInterface( &( this->m_Interface ) );
88 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
89 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
91 // Connect actions to slots
92 PipelineEditor_ConnectButton( LoadPluginsFile );
93 PipelineEditor_ConnectButton( LoadPluginsPath );
94 PipelineEditor_ConnectAction( OpenWorkspace );
95 PipelineEditor_ConnectAction( SaveWorkspace );
97 this->m_UI->Canvas->editor( ),
98 SIGNAL( execFilter( const std::string& ) ),
100 SLOT( _ExecFilter( const std::string& ) )
103 this->m_UI->Canvas->editor( ),
104 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
106 SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
110 // -------------------------------------------------------------------------
114 if( this->m_Workspace != NULL )
115 delete this->m_Workspace;
116 // TODO: this causes a segfault (?)
117 // delete this->m_UI;
120 // -------------------------------------------------------------------------
121 void PipelineEditor::
122 _LoadPluginsFromPath( const std::string& path )
124 QDir dir( path.c_str( ) );
125 std::stringstream filters_str;
126 filters_str << "*." << cpPlugins_PLUGIN_EXT;
128 filters << filters_str.str( ).c_str( );
129 auto files = dir.entryList( filters );
130 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
134 this->m_Interface.LoadPluginFile( fIt->toStdString( ) );
138 // Just ignore un-loadable libraries
142 this->_UpdateLoadedPlugins( );
145 // -------------------------------------------------------------------------
146 void PipelineEditor::
147 _UpdateLoadedPlugins( )
150 auto filters = this->m_Interface.GetFilters( );
151 if( filters.size( ) == 0 )
153 QMessageBox::critical(
155 "Error loading default plugins",
156 "No plugins loaded: remember to load some!!!"
162 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
164 // Create or get category
165 QList< QTreeWidgetItem* > cat_items =
166 this->m_UI->LoadedPlugins->findItems(
167 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
169 QTreeWidgetItem* cat = NULL;
170 if( cat_items.size( ) == 0 )
172 cat = new QTreeWidgetItem(
173 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
175 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
178 cat = cat_items[ 0 ];
181 auto fIt = cIt->second.begin( );
182 for( ; fIt != cIt->second.end( ); ++fIt )
184 QList< QTreeWidgetItem* > filter_items =
185 this->m_UI->LoadedPlugins->findItems(
186 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
188 auto fiIt = filter_items.begin( );
189 auto found_fiIt = filter_items.end( );
190 for( ; fiIt != filter_items.end( ); ++fiIt )
191 if( ( *fiIt )->parent( ) == cat )
195 if( found_fiIt == filter_items.end( ) )
196 QTreeWidgetItem* filter = new QTreeWidgetItem(
197 cat, QStringList( fIt->c_str( ) )
206 // -------------------------------------------------------------------------
207 void PipelineEditor::
210 this->m_Application->setOverrideCursor( Qt::WaitCursor );
211 this->m_Application->installEventFilter( &( this->m_Blocker ) );
214 // -------------------------------------------------------------------------
215 void PipelineEditor::
218 while( this->m_Application->overrideCursor( ) )
219 this->m_Application->restoreOverrideCursor( );
220 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
223 // -------------------------------------------------------------------------
224 void PipelineEditor::
225 _ButtonLoadPluginsFile( )
228 QFileDialog dlg( this );
229 dlg.setFileMode( QFileDialog::ExistingFiles );
230 dlg.setDirectory( "." );
232 std::stringstream name_filter;
233 std::string suffix = std::string( cpPlugins_PLUGIN_EXT ).substr( 1 );
235 name_filter << "Plugins file (*" << cpPlugins_PLUGIN_EXT << ");;All files (*)";
236 dlg.setNameFilter( name_filter.str( ).c_str( ) );
237 dlg.setDefaultSuffix( suffix.c_str( ) );
239 if( !( dlg.exec( ) ) )
243 QStringList names = dlg.selectedFiles( );
244 std::stringstream err_str;
245 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
246 err_str << this->m_Interface->Load( qIt->toStdString( ) );
248 // Show an error message
249 std::string err = err_str.str( );
250 if( err.size( ) > 0 )
251 QMessageBox::critical(
253 "Error loading plugins",
258 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
259 this->_UpdateLoadedPlugins( );
263 // -------------------------------------------------------------------------
264 void PipelineEditor::
265 _ButtonLoadPluginsPath( )
268 QFileDialog dlg( this );
269 dlg.setFileMode( QFileDialog::DirectoryOnly );
270 dlg.setDirectory( "." );
271 if( !( dlg.exec( ) ) )
275 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
276 std::string err = this->m_Interface->LoadFromFolder( dir, false );
278 QMessageBox::critical(
280 "Error loading plugins directory",
285 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
286 this->_UpdateLoadedPlugins( );
290 // -------------------------------------------------------------------------
291 void PipelineEditor::
292 _ActionOpenWorkspace( )
294 QFileDialog dlg( this );
295 dlg.setFileMode( QFileDialog::ExistingFile );
296 dlg.setDirectory( "." );
298 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
300 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
301 if( !( dlg.exec( ) ) )
304 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
305 if( this->m_Workspace != NULL )
306 delete this->m_Workspace;
307 this->m_Workspace = new cpPlugins::Workspace( );
308 this->m_Workspace->SetInterface( &( this->m_Interface ) );
309 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
310 std::string err = this->m_Workspace->LoadWorkspace( fname );
313 delete this->m_Workspace;
314 this->m_Workspace = NULL;
315 QMessageBox::critical(
317 QMessageBox::tr( "Error loading workspace" ),
318 QMessageBox::tr( err.c_str( ) )
322 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
325 // -------------------------------------------------------------------------
326 void PipelineEditor::
327 _ActionSaveWorkspace( )
329 if( this->m_Workspace == NULL )
332 QFileDialog dlg( this );
333 dlg.setFileMode( QFileDialog::AnyFile );
334 dlg.setDirectory( "." );
335 dlg.setAcceptMode( QFileDialog::AcceptSave );
337 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
339 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
340 if( !( dlg.exec( ) ) )
342 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
344 std::string err = this->m_Workspace->SaveWorkspace( fname );
346 QMessageBox::critical(
348 QMessageBox::tr( "Error saving workspace" ),
349 QMessageBox::tr( err.c_str( ) )
354 // -------------------------------------------------------------------------
355 void PipelineEditor::
356 _ExecFilter( const std::string& filter_name )
358 if( this->m_Workspace != NULL )
360 // Update filter, if needed
362 std::string err = this->m_Workspace->Execute( filter_name );
365 QMessageBox::critical(
367 QMessageBox::tr( "Error executing filter" ),
368 QMessageBox::tr( err.c_str( ) )
374 // -------------------------------------------------------------------------
375 void PipelineEditor::
377 const std::string& filter_name, const std::string& output_name
380 typedef cpPlugins::DataObject _TDataObject;
382 // Update filter, if needed
383 this->_ExecFilter( filter_name );
386 auto filter = this->m_Workspace->GetFilter( filter_name );
389 auto output = filter->GetOutputData( output_name );
392 std::string data_name = output_name + "@" + filter_name;
393 auto idata = output->GetVTK< vtkImageData >( );
394 auto mdata = output->GetVTK< vtkPolyData >( );
397 if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
399 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
400 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
402 this->m_UI->Viewer->SetMainImage( data_name );
404 this->m_UI->Viewer->ShowData( data_name );
409 else if( mdata != NULL )
413 QMessageBox::critical(
415 QMessageBox::tr( "Error showing data" ),
416 QMessageBox::tr( "No known VTK conversion!" )
420 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
422 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
423 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
425 this->m_UI->Viewer->SetMainImage( data_name );
426 this->m_UI->Viewer->ShowData( data_name );