]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
Cast image filter added. ROI filter modified.
[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_BaseWindowTitle( "cpBaseQtApplication" ),
20     m_Canvas( NULL ),
21     m_Navigator( NULL ),
22     m_Viewer( NULL )
23 {
24   this->m_RunPath = QDir( "." ).canonicalPath( ).toStdString( );
25   this->m_Plugins = TPlugins::New( );
26   try
27   {
28     this->m_Plugins->GuessEnvironment( this->m_RunPath );
29     this->m_Plugins->GuessPlugins( );
30   }
31   catch( std::exception& err )
32   {
33     QMessageBox::critical( this, "Error guessing plugins.", err.what( ) );
34
35   } // yrt
36   this->_clearWorkspace( );
37 }
38
39 // -------------------------------------------------------------------------
40 cpBaseQtApplication::MainWindow::
41 ~MainWindow( )
42 {
43 }
44
45 // -------------------------------------------------------------------------
46 cpBaseQtApplication::MainWindow::
47 TWorkspace* cpBaseQtApplication::MainWindow::
48 workspace( )
49 {
50   return( this->m_Workspace );
51 }
52
53 // -------------------------------------------------------------------------
54 const cpBaseQtApplication::MainWindow::
55 TWorkspace* cpBaseQtApplication::MainWindow::
56 workspace( ) const
57 {
58   return( this->m_Workspace );
59 }
60
61 // -------------------------------------------------------------------------
62 cpBaseQtApplication::Pipeline::Canvas*
63 cpBaseQtApplication::MainWindow::
64 canvas( )
65 {
66   return( this->m_Canvas );
67 }
68
69 // -------------------------------------------------------------------------
70 const cpBaseQtApplication::Pipeline::Canvas*
71 cpBaseQtApplication::MainWindow::
72 canvas( ) const
73 {
74   return( this->m_Canvas );
75 }
76
77 // -------------------------------------------------------------------------
78 void cpBaseQtApplication::MainWindow::
79 setCanvas( cpBaseQtApplication::Pipeline::Canvas* c )
80 {
81   this->m_Canvas = c;
82   if( this->m_Canvas != NULL )
83     this->m_Canvas->setWorkspace( this->m_Workspace );
84
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   if( this->m_Viewer != NULL )
132   {
133     auto interactors = this->m_Viewer->GetInteractors( );
134     for( auto i : interactors )
135       this->m_Workspace->AddInteractor( i );
136
137   } // fi
138 }
139
140 // -------------------------------------------------------------------------
141 void cpBaseQtApplication::MainWindow::
142 _loadPlugins( const std::string& filename )
143 {
144   try
145   {
146     this->m_Plugins->LoadPluginsFile( filename );
147     if( this->m_Navigator != NULL )
148       this->m_Navigator->Update( );
149   }
150   catch( std::exception& err )
151   {
152     QMessageBox::critical(
153       this,
154       "Error loading plugins path",
155       err.what( )
156       );
157
158   } // yrt
159 }
160
161 // -------------------------------------------------------------------------
162 void cpBaseQtApplication::MainWindow::
163 _loadPlugins( )
164 {
165   QFileDialog dlg( this );
166   dlg.setFileMode( QFileDialog::ExistingFiles );
167
168   std::stringstream filter;
169   std::string suffix = std::string( cpPlugins_LIB_EXT );
170   filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
171   dlg.setNameFilter( filter.str( ).c_str( ) );
172   dlg.setDefaultSuffix( suffix.c_str( ) );
173   if( !( dlg.exec( ) ) )
174     return;
175   QStringList names = dlg.selectedFiles( );
176   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
177     this->_loadPlugins( qIt->toStdString( ) );
178 }
179
180 // -------------------------------------------------------------------------
181 void cpBaseQtApplication::MainWindow::
182 _loadPluginsFromPath( const std::string& path )
183 {
184   try
185   {
186     this->m_Plugins->LoadPluginsDirectory( path );
187     this->m_Plugins->SaveEnvironment( this->m_RunPath );
188     if( this->m_Navigator != NULL )
189       this->m_Navigator->Update( );
190   }
191   catch( std::exception& err )
192   {
193     QMessageBox::critical(
194       this,
195       "Error loading plugins path",
196       err.what( )
197       );
198
199   } // yrt
200 }
201
202 // -------------------------------------------------------------------------
203 void cpBaseQtApplication::MainWindow::
204 _loadPluginsFromPath( )
205 {
206   QFileDialog d( this );
207   d.setFileMode( QFileDialog::DirectoryOnly );
208   if( !( d.exec( ) ) )
209     return;
210   this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
211 }
212
213 // -------------------------------------------------------------------------
214 void cpBaseQtApplication::MainWindow::
215 _clearWorkspace( )
216 {
217   this->setWindowTitle( this->m_BaseWindowTitle.c_str( ) );
218   this->m_Workspace = TWorkspace::New( );
219   if( this->m_Canvas != NULL )
220   {
221     this->m_Canvas->clear( );
222     this->m_Canvas->setWorkspace( this->m_Workspace );
223
224   } // fi
225   if( this->m_Viewer != NULL )
226   {
227     // TODO: this->m_Viewer->clear( );
228     auto interactors = this->m_Viewer->GetInteractors( );
229     for( auto i : interactors )
230       this->m_Workspace->AddInteractor( i );
231
232   } // fi
233 }
234
235 // -------------------------------------------------------------------------
236 void cpBaseQtApplication::MainWindow::
237 _saveWorkspace( const std::string& fname )
238 {
239   this->m_LastSaveFileName = fname;
240   this->m_Workspace->Save( this->m_LastSaveFileName );
241 }
242
243 // -------------------------------------------------------------------------
244 void cpBaseQtApplication::MainWindow::
245 _saveWorkspace( )
246 {
247   if( this->m_LastSaveFileName == "" )
248   {
249     QFileDialog dlg( this );
250     dlg.setFileMode( QFileDialog::AnyFile );
251     dlg.setDirectory( "." );
252     dlg.setAcceptMode( QFileDialog::AcceptSave );
253     dlg.setNameFilter(
254       QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
255       );
256     dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
257     dlg.setWindowTitle( "Saving workspace" );
258     if( dlg.exec( ) )
259       this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
260   }
261   else
262     this->_saveWorkspace( this->m_LastSaveFileName );
263 }
264
265 // -------------------------------------------------------------------------
266 void cpBaseQtApplication::MainWindow::
267 _loadWorkspace( const std::string& fname )
268 {
269   try
270   {
271     this->_clearWorkspace( );
272     this->m_Workspace->Load( fname );
273     this->m_LastSaveFileName = "";
274     if( this->m_Canvas != NULL )
275       this->m_Canvas->setWorkspace( this->m_Workspace );
276   }
277   catch( std::exception& err )
278   {
279     QMessageBox::critical(
280       this,
281       QMessageBox::tr( "Error loading workspace" ),
282       QMessageBox::tr( err.what( ) )
283       );
284
285   } // yrt
286 }
287
288 // -------------------------------------------------------------------------
289 void cpBaseQtApplication::MainWindow::
290 _loadWorkspace( )
291 {
292   QFileDialog dlg( this );
293   dlg.setFileMode( QFileDialog::ExistingFile );
294   dlg.setDirectory( "." );
295   dlg.setNameFilter(
296     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
297     );
298   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
299   if( !( dlg.exec( ) ) )
300     return;
301   this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
302 }
303
304 // -------------------------------------------------------------------------
305 void cpBaseQtApplication::MainWindow::
306 _actorsProperties( )
307 {
308   auto data =
309     dynamic_cast< cpExtensions::QT::ActorsWidgetInterface* >(
310       this->m_Viewer
311       );
312   if( data != NULL )
313   {
314     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
315     dlg->setData( data );
316     dlg->exec( );
317
318   } // fi
319 }
320
321 // eof - $RCSfile$