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