]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
9b16236acb94760f09af7f12bf3fb1f8d7e8c6ad
[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_SingleWorkspace( false ),
20     m_BaseWindowTitle( "cpBaseQtApplication" ),
21     m_Canvas( NULL ),
22     m_Navigator( NULL ),
23     m_Viewer( NULL )
24 {
25   this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
26   this->m_Plugins = TPlugins::New( );
27   try
28   {
29     this->m_Plugins->GuessEnvironment( this->m_RunPath );
30     this->m_Plugins->GuessPlugins( );
31   }
32   catch( std::exception& err )
33   {
34     QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
35
36   } // yrt
37 }
38
39 // -------------------------------------------------------------------------
40 cpBaseQtApplication::MainWindow::
41 ~MainWindow( )
42 {
43 }
44
45 // -------------------------------------------------------------------------
46 cpBaseQtApplication::MainWindow::
47 TWorkspace* cpBaseQtApplication::MainWindow::
48 workspace( const std::string& wname )
49 {
50   auto wIt = this->m_Workspaces.find( wname );
51   if( wIt != this->m_Workspaces.end( ) )
52     return( wIt->second.GetPointer( ) );
53   else
54     return( NULL );
55 }
56
57 // -------------------------------------------------------------------------
58 const cpBaseQtApplication::MainWindow::
59 TWorkspace* cpBaseQtApplication::MainWindow::
60 workspace( const std::string& wname ) const
61 {
62   auto wIt = this->m_Workspaces.find( wname );
63   if( wIt != this->m_Workspaces.end( ) )
64     return( wIt->second.GetPointer( ) );
65   else
66     return( NULL );
67 }
68
69 // -------------------------------------------------------------------------
70 cpBaseQtApplication::Pipeline::Canvas*
71 cpBaseQtApplication::MainWindow::
72 canvas( )
73 {
74   return( this->m_Canvas );
75 }
76
77 // -------------------------------------------------------------------------
78 const cpBaseQtApplication::Pipeline::Canvas*
79 cpBaseQtApplication::MainWindow::
80 canvas( ) const
81 {
82   return( this->m_Canvas );
83 }
84
85 // -------------------------------------------------------------------------
86 void cpBaseQtApplication::MainWindow::
87 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
88 {
89   this->m_Canvas = c;
90 }
91
92 // -------------------------------------------------------------------------
93 cpBaseQtApplication::Plugins::Navigator*
94 cpBaseQtApplication::MainWindow::
95 navigator( )
96 {
97   return( this->m_Navigator );
98 }
99
100 // -------------------------------------------------------------------------
101 const cpBaseQtApplication::Plugins::Navigator*
102 cpBaseQtApplication::MainWindow::
103 navigator( ) const
104 {
105   return( this->m_Navigator );
106 }
107
108 // -------------------------------------------------------------------------
109 void cpBaseQtApplication::MainWindow::
110 setNavigator( cpBaseQtApplication::Plugins::Navigator* n )
111 {
112   this->m_Navigator = n;
113 }
114
115 // -------------------------------------------------------------------------
116 cpExtensions::QT::ActorsWidgetInterface*
117 cpBaseQtApplication::MainWindow::
118 viewer( )
119 {
120   return( this->m_Viewer );
121 }
122
123 // -------------------------------------------------------------------------
124 const cpExtensions::QT::ActorsWidgetInterface*
125 cpBaseQtApplication::MainWindow::
126 viewer( ) const
127 {
128   return( this->m_Viewer );
129 }
130
131 // -------------------------------------------------------------------------
132 void cpBaseQtApplication::MainWindow::
133 setViewer( cpExtensions::QT::ActorsWidgetInterface* v )
134 {
135   this->m_Viewer = v;
136   if( this->m_Viewer != NULL )
137   {
138     auto interactors = this->m_Viewer->GetInteractors( );
139     for( auto wIt : this->m_Workspaces )
140       for( auto i : interactors )
141         wIt.second->AddInteractor( i );
142
143   } // fi
144 }
145
146 // -------------------------------------------------------------------------
147 void cpBaseQtApplication::MainWindow::
148 _loadPlugins( const std::string& filename )
149 {
150   try
151   {
152     this->m_Plugins->LoadPluginsFile( filename );
153     if( this->m_Navigator != NULL )
154       this->m_Navigator->Update( );
155   }
156   catch( std::exception& err )
157   {
158     QMessageBox::critical(
159       this,
160       "Error loading plugins path",
161       err.what( )
162       );
163
164   } // yrt
165 }
166
167 // -------------------------------------------------------------------------
168 void cpBaseQtApplication::MainWindow::
169 _loadPlugins( )
170 {
171   QFileDialog dlg( this );
172   dlg.setFileMode( QFileDialog::ExistingFiles );
173
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( ) ) )
180     return;
181   QStringList names = dlg.selectedFiles( );
182   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
183     this->_loadPlugins( qIt->toStdString( ) );
184 }
185
186 // -------------------------------------------------------------------------
187 void cpBaseQtApplication::MainWindow::
188 _loadPluginsFromPath( const std::string& path )
189 {
190   try
191   {
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( );
196   }
197   catch( std::exception& err )
198   {
199     QMessageBox::critical(
200       this,
201       "Error loading plugins path",
202       err.what( )
203       );
204
205   } // yrt
206 }
207
208 // -------------------------------------------------------------------------
209 void cpBaseQtApplication::MainWindow::
210 _loadPluginsFromPath( )
211 {
212   QFileDialog d( this );
213   d.setFileMode( QFileDialog::DirectoryOnly );
214   if( !( d.exec( ) ) )
215     return;
216   this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
217 }
218
219 // -------------------------------------------------------------------------
220 void cpBaseQtApplication::MainWindow::
221 _clearWorkspaces( )
222 {
223   this->m_Workspaces.clear( );
224 }
225
226 // -------------------------------------------------------------------------
227 void cpBaseQtApplication::MainWindow::
228 _addWorkspace( const std::string& name )
229 {
230   auto wIt = this->m_Workspaces.find( name );
231   if( wIt == this->m_Workspaces.end( ) )
232   {
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 )
239     {
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 );
244
245     } // fi
246     this->setWindowTitle( ( this->m_BaseWindowTitle + name ).c_str( ) );
247
248   } // fi
249 }
250
251 // -------------------------------------------------------------------------
252 void cpBaseQtApplication::MainWindow::
253 _addWorkspace( )
254 {
255   bool ok;
256   QString text =
257     QInputDialog::getText(
258       this, "Creating a new workspace...",
259       "New workspace name: ",
260       QLineEdit::Normal,
261       "new_workspace",
262       &ok
263       );
264   if( ok && !text.isEmpty( ) )
265     this->_addWorkspace( text.toStdString( ) );
266 }
267
268 // -------------------------------------------------------------------------
269 void cpBaseQtApplication::MainWindow::
270 _saveWorkspace( const std::string& wname, const std::string& fname )
271 {
272   auto wIt = this->m_Workspaces.find( wname );
273   if( wIt != this->m_Workspaces.end( ) )
274   {
275     try
276     {
277       wIt->second->Save( fname );
278       this->m_LastSaveFileName = fname;
279     }
280     catch( std::exception& err )
281     {
282       QMessageBox::critical(
283         this,
284         QMessageBox::tr( "Error saving workspace" ),
285         QMessageBox::tr( err.what( ) )
286         );
287
288     } // yrt
289   }
290   else
291     QMessageBox::critical(
292       this,
293       "Error saving workspace",
294       (
295         std::string( "Workspace \"" ) + wname +
296         std::string( "\" does not exist." )
297         ).c_str( )
298       );
299 }
300
301 // -------------------------------------------------------------------------
302 void cpBaseQtApplication::MainWindow::
303 _saveWorkspace( const std::string& wname, bool force )
304 {
305   auto wIt = this->m_Workspaces.find( wname );
306   if( wIt != this->m_Workspaces.end( ) )
307   {
308     if( this->m_LastSaveFileName == "" || force )
309     {
310       QFileDialog dlg( this );
311       dlg.setFileMode( QFileDialog::AnyFile );
312       dlg.setDirectory( "." );
313       dlg.setAcceptMode( QFileDialog::AcceptSave );
314       dlg.setNameFilter(
315         QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
316         );
317       dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
318       dlg.setWindowTitle(
319         (
320           std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
321           ).c_str( )
322         );
323       if( dlg.exec( ) )
324         this->_saveWorkspace(
325           wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
326           );
327     }
328     else
329       this->_saveWorkspace( wIt->first, this->m_LastSaveFileName );
330   }
331   else
332     QMessageBox::critical(
333       this,
334       "Error saving workspace",
335       (
336         std::string( "Workspace \"" ) + wname +
337         std::string( "\" does not exist." )
338         ).c_str( )
339       );
340 }
341
342 // -------------------------------------------------------------------------
343 void cpBaseQtApplication::MainWindow::
344 _saveWorkspace( )
345 {
346   for(
347     auto wIt = this->m_Workspaces.begin( );
348     wIt != this->m_Workspaces.end( );
349     ++wIt
350     )
351   {
352     QFileDialog dlg( this );
353     dlg.setFileMode( QFileDialog::AnyFile );
354     dlg.setDirectory( "." );
355     dlg.setAcceptMode( QFileDialog::AcceptSave );
356     dlg.setNameFilter(
357       QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
358       );
359     dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
360     dlg.setWindowTitle(
361       (
362         std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
363         ).c_str( )
364       );
365     if( dlg.exec( ) )
366       this->_saveWorkspace(
367         wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
368         );
369
370   } // rof
371 }
372
373 // -------------------------------------------------------------------------
374 void cpBaseQtApplication::MainWindow::
375 _loadWorkspace( const std::string& fname )
376 {
377   try
378   {
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 ] );
383   }
384   catch( std::exception& err )
385   {
386     QMessageBox::critical(
387       this,
388       QMessageBox::tr( "Error loading workspace" ),
389       QMessageBox::tr( err.what( ) )
390       );
391
392   } // yrt
393 }
394
395 // -------------------------------------------------------------------------
396 void cpBaseQtApplication::MainWindow::
397 _loadWorkspace( )
398 {
399   QFileDialog dlg( this );
400   dlg.setFileMode( QFileDialog::ExistingFile );
401   dlg.setDirectory( "." );
402   dlg.setNameFilter(
403     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
404     );
405   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
406   if( !( dlg.exec( ) ) )
407     return;
408   this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
409 }
410
411 // -------------------------------------------------------------------------
412 void cpBaseQtApplication::MainWindow::
413 _actorsProperties( )
414 {
415   auto data =
416     dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
417       this->m_Viewer
418       );
419   if( data != NULL )
420   {
421     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
422     dlg->setData( data );
423     dlg->exec( );
424
425   } // fi
426 }
427
428 // eof - $RCSfile$