]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
37b18aa56168693a98df86b999010b239002e2db
[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 }
137
138 // -------------------------------------------------------------------------
139 void cpBaseQtApplication::MainWindow::
140 _loadPlugins( const std::string& filename )
141 {
142   try
143   {
144     this->m_Plugins->LoadPluginsFile( filename );
145     if( this->m_Navigator != NULL )
146       this->m_Navigator->Update( );
147   }
148   catch( std::exception& err )
149   {
150     QMessageBox::critical(
151       this,
152       "Error loading plugins path",
153       err.what( )
154       );
155
156   } // yrt
157 }
158
159 // -------------------------------------------------------------------------
160 void cpBaseQtApplication::MainWindow::
161 _loadPlugins( )
162 {
163   QFileDialog dlg( this );
164   dlg.setFileMode( QFileDialog::ExistingFiles );
165
166   std::stringstream filter;
167   std::string suffix = std::string( cpPlugins_LIB_EXT );
168   filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
169   dlg.setNameFilter( filter.str( ).c_str( ) );
170   dlg.setDefaultSuffix( suffix.c_str( ) );
171   if( !( dlg.exec( ) ) )
172     return;
173   QStringList names = dlg.selectedFiles( );
174   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
175     this->_loadPlugins( qIt->toStdString( ) );
176 }
177
178 // -------------------------------------------------------------------------
179 void cpBaseQtApplication::MainWindow::
180 _loadPluginsFromPath( const std::string& path )
181 {
182   try
183   {
184     this->m_Plugins->LoadPluginsDirectory( path );
185     this->m_Plugins->SaveEnvironment( this->m_RunPath );
186     if( this->m_Navigator != NULL )
187       this->m_Navigator->Update( );
188   }
189   catch( std::exception& err )
190   {
191     QMessageBox::critical(
192       this,
193       "Error loading plugins path",
194       err.what( )
195       );
196
197   } // yrt
198 }
199
200 // -------------------------------------------------------------------------
201 void cpBaseQtApplication::MainWindow::
202 _loadPluginsFromPath( )
203 {
204   QFileDialog d( this );
205   d.setFileMode( QFileDialog::DirectoryOnly );
206   if( !( d.exec( ) ) )
207     return;
208   this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
209 }
210
211 // -------------------------------------------------------------------------
212 void cpBaseQtApplication::MainWindow::
213 _clearWorkspaces( )
214 {
215   this->m_Workspaces.clear( );
216 }
217
218 // -------------------------------------------------------------------------
219 void cpBaseQtApplication::MainWindow::
220 _addWorkspace( const std::string& name )
221 {
222   auto wIt = this->m_Workspaces.find( name );
223   if( wIt == this->m_Workspaces.end( ) )
224   {
225     if( this->m_SingleWorkspace )
226       this->m_Workspaces.clear( );
227     this->m_Workspaces[ name ] = TWorkspace::New( );
228     if( this->m_Canvas != NULL )
229       this->m_Canvas->setWorkspace( this->m_Workspaces[ name ] );
230     this->setWindowTitle( ( this->m_BaseWindowTitle + name ).c_str( ) );
231
232   } // fi
233 }
234
235 // -------------------------------------------------------------------------
236 void cpBaseQtApplication::MainWindow::
237 _addWorkspace( )
238 {
239   bool ok;
240   QString text =
241     QInputDialog::getText(
242       this, "Creating a new workspace...",
243       "New workspace name: ",
244       QLineEdit::Normal,
245       "new_workspace",
246       &ok
247       );
248   if( ok && !text.isEmpty( ) )
249     this->_addWorkspace( text.toStdString( ) );
250 }
251
252 // -------------------------------------------------------------------------
253 void cpBaseQtApplication::MainWindow::
254 _saveWorkspace( const std::string& wname, const std::string& fname )
255 {
256   auto wIt = this->m_Workspaces.find( wname );
257   if( wIt != this->m_Workspaces.end( ) )
258   {
259     try
260     {
261       wIt->second->Save( fname );
262       this->m_LastSaveFileName = fname;
263     }
264     catch( std::exception& err )
265     {
266       QMessageBox::critical(
267         this,
268         QMessageBox::tr( "Error saving workspace" ),
269         QMessageBox::tr( err.what( ) )
270         );
271
272     } // yrt
273   }
274   else
275     QMessageBox::critical(
276       this,
277       "Error saving workspace",
278       (
279         std::string( "Workspace \"" ) + wname +
280         std::string( "\" does not exist." )
281         ).c_str( )
282       );
283 }
284
285 // -------------------------------------------------------------------------
286 void cpBaseQtApplication::MainWindow::
287 _saveWorkspace( const std::string& wname, bool force )
288 {
289   auto wIt = this->m_Workspaces.find( wname );
290   if( wIt != this->m_Workspaces.end( ) )
291   {
292     if( this->m_LastSaveFileName == "" || force )
293     {
294       QFileDialog dlg( this );
295       dlg.setFileMode( QFileDialog::AnyFile );
296       dlg.setDirectory( "." );
297       dlg.setAcceptMode( QFileDialog::AcceptSave );
298       dlg.setNameFilter(
299         QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
300         );
301       dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
302       dlg.setWindowTitle(
303         (
304           std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
305           ).c_str( )
306         );
307       if( dlg.exec( ) )
308         this->_saveWorkspace(
309           wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
310           );
311     }
312     else
313       this->_saveWorkspace( wIt->first, this->m_LastSaveFileName );
314   }
315   else
316     QMessageBox::critical(
317       this,
318       "Error saving workspace",
319       (
320         std::string( "Workspace \"" ) + wname +
321         std::string( "\" does not exist." )
322         ).c_str( )
323       );
324 }
325
326 // -------------------------------------------------------------------------
327 void cpBaseQtApplication::MainWindow::
328 _saveWorkspace( )
329 {
330   for(
331     auto wIt = this->m_Workspaces.begin( );
332     wIt != this->m_Workspaces.end( );
333     ++wIt
334     )
335   {
336     QFileDialog dlg( this );
337     dlg.setFileMode( QFileDialog::AnyFile );
338     dlg.setDirectory( "." );
339     dlg.setAcceptMode( QFileDialog::AcceptSave );
340     dlg.setNameFilter(
341       QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
342       );
343     dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
344     dlg.setWindowTitle(
345       (
346         std::string( "Saving \"" ) + wIt->first + std::string( "\"..." )
347         ).c_str( )
348       );
349     if( dlg.exec( ) )
350       this->_saveWorkspace(
351         wIt->first, dlg.selectedFiles( ).begin( )->toStdString( )
352         );
353
354   } // rof
355 }
356
357 // -------------------------------------------------------------------------
358 void cpBaseQtApplication::MainWindow::
359 _loadWorkspace( const std::string& fname )
360 {
361   try
362   {
363     this->_addWorkspace( fname );
364     this->m_Workspaces[ fname ]->Load( fname );
365     if( this->m_Canvas != NULL )
366       this->m_Canvas->setWorkspace( this->m_Workspaces[ fname ] );
367   }
368   catch( std::exception& err )
369   {
370     QMessageBox::critical(
371       this,
372       QMessageBox::tr( "Error loading workspace" ),
373       QMessageBox::tr( err.what( ) )
374       );
375
376   } // yrt
377 }
378
379 // -------------------------------------------------------------------------
380 void cpBaseQtApplication::MainWindow::
381 _loadWorkspace( )
382 {
383   QFileDialog dlg( this );
384   dlg.setFileMode( QFileDialog::ExistingFile );
385   dlg.setDirectory( "." );
386   dlg.setNameFilter(
387     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
388     );
389   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
390   if( !( dlg.exec( ) ) )
391     return;
392   this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
393 }
394
395 // -------------------------------------------------------------------------
396 void cpBaseQtApplication::MainWindow::
397 _actorsProperties( )
398 {
399   auto data =
400     dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
401       this->m_Viewer
402       );
403   if( data != NULL )
404   {
405     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
406     dlg->setData( data );
407     dlg->exec( );
408
409   } // fi
410 }
411
412 // eof - $RCSfile$