1 #include <cpPipelineEditor/BaseQtMainWindow.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpPlugins/ActorPropertiesQtDialog.h>
5 #include <cpPipelineEditor/Editor.h>
6 #include <vtkRenderer.h>
7 #include <QApplication>
8 #include <QColorDialog>
10 #include <QFileDialog>
12 #include <QMessageBox>
13 #include <QTreeWidget>
15 // -------------------------------------------------------------------------
16 bool cpPipelineEditor::BaseQtMainWindow::_TBlocker::
17 eventFilter( QObject* obj, QEvent* event )
19 return( true ); // -> Block all events
20 /* NOTE: this should be the correct implementation:
21 switch( event->type( ) )
23 //list event you want to prevent here ...
24 case QEvent::KeyPress:
25 case QEvent::KeyRelease:
26 case QEvent::MouseButtonRelease:
27 case QEvent::MouseButtonPress:
28 case QEvent::MouseButtonDblClick:
32 return( this->QObject::eventFilter( obj, event ) );
36 // -------------------------------------------------------------------------
37 cpPipelineEditor::BaseQtMainWindow::
39 int argc, char* argv[],
43 : Superclass( parent ),
52 this->m_ApplicationPath = ".";
53 QFileInfo info( argv[ 0 ] );
55 this->m_ApplicationPath = info.canonicalPath( ).toStdString( );
58 // -------------------------------------------------------------------------
59 cpPipelineEditor::BaseQtMainWindow::
62 if( this->m_Workspace != NULL )
63 delete this->m_Workspace;
64 delete this->m_Interface;
67 // -------------------------------------------------------------------------
68 void cpPipelineEditor::BaseQtMainWindow::
71 cpExtensions::QT::SimpleMPRWidget* mpr,
72 cpPipelineEditor::Editor* editor
75 if( this->m_Interface != NULL )
77 delete this->m_Interface;
78 this->m_Interface = NULL;
84 this->m_Interface = new cpPlugins::Interface( );
85 this->m_Interface->GuessAccesiblePlugins( );
87 catch( std::exception& err )
89 if( this->m_Interface != NULL )
90 delete this->m_Interface;
91 this->m_Interface = NULL;
92 QMessageBox::critical(
94 "Error creating plugins interface",
101 // Try to load plugins from executable dir
102 this->_LoadPluginsFromPath( this->m_ApplicationPath );
104 // Finish configuration
105 this->m_TreeWidget = tree;
106 if( this->m_TreeWidget != NULL )
107 this->_UpdateLoadedPlugins( );
108 this->m_Editor = editor;
110 this->_CreateWorkspace( );
113 // -------------------------------------------------------------------------
114 void cpPipelineEditor::BaseQtMainWindow::
117 if( this->m_Workspace != NULL )
118 delete this->m_Workspace;
119 this->m_Workspace = new cpPlugins::Workspace( );
120 this->m_Workspace->SetInterface( this->m_Interface );
121 if( this->m_Editor != NULL )
122 this->m_Editor->setWorkspace( this->m_Workspace );
123 if( this->m_MPR != NULL )
124 this->m_Workspace->SetMPRViewer( this->m_MPR );
127 // -------------------------------------------------------------------------
128 void cpPipelineEditor::BaseQtMainWindow::
129 _LoadPlugins( const std::string& filename )
133 this->m_Interface->LoadPluginFile( filename );
134 this->_UpdateLoadedPlugins( );
136 catch( std::exception& err )
138 QMessageBox::critical(
140 "Error loading plugins path",
147 // -------------------------------------------------------------------------
148 void cpPipelineEditor::BaseQtMainWindow::
149 _LoadPluginsFromPath( const std::string& path )
153 this->m_Interface->LoadPluginDir( path );
154 this->_UpdateLoadedPlugins( );
156 catch( std::exception& err )
158 QMessageBox::critical(
160 "Error loading plugins path",
167 // -------------------------------------------------------------------------
168 void cpPipelineEditor::BaseQtMainWindow::
169 _UpdateLoadedPlugins( )
172 auto filters = this->m_Interface->GetFilters( );
173 if( filters.size( ) == 0 )
176 QMessageBox::critical(
178 "Error loading default plugins",
179 "No plugins loaded: remember to load some!!!"
185 if( this->m_TreeWidget != NULL )
187 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
189 // Create or get category
190 QList< QTreeWidgetItem* > cat_items =
191 this->m_TreeWidget->findItems(
192 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
194 QTreeWidgetItem* cat = NULL;
195 if( cat_items.size( ) == 0 )
197 cat = new QTreeWidgetItem(
198 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
200 this->m_TreeWidget->addTopLevelItem( cat );
203 cat = cat_items[ 0 ];
206 auto fIt = cIt->second.begin( );
207 for( ; fIt != cIt->second.end( ); ++fIt )
209 QList< QTreeWidgetItem* > filter_items =
210 this->m_TreeWidget->findItems(
211 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
213 auto fiIt = filter_items.begin( );
214 auto found_fiIt = filter_items.end( );
215 for( ; fiIt != filter_items.end( ); ++fiIt )
216 if( ( *fiIt )->parent( ) == cat )
220 if( found_fiIt == filter_items.end( ) )
221 QTreeWidgetItem* filter = new QTreeWidgetItem(
222 cat, QStringList( fIt->c_str( ) )
233 // -------------------------------------------------------------------------
234 void cpPipelineEditor::BaseQtMainWindow::
237 this->m_Application->setOverrideCursor( Qt::WaitCursor );
238 this->m_Application->installEventFilter( &( this->m_Blocker ) );
241 // -------------------------------------------------------------------------
242 void cpPipelineEditor::BaseQtMainWindow::
245 while( this->m_Application->overrideCursor( ) )
246 this->m_Application->restoreOverrideCursor( );
247 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
250 // -------------------------------------------------------------------------
251 void cpPipelineEditor::BaseQtMainWindow::
252 _LoadWorkspace( const std::string& filename )
254 this->_CreateWorkspace( );
255 std::string err = this->m_Workspace->LoadWorkspace( filename );
258 QMessageBox::critical(
260 QMessageBox::tr( "Error loading workspace" ),
261 QMessageBox::tr( err.c_str( ) )
266 if( this->m_Editor != NULL )
267 this->m_Editor->setWorkspace( this->m_Workspace );
272 // -------------------------------------------------------------------------
273 void cpPipelineEditor::BaseQtMainWindow::
274 _SaveWorkspace( const std::string& filename )
276 if( this->m_Workspace != NULL )
278 std::string err = this->m_Workspace->SaveWorkspace( filename );
280 QMessageBox::critical(
282 QMessageBox::tr( "Error saving workspace" ),
283 QMessageBox::tr( err.c_str( ) )
289 // -------------------------------------------------------------------------
290 void cpPipelineEditor::BaseQtMainWindow::
291 _ShowData( const std::string& filter_name, const std::string& output_name )
293 if( this->m_MPR == NULL || this->m_Workspace == NULL )
296 auto output = this->m_Workspace->GetOutput( filter_name, output_name );
300 auto actor = output->GetVTKActor( );
303 this->m_MPR->AddActor(
304 actor, output_name + std::string( "@" ) + filter_name
311 QMessageBox::critical(
313 QMessageBox::tr( "Error showing data" ),
314 QMessageBox::tr( "Unknown VTK conversion." )
320 QMessageBox::critical(
322 QMessageBox::tr( "Error showing data" ),
323 QMessageBox::tr( "Unknown port name." )
327 // -------------------------------------------------------------------------
328 void cpPipelineEditor::BaseQtMainWindow::
329 _HideData( const std::string& filter, const std::string& output )
331 std::cout << "BaseQtMainWindow::HideData" << std::endl;
336 // -------------------------------------------------------------------------
337 void cpPipelineEditor::BaseQtMainWindow::
339 const std::string& filter_name, const std::string& output_name
342 if( this->m_MPR == NULL || this->m_Workspace == NULL )
345 auto output = this->m_Workspace->GetOutput( filter_name, output_name );
349 auto actors = this->m_MPR->GetActors(
350 output_name + std::string( "@" ) + filter_name
352 auto dlg = new cpPlugins::ActorPropertiesQtDialog( NULL );
353 for( auto i = actors.begin( ); i != actors.end( ); ++i )
355 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
356 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
357 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
358 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
363 QMessageBox::critical(
365 QMessageBox::tr( "Error showing data" ),
366 QMessageBox::tr( "Unknown port name." )
370 // -------------------------------------------------------------------------
371 void cpPipelineEditor::BaseQtMainWindow::
372 _BackgroundProperties( unsigned int i )
374 if( this->m_MPR == NULL )
378 QColorDialog::getColor(
382 QColorDialog::DontUseNativeDialog
384 if( color.isValid( ) )
386 double r = double( color.red( ) ) / double( 255 );
387 double g = double( color.green( ) ) / double( 255 );
388 double b = double( color.blue( ) ) / double( 255 );
391 unsigned int maxId = ( i == 4 )? 3: 4;
392 for( unsigned int j = 0; j < maxId; ++j )
394 auto ren = this->m_MPR->GetRenderer( j );
397 ren->SetBackground( r, g, b );
406 auto ren = this->m_MPR->GetRenderer( i );
409 ren->SetBackground( r, g, b );
419 // -------------------------------------------------------------------------
420 void cpPipelineEditor::BaseQtMainWindow::
421 _InteractiveLoadPlugins( )
423 QFileDialog dlg( this );
424 dlg.setFileMode( QFileDialog::ExistingFiles );
425 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
427 std::stringstream filter;
428 std::string suffix = std::string( cpPlugins_LIB_EXT );
429 filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
430 dlg.setNameFilter( filter.str( ).c_str( ) );
431 dlg.setDefaultSuffix( suffix.c_str( ) );
432 if( !( dlg.exec( ) ) )
434 QStringList names = dlg.selectedFiles( );
435 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
436 this->_LoadPlugins( qIt->toStdString( ) );
439 // -------------------------------------------------------------------------
440 void cpPipelineEditor::BaseQtMainWindow::
441 _InteractiveLoadPluginsFromPath( )
443 QFileDialog d( this );
444 d.setFileMode( QFileDialog::DirectoryOnly );
445 d.setDirectory( this->m_PluginsPath.c_str( ) );
448 this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
451 // -------------------------------------------------------------------------
452 void cpPipelineEditor::BaseQtMainWindow::
453 _InteractiveLoadWorkspace( )
455 QFileDialog dlg( this );
456 dlg.setFileMode( QFileDialog::ExistingFile );
457 dlg.setDirectory( "." );
459 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
461 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
462 if( !( dlg.exec( ) ) )
464 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
467 // -------------------------------------------------------------------------
468 void cpPipelineEditor::BaseQtMainWindow::
469 _InteractiveSaveWorkspace( )
471 if( this->m_Workspace != NULL )
473 QFileDialog dlg( this );
474 dlg.setFileMode( QFileDialog::AnyFile );
475 dlg.setDirectory( "." );
476 dlg.setAcceptMode( QFileDialog::AcceptSave );
478 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
480 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
481 if( !( dlg.exec( ) ) )
483 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
488 // -------------------------------------------------------------------------
489 void cpPipelineEditor::BaseQtMainWindow::
490 _ExecFilter( const std::string& filter_name )
492 if( this->m_Workspace == NULL )
498 this->m_Workspace->Execute( filter_name );
501 catch( itk::ExceptionObject& err1 )
504 QMessageBox::critical(
506 QMessageBox::tr( "Error executing filter" ),
507 QMessageBox::tr( err1.GetDescription( ) )
510 catch( std::exception& err2 )
513 QMessageBox::critical(
515 QMessageBox::tr( "Error executing filter" ),
516 QMessageBox::tr( err2.what( ) )
522 QMessageBox::critical(
524 QMessageBox::tr( "Error executing filter" ),
525 QMessageBox::tr( "Unknown error" )