1 #include <cpBaseQtApplication/MainWindow.h>
3 #include <QApplication>
9 #include <cpExtensions/QT/SimpleMPRWidget.h>
10 #include <cpExtensions/QT/ConfigurationChooser.h>
12 #include <vtkDataSet.h>
14 #include <cpBaseQtApplication/Editor.h>
15 #include <cpBaseQtApplication/PathsDialog.h>
16 #include <cpBaseQtApplication/PluginsNavigator.h>
17 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
20 #include <cpPlugins/Interface/Plugins.h>
21 #include <cpPlugins/BaseObjects/Widget.h>
22 #include <vtkImageData.h>
23 #include <vtkPolyData.h>
24 #include <vtkRenderer.h>
25 #include <QColorDialog>
28 // -------------------------------------------------------------------------
29 bool cpBaseQtApplication::MainWindow::_TBlocker::
30 eventFilter( QObject* obj, QEvent* event )
32 return( true ); // -> Block all events
33 /* NOTE: this should be the 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 // -------------------------------------------------------------------------
50 cpBaseQtApplication::MainWindow::
51 MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent )
52 : Superclass( parent ),
58 // Use some evident paths
59 QFileInfo e_path( argv[ 0 ] );
61 std::set< std::string > paths;
62 paths.insert( e_path.canonicalPath( ).toStdString( ) );
63 paths.insert( r_path.canonicalPath( ).toStdString( ) );
64 std::stringstream env;
65 for( auto p = paths.begin( ); p != paths.end( ); ++p )
66 env << *p << cpPlugins_ENV_SEPARATOR;
69 this->m_RunPath = r_path.canonicalPath( ).toStdString( );
71 // Get the plugins interface
72 this->m_Plugins = TPlugins::New( );
73 try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { }
74 try { this->m_Plugins->LoadPaths( this->m_RunPath ); } catch( ... ) { }
75 this->updateEnvironment( );
76 try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { }
78 // Create local workspace
79 this->m_Workspace = TWorkspace::New( );
82 // -------------------------------------------------------------------------
83 cpBaseQtApplication::MainWindow::
88 // -------------------------------------------------------------------------
89 void cpBaseQtApplication::MainWindow::
94 this->m_Plugins->LoadEnvironments( );
95 this->m_Plugins->GuessPlugins( );
97 catch( std::exception& err )
99 QMessageBox::critical(
100 this, "Error loading required libraries", err.what( )
107 // -------------------------------------------------------------------------
108 void cpBaseQtApplication::MainWindow::
109 updateFilter( const std::string& name )
111 auto filter = this->m_Workspace->GetFilter( name );
113 cpBaseQtApplication_Execute( filter->Update( ) );
116 // -------------------------------------------------------------------------
117 void cpBaseQtApplication::MainWindow::
118 showData( const std::string& name, const std::string& port )
120 this->updateFilter( name );
121 auto filter = this->m_Workspace->GetFilter( name );
124 filter->GetOutput( port ), port + std::string( "@" ) + name
127 QMessageBox::critical(
129 QMessageBox::tr( "Error showing data" ),
130 QMessageBox::tr( "Unknown filter." )
134 // -------------------------------------------------------------------------
135 void cpBaseQtApplication::MainWindow::
136 showData( cpPlugins::BaseObjects::DataObject* data, const std::string& name )
138 if( this->m_MPR == NULL || data == NULL )
141 // Associate visual data
143 bool success = this->m_MPR->Add( data->GetVTK< vtkDataSet >( ), name );
146 // Show data or show an error
148 this->m_MPR->Render( );
150 QMessageBox::critical(
152 QMessageBox::tr( "Error showing data" ),
153 QMessageBox::tr( "Unknown conversion to a \"vtkProp\" object." )
157 // -------------------------------------------------------------------------
159 void cpBaseQtApplication::MainWindow::
160 hideData( const std::string& name, const std::string& port )
162 this->hideData( port + std::string( "@" ) + name );
165 // -------------------------------------------------------------------------
166 void cpBaseQtApplication::MainWindow::
167 hideData( const std::string& name )
172 // -------------------------------------------------------------------------
173 void cpBaseQtApplication::MainWindow::
174 dataProperties( const std::string& name, const std::string& port )
176 this->dataProperties( port + std::string( "@" ) + name );
179 // -------------------------------------------------------------------------
180 void cpBaseQtApplication::MainWindow::
181 dataProperties( const std::string& name )
183 if( this->m_MPR == NULL )
186 auto props = this->m_MPR->GetViewProps( name );
187 if( props.size( ) > 0 )
190 auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL );
191 dlg->setProps( props );
192 dlg->addRenderWindow( this->m_MPR->GetXRenderWindow( ) );
193 dlg->addRenderWindow( this->m_MPR->GetYRenderWindow( ) );
194 dlg->addRenderWindow( this->m_MPR->GetZRenderWindow( ) );
195 dlg->addRenderWindow( this->m_MPR->GetWRenderWindow( ) );
202 // -------------------------------------------------------------------------
203 void cpBaseQtApplication::MainWindow::
206 if( this->m_Application != NULL )
208 this->m_Application->setOverrideCursor( Qt::WaitCursor );
209 this->m_Application->installEventFilter( &( this->m_Blocker ) );
214 // -------------------------------------------------------------------------
215 void cpBaseQtApplication::MainWindow::
218 if( this->m_Application != NULL )
220 while( this->m_Application->overrideCursor( ) )
221 this->m_Application->restoreOverrideCursor( );
222 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
227 // -------------------------------------------------------------------------
228 void cpBaseQtApplication::MainWindow::
229 _configure( PluginsNavigator* nav, TMPR* mpr, Editor* edt )
231 this->m_Navigator = nav;
233 this->m_Editor = edt;
234 this->_updatePlugins( );
235 if( this->m_Editor != NULL )
236 this->m_Editor->setWorkspace( this->m_Workspace );
238 // Associate interactors
239 if( this->m_MPR != NULL )
241 this->m_Workspace->AddInteractor( this->m_MPR->GetXInteractor( ) );
242 this->m_Workspace->AddInteractor( this->m_MPR->GetYInteractor( ) );
243 this->m_Workspace->AddInteractor( this->m_MPR->GetZInteractor( ) );
244 this->m_Workspace->AddInteractor( this->m_MPR->GetWInteractor( ) );
249 // -------------------------------------------------------------------------
250 void cpBaseQtApplication::MainWindow::
253 if( this->m_Navigator == NULL )
256 this->m_Navigator->Update( );
260 // -------------------------------------------------------------------------
261 void cpBaseQtApplication::MainWindow::
262 _loadWorkspace( const std::string& filename )
266 this->m_Workspace->Load( filename );
268 catch( std::exception& err )
270 QMessageBox::critical(
272 QMessageBox::tr( "Error loading workspace" ),
273 QMessageBox::tr( err.what( ) )
277 if( this->m_Editor != NULL )
278 this->m_Editor->redrawWorkspace( );
281 // -------------------------------------------------------------------------
282 void cpBaseQtApplication::MainWindow::
285 QFileDialog dlg( this );
286 dlg.setFileMode( QFileDialog::ExistingFile );
287 dlg.setDirectory( "." );
289 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
291 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
292 if( !( dlg.exec( ) ) )
294 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
297 // -------------------------------------------------------------------------
298 void cpBaseQtApplication::MainWindow::
299 _saveWorkspace( const std::string& filename )
303 this->m_Workspace->Save( filename );
305 catch( std::exception& err )
307 QMessageBox::critical(
309 QMessageBox::tr( "Error saving workspace" ),
310 QMessageBox::tr( err.what( ) )
316 // -------------------------------------------------------------------------
317 void cpBaseQtApplication::MainWindow::
320 QFileDialog dlg( this );
321 dlg.setFileMode( QFileDialog::AnyFile );
322 dlg.setDirectory( "." );
323 dlg.setAcceptMode( QFileDialog::AcceptSave );
325 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
327 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
328 if( !( dlg.exec( ) ) )
330 this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
333 // -------------------------------------------------------------------------
334 void cpBaseQtApplication::MainWindow::
337 std::stringstream info;
338 this->m_Plugins->Print( info );
339 QMessageBox::information(
341 "Loaded libraries and plugins information",
346 // -------------------------------------------------------------------------
347 void cpBaseQtApplication::MainWindow::
348 _addEnvironmentPaths( const std::string& envs )
350 try { this->m_Plugins->AddEnvironments( envs ); } catch( ... ) { }
351 try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { }
352 this->updateEnvironment( );
353 this->_updatePlugins( );
356 // -------------------------------------------------------------------------
357 void cpBaseQtApplication::MainWindow::
358 _addEnvironmentPaths( )
360 PathsDialog dlg( this );
361 dlg.addPaths( this->m_Plugins->GetPaths( ) );
364 auto paths = dlg.getPaths( );
365 std::stringstream envs;
366 for( auto p = paths.begin( ); p != paths.end( ); ++p )
367 envs << *p << cpPlugins_ENV_SEPARATOR;
368 this->_addEnvironmentPaths( envs.str( ) );
373 // -------------------------------------------------------------------------
374 void cpBaseQtApplication::MainWindow::
375 _loadPlugins( const std::string& filename )
379 this->m_Plugins->LoadFile( filename );
380 this->_updatePlugins( );
382 catch( std::exception& err )
384 QMessageBox::critical(
386 "Error loading plugins path",
393 // -------------------------------------------------------------------------
394 void cpBaseQtApplication::MainWindow::
397 QFileDialog dlg( this );
398 dlg.setFileMode( QFileDialog::ExistingFiles );
400 std::stringstream filter;
401 std::string suffix = std::string( cpPlugins_LIB_EXT );
402 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
403 dlg.setNameFilter( filter.str( ).c_str( ) );
404 dlg.setDefaultSuffix( suffix.c_str( ) );
405 if( !( dlg.exec( ) ) )
407 QStringList names = dlg.selectedFiles( );
408 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
409 this->_loadPlugins( qIt->toStdString( ) );
412 // -------------------------------------------------------------------------
413 void cpBaseQtApplication::MainWindow::
414 _loadPluginsFromPath( const std::string& path )
418 this->m_Plugins->LoadDirectory( path );
419 this->_updatePlugins( );
421 catch( std::exception& err )
423 QMessageBox::critical(
425 "Error loading plugins path",
432 // -------------------------------------------------------------------------
433 void cpBaseQtApplication::MainWindow::
434 _loadPluginsFromPath( )
436 QFileDialog d( this );
437 d.setFileMode( QFileDialog::DirectoryOnly );
440 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
443 // -------------------------------------------------------------------------
444 void cpBaseQtApplication::MainWindow::
447 if( this->m_MPR != NULL )
449 auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
450 dlg->setData( this->m_MPR );
456 // -------------------------------------------------------------------------
458 void cpBaseQtApplication::MainWindow::
461 if( this->m_Editor != NULL )
462 this->m_Editor->clear( );
463 this->m_Workspace->Clear( );