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 ),
50 this->m_Interface.GuessAccesiblePlugins( );
52 // Try to load plugins from executable dir
53 QFileInfo info( argv[ 0 ] );
55 this->_LoadPluginsFromPath( info.canonicalPath( ).toStdString( ) );
58 this->m_Workspace.SetInterface( &( this->m_Interface ) );
61 // -------------------------------------------------------------------------
62 cpPipelineEditor::BaseQtMainWindow::
65 this->m_Interface.UnloadAll( );
68 // -------------------------------------------------------------------------
69 void cpPipelineEditor::BaseQtMainWindow::
72 cpExtensions::QT::SimpleMPRWidget* mpr,
73 cpPipelineEditor::Editor* editor
76 this->m_TreeWidget = tree;
77 if( this->m_TreeWidget != NULL )
78 this->_UpdateLoadedPlugins( );
79 this->m_Editor = editor;
80 if( this->m_Editor != NULL )
81 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
83 this->m_Workspace.SetMPRViewer( mpr );
87 // -------------------------------------------------------------------------
88 void cpPipelineEditor::BaseQtMainWindow::
89 _LoadPlugins( const std::string& filename )
93 this->m_Interface.LoadPluginFile( filename );
94 this->_UpdateLoadedPlugins( );
96 catch( std::exception& err )
98 QMessageBox::critical(
100 "Error loading plugins path",
107 // -------------------------------------------------------------------------
108 void cpPipelineEditor::BaseQtMainWindow::
109 _LoadPluginsFromPath( const std::string& path )
113 this->m_Interface.LoadPluginDir( path );
114 this->_UpdateLoadedPlugins( );
116 catch( std::exception& err )
118 QMessageBox::critical(
120 "Error loading plugins path",
127 // -------------------------------------------------------------------------
128 void cpPipelineEditor::BaseQtMainWindow::
129 _UpdateLoadedPlugins( )
132 auto filters = this->m_Interface.GetFilters( );
133 if( filters.size( ) == 0 )
136 QMessageBox::critical(
138 "Error loading default plugins",
139 "No plugins loaded: remember to load some!!!"
145 if( this->m_TreeWidget != NULL )
147 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
149 // Create or get category
150 QList< QTreeWidgetItem* > cat_items =
151 this->m_TreeWidget->findItems(
152 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
154 QTreeWidgetItem* cat = NULL;
155 if( cat_items.size( ) == 0 )
157 cat = new QTreeWidgetItem(
158 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
160 this->m_TreeWidget->addTopLevelItem( cat );
163 cat = cat_items[ 0 ];
166 auto fIt = cIt->second.begin( );
167 for( ; fIt != cIt->second.end( ); ++fIt )
169 QList< QTreeWidgetItem* > filter_items =
170 this->m_TreeWidget->findItems(
171 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
173 auto fiIt = filter_items.begin( );
174 auto found_fiIt = filter_items.end( );
175 for( ; fiIt != filter_items.end( ); ++fiIt )
176 if( ( *fiIt )->parent( ) == cat )
180 if( found_fiIt == filter_items.end( ) )
181 QTreeWidgetItem* filter = new QTreeWidgetItem(
182 cat, QStringList( fIt->c_str( ) )
193 // -------------------------------------------------------------------------
194 void cpPipelineEditor::BaseQtMainWindow::
197 this->m_Application->setOverrideCursor( Qt::WaitCursor );
198 this->m_Application->installEventFilter( &( this->m_Blocker ) );
201 // -------------------------------------------------------------------------
202 void cpPipelineEditor::BaseQtMainWindow::
205 while( this->m_Application->overrideCursor( ) )
206 this->m_Application->restoreOverrideCursor( );
207 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
210 // -------------------------------------------------------------------------
211 void cpPipelineEditor::BaseQtMainWindow::
212 _LoadWorkspace( const std::string& filename )
214 std::string err = this->m_Workspace.LoadWorkspace( filename );
217 QMessageBox::critical(
219 QMessageBox::tr( "Error loading workspace" ),
220 QMessageBox::tr( err.c_str( ) )
225 if( this->m_Editor != NULL )
226 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
231 // -------------------------------------------------------------------------
232 void cpPipelineEditor::BaseQtMainWindow::
233 _SaveWorkspace( const std::string& filename )
235 std::string err = this->m_Workspace.SaveWorkspace( filename );
237 QMessageBox::critical(
239 QMessageBox::tr( "Error saving workspace" ),
240 QMessageBox::tr( err.c_str( ) )
244 // -------------------------------------------------------------------------
245 void cpPipelineEditor::BaseQtMainWindow::
246 _ShowData( const std::string& filter_name, const std::string& output_name )
248 if( this->m_MPR == NULL )
250 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
254 auto actor = output->GetVTKActor( );
257 this->m_MPR->AddActor(
258 actor, output_name + std::string( "@" ) + filter_name
265 QMessageBox::critical(
267 QMessageBox::tr( "Error showing data" ),
268 QMessageBox::tr( "Unknown VTK conversion." )
274 QMessageBox::critical(
276 QMessageBox::tr( "Error showing data" ),
277 QMessageBox::tr( "Unknown port name." )
281 // -------------------------------------------------------------------------
282 void cpPipelineEditor::BaseQtMainWindow::
283 _HideData( const std::string& filter, const std::string& output )
287 // -------------------------------------------------------------------------
288 void cpPipelineEditor::BaseQtMainWindow::
290 const std::string& filter_name, const std::string& output_name
293 if( this->m_MPR == NULL )
295 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
299 auto actors = this->m_MPR->GetActors(
300 output_name + std::string( "@" ) + filter_name
302 auto dlg = new cpPlugins::ActorPropertiesQtDialog( NULL );
303 for( auto i = actors.begin( ); i != actors.end( ); ++i )
305 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
306 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
307 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
308 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
313 QMessageBox::critical(
315 QMessageBox::tr( "Error showing data" ),
316 QMessageBox::tr( "Unknown port name." )
320 // -------------------------------------------------------------------------
321 void cpPipelineEditor::BaseQtMainWindow::
322 _BackgroundProperties( unsigned int i )
325 QColorDialog::getColor(
329 QColorDialog::DontUseNativeDialog
331 if( color.isValid( ) )
333 double r = double( color.red( ) ) / double( 255 );
334 double g = double( color.green( ) ) / double( 255 );
335 double b = double( color.blue( ) ) / double( 255 );
338 unsigned int maxId = ( i == 4 )? 3: 4;
339 for( unsigned int j = 0; j < maxId; ++j )
341 auto ren = this->m_MPR->GetRenderer( j );
344 ren->SetBackground( r, g, b );
353 auto ren = this->m_MPR->GetRenderer( i );
356 ren->SetBackground( r, g, b );
366 // -------------------------------------------------------------------------
367 void cpPipelineEditor::BaseQtMainWindow::
368 _InteractiveLoadPlugins( )
370 QFileDialog dlg( this );
371 dlg.setFileMode( QFileDialog::ExistingFiles );
372 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
374 std::stringstream name_filter;
375 std::string suffix = std::string( cpPlugins_LIB_EXT );
377 << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
378 dlg.setNameFilter( name_filter.str( ).c_str( ) );
379 dlg.setDefaultSuffix( suffix.c_str( ) );
380 if( !( dlg.exec( ) ) )
383 QStringList names = dlg.selectedFiles( );
384 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
385 this->_LoadPlugins( qIt->toStdString( ) );
388 // -------------------------------------------------------------------------
389 void cpPipelineEditor::BaseQtMainWindow::
390 _InteractiveLoadPluginsFromPath( )
392 QFileDialog dlg( this );
393 dlg.setFileMode( QFileDialog::DirectoryOnly );
394 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
395 if( !( dlg.exec( ) ) )
397 this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
400 // -------------------------------------------------------------------------
401 void cpPipelineEditor::BaseQtMainWindow::
402 _InteractiveLoadWorkspace( )
404 QFileDialog dlg( this );
405 dlg.setFileMode( QFileDialog::ExistingFile );
406 dlg.setDirectory( "." );
408 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
410 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
411 if( !( dlg.exec( ) ) )
413 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
416 // -------------------------------------------------------------------------
417 void cpPipelineEditor::BaseQtMainWindow::
418 _InteractiveSaveWorkspace( )
420 QFileDialog dlg( this );
421 dlg.setFileMode( QFileDialog::AnyFile );
422 dlg.setDirectory( "." );
423 dlg.setAcceptMode( QFileDialog::AcceptSave );
425 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
427 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
428 if( !( dlg.exec( ) ) )
430 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
433 // -------------------------------------------------------------------------
434 void cpPipelineEditor::BaseQtMainWindow::
435 _ExecFilter( const std::string& filter_name )
440 this->m_Workspace.Execute( filter_name );
443 catch( itk::ExceptionObject& err1 )
446 QMessageBox::critical(
448 QMessageBox::tr( "Error executing filter" ),
449 QMessageBox::tr( err1.GetDescription( ) )
452 catch( std::exception& err2 )
455 QMessageBox::critical(
457 QMessageBox::tr( "Error executing filter" ),
458 QMessageBox::tr( err2.what( ) )
464 QMessageBox::critical(
466 QMessageBox::tr( "Error executing filter" ),
467 QMessageBox::tr( "Unknown error" )