1 #include <cpBaseQtApplication/MainWindow.h>
2 #include <cpBaseQtApplication/Plugins/Navigator.h>
6 #include <QInputDialog>
8 // -------------------------------------------------------------------------
9 cpBaseQtApplication::MainWindow::
11 int argc, char* argv[],
14 : Superclass( parent ),
15 m_LastSaveFileName( "" ),
20 this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
21 this->m_Plugins = TPlugins::New( );
24 this->m_Plugins->GuessEnvironment( this->m_RunPath );
25 this->m_Plugins->GuessPlugins( );
27 catch( std::exception& err )
29 QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
34 // -------------------------------------------------------------------------
35 cpBaseQtApplication::MainWindow::
40 // -------------------------------------------------------------------------
41 cpBaseQtApplication::MainWindow::
42 TWorkspace* cpBaseQtApplication::MainWindow::
43 workspace( const std::string& wname )
45 auto wIt = this->m_Workspaces.find( wname );
46 if( wIt != this->m_Workspaces.end( ) )
47 return( wIt->second.GetPointer( ) );
52 // -------------------------------------------------------------------------
53 const cpBaseQtApplication::MainWindow::
54 TWorkspace* cpBaseQtApplication::MainWindow::
55 workspace( const std::string& wname ) const
57 auto wIt = this->m_Workspaces.find( wname );
58 if( wIt != this->m_Workspaces.end( ) )
59 return( wIt->second.GetPointer( ) );
64 // -------------------------------------------------------------------------
65 cpBaseQtApplication::Pipeline::Canvas*
66 cpBaseQtApplication::MainWindow::
69 return( this->m_Canvas );
72 // -------------------------------------------------------------------------
73 const cpBaseQtApplication::Pipeline::Canvas*
74 cpBaseQtApplication::MainWindow::
77 return( this->m_Canvas );
80 // -------------------------------------------------------------------------
81 void cpBaseQtApplication::MainWindow::
82 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
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 )
133 // -------------------------------------------------------------------------
134 void cpBaseQtApplication::MainWindow::
135 _loadPlugins( const std::string& filename )
139 this->m_Plugins->LoadPluginsFile( filename );
140 if( this->m_Navigator != NULL )
141 this->m_Navigator->Update( );
143 catch( std::exception& err )
145 QMessageBox::critical(
147 "Error loading plugins path",
154 // -------------------------------------------------------------------------
155 void cpBaseQtApplication::MainWindow::
158 QFileDialog dlg( this );
159 dlg.setFileMode( QFileDialog::ExistingFiles );
161 std::stringstream filter;
162 std::string suffix = std::string( cpPlugins_LIB_EXT );
163 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
164 dlg.setNameFilter( filter.str( ).c_str( ) );
165 dlg.setDefaultSuffix( suffix.c_str( ) );
166 if( !( dlg.exec( ) ) )
168 QStringList names = dlg.selectedFiles( );
169 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
170 this->_loadPlugins( qIt->toStdString( ) );
173 // -------------------------------------------------------------------------
174 void cpBaseQtApplication::MainWindow::
175 _loadPluginsFromPath( const std::string& path )
179 this->m_Plugins->LoadPluginsDirectory( path );
180 this->m_Plugins->SaveEnvironment( this->m_RunPath );
181 if( this->m_Navigator != NULL )
182 this->m_Navigator->Update( );
184 catch( std::exception& err )
186 QMessageBox::critical(
188 "Error loading plugins path",
195 // -------------------------------------------------------------------------
196 void cpBaseQtApplication::MainWindow::
197 _loadPluginsFromPath( )
199 QFileDialog d( this );
200 d.setFileMode( QFileDialog::DirectoryOnly );
203 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
206 // -------------------------------------------------------------------------
207 void cpBaseQtApplication::MainWindow::
210 this->m_Workspaces.clear( );
213 // -------------------------------------------------------------------------
214 void cpBaseQtApplication::MainWindow::
215 _addWorkspace( const std::string& name )
217 auto wIt = this->m_Workspaces.find( name );
218 if( wIt == this->m_Workspaces.end( ) )
219 this->m_Workspaces[ name ] = TWorkspace::New( );
222 // -------------------------------------------------------------------------
223 void cpBaseQtApplication::MainWindow::
228 QInputDialog::getText(
229 this, "Creating a new workspace...",
230 "New workspace name: ",
235 if( ok && !text.isEmpty( ) )
236 this->_addWorkspace( text.toStdString( ) );
239 // -------------------------------------------------------------------------
240 void cpBaseQtApplication::MainWindow::
241 _saveWorkspace( const std::string& wname, const std::string& fname )
243 auto wIt = this->m_Workspaces.find( wname );
244 if( wIt != this->m_Workspaces.end( ) )
248 wIt->second->Save( fname );
249 this->m_LastSaveFileName = fname;
251 catch( std::exception& err )
253 QMessageBox::critical(
255 QMessageBox::tr( "Error saving workspace" ),
256 QMessageBox::tr( err.what( ) )
262 QMessageBox::critical(
264 "Error saving workspace",
266 std::string( "Workspace \"" ) + wname +
267 std::string( "\" does not exist." )
272 // -------------------------------------------------------------------------
273 void cpBaseQtApplication::MainWindow::
274 _saveWorkspace( const std::string& wname, bool force )
276 auto wIt = this->m_Workspaces.find( wname );
277 if( wIt != this->m_Workspaces.end( ) )
279 if( this->m_LastSaveFileName == "" || force )
281 QFileDialog dlg( this );
282 dlg.setFileMode( QFileDialog::AnyFile );
283 dlg.setDirectory( "." );
284 dlg.setAcceptMode( QFileDialog::AcceptSave );
286 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
288 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
291 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
295 this->_saveWorkspace(
296 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
300 this->_saveWorkspace( wIt->first, this->m_LastSaveFileName );
303 QMessageBox::critical(
305 "Error saving workspace",
307 std::string( "Workspace \"" ) + wname +
308 std::string( "\" does not exist." )
313 // -------------------------------------------------------------------------
314 void cpBaseQtApplication::MainWindow::
318 auto wIt = this->m_Workspaces.begin( );
319 wIt != this->m_Workspaces.end( );
323 QFileDialog dlg( this );
324 dlg.setFileMode( QFileDialog::AnyFile );
325 dlg.setDirectory( "." );
326 dlg.setAcceptMode( QFileDialog::AcceptSave );
328 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
330 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
333 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
337 this->_saveWorkspace(
338 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
344 // -------------------------------------------------------------------------
345 void cpBaseQtApplication::MainWindow::
346 _loadWorkspace( const std::string& fname )
350 this->_addWorkspace( fname );
351 this->m_Workspaces[ fname ]->Load( fname );
353 catch( std::exception& err )
355 QMessageBox::critical(
357 QMessageBox::tr( "Error loading workspace" ),
358 QMessageBox::tr( err.what( ) )
364 // -------------------------------------------------------------------------
365 void cpBaseQtApplication::MainWindow::
368 QFileDialog dlg( this );
369 dlg.setFileMode( QFileDialog::ExistingFile );
370 dlg.setDirectory( "." );
372 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
374 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
375 if( !( dlg.exec( ) ) )
377 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );