1 #include <cpBaseQtApplication/MainWindow.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
5 #include <cpBaseQtApplication/Editor.h>
6 #include <cpBaseQtApplication/PathsDialog.h>
7 #include <vtkRenderer.h>
8 #include <QApplication>
9 #include <QColorDialog>
11 #include <QFileDialog>
13 #include <QMessageBox>
14 #include <QTreeWidget>
16 // -------------------------------------------------------------------------
17 bool cpBaseQtApplication::MainWindow::_TBlocker::
18 eventFilter( QObject* obj, QEvent* event )
20 return( true ); // -> Block all events
21 /* NOTE: this should be the correct implementation:
22 switch( event->type( ) )
24 //list event you want to prevent here ...
25 case QEvent::KeyPress:
26 case QEvent::KeyRelease:
27 case QEvent::MouseButtonRelease:
28 case QEvent::MouseButtonPress:
29 case QEvent::MouseButtonDblClick:
33 return( this->QObject::eventFilter( obj, event ) );
37 // -------------------------------------------------------------------------
38 cpBaseQtApplication::MainWindow::
40 int argc, char* argv[],
44 : Superclass( parent ),
51 QFileInfo e_path( argv[ 0 ] );
53 this->m_RunPath = r_path.canonicalPath( ).toStdString( );
54 std::stringstream paths;
56 << e_path.canonicalPath( ).toStdString( )
57 << cpPlugins_SEPARATOR
60 try { this->m_Interface.AddEnvironments( this->m_RunPath ); }
62 try { this->m_Interface.OpenEnvironments( this->m_RunPath ); }
64 try { this->m_Interface.SaveEnvironments( this->m_RunPath ); }
66 this->UpdateEnvironment( );
67 this->m_Workspace.SetInterface( &( this->m_Interface ) );
70 // -------------------------------------------------------------------------
71 cpBaseQtApplication::MainWindow::
76 // -------------------------------------------------------------------------
77 void cpBaseQtApplication::MainWindow::
82 this->m_Interface.LoadEnvironments( );
83 this->m_Interface.GuessPlugins( );
85 catch( std::exception& err )
87 QMessageBox::critical(
88 this, "Error loading required libraries", err.what( )
95 // -------------------------------------------------------------------------
96 void cpBaseQtApplication::MainWindow::
99 cpExtensions::QT::SimpleMPRWidget* mpr,
100 cpBaseQtApplication::Editor* editor
103 this->m_TreeWidget = tree;
104 this->_UpdateLoadedPlugins( );
105 this->m_Editor = editor;
107 if( this->m_Editor != NULL )
109 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
114 SIGNAL( execFilter( const std::string& ) ),
116 SLOT( _ExecFilter( const std::string& ) )
120 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
122 SLOT( _ShowData( const std::string&, const std::string& ) )
126 SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ),
128 SLOT( _HideData( const std::string&, const std::string& ) )
132 SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ),
134 SLOT( _DataProperties( const std::string&, const std::string& ) )
138 if( this->m_MPR != NULL )
139 this->m_Workspace.SetMPRViewer( this->m_MPR );
140 this->_ClearWorkspace( );
143 // -------------------------------------------------------------------------
144 void cpBaseQtApplication::MainWindow::
147 if( this->m_Editor != NULL )
148 this->m_Editor->clear( );
149 if( this->m_MPR != NULL )
150 this->m_MPR->Clear( );
151 this->m_Workspace.Clear( );
154 // -------------------------------------------------------------------------
155 void cpBaseQtApplication::MainWindow::
156 _AddEnvironments( const std::string& environments )
158 try { this->m_Interface.AddEnvironments( environments ); }
160 try { this->m_Interface.SaveEnvironments( this->m_RunPath ); }
162 this->UpdateEnvironment( );
165 // -------------------------------------------------------------------------
166 void cpBaseQtApplication::MainWindow::
167 _LoadPlugins( const std::string& filename )
171 this->m_Interface.LoadFile( filename );
172 this->_UpdateLoadedPlugins( );
174 catch( std::exception& err )
176 QMessageBox::critical(
178 "Error loading plugins path",
185 // -------------------------------------------------------------------------
186 void cpBaseQtApplication::MainWindow::
187 _LoadPluginsFromPath( const std::string& path )
191 this->m_Interface.LoadDirectory( path );
192 this->_UpdateLoadedPlugins( );
194 catch( std::exception& err )
196 QMessageBox::critical(
198 "Error loading plugins path",
205 // -------------------------------------------------------------------------
206 void cpBaseQtApplication::MainWindow::
207 _UpdateLoadedPlugins( )
209 if( this->m_TreeWidget == NULL )
212 auto categories = this->m_Interface.GetCategories( );
213 unsigned int filter_count = 0;
214 for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
216 // Create or get category
217 QList< QTreeWidgetItem* > cat_items =
218 this->m_TreeWidget->findItems(
219 cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
221 QTreeWidgetItem* cat = NULL;
222 if( cat_items.size( ) == 0 )
224 cat = new QTreeWidgetItem(
225 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
227 this->m_TreeWidget->addTopLevelItem( cat );
230 cat = cat_items[ 0 ];
233 auto filters = this->m_Interface.GetFilters( *cIt );
234 filter_count += filters.size( );
235 for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
237 QList< QTreeWidgetItem* > filter_items =
238 this->m_TreeWidget->findItems(
239 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
241 auto fiIt = filter_items.begin( );
242 auto found_fiIt = filter_items.end( );
243 for( ; fiIt != filter_items.end( ); ++fiIt )
244 if( ( *fiIt )->parent( ) == cat )
248 if( found_fiIt == filter_items.end( ) )
249 QTreeWidgetItem* filter = new QTreeWidgetItem(
250 cat, QStringList( fIt->c_str( ) )
256 this->m_TreeWidget->expandAll( );
259 if( filter_count == 0 )
260 QMessageBox::critical(
262 "Error loading default plugins",
263 "No plugins loaded: remember to load some!!!"
267 // -------------------------------------------------------------------------
268 void cpBaseQtApplication::MainWindow::
271 this->m_Application->setOverrideCursor( Qt::WaitCursor );
272 this->m_Application->installEventFilter( &( this->m_Blocker ) );
275 // -------------------------------------------------------------------------
276 void cpBaseQtApplication::MainWindow::
279 while( this->m_Application->overrideCursor( ) )
280 this->m_Application->restoreOverrideCursor( );
281 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
284 // -------------------------------------------------------------------------
285 void cpBaseQtApplication::MainWindow::
286 _LoadWorkspace( const std::string& filename )
288 this->_ClearWorkspace( );
289 std::string err = this->m_Workspace.LoadWorkspace( filename );
292 QMessageBox::critical(
294 QMessageBox::tr( "Error loading workspace" ),
295 QMessageBox::tr( err.c_str( ) )
300 if( this->m_Editor != NULL )
301 this->m_Editor->redrawWorkspace( );
306 // -------------------------------------------------------------------------
307 void cpBaseQtApplication::MainWindow::
308 _SaveWorkspace( const std::string& filename )
310 std::string err = this->m_Workspace.SaveWorkspace( filename );
312 QMessageBox::critical(
314 QMessageBox::tr( "Error saving workspace" ),
315 QMessageBox::tr( err.c_str( ) )
319 // -------------------------------------------------------------------------
320 void cpBaseQtApplication::MainWindow::
321 _BackgroundProperties( unsigned int i )
323 if( this->m_MPR == NULL )
327 QColorDialog::getColor(
331 QColorDialog::DontUseNativeDialog
333 if( color.isValid( ) )
335 double r = double( color.red( ) ) / double( 255 );
336 double g = double( color.green( ) ) / double( 255 );
337 double b = double( color.blue( ) ) / double( 255 );
340 unsigned int maxId = ( i == 4 )? 3: 4;
341 for( unsigned int j = 0; j < maxId; ++j )
343 auto ren = this->m_MPR->GetRenderer( j );
346 ren->SetBackground( r, g, b );
355 auto ren = this->m_MPR->GetRenderer( i );
358 ren->SetBackground( r, g, b );
368 // -------------------------------------------------------------------------
369 void cpBaseQtApplication::MainWindow::
370 _DataProperties( const std::string& actor )
372 if( this->m_MPR == NULL )
375 auto actors = this->m_MPR->GetActors( actor );
376 auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL );
377 for( auto i = actors.begin( ); i != actors.end( ); ++i )
379 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
380 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
381 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
382 dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
387 // -------------------------------------------------------------------------
388 void cpBaseQtApplication::MainWindow::
389 _ShowData( const std::string& filter_name, const std::string& output_name )
391 if( this->m_MPR == NULL )
394 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
398 auto actor = output->GetVTKActor( );
401 this->m_MPR->AddActor(
402 actor, output_name + std::string( "@" ) + filter_name
409 QMessageBox::critical(
411 QMessageBox::tr( "Error showing data" ),
412 QMessageBox::tr( "Unknown VTK conversion." )
418 QMessageBox::critical(
420 QMessageBox::tr( "Error showing data" ),
421 QMessageBox::tr( "Unknown port name." )
425 // -------------------------------------------------------------------------
426 void cpBaseQtApplication::MainWindow::
427 _HideData( const std::string& filter, const std::string& output )
429 std::cout << "MainWindow::HideData" << std::endl;
434 // -------------------------------------------------------------------------
435 void cpBaseQtApplication::MainWindow::
437 const std::string& filter_name, const std::string& output_name
440 if( this->m_MPR == NULL )
443 auto output = this->m_Workspace.GetOutput( filter_name, output_name );
445 this->_DataProperties( output_name + std::string( "@" ) + filter_name );
447 QMessageBox::critical(
449 QMessageBox::tr( "Error showing data" ),
450 QMessageBox::tr( "Unknown port name." )
454 // -------------------------------------------------------------------------
455 void cpBaseQtApplication::MainWindow::
456 _InteractiveLoadPlugins( )
458 QFileDialog dlg( this );
459 dlg.setFileMode( QFileDialog::ExistingFiles );
460 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
462 std::stringstream filter;
463 std::string suffix = std::string( cpPlugins_LIB_EXT );
464 filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
465 dlg.setNameFilter( filter.str( ).c_str( ) );
466 dlg.setDefaultSuffix( suffix.c_str( ) );
467 if( !( dlg.exec( ) ) )
469 QStringList names = dlg.selectedFiles( );
470 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
471 this->_LoadPlugins( qIt->toStdString( ) );
474 // -------------------------------------------------------------------------
475 void cpBaseQtApplication::MainWindow::
476 _InteractiveLoadPluginsFromPath( )
478 QFileDialog d( this );
479 d.setFileMode( QFileDialog::DirectoryOnly );
480 d.setDirectory( this->m_PluginsPath.c_str( ) );
483 this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
486 // -------------------------------------------------------------------------
487 void cpBaseQtApplication::MainWindow::
490 auto paths = this->m_Interface.GetPaths( );
491 auto plugins = this->m_Interface.GetPlugins( );
492 std::stringstream info;
493 info << "----- PATHS -----" << std::endl;
494 for( auto i = paths.begin( ); i != paths.end( ); ++i )
495 info << " " << *i << std::endl;
496 info << std::endl << "----- PLUGINS -----" << std::endl;
497 for( auto i = plugins.begin( ); i != plugins.end( ); ++i )
498 info << " " << *i << std::endl;
499 QMessageBox::information(
501 "Loaded libraries and plugins information",
506 // -------------------------------------------------------------------------
507 void cpBaseQtApplication::MainWindow::
508 _InteractiveLoadWorkspace( )
510 QFileDialog dlg( this );
511 dlg.setFileMode( QFileDialog::ExistingFile );
512 dlg.setDirectory( "." );
514 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
516 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
517 if( !( dlg.exec( ) ) )
519 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
522 // -------------------------------------------------------------------------
523 void cpBaseQtApplication::MainWindow::
524 _InteractiveSaveWorkspace( )
526 QFileDialog dlg( this );
527 dlg.setFileMode( QFileDialog::AnyFile );
528 dlg.setDirectory( "." );
529 dlg.setAcceptMode( QFileDialog::AcceptSave );
531 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
533 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
534 if( !( dlg.exec( ) ) )
536 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
539 // -------------------------------------------------------------------------
540 void cpBaseQtApplication::MainWindow::
541 _InteractiveAddEnviromentPaths( )
543 PathsDialog dlg( this );
544 dlg.addPaths( this->m_Interface.GetPaths( ) );
547 auto paths = dlg.getPaths( );
548 std::stringstream envs;
549 for( auto p = paths.begin( ); p != paths.end( ); ++p )
550 envs << *p << cpPlugins_SEPARATOR;
551 this->_AddEnvironments( envs.str( ) );
552 this->UpdateEnvironment( );
557 // -------------------------------------------------------------------------
558 void cpBaseQtApplication::MainWindow::
559 _ExecFilter( const std::string& filter_name )
561 cpBaseQtApplication_Execute( this->m_Workspace.Execute( filter_name ) );