1 #include <cpBaseQtApplication/MainWindow.h>
3 #include <QApplication>
9 #include <cpExtensions/QT/SimpleMPRWidget.h>
11 #include <vtkDataSet.h>
13 #include <cpBaseQtApplication/Editor.h>
14 #include <cpBaseQtApplication/PathsDialog.h>
15 #include <cpBaseQtApplication/PluginsNavigator.h>
16 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
19 #include <cpPlugins/Interface/Plugins.h>
20 #include <cpPlugins/BaseObjects/Widget.h>
21 #include <vtkImageData.h>
22 #include <vtkPolyData.h>
23 #include <vtkRenderer.h>
24 #include <QColorDialog>
27 // -------------------------------------------------------------------------
28 bool cpBaseQtApplication::MainWindow::_TBlocker::
29 eventFilter( QObject* obj, QEvent* event )
31 return( true ); // -> Block all events
32 /* NOTE: this should be the correct implementation:
33 switch( event->type( ) )
35 //list event you want to prevent here ...
36 case QEvent::KeyPress:
37 case QEvent::KeyRelease:
38 case QEvent::MouseButtonRelease:
39 case QEvent::MouseButtonPress:
40 case QEvent::MouseButtonDblClick:
44 return( this->QObject::eventFilter( obj, event ) );
48 // -------------------------------------------------------------------------
49 cpBaseQtApplication::MainWindow::
50 MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent )
51 : Superclass( parent ),
57 // Use some evident paths
58 QFileInfo e_path( argv[ 0 ] );
60 std::set< std::string > paths;
61 paths.insert( e_path.canonicalPath( ).toStdString( ) );
62 paths.insert( r_path.canonicalPath( ).toStdString( ) );
63 std::stringstream env;
64 for( auto p = paths.begin( ); p != paths.end( ); ++p )
65 env << *p << cpPlugins_ENV_SEPARATOR;
68 this->m_RunPath = r_path.canonicalPath( ).toStdString( );
70 // Get the plugins interface
71 this->m_Plugins = TPlugins::New( );
72 try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { }
73 try { this->m_Plugins->OpenEnvironments( env.str( ) ); } catch( ... ) { }
74 try { this->m_Plugins->SaveEnvironments( this->m_RunPath ); } catch( ... ) { }
75 this->updateEnvironment( );
77 // Create local workspace
78 this->m_Workspace = TWorkspace::New( );
81 // -------------------------------------------------------------------------
82 cpBaseQtApplication::MainWindow::
87 // -------------------------------------------------------------------------
88 void cpBaseQtApplication::MainWindow::
93 this->m_Plugins->LoadEnvironments( );
94 this->m_Plugins->GuessPlugins( );
96 catch( std::exception& err )
98 QMessageBox::critical(
99 this, "Error loading required libraries", err.what( )
106 // -------------------------------------------------------------------------
107 void cpBaseQtApplication::MainWindow::
108 updateFilter( const std::string& name )
110 auto filter = this->m_Workspace->GetFilter( name );
112 cpBaseQtApplication_Execute( filter->Update( ) );
115 // -------------------------------------------------------------------------
116 void cpBaseQtApplication::MainWindow::
117 showData( const std::string& name, const std::string& port )
119 this->updateFilter( name );
120 auto filter = this->m_Workspace->GetFilter( name );
123 filter->GetOutput( port ), port + std::string( "@" ) + name
126 QMessageBox::critical(
128 QMessageBox::tr( "Error showing data" ),
129 QMessageBox::tr( "Unknown filter." )
133 // -------------------------------------------------------------------------
134 void cpBaseQtApplication::MainWindow::
135 showData( cpPlugins::BaseObjects::DataObject* data, const std::string& name )
137 if( this->m_MPR == NULL || data == NULL )
140 // Associate visual data
142 bool success = this->m_MPR->Add( data->GetVTK< vtkDataSet >( ), name );
145 // Show data or show an error
147 this->m_MPR->Render( );
149 QMessageBox::critical(
151 QMessageBox::tr( "Error showing data" ),
152 QMessageBox::tr( "Unknown conversion to a \"vtkProp\" object." )
156 // -------------------------------------------------------------------------
158 void cpBaseQtApplication::MainWindow::
159 hideData( const std::string& name, const std::string& port )
161 this->hideData( port + std::string( "@" ) + name );
164 // -------------------------------------------------------------------------
165 void cpBaseQtApplication::MainWindow::
166 hideData( const std::string& name )
171 // -------------------------------------------------------------------------
172 void cpBaseQtApplication::MainWindow::
173 dataProperties( const std::string& name, const std::string& port )
175 this->dataProperties( port + std::string( "@" ) + name );
178 // -------------------------------------------------------------------------
179 void cpBaseQtApplication::MainWindow::
180 dataProperties( const std::string& name )
182 if( this->m_MPR == NULL )
185 auto props = this->m_MPR->GetViewProps( name );
186 if( props.size( ) > 0 )
189 auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL );
190 dlg->setProps( props );
191 dlg->addRenderWindow( this->m_MPR->GetXRenderWindow( ) );
192 dlg->addRenderWindow( this->m_MPR->GetYRenderWindow( ) );
193 dlg->addRenderWindow( this->m_MPR->GetZRenderWindow( ) );
194 dlg->addRenderWindow( this->m_MPR->GetWRenderWindow( ) );
201 // -------------------------------------------------------------------------
202 void cpBaseQtApplication::MainWindow::
205 if( this->m_Application != NULL )
207 this->m_Application->setOverrideCursor( Qt::WaitCursor );
208 this->m_Application->installEventFilter( &( this->m_Blocker ) );
213 // -------------------------------------------------------------------------
214 void cpBaseQtApplication::MainWindow::
217 if( this->m_Application != NULL )
219 while( this->m_Application->overrideCursor( ) )
220 this->m_Application->restoreOverrideCursor( );
221 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
226 // -------------------------------------------------------------------------
227 void cpBaseQtApplication::MainWindow::
228 _configure( PluginsNavigator* nav, TMPR* mpr, Editor* edt )
230 this->m_Navigator = nav;
232 this->m_Editor = edt;
233 this->_updatePlugins( );
234 if( this->m_Editor != NULL )
235 this->m_Editor->setWorkspace( this->m_Workspace );
237 // Associate interactors
238 if( this->m_MPR != NULL )
240 this->m_Workspace->AddInteractor( this->m_MPR->GetXInteractor( ) );
241 this->m_Workspace->AddInteractor( this->m_MPR->GetYInteractor( ) );
242 this->m_Workspace->AddInteractor( this->m_MPR->GetZInteractor( ) );
243 this->m_Workspace->AddInteractor( this->m_MPR->GetWInteractor( ) );
248 // -------------------------------------------------------------------------
249 void cpBaseQtApplication::MainWindow::
252 if( this->m_Navigator == NULL )
255 this->m_Navigator->Update( );
259 // -------------------------------------------------------------------------
260 void cpBaseQtApplication::MainWindow::
261 _loadWorkspace( const std::string& filename )
265 this->m_Workspace->Load( filename );
267 catch( std::exception& err )
269 QMessageBox::critical(
271 QMessageBox::tr( "Error loading workspace" ),
272 QMessageBox::tr( err.what( ) )
276 if( this->m_Editor != NULL )
277 this->m_Editor->redrawWorkspace( );
280 // -------------------------------------------------------------------------
281 void cpBaseQtApplication::MainWindow::
284 QFileDialog dlg( this );
285 dlg.setFileMode( QFileDialog::ExistingFile );
286 dlg.setDirectory( "." );
288 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
290 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
291 if( !( dlg.exec( ) ) )
293 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
296 // -------------------------------------------------------------------------
297 void cpBaseQtApplication::MainWindow::
298 _saveWorkspace( const std::string& filename )
302 this->m_Workspace->Save( filename );
304 catch( std::exception& err )
306 QMessageBox::critical(
308 QMessageBox::tr( "Error saving workspace" ),
309 QMessageBox::tr( err.what( ) )
315 // -------------------------------------------------------------------------
316 void cpBaseQtApplication::MainWindow::
319 QFileDialog dlg( this );
320 dlg.setFileMode( QFileDialog::AnyFile );
321 dlg.setDirectory( "." );
322 dlg.setAcceptMode( QFileDialog::AcceptSave );
324 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
326 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
327 if( !( dlg.exec( ) ) )
329 this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
332 // -------------------------------------------------------------------------
333 void cpBaseQtApplication::MainWindow::
336 std::stringstream info;
337 this->m_Plugins->Print( info );
338 QMessageBox::information(
340 "Loaded libraries and plugins information",
345 // -------------------------------------------------------------------------
346 void cpBaseQtApplication::MainWindow::
347 _addEnvironmentPaths( const std::string& envs )
349 try { this->m_Plugins->AddEnvironments( envs ); } catch( ... ) { }
350 try { this->m_Plugins->SaveEnvironments( this->m_RunPath ); } catch( ... ) { }
351 this->updateEnvironment( );
352 this->_updatePlugins( );
355 // -------------------------------------------------------------------------
356 void cpBaseQtApplication::MainWindow::
357 _addEnvironmentPaths( )
359 PathsDialog dlg( this );
360 dlg.addPaths( this->m_Plugins->GetPaths( ) );
363 auto paths = dlg.getPaths( );
364 std::stringstream envs;
365 for( auto p = paths.begin( ); p != paths.end( ); ++p )
366 envs << *p << cpPlugins_ENV_SEPARATOR;
367 this->_addEnvironmentPaths( envs.str( ) );
372 // -------------------------------------------------------------------------
373 void cpBaseQtApplication::MainWindow::
374 _loadPlugins( const std::string& filename )
378 this->m_Plugins->LoadFile( filename );
379 this->_updatePlugins( );
381 catch( std::exception& err )
383 QMessageBox::critical(
385 "Error loading plugins path",
392 // -------------------------------------------------------------------------
393 void cpBaseQtApplication::MainWindow::
396 QFileDialog dlg( this );
397 dlg.setFileMode( QFileDialog::ExistingFiles );
399 std::stringstream filter;
400 std::string suffix = std::string( cpPlugins_LIB_EXT );
401 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
402 dlg.setNameFilter( filter.str( ).c_str( ) );
403 dlg.setDefaultSuffix( suffix.c_str( ) );
404 if( !( dlg.exec( ) ) )
406 QStringList names = dlg.selectedFiles( );
407 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
408 this->_loadPlugins( qIt->toStdString( ) );
411 // -------------------------------------------------------------------------
412 void cpBaseQtApplication::MainWindow::
413 _loadPluginsFromPath( const std::string& path )
417 this->m_Plugins->LoadDirectory( path );
418 this->_updatePlugins( );
420 catch( std::exception& err )
422 QMessageBox::critical(
424 "Error loading plugins path",
431 // -------------------------------------------------------------------------
432 void cpBaseQtApplication::MainWindow::
433 _loadPluginsFromPath( )
435 QFileDialog d( this );
436 d.setFileMode( QFileDialog::DirectoryOnly );
439 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
442 // -------------------------------------------------------------------------
444 void cpBaseQtApplication::MainWindow::
447 if( this->m_Editor != NULL )
448 this->m_Editor->clear( );
449 this->m_Workspace->Clear( );