1 #include <cpBaseQtApplication/MainWindow.h>
2 #include <cpBaseQtApplication/Plugins/Navigator.h>
3 #include <cpBaseQtApplication/Pipeline/Canvas.h>
4 #include <cpExtensions/QT/ConfigurationChooser.h>
5 #include <cpExtensions/QT/ActorsWidgetInterface.h>
9 #include <QInputDialog>
11 // -------------------------------------------------------------------------
12 cpBaseQtApplication::MainWindow::
14 int argc, char* argv[],
17 : Superclass( parent ),
18 m_LastSaveFileName( "" ),
19 m_BaseWindowTitle( "cpBaseQtApplication" ),
24 this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
25 this->m_Plugins = TPlugins::New( );
28 this->m_Plugins->GuessEnvironment( this->m_RunPath );
29 this->m_Plugins->GuessPlugins( );
31 catch( std::exception& err )
33 QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
36 this->_clearWorkspace( );
39 // -------------------------------------------------------------------------
40 cpBaseQtApplication::MainWindow::
45 // -------------------------------------------------------------------------
46 cpBaseQtApplication::MainWindow::
47 TWorkspace* cpBaseQtApplication::MainWindow::
50 return( this->m_Workspace );
53 // -------------------------------------------------------------------------
54 const cpBaseQtApplication::MainWindow::
55 TWorkspace* cpBaseQtApplication::MainWindow::
58 return( this->m_Workspace );
61 // -------------------------------------------------------------------------
62 cpBaseQtApplication::Pipeline::Canvas*
63 cpBaseQtApplication::MainWindow::
66 return( this->m_Canvas );
69 // -------------------------------------------------------------------------
70 const cpBaseQtApplication::Pipeline::Canvas*
71 cpBaseQtApplication::MainWindow::
74 return( this->m_Canvas );
77 // -------------------------------------------------------------------------
78 void cpBaseQtApplication::MainWindow::
79 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
82 if( this->m_Canvas != NULL )
83 this->m_Canvas->setWorkspace( this->m_Workspace );
87 // -------------------------------------------------------------------------
88 cpBaseQtApplication::Plugins::Navigator*
89 cpBaseQtApplication::MainWindow::
92 return( this->m_Navigator );
95 // -------------------------------------------------------------------------
96 const cpBaseQtApplication::Plugins::Navigator*
97 cpBaseQtApplication::MainWindow::
100 return( this->m_Navigator );
103 // -------------------------------------------------------------------------
104 void cpBaseQtApplication::MainWindow::
105 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
107 this->m_Navigator = n;
110 // -------------------------------------------------------------------------
111 cpExtensions::QT::ActorsWidgetInterface*
112 cpBaseQtApplication::MainWindow::
115 return( this->m_Viewer );
118 // -------------------------------------------------------------------------
119 const cpExtensions::QT::ActorsWidgetInterface*
120 cpBaseQtApplication::MainWindow::
123 return( this->m_Viewer );
126 // -------------------------------------------------------------------------
127 void cpBaseQtApplication::MainWindow::
128 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
131 if( this->m_Viewer != NULL )
133 auto interactors = this->m_Viewer->GetInteractors( );
134 for( auto i : interactors )
135 this->m_Workspace->AddInteractor( i );
140 // -------------------------------------------------------------------------
141 void cpBaseQtApplication::MainWindow::
142 _loadPlugins( const std::string& filename )
146 this->m_Plugins->LoadPluginsFile( filename );
147 if( this->m_Navigator != NULL )
148 this->m_Navigator->Update( );
150 catch( std::exception& err )
152 QMessageBox::critical(
154 "Error loading plugins path",
161 // -------------------------------------------------------------------------
162 void cpBaseQtApplication::MainWindow::
165 QFileDialog dlg( this );
166 dlg.setFileMode( QFileDialog::ExistingFiles );
168 std::stringstream filter;
169 std::string suffix = std::string( cpPlugins_LIB_EXT );
170 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
171 dlg.setNameFilter( filter.str( ).c_str( ) );
172 dlg.setDefaultSuffix( suffix.c_str( ) );
173 if( !( dlg.exec( ) ) )
175 QStringList names = dlg.selectedFiles( );
176 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
177 this->_loadPlugins( qIt->toStdString( ) );
180 // -------------------------------------------------------------------------
181 void cpBaseQtApplication::MainWindow::
182 _loadPluginsFromPath( const std::string& path )
186 this->m_Plugins->LoadPluginsDirectory( path );
187 this->m_Plugins->SaveEnvironment( this->m_RunPath );
188 if( this->m_Navigator != NULL )
189 this->m_Navigator->Update( );
191 catch( std::exception& err )
193 QMessageBox::critical(
195 "Error loading plugins path",
202 // -------------------------------------------------------------------------
203 void cpBaseQtApplication::MainWindow::
204 _loadPluginsFromPath( )
206 QFileDialog d( this );
207 d.setFileMode( QFileDialog::DirectoryOnly );
210 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
213 // -------------------------------------------------------------------------
214 void cpBaseQtApplication::MainWindow::
217 this->setWindowTitle( this->m_BaseWindowTitle.c_str( ) );
218 this->m_Workspace = TWorkspace::New( );
219 if( this->m_Canvas != NULL )
221 this->m_Canvas->clear( );
222 this->m_Canvas->setWorkspace( this->m_Workspace );
225 if( this->m_Viewer != NULL )
227 // TODO: this->m_Viewer->clear( );
228 auto interactors = this->m_Viewer->GetInteractors( );
229 for( auto i : interactors )
230 this->m_Workspace->AddInteractor( i );
235 // -------------------------------------------------------------------------
236 void cpBaseQtApplication::MainWindow::
237 _saveWorkspace( const std::string& fname )
239 this->m_LastSaveFileName = fname;
240 this->m_Workspace->Save( this->m_LastSaveFileName );
243 // -------------------------------------------------------------------------
244 void cpBaseQtApplication::MainWindow::
247 if( this->m_LastSaveFileName == "" )
249 QFileDialog dlg( this );
250 dlg.setFileMode( QFileDialog::AnyFile );
251 dlg.setDirectory( "." );
252 dlg.setAcceptMode( QFileDialog::AcceptSave );
254 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
256 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
257 dlg.setWindowTitle( "Saving workspace" );
259 this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
262 this->_saveWorkspace( this->m_LastSaveFileName );
265 // -------------------------------------------------------------------------
266 void cpBaseQtApplication::MainWindow::
267 _loadWorkspace( const std::string& fname )
271 this->_clearWorkspace( );
272 this->m_Workspace->Load( fname );
273 this->m_LastSaveFileName = "";
274 if( this->m_Canvas != NULL )
275 this->m_Canvas->setWorkspace( this->m_Workspace );
277 catch( std::exception& err )
279 QMessageBox::critical(
281 QMessageBox::tr( "Error loading workspace" ),
282 QMessageBox::tr( err.what( ) )
288 // -------------------------------------------------------------------------
289 void cpBaseQtApplication::MainWindow::
292 QFileDialog dlg( this );
293 dlg.setFileMode( QFileDialog::ExistingFile );
294 dlg.setDirectory( "." );
296 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
298 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
299 if( !( dlg.exec( ) ) )
301 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
304 // -------------------------------------------------------------------------
305 void cpBaseQtApplication::MainWindow::
309 dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
314 auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
315 dlg->setData( data );