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_SingleWorkspace( false ),
20 m_BaseWindowTitle( "cpBaseQtApplication" ),
25 this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
26 this->m_Plugins = TPlugins::New( );
29 this->m_Plugins->GuessEnvironment( this->m_RunPath );
30 this->m_Plugins->GuessPlugins( );
32 catch( std::exception& err )
34 QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
39 // -------------------------------------------------------------------------
40 cpBaseQtApplication::MainWindow::
45 // -------------------------------------------------------------------------
46 cpBaseQtApplication::MainWindow::
47 TWorkspace* cpBaseQtApplication::MainWindow::
48 workspace( const std::string& wname )
50 auto wIt = this->m_Workspaces.find( wname );
51 if( wIt != this->m_Workspaces.end( ) )
52 return( wIt->second.GetPointer( ) );
57 // -------------------------------------------------------------------------
58 const cpBaseQtApplication::MainWindow::
59 TWorkspace* cpBaseQtApplication::MainWindow::
60 workspace( const std::string& wname ) const
62 auto wIt = this->m_Workspaces.find( wname );
63 if( wIt != this->m_Workspaces.end( ) )
64 return( wIt->second.GetPointer( ) );
69 // -------------------------------------------------------------------------
70 cpBaseQtApplication::Pipeline::Canvas*
71 cpBaseQtApplication::MainWindow::
74 return( this->m_Canvas );
77 // -------------------------------------------------------------------------
78 const cpBaseQtApplication::Pipeline::Canvas*
79 cpBaseQtApplication::MainWindow::
82 return( this->m_Canvas );
85 // -------------------------------------------------------------------------
86 void cpBaseQtApplication::MainWindow::
87 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
92 // -------------------------------------------------------------------------
93 cpBaseQtApplication::Plugins::Navigator*
94 cpBaseQtApplication::MainWindow::
97 return( this->m_Navigator );
100 // -------------------------------------------------------------------------
101 const cpBaseQtApplication::Plugins::Navigator*
102 cpBaseQtApplication::MainWindow::
105 return( this->m_Navigator );
108 // -------------------------------------------------------------------------
109 void cpBaseQtApplication::MainWindow::
110 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
112 this->m_Navigator = n;
115 // -------------------------------------------------------------------------
116 cpExtensions::QT::ActorsWidgetInterface*
117 cpBaseQtApplication::MainWindow::
120 return( this->m_Viewer );
123 // -------------------------------------------------------------------------
124 const cpExtensions::QT::ActorsWidgetInterface*
125 cpBaseQtApplication::MainWindow::
128 return( this->m_Viewer );
131 // -------------------------------------------------------------------------
132 void cpBaseQtApplication::MainWindow::
133 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
138 // -------------------------------------------------------------------------
139 void cpBaseQtApplication::MainWindow::
140 _loadPlugins( const std::string& filename )
144 this->m_Plugins->LoadPluginsFile( filename );
145 if( this->m_Navigator != NULL )
146 this->m_Navigator->Update( );
148 catch( std::exception& err )
150 QMessageBox::critical(
152 "Error loading plugins path",
159 // -------------------------------------------------------------------------
160 void cpBaseQtApplication::MainWindow::
163 QFileDialog dlg( this );
164 dlg.setFileMode( QFileDialog::ExistingFiles );
166 std::stringstream filter;
167 std::string suffix = std::string( cpPlugins_LIB_EXT );
168 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
169 dlg.setNameFilter( filter.str( ).c_str( ) );
170 dlg.setDefaultSuffix( suffix.c_str( ) );
171 if( !( dlg.exec( ) ) )
173 QStringList names = dlg.selectedFiles( );
174 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
175 this->_loadPlugins( qIt->toStdString( ) );
178 // -------------------------------------------------------------------------
179 void cpBaseQtApplication::MainWindow::
180 _loadPluginsFromPath( const std::string& path )
184 this->m_Plugins->LoadPluginsDirectory( path );
185 this->m_Plugins->SaveEnvironment( this->m_RunPath );
186 if( this->m_Navigator != NULL )
187 this->m_Navigator->Update( );
189 catch( std::exception& err )
191 QMessageBox::critical(
193 "Error loading plugins path",
200 // -------------------------------------------------------------------------
201 void cpBaseQtApplication::MainWindow::
202 _loadPluginsFromPath( )
204 QFileDialog d( this );
205 d.setFileMode( QFileDialog::DirectoryOnly );
208 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
211 // -------------------------------------------------------------------------
212 void cpBaseQtApplication::MainWindow::
215 this->m_Workspaces.clear( );
218 // -------------------------------------------------------------------------
219 void cpBaseQtApplication::MainWindow::
220 _addWorkspace( const std::string& name )
222 auto wIt = this->m_Workspaces.find( name );
223 if( wIt == this->m_Workspaces.end( ) )
225 if( this->m_SingleWorkspace )
226 this->m_Workspaces.clear( );
227 this->m_Workspaces[ name ] = TWorkspace::New( );
228 if( this->m_Canvas != NULL )
229 this->m_Canvas->setWorkspace( this->m_Workspaces[ name ] );
230 this->setWindowTitle( ( this->m_BaseWindowTitle + name ).c_str( ) );
235 // -------------------------------------------------------------------------
236 void cpBaseQtApplication::MainWindow::
241 QInputDialog::getText(
242 this, "Creating a new workspace...",
243 "New workspace name: ",
248 if( ok && !text.isEmpty( ) )
249 this->_addWorkspace( text.toStdString( ) );
252 // -------------------------------------------------------------------------
253 void cpBaseQtApplication::MainWindow::
254 _saveWorkspace( const std::string& wname, const std::string& fname )
256 auto wIt = this->m_Workspaces.find( wname );
257 if( wIt != this->m_Workspaces.end( ) )
261 wIt->second->Save( fname );
262 this->m_LastSaveFileName = fname;
264 catch( std::exception& err )
266 QMessageBox::critical(
268 QMessageBox::tr( "Error saving workspace" ),
269 QMessageBox::tr( err.what( ) )
275 QMessageBox::critical(
277 "Error saving workspace",
279 std::string( "Workspace \"" ) + wname +
280 std::string( "\" does not exist." )
285 // -------------------------------------------------------------------------
286 void cpBaseQtApplication::MainWindow::
287 _saveWorkspace( const std::string& wname, bool force )
289 auto wIt = this->m_Workspaces.find( wname );
290 if( wIt != this->m_Workspaces.end( ) )
292 if( this->m_LastSaveFileName == "" || force )
294 QFileDialog dlg( this );
295 dlg.setFileMode( QFileDialog::AnyFile );
296 dlg.setDirectory( "." );
297 dlg.setAcceptMode( QFileDialog::AcceptSave );
299 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
301 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
304 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
308 this->_saveWorkspace(
309 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
313 this->_saveWorkspace( wIt->first, this->m_LastSaveFileName );
316 QMessageBox::critical(
318 "Error saving workspace",
320 std::string( "Workspace \"" ) + wname +
321 std::string( "\" does not exist." )
326 // -------------------------------------------------------------------------
327 void cpBaseQtApplication::MainWindow::
331 auto wIt = this->m_Workspaces.begin( );
332 wIt != this->m_Workspaces.end( );
336 QFileDialog dlg( this );
337 dlg.setFileMode( QFileDialog::AnyFile );
338 dlg.setDirectory( "." );
339 dlg.setAcceptMode( QFileDialog::AcceptSave );
341 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
343 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
346 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
350 this->_saveWorkspace(
351 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
357 // -------------------------------------------------------------------------
358 void cpBaseQtApplication::MainWindow::
359 _loadWorkspace( const std::string& fname )
363 this->_addWorkspace( fname );
364 this->m_Workspaces[ fname ]->Load( fname );
365 if( this->m_Canvas != NULL )
366 this->m_Canvas->setWorkspace( this->m_Workspaces[ fname ] );
368 catch( std::exception& err )
370 QMessageBox::critical(
372 QMessageBox::tr( "Error loading workspace" ),
373 QMessageBox::tr( err.what( ) )
379 // -------------------------------------------------------------------------
380 void cpBaseQtApplication::MainWindow::
383 QFileDialog dlg( this );
384 dlg.setFileMode( QFileDialog::ExistingFile );
385 dlg.setDirectory( "." );
387 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
389 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
390 if( !( dlg.exec( ) ) )
392 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
395 // -------------------------------------------------------------------------
396 void cpBaseQtApplication::MainWindow::
400 dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
405 auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
406 dlg->setData( data );