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 )
136 if( this->m_Viewer != NULL )
138 auto interactors = this->m_Viewer->GetInteractors( );
139 for( auto wIt : this->m_Workspaces )
140 for( auto i : interactors )
141 wIt.second->AddInteractor( i );
146 // -------------------------------------------------------------------------
147 void cpBaseQtApplication::MainWindow::
148 _loadPlugins( const std::string& filename )
152 this->m_Plugins->LoadPluginsFile( filename );
153 if( this->m_Navigator != NULL )
154 this->m_Navigator->Update( );
156 catch( std::exception& err )
158 QMessageBox::critical(
160 "Error loading plugins path",
167 // -------------------------------------------------------------------------
168 void cpBaseQtApplication::MainWindow::
171 QFileDialog dlg( this );
172 dlg.setFileMode( QFileDialog::ExistingFiles );
174 std::stringstream filter;
175 std::string suffix = std::string( cpPlugins_LIB_EXT );
176 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
177 dlg.setNameFilter( filter.str( ).c_str( ) );
178 dlg.setDefaultSuffix( suffix.c_str( ) );
179 if( !( dlg.exec( ) ) )
181 QStringList names = dlg.selectedFiles( );
182 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
183 this->_loadPlugins( qIt->toStdString( ) );
186 // -------------------------------------------------------------------------
187 void cpBaseQtApplication::MainWindow::
188 _loadPluginsFromPath( const std::string& path )
192 this->m_Plugins->LoadPluginsDirectory( path );
193 this->m_Plugins->SaveEnvironment( this->m_RunPath );
194 if( this->m_Navigator != NULL )
195 this->m_Navigator->Update( );
197 catch( std::exception& err )
199 QMessageBox::critical(
201 "Error loading plugins path",
208 // -------------------------------------------------------------------------
209 void cpBaseQtApplication::MainWindow::
210 _loadPluginsFromPath( )
212 QFileDialog d( this );
213 d.setFileMode( QFileDialog::DirectoryOnly );
216 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
219 // -------------------------------------------------------------------------
220 void cpBaseQtApplication::MainWindow::
223 this->m_Workspaces.clear( );
226 // -------------------------------------------------------------------------
227 void cpBaseQtApplication::MainWindow::
228 _addWorkspace( const std::string& name )
230 auto wIt = this->m_Workspaces.find( name );
231 if( wIt == this->m_Workspaces.end( ) )
233 if( this->m_SingleWorkspace )
234 this->m_Workspaces.clear( );
235 this->m_Workspaces[ name ] = TWorkspace::New( );
236 if( this->m_Canvas != NULL )
237 this->m_Canvas->setWorkspace( this->m_Workspaces[ name ] );
238 if( this->m_Viewer != NULL )
240 auto interactors = this->m_Viewer->GetInteractors( );
241 auto wIt = this->m_Workspaces.find( name );
242 for( auto i : interactors )
243 wIt->second->AddInteractor( i );
246 this->setWindowTitle( ( this->m_BaseWindowTitle + name ).c_str( ) );
251 // -------------------------------------------------------------------------
252 void cpBaseQtApplication::MainWindow::
257 QInputDialog::getText(
258 this, "Creating a new workspace...",
259 "New workspace name: ",
264 if( ok && !text.isEmpty( ) )
265 this->_addWorkspace( text.toStdString( ) );
268 // -------------------------------------------------------------------------
269 void cpBaseQtApplication::MainWindow::
270 _saveWorkspace( const std::string& wname, const std::string& fname )
272 auto wIt = this->m_Workspaces.find( wname );
273 if( wIt != this->m_Workspaces.end( ) )
277 wIt->second->Save( fname );
278 this->m_LastSaveFileName = fname;
280 catch( std::exception& err )
282 QMessageBox::critical(
284 QMessageBox::tr( "Error saving workspace" ),
285 QMessageBox::tr( err.what( ) )
291 QMessageBox::critical(
293 "Error saving workspace",
295 std::string( "Workspace \"" ) + wname +
296 std::string( "\" does not exist." )
301 // -------------------------------------------------------------------------
302 void cpBaseQtApplication::MainWindow::
303 _saveWorkspace( const std::string& wname, bool force )
305 auto wIt = this->m_Workspaces.find( wname );
306 if( wIt != this->m_Workspaces.end( ) )
308 if( this->m_LastSaveFileName == "" || force )
310 QFileDialog dlg( this );
311 dlg.setFileMode( QFileDialog::AnyFile );
312 dlg.setDirectory( "." );
313 dlg.setAcceptMode( QFileDialog::AcceptSave );
315 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
317 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
320 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
324 this->_saveWorkspace(
325 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
329 this->_saveWorkspace( wIt->first, this->m_LastSaveFileName );
332 QMessageBox::critical(
334 "Error saving workspace",
336 std::string( "Workspace \"" ) + wname +
337 std::string( "\" does not exist." )
342 // -------------------------------------------------------------------------
343 void cpBaseQtApplication::MainWindow::
347 auto wIt = this->m_Workspaces.begin( );
348 wIt != this->m_Workspaces.end( );
352 QFileDialog dlg( this );
353 dlg.setFileMode( QFileDialog::AnyFile );
354 dlg.setDirectory( "." );
355 dlg.setAcceptMode( QFileDialog::AcceptSave );
357 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
359 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
362 std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
366 this->_saveWorkspace(
367 wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
373 // -------------------------------------------------------------------------
374 void cpBaseQtApplication::MainWindow::
375 _loadWorkspace( const std::string& fname )
379 this->_addWorkspace( fname );
380 this->m_Workspaces[ fname ]->Load( fname );
381 if( this->m_Canvas != NULL )
382 this->m_Canvas->setWorkspace( this->m_Workspaces[ fname ] );
384 catch( std::exception& err )
386 QMessageBox::critical(
388 QMessageBox::tr( "Error loading workspace" ),
389 QMessageBox::tr( err.what( ) )
395 // -------------------------------------------------------------------------
396 void cpBaseQtApplication::MainWindow::
399 QFileDialog dlg( this );
400 dlg.setFileMode( QFileDialog::ExistingFile );
401 dlg.setDirectory( "." );
403 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
405 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
406 if( !( dlg.exec( ) ) )
408 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
411 // -------------------------------------------------------------------------
412 void cpBaseQtApplication::MainWindow::
416 dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
421 auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
422 dlg->setData( data );