]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
ac8858ed934da7028651f2cb0b9d7e29261a13fa
[cpPlugins.git] / lib / cpBaseQtApplication / MainWindow.cxx
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>
6
7 #include <QDir>
8 #include <QFileDialog>
9 #include <QInputDialog>
10
11 // -------------------------------------------------------------------------
12 cpBaseQtApplication::MainWindow::
13 MainWindow(
14   int argc, char* argv[],
15   QWidget* parent
16   )
17   : Superclass( parent ),
18     m_LastSaveFileName( "" ),
19     m_BaseWindowTitle( "cpBaseQtApplication" ),
20     m_Canvas( NULL ),
21     m_Navigator( NULL ),
22     m_Viewer( NULL )
23 {
24   this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
25   this->m_Plugins = TPlugins::New( );
26   try
27   {
28     this->m_Plugins->GuessEnvironment( this->m_RunPath );
29     this->m_Plugins->GuessPlugins( );
30   }
31   catch( std::exception& err )
32   {
33     QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
34
35   } // yrt
36   this->_clearWorkspace( );
37 }
38
39 // -------------------------------------------------------------------------
40 cpBaseQtApplication::MainWindow::
41 ~MainWindow( )
42 {
43 }
44
45 // -------------------------------------------------------------------------
46 cpBaseQtApplication::MainWindow::
47 TWorkspace* cpBaseQtApplication::MainWindow::
48 workspace( )
49 {
50   return( this->m_Workspace );
51 }
52
53 // -------------------------------------------------------------------------
54 const cpBaseQtApplication::MainWindow::
55 TWorkspace* cpBaseQtApplication::MainWindow::
56 workspace( ) const
57 {
58   return( this->m_Workspace );
59 }
60
61 // -------------------------------------------------------------------------
62 cpBaseQtApplication::Pipeline::Canvas*
63 cpBaseQtApplication::MainWindow::
64 canvas( )
65 {
66   return( this->m_Canvas );
67 }
68
69 // -------------------------------------------------------------------------
70 const cpBaseQtApplication::Pipeline::Canvas*
71 cpBaseQtApplication::MainWindow::
72 canvas( ) const
73 {
74   return( this->m_Canvas );
75 }
76
77 // -------------------------------------------------------------------------
78 void cpBaseQtApplication::MainWindow::
79 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
80 {
81   this->m_Canvas = c;
82 }
83
84 // -------------------------------------------------------------------------
85 cpBaseQtApplication::Plugins::Navigator*
86 cpBaseQtApplication::MainWindow::
87 navigator( )
88 {
89   return( this->m_Navigator );
90 }
91
92 // -------------------------------------------------------------------------
93 const cpBaseQtApplication::Plugins::Navigator*
94 cpBaseQtApplication::MainWindow::
95 navigator( ) const
96 {
97   return( this->m_Navigator );
98 }
99
100 // -------------------------------------------------------------------------
101 void cpBaseQtApplication::MainWindow::
102 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
103 {
104   this->m_Navigator = n;
105 }
106
107 // -------------------------------------------------------------------------
108 cpExtensions::QT::ActorsWidgetInterface*
109 cpBaseQtApplication::MainWindow::
110 viewer( )
111 {
112   return( this->m_Viewer );
113 }
114
115 // -------------------------------------------------------------------------
116 const cpExtensions::QT::ActorsWidgetInterface*
117 cpBaseQtApplication::MainWindow::
118 viewer( ) const
119 {
120   return( this->m_Viewer );
121 }
122
123 // -------------------------------------------------------------------------
124 void cpBaseQtApplication::MainWindow::
125 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
126 {
127   this->m_Viewer = v;
128   if( this->m_Viewer != NULL )
129   {
130     auto interactors = this->m_Viewer->GetInteractors( );
131     for( auto i : interactors )
132       this->m_Workspace->AddInteractor( i );
133
134   } // fi
135 }
136
137 // -------------------------------------------------------------------------
138 void cpBaseQtApplication::MainWindow::
139 _loadPlugins( const std::string& filename )
140 {
141   try
142   {
143     this->m_Plugins->LoadPluginsFile( filename );
144     if( this->m_Navigator != NULL )
145       this->m_Navigator->Update( );
146   }
147   catch( std::exception& err )
148   {
149     QMessageBox::critical(
150       this,
151       "Error loading plugins path",
152       err.what( )
153       );
154
155   } // yrt
156 }
157
158 // -------------------------------------------------------------------------
159 void cpBaseQtApplication::MainWindow::
160 _loadPlugins( )
161 {
162   QFileDialog dlg( this );
163   dlg.setFileMode( QFileDialog::ExistingFiles );
164
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( ) ) )
171     return;
172   QStringList names = dlg.selectedFiles( );
173   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
174     this->_loadPlugins( qIt->toStdString( ) );
175 }
176
177 // -------------------------------------------------------------------------
178 void cpBaseQtApplication::MainWindow::
179 _loadPluginsFromPath( const std::string& path )
180 {
181   try
182   {
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( );
187   }
188   catch( std::exception& err )
189   {
190     QMessageBox::critical(
191       this,
192       "Error loading plugins path",
193       err.what( )
194       );
195
196   } // yrt
197 }
198
199 // -------------------------------------------------------------------------
200 void cpBaseQtApplication::MainWindow::
201 _loadPluginsFromPath( )
202 {
203   QFileDialog d( this );
204   d.setFileMode( QFileDialog::DirectoryOnly );
205   if( !( d.exec( ) ) )
206     return;
207   this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
208 }
209
210 // -------------------------------------------------------------------------
211 void cpBaseQtApplication::MainWindow::
212 _clearWorkspace( )
213 {
214   this->setWindowTitle( this->m_BaseWindowTitle.c_str( ) );
215   this->m_Workspace = TWorkspace::New( );
216   if( this->m_Canvas != NULL )
217   {
218     this->m_Canvas->clear( );
219     this->m_Canvas->setWorkspace( this->m_Workspace );
220
221   } // fi
222   if( this->m_Viewer != NULL )
223   {
224     // TODO: this->m_Viewer->clear( );
225     auto interactors = this->m_Viewer->GetInteractors( );
226     for( auto i : interactors )
227       this->m_Workspace->AddInteractor( i );
228
229   } // fi
230 }
231
232 // -------------------------------------------------------------------------
233 void cpBaseQtApplication::MainWindow::
234 _saveWorkspace( const std::string& fname )
235 {
236   this->m_LastSaveFileName = fname;
237   this->m_Workspace->Save( this->m_LastSaveFileName );
238 }
239
240 // -------------------------------------------------------------------------
241 void cpBaseQtApplication::MainWindow::
242 _saveWorkspace( )
243 {
244   if( this->m_LastSaveFileName == "" )
245   {
246     QFileDialog dlg( this );
247     dlg.setFileMode( QFileDialog::AnyFile );
248     dlg.setDirectory( "." );
249     dlg.setAcceptMode( QFileDialog::AcceptSave );
250     dlg.setNameFilter(
251       QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
252       );
253     dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
254     dlg.setWindowTitle( "Saving workspace" );
255     if( dlg.exec( ) )
256       this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
257   }
258   else
259     this->_saveWorkspace( this->m_LastSaveFileName );
260 }
261
262 // -------------------------------------------------------------------------
263 void cpBaseQtApplication::MainWindow::
264 _loadWorkspace( const std::string& fname )
265 {
266   try
267   {
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 );
273   }
274   catch( std::exception& err )
275   {
276     QMessageBox::critical(
277       this,
278       QMessageBox::tr( "Error loading workspace" ),
279       QMessageBox::tr( err.what( ) )
280       );
281
282   } // yrt
283 }
284
285 // -------------------------------------------------------------------------
286 void cpBaseQtApplication::MainWindow::
287 _loadWorkspace( )
288 {
289   QFileDialog dlg( this );
290   dlg.setFileMode( QFileDialog::ExistingFile );
291   dlg.setDirectory( "." );
292   dlg.setNameFilter(
293     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
294     );
295   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
296   if( !( dlg.exec( ) ) )
297     return;
298   this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
299 }
300
301 // -------------------------------------------------------------------------
302 void cpBaseQtApplication::MainWindow::
303 _actorsProperties( )
304 {
305   auto data =
306     dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
307       this->m_Viewer
308       );
309   if( data != NULL )
310   {
311     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
312     dlg->setData( data );
313     dlg->exec( );
314
315   } // fi
316 }
317
318 // eof - $RCSfile$