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;
119 // -------------------------------------------------------------------------
120 void PipelineEditor::
121 _LoadPluginsFromPath( const std::string& path )
123 QDir dir( path.c_str( ) );
124 std::stringstream filters_str;
125 filters_str << "*." << cpPlugins_PLUGIN_EXT;
127 filters << filters_str.str( ).c_str( );
128 auto files = dir.entryList( filters );
129 for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
133 this->m_Interface.LoadPluginFile( fIt->toStdString( ) );
137 // Just ignore un-loadable libraries
141 this->_UpdateLoadedPlugins( );
144 // -------------------------------------------------------------------------
145 void PipelineEditor::
146 _UpdateLoadedPlugins( )
149 auto filters = this->m_Interface.GetFilters( );
150 if( filters.size( ) == 0 )
152 QMessageBox::critical(
154 "Error loading default plugins",
155 "No plugins loaded: remember to load some!!!"
161 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
163 // Create or get category
164 QList< QTreeWidgetItem* > cat_items =
165 this->m_UI->LoadedPlugins->findItems(
166 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
168 QTreeWidgetItem* cat = NULL;
169 if( cat_items.size( ) == 0 )
171 cat = new QTreeWidgetItem(
172 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
174 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
177 cat = cat_items[ 0 ];
180 auto fIt = cIt->second.begin( );
181 for( ; fIt != cIt->second.end( ); ++fIt )
183 QList< QTreeWidgetItem* > filter_items =
184 this->m_UI->LoadedPlugins->findItems(
185 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
187 auto fiIt = filter_items.begin( );
188 auto found_fiIt = filter_items.end( );
189 for( ; fiIt != filter_items.end( ); ++fiIt )
190 if( ( *fiIt )->parent( ) == cat )
194 if( found_fiIt == filter_items.end( ) )
195 QTreeWidgetItem* filter = new QTreeWidgetItem(
196 cat, QStringList( fIt->c_str( ) )
205 // -------------------------------------------------------------------------
206 void PipelineEditor::
209 this->m_Application->setOverrideCursor( Qt::WaitCursor );
210 this->m_Application->installEventFilter( &( this->m_Blocker ) );
213 // -------------------------------------------------------------------------
214 void PipelineEditor::
217 while( this->m_Application->overrideCursor( ) )
218 this->m_Application->restoreOverrideCursor( );
219 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
222 // -------------------------------------------------------------------------
223 void PipelineEditor::
224 _ButtonLoadPluginsFile( )
227 QFileDialog dlg( this );
228 dlg.setFileMode( QFileDialog::ExistingFiles );
229 dlg.setDirectory( "." );
231 std::stringstream name_filter;
232 std::string suffix = std::string( cpPlugins_PLUGIN_EXT ).substr( 1 );
234 name_filter << "Plugins file (*" << cpPlugins_PLUGIN_EXT << ");;All files (*)";
235 dlg.setNameFilter( name_filter.str( ).c_str( ) );
236 dlg.setDefaultSuffix( suffix.c_str( ) );
238 if( !( dlg.exec( ) ) )
242 QStringList names = dlg.selectedFiles( );
243 std::stringstream err_str;
244 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
245 err_str << this->m_Interface->Load( qIt->toStdString( ) );
247 // Show an error message
248 std::string err = err_str.str( );
249 if( err.size( ) > 0 )
250 QMessageBox::critical(
252 "Error loading plugins",
257 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
258 this->_UpdateLoadedPlugins( );
262 // -------------------------------------------------------------------------
263 void PipelineEditor::
264 _ButtonLoadPluginsPath( )
267 QFileDialog dlg( this );
268 dlg.setFileMode( QFileDialog::DirectoryOnly );
269 dlg.setDirectory( "." );
270 if( !( dlg.exec( ) ) )
274 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
275 std::string err = this->m_Interface->LoadFromFolder( dir, false );
277 QMessageBox::critical(
279 "Error loading plugins directory",
284 this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
285 this->_UpdateLoadedPlugins( );
289 // -------------------------------------------------------------------------
290 void PipelineEditor::
291 _ActionOpenWorkspace( )
293 QFileDialog dlg( this );
294 dlg.setFileMode( QFileDialog::ExistingFile );
295 dlg.setDirectory( "." );
297 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
299 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
300 if( !( dlg.exec( ) ) )
303 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
304 if( this->m_Workspace != NULL )
305 delete this->m_Workspace;
306 this->m_Workspace = new cpPlugins::Workspace( );
307 this->m_Workspace->SetInterface( &( this->m_Interface ) );
308 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
309 std::string err = this->m_Workspace->LoadWorkspace( fname );
312 delete this->m_Workspace;
313 this->m_Workspace = NULL;
314 QMessageBox::critical(
316 QMessageBox::tr( "Error loading workspace" ),
317 QMessageBox::tr( err.c_str( ) )
321 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
324 // -------------------------------------------------------------------------
325 void PipelineEditor::
326 _ActionSaveWorkspace( )
328 if( this->m_Workspace == NULL )
331 QFileDialog dlg( this );
332 dlg.setFileMode( QFileDialog::AnyFile );
333 dlg.setDirectory( "." );
334 dlg.setAcceptMode( QFileDialog::AcceptSave );
336 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
338 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
339 if( !( dlg.exec( ) ) )
341 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
343 std::string err = this->m_Workspace->SaveWorkspace( fname );
345 QMessageBox::critical(
347 QMessageBox::tr( "Error saving workspace" ),
348 QMessageBox::tr( err.c_str( ) )
353 // -------------------------------------------------------------------------
354 void PipelineEditor::
355 _ExecFilter( const std::string& filter_name )
357 if( this->m_Workspace != NULL )
359 // Update filter, if needed
361 std::string err = this->m_Workspace->Execute( filter_name );
364 QMessageBox::critical(
366 QMessageBox::tr( "Error executing filter" ),
367 QMessageBox::tr( err.c_str( ) )
373 // -------------------------------------------------------------------------
374 void PipelineEditor::
376 const std::string& filter_name, const std::string& output_name
379 typedef cpPlugins::DataObject _TDataObject;
381 // Update filter, if needed
382 this->_ExecFilter( filter_name );
385 auto filter = this->m_Workspace->GetFilter( filter_name );
388 auto output = filter->GetOutputData( output_name );
391 std::string data_name = output_name + "@" + filter_name;
392 auto idata = output->GetVTK< vtkImageData >( );
393 auto mdata = output->GetVTK< vtkPolyData >( );
396 if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
398 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
399 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
401 this->m_UI->Viewer->SetMainImage( data_name );
403 this->m_UI->Viewer->ShowData( data_name );
408 else if( mdata != NULL )
412 QMessageBox::critical(
414 QMessageBox::tr( "Error showing data" ),
415 QMessageBox::tr( "No known VTK conversion!" )
419 if( this->m_UI->Viewer->AddData( output, 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 );
425 this->m_UI->Viewer->ShowData( data_name );