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::
13 MainWindow( int argc, char* argv[], QWidget* parent )
14 : Superclass( parent ),
15 m_LastSaveFileName( "" ),
16 m_BaseWindowTitle( "cpBaseQtApplication" ),
21 this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
22 this->m_Loader.GuessEnvironment( this->m_RunPath );
23 this->m_Loader.SaveEnvironment( this->m_RunPath );
24 this->_clearWorkspace( );
27 // -------------------------------------------------------------------------
28 cpBaseQtApplication::MainWindow::
33 // -------------------------------------------------------------------------
34 cpBaseQtApplication::MainWindow::
35 TWorkspace* cpBaseQtApplication::MainWindow::
38 return( this->m_Workspace );
41 // -------------------------------------------------------------------------
42 const cpBaseQtApplication::MainWindow::
43 TWorkspace* cpBaseQtApplication::MainWindow::
46 return( this->m_Workspace );
49 // -------------------------------------------------------------------------
50 cpBaseQtApplication::Pipeline::Canvas*
51 cpBaseQtApplication::MainWindow::
54 return( this->m_Canvas );
57 // -------------------------------------------------------------------------
58 const cpBaseQtApplication::Pipeline::Canvas*
59 cpBaseQtApplication::MainWindow::
62 return( this->m_Canvas );
65 // -------------------------------------------------------------------------
66 void cpBaseQtApplication::MainWindow::
67 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
70 if( this->m_Canvas != NULL )
71 this->m_Canvas->setWorkspace( this->m_Workspace );
75 // -------------------------------------------------------------------------
76 cpBaseQtApplication::Plugins::Navigator*
77 cpBaseQtApplication::MainWindow::
80 return( this->m_Navigator );
83 // -------------------------------------------------------------------------
84 const cpBaseQtApplication::Plugins::Navigator*
85 cpBaseQtApplication::MainWindow::
88 return( this->m_Navigator );
91 // -------------------------------------------------------------------------
92 void cpBaseQtApplication::MainWindow::
93 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
95 this->m_Navigator = n;
98 // -------------------------------------------------------------------------
99 cpExtensions::QT::ActorsWidgetInterface*
100 cpBaseQtApplication::MainWindow::
103 return( this->m_Viewer );
106 // -------------------------------------------------------------------------
107 const cpExtensions::QT::ActorsWidgetInterface*
108 cpBaseQtApplication::MainWindow::
111 return( this->m_Viewer );
114 // -------------------------------------------------------------------------
115 void cpBaseQtApplication::MainWindow::
116 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
119 if( this->m_Viewer != NULL )
121 auto interactors = this->m_Viewer->GetInteractors( );
122 for( auto i : interactors )
123 this->m_Workspace->AddInteractor( i );
128 // -------------------------------------------------------------------------
129 void cpBaseQtApplication::MainWindow::
130 _loadPlugins( const std::string& filename )
134 this->m_Loader.Register( filename );
135 if( this->m_Navigator != NULL )
136 this->m_Navigator->Update( );
138 catch( std::exception& err )
140 QMessageBox::critical(
142 "Error loading plugins path",
149 // -------------------------------------------------------------------------
150 void cpBaseQtApplication::MainWindow::
153 QFileDialog dlg( this );
154 dlg.setFileMode( QFileDialog::ExistingFiles );
156 std::stringstream filter;
157 std::string suffix = std::string( cpPlugins_LIB_EXT );
158 filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
159 dlg.setNameFilter( filter.str( ).c_str( ) );
160 dlg.setDefaultSuffix( suffix.c_str( ) );
161 if( !( dlg.exec( ) ) )
163 QStringList names = dlg.selectedFiles( );
164 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
165 this->_loadPlugins( qIt->toStdString( ) );
168 // -------------------------------------------------------------------------
169 void cpBaseQtApplication::MainWindow::
170 _loadPluginsFromPath( const std::string& path )
174 this->m_Loader.RegisterFromDirectory( path );
175 this->m_Loader.SaveEnvironment( this->m_RunPath );
176 if( this->m_Navigator != NULL )
177 this->m_Navigator->Update( );
179 catch( std::exception& err )
181 QMessageBox::critical(
183 "Error loading plugins path",
190 // -------------------------------------------------------------------------
191 void cpBaseQtApplication::MainWindow::
192 _loadPluginsFromPath( )
194 QFileDialog d( this );
195 d.setFileMode( QFileDialog::DirectoryOnly );
198 this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
201 // -------------------------------------------------------------------------
202 void cpBaseQtApplication::MainWindow::
205 this->setWindowTitle( this->m_BaseWindowTitle.c_str( ) );
206 this->m_Workspace = TWorkspace::New( );
207 if( this->m_Canvas != NULL )
209 this->m_Canvas->clear( );
210 this->m_Canvas->setWorkspace( this->m_Workspace );
213 if( this->m_Viewer != NULL )
215 // TODO: this->m_Viewer->clear( );
216 auto interactors = this->m_Viewer->GetInteractors( );
217 for( auto i : interactors )
218 this->m_Workspace->AddInteractor( i );
223 // -------------------------------------------------------------------------
224 void cpBaseQtApplication::MainWindow::
225 _saveWorkspace( const std::string& fname )
227 this->m_LastSaveFileName = fname;
228 this->m_Workspace->Save( this->m_LastSaveFileName );
231 // -------------------------------------------------------------------------
232 void cpBaseQtApplication::MainWindow::
235 if( this->m_LastSaveFileName == "" )
237 QFileDialog dlg( this );
238 dlg.setFileMode( QFileDialog::AnyFile );
239 dlg.setDirectory( "." );
240 dlg.setAcceptMode( QFileDialog::AcceptSave );
242 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
244 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
245 dlg.setWindowTitle( "Saving workspace" );
247 this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
250 this->_saveWorkspace( this->m_LastSaveFileName );
253 // -------------------------------------------------------------------------
254 void cpBaseQtApplication::MainWindow::
255 _loadWorkspace( const std::string& fname )
259 this->_clearWorkspace( );
260 this->m_Workspace->Load( fname );
261 this->m_LastSaveFileName = "";
262 if( this->m_Canvas != NULL )
263 this->m_Canvas->setWorkspace( this->m_Workspace );
265 catch( std::exception& err )
267 QMessageBox::critical(
269 QMessageBox::tr( "Error loading workspace" ),
270 QMessageBox::tr( err.what( ) )
276 // -------------------------------------------------------------------------
277 void cpBaseQtApplication::MainWindow::
280 QFileDialog dlg( this );
281 dlg.setFileMode( QFileDialog::ExistingFile );
282 dlg.setDirectory( "." );
284 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
286 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
287 if( !( dlg.exec( ) ) )
289 this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
292 // -------------------------------------------------------------------------
293 void cpBaseQtApplication::MainWindow::
297 dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
302 auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
303 dlg->setData( data );