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 )
84 // -------------------------------------------------------------------------
85 cpBaseQtApplication::Plugins::Navigator*
86 cpBaseQtApplication::MainWindow::
89 return( this->m_Navigator );
92 // -------------------------------------------------------------------------
93 const cpBaseQtApplication::Plugins::Navigator*
94 cpBaseQtApplication::MainWindow::
97 return( this->m_Navigator );
100 // -------------------------------------------------------------------------
101 void cpBaseQtApplication::MainWindow::
102 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
104 this->m_Navigator = n;
107 // -------------------------------------------------------------------------
108 cpExtensions::QT::ActorsWidgetInterface*
109 cpBaseQtApplication::MainWindow::
112 return( this->m_Viewer );
115 // -------------------------------------------------------------------------
116 const cpExtensions::QT::ActorsWidgetInterface*
117 cpBaseQtApplication::MainWindow::
120 return( this->m_Viewer );
123 // -------------------------------------------------------------------------
124 void cpBaseQtApplication::MainWindow::
125 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
128 if( this->m_Viewer != NULL )
130 auto interactors = this->m_Viewer->GetInteractors( );
131 for( auto i : interactors )
132 this->m_Workspace->AddInteractor( i );
137 // -------------------------------------------------------------------------
138 void cpBaseQtApplication::MainWindow::
139 _loadPlugins( const std::string& filename )
143 this->m_Plugins->LoadPluginsFile( filename );
144 if( this->m_Navigator != NULL )
145 this->m_Navigator->Update( );
147 catch( std::exception& err )
149 QMessageBox::critical(
151 "Error loading plugins path",
158 // -------------------------------------------------------------------------
159 void cpBaseQtApplication::MainWindow::
162 QFileDialog dlg( this );
163 dlg.setFileMode( QFileDialog::ExistingFiles );
165 std::stringstream filter;
166 std::string suffix = std::string( cpPlugins_LIB_EXT );
167 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
168 dlg.setNameFilter( filter.str( ).c_str( ) );
169 dlg.setDefaultSuffix( suffix.c_str( ) );
170 if( !( dlg.exec( ) ) )
172 QStringList names = dlg.selectedFiles( );
173 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
174 this->_loadPlugins( qIt->toStdString( ) );
177 // -------------------------------------------------------------------------
178 void cpBaseQtApplication::MainWindow::
179 _loadPluginsFromPath( const std::string& path )
183 this->m_Plugins->LoadPluginsDirectory( path );
184 this->m_Plugins->SaveEnvironment( this->m_RunPath );
185 if( this->m_Navigator != NULL )
186 this->m_Navigator->Update( );
188 catch( std::exception& err )
190 QMessageBox::critical(
192 "Error loading plugins path",
199 // -------------------------------------------------------------------------
200 void cpBaseQtApplication::MainWindow::
201 _loadPluginsFromPath( )
203 QFileDialog d( this );
204 d.setFileMode( QFileDialog::DirectoryOnly );
207 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
210 // -------------------------------------------------------------------------
211 void cpBaseQtApplication::MainWindow::
214 this->setWindowTitle( this->m_BaseWindowTitle.c_str( ) );
215 this->m_Workspace = TWorkspace::New( );
216 if( this->m_Canvas != NULL )
218 this->m_Canvas->clear( );
219 this->m_Canvas->setWorkspace( this->m_Workspace );
222 if( this->m_Viewer != NULL )
224 // TODO: this->m_Viewer->clear( );
225 auto interactors = this->m_Viewer->GetInteractors( );
226 for( auto i : interactors )
227 this->m_Workspace->AddInteractor( i );
232 // -------------------------------------------------------------------------
233 void cpBaseQtApplication::MainWindow::
234 _saveWorkspace( const std::string& fname )
236 this->m_LastSaveFileName = fname;
237 this->m_Workspace->Save( this->m_LastSaveFileName );
240 // -------------------------------------------------------------------------
241 void cpBaseQtApplication::MainWindow::
244 if( this->m_LastSaveFileName == "" )
246 QFileDialog dlg( this );
247 dlg.setFileMode( QFileDialog::AnyFile );
248 dlg.setDirectory( "." );
249 dlg.setAcceptMode( QFileDialog::AcceptSave );
251 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
253 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
254 dlg.setWindowTitle( "Saving workspace" );
256 this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
259 this->_saveWorkspace( this->m_LastSaveFileName );
262 // -------------------------------------------------------------------------
263 void cpBaseQtApplication::MainWindow::
264 _loadWorkspace( const std::string& fname )
268 this->_clearWorkspace( );
269 this->m_Workspace->Load( fname );
270 this->m_LastSaveFileName = "";
271 if( this->m_Canvas != NULL )
272 this->m_Canvas->setWorkspace( this->m_Workspace );
274 catch( std::exception& err )
276 QMessageBox::critical(
278 QMessageBox::tr( "Error loading workspace" ),
279 QMessageBox::tr( err.what( ) )
285 // -------------------------------------------------------------------------
286 void cpBaseQtApplication::MainWindow::
289 QFileDialog dlg( this );
290 dlg.setFileMode( QFileDialog::ExistingFile );
291 dlg.setDirectory( "." );
293 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
295 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
296 if( !( dlg.exec( ) ) )
298 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
301 // -------------------------------------------------------------------------
302 void cpBaseQtApplication::MainWindow::
306 dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
311 auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
312 dlg->setData( data );