1 #include <cpBaseQtApplication/MainWindow.h>
2 #include <cpBaseQtApplication/Plugins/Navigator.h>
3 #include <cpBaseQtApplication/Pipeline/Canvas.h>
7 #include <QInputDialog>
9 // -------------------------------------------------------------------------
10 cpBaseQtApplication::MainWindow::
12 int argc, char* argv[],
15 : Superclass( parent ),
16 m_LastSaveFileName( "" ),
17 m_SingleWorkspace( false ),
18 m_BaseWindowTitle( "cpBaseQtApplication" ),
23 this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
24 this->m_Plugins = TPlugins::New( );
27 this->m_Plugins->GuessEnvironment( this->m_RunPath );
28 this->m_Plugins->GuessPlugins( );
30 catch( std::exception& err )
32 QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
37 // -------------------------------------------------------------------------
38 cpBaseQtApplication::MainWindow::
43 // -------------------------------------------------------------------------
44 cpBaseQtApplication::MainWindow::
45 TWorkspace* cpBaseQtApplication::MainWindow::
46 workspace( const std::string& wname )
48 auto wIt = this->m_Workspaces.find( wname );
49 if( wIt != this->m_Workspaces.end( ) )
50 return( wIt->second.GetPointer( ) );
55 // -------------------------------------------------------------------------
56 const cpBaseQtApplication::MainWindow::
57 TWorkspace* cpBaseQtApplication::MainWindow::
58 workspace( const std::string& wname ) const
60 auto wIt = this->m_Workspaces.find( wname );
61 if( wIt != this->m_Workspaces.end( ) )
62 return( wIt->second.GetPointer( ) );
67 // -------------------------------------------------------------------------
68 cpBaseQtApplication::Pipeline::Canvas*
69 cpBaseQtApplication::MainWindow::
72 return( this->m_Canvas );
75 // -------------------------------------------------------------------------
76 const cpBaseQtApplication::Pipeline::Canvas*
77 cpBaseQtApplication::MainWindow::
80 return( this->m_Canvas );
83 // -------------------------------------------------------------------------
84 void cpBaseQtApplication::MainWindow::
85 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
90 // -------------------------------------------------------------------------
91 cpBaseQtApplication::Plugins::Navigator*
92 cpBaseQtApplication::MainWindow::
95 return( this->m_Navigator );
98 // -------------------------------------------------------------------------
99 const cpBaseQtApplication::Plugins::Navigator*
100 cpBaseQtApplication::MainWindow::
103 return( this->m_Navigator );
106 // -------------------------------------------------------------------------
107 void cpBaseQtApplication::MainWindow::
108 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
110 this->m_Navigator = n;
113 // -------------------------------------------------------------------------
114 cpExtensions::QT::ActorsWidgetInterface*
115 cpBaseQtApplication::MainWindow::
118 return( this->m_Viewer );
121 // -------------------------------------------------------------------------
122 const cpExtensions::QT::ActorsWidgetInterface*
123 cpBaseQtApplication::MainWindow::
126 return( this->m_Viewer );
129 // -------------------------------------------------------------------------
130 void cpBaseQtApplication::MainWindow::
131 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
136 // -------------------------------------------------------------------------
137 void cpBaseQtApplication::MainWindow::
138 _loadPlugins( const std::string& filename )
142 this->m_Plugins->LoadPluginsFile( filename );
143 if( this->m_Navigator != NULL )
144 this->m_Navigator->Update( );
146 catch( std::exception& err )
148 QMessageBox::critical(
150 "Error loading plugins path",
157 // -------------------------------------------------------------------------
158 void cpBaseQtApplication::MainWindow::
161 QFileDialog dlg( this );
162 dlg.setFileMode( QFileDialog::ExistingFiles );
164 std::stringstream filter;
165 std::string suffix = std::string( cpPlugins_LIB_EXT );
166 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
167 dlg.setNameFilter( filter.str( ).c_str( ) );
168 dlg.setDefaultSuffix( suffix.c_str( ) );
169 if( !( dlg.exec( ) ) )
171 QStringList names = dlg.selectedFiles( );
172 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
173 this->_loadPlugins( qIt->toStdString( ) );
176 // -------------------------------------------------------------------------
177 void cpBaseQtApplication::MainWindow::
178 _loadPluginsFromPath( const std::string& path )
182 this->m_Plugins->LoadPluginsDirectory( path );
183 this->m_Plugins->SaveEnvironment( this->m_RunPath );
184 if( this->m_Navigator != NULL )
185 this->m_Navigator->Update( );
187 catch( std::exception& err )
189 QMessageBox::critical(
191 "Error loading plugins path",
198 // -------------------------------------------------------------------------
199 void cpBaseQtApplication::MainWindow::
200 _loadPluginsFromPath( )
202 QFileDialog d( this );
203 d.setFileMode( QFileDialog::DirectoryOnly );
206 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
209 // -------------------------------------------------------------------------
210 void cpBaseQtApplication::MainWindow::
213 this->m_Workspaces.clear( );
216 // -------------------------------------------------------------------------
217 void cpBaseQtApplication::MainWindow::
218 _addWorkspace( const std::string& name )
220 auto wIt = this->m_Workspaces.find( name );
221 if( wIt == this->m_Workspaces.end( ) )
223 if( this->m_SingleWorkspace )
224 this->m_Workspaces.clear( );
225 this->m_Workspaces[ name ] = TWorkspace::New( );
226 this->setWindowTitle( ( this->m_BaseWindowTitle + name ).c_str( ) );
231 // -------------------------------------------------------------------------
232 void cpBaseQtApplication::MainWindow::
237 QInputDialog::getText(
238 this, "Creating a new workspace...",
239 "New workspace name: ",
244 if( ok && !text.isEmpty( ) )
245 this->_addWorkspace( text.toStdString( ) );
248 // -------------------------------------------------------------------------
249 void cpBaseQtApplication::MainWindow::
250 _saveWorkspace( const std::string& wname, const std::string& fname )
252 auto wIt = this->m_Workspaces.find( wname );
253 if( wIt != this->m_Workspaces.end( ) )
257 wIt->second->Save( fname );
258 this->m_LastSaveFileName = fname;
260 catch( std::exception& err )
262 QMessageBox::critical(
264 QMessageBox::tr( "Error saving workspace" ),
265 QMessageBox::tr( err.what( ) )
271 QMessageBox::critical(
273 "Error saving workspace",
275 std::string( "Workspace \"" ) + wname +
276 std::string( "\" does not exist." )
281 // -------------------------------------------------------------------------
282 void cpBaseQtApplication::MainWindow::
283 _saveWorkspace( const std::string& wname, bool force )
285 auto wIt = this->m_Workspaces.find( wname );
286 if( wIt != this->m_Workspaces.end( ) )
288 if( this->m_LastSaveFileName == "" || force )
290 QFileDialog dlg( this );
291 dlg.setFileMode( QFileDialog::AnyFile );
292 dlg.setDirectory( "." );
293 dlg.setAcceptMode( QFileDialog::AcceptSave );
295 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
297 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
300 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
304 this->_saveWorkspace(
305 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
309 this->_saveWorkspace( wIt->first, this->m_LastSaveFileName );
312 QMessageBox::critical(
314 "Error saving workspace",
316 std::string( "Workspace \"" ) + wname +
317 std::string( "\" does not exist." )
322 // -------------------------------------------------------------------------
323 void cpBaseQtApplication::MainWindow::
327 auto wIt = this->m_Workspaces.begin( );
328 wIt != this->m_Workspaces.end( );
332 QFileDialog dlg( this );
333 dlg.setFileMode( QFileDialog::AnyFile );
334 dlg.setDirectory( "." );
335 dlg.setAcceptMode( QFileDialog::AcceptSave );
337 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
339 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
342 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
346 this->_saveWorkspace(
347 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
353 // -------------------------------------------------------------------------
354 void cpBaseQtApplication::MainWindow::
355 _loadWorkspace( const std::string& fname )
359 this->_addWorkspace( fname );
360 this->m_Workspaces[ fname ]->Load( fname );
361 if( this->m_Canvas != NULL )
362 this->m_Canvas->setWorkspace( this->m_Workspaces[ fname ] );
364 catch( std::exception& err )
366 QMessageBox::critical(
368 QMessageBox::tr( "Error loading workspace" ),
369 QMessageBox::tr( err.what( ) )
375 // -------------------------------------------------------------------------
376 void cpBaseQtApplication::MainWindow::
379 QFileDialog dlg( this );
380 dlg.setFileMode( QFileDialog::ExistingFile );
381 dlg.setDirectory( "." );
383 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
385 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
386 if( !( dlg.exec( ) ) )
388 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );