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