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