]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
f8ac71ad118762a584da6459736ac0587d0a94db
[cpPlugins.git] / lib / cpBaseQtApplication / MainWindow.cxx
1 #include <cpBaseQtApplication/MainWindow.h>
2
3 #include <QApplication>
4 #include <QDir>
5 #include <QFileDialog>
6 #include <QFileInfo>
7 #include <QMessageBox>
8
9 #include <cpExtensions/QT/SimpleMPRWidget.h>
10 #include <cpExtensions/QT/ConfigurationChooser.h>
11
12 #include <vtkDataSet.h>
13
14 #include <cpBaseQtApplication/Editor.h>
15 #include <cpBaseQtApplication/PathsDialog.h>
16 #include <cpBaseQtApplication/PluginsNavigator.h>
17 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
18
19 /* TODO
20    #include <cpPlugins/Interface/Plugins.h>
21    #include <cpPlugins/BaseObjects/Widget.h>
22    #include <vtkImageData.h>
23    #include <vtkPolyData.h>
24    #include <vtkRenderer.h>
25    #include <QColorDialog>
26 */
27
28 // -------------------------------------------------------------------------
29 bool cpBaseQtApplication::MainWindow::_TBlocker::
30 eventFilter( QObject* obj, QEvent* event )
31 {
32   return( true ); // -> Block all events
33   /* NOTE: this should be the correct implementation:
34      switch( event->type( ) )
35      {
36      //list event you want to prevent here ...
37      case QEvent::KeyPress:
38      case QEvent::KeyRelease:
39      case QEvent::MouseButtonRelease:
40      case QEvent::MouseButtonPress:
41      case QEvent::MouseButtonDblClick:
42      //...
43      return( true );
44      } // hctiws
45      return( this->QObject::eventFilter( obj, event ) );
46   */
47 }
48
49 // -------------------------------------------------------------------------
50 cpBaseQtApplication::MainWindow::
51 MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent )
52   : Superclass( parent ),
53     m_Application( app ),
54     m_Navigator( NULL ),
55     m_Editor( NULL ),
56     m_MPR( NULL )
57 {
58   // Use some evident paths
59   QFileInfo e_path( argv[ 0 ] );
60   QDir r_path( "." );
61   std::set< std::string > paths;
62   paths.insert( e_path.canonicalPath( ).toStdString( ) );
63   paths.insert( r_path.canonicalPath( ).toStdString( ) );
64   std::stringstream env;
65   for( auto p = paths.begin( ); p != paths.end( ); ++p )
66     env << *p << cpPlugins_ENV_SEPARATOR;
67
68   // Some variables
69   this->m_RunPath = r_path.canonicalPath( ).toStdString( );
70
71   // Get the plugins interface
72   this->m_Plugins = TPlugins::New( );
73   try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { }
74   this->updateEnvironment( );
75   try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { }
76
77   // Create local workspace
78   this->m_Workspace = TWorkspace::New( );
79 }
80
81 // -------------------------------------------------------------------------
82 cpBaseQtApplication::MainWindow::
83 ~MainWindow( )
84 {
85 }
86
87 // -------------------------------------------------------------------------
88 void cpBaseQtApplication::MainWindow::
89 updateEnvironment( )
90 {
91   try
92   {
93     this->m_Plugins->LoadEnvironments( );
94     this->m_Plugins->GuessPlugins( );
95   }
96   catch( std::exception& err )
97   {
98     QMessageBox::critical(
99       this, "Error loading required libraries", err.what( )
100       );
101     return;
102
103   } // yrt
104 }
105
106 // -------------------------------------------------------------------------
107 void cpBaseQtApplication::MainWindow::
108 updateFilter( const std::string& name )
109 {
110   auto filter = this->m_Workspace->GetFilter( name );
111   if( filter != NULL )
112     cpBaseQtApplication_Execute( filter->Update( ) );
113 }
114
115 // -------------------------------------------------------------------------
116 void cpBaseQtApplication::MainWindow::
117 showData( const std::string& name, const std::string& port )
118 {
119   this->updateFilter( name );
120   auto filter = this->m_Workspace->GetFilter( name );
121   if( filter != NULL )
122     this->showData(
123       filter->GetOutput( port ), port + std::string( "@" ) + name
124       );
125   else
126     QMessageBox::critical(
127       this,
128       QMessageBox::tr( "Error showing data" ),
129       QMessageBox::tr( "Unknown filter." )
130       );
131 }
132
133 // -------------------------------------------------------------------------
134 void cpBaseQtApplication::MainWindow::
135 showData( cpPlugins::BaseObjects::DataObject* data, const std::string& name )
136 {
137   if( this->m_MPR == NULL || data == NULL )
138     return;
139
140   // Associate visual data
141   this->_block( );
142   bool success = this->m_MPR->Add( data->GetVTK< vtkDataSet >( ), name );
143   this->_unBlock( );
144
145   // Show data or show an error
146   if( success )
147     this->m_MPR->Render( );
148   else
149     QMessageBox::critical(
150       this,
151       QMessageBox::tr( "Error showing data" ),
152       QMessageBox::tr( "Unknown conversion to a \"vtkProp\" object." )
153       );
154 }
155
156 // -------------------------------------------------------------------------
157 /* TODO
158    void cpBaseQtApplication::MainWindow::
159    hideData( const std::string& name, const std::string& port )
160    {
161    this->hideData( port + std::string( "@" ) + name );
162    }
163
164    // -------------------------------------------------------------------------
165    void cpBaseQtApplication::MainWindow::
166    hideData( const std::string& name )
167    {
168    }
169 */
170
171 // -------------------------------------------------------------------------
172 void cpBaseQtApplication::MainWindow::
173 dataProperties( const std::string& name, const std::string& port )
174 {
175   this->dataProperties( port + std::string( "@" ) + name );
176 }
177
178 // -------------------------------------------------------------------------
179 void cpBaseQtApplication::MainWindow::
180 dataProperties( const std::string& name )
181 {
182   if( this->m_MPR == NULL )
183     return;
184
185   auto props = this->m_MPR->GetViewProps( name );
186   if( props.size( ) > 0 )
187   {
188     this->_block( );
189     auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL );
190     dlg->setProps( props );
191     dlg->addRenderWindow( this->m_MPR->GetXRenderWindow( ) );
192     dlg->addRenderWindow( this->m_MPR->GetYRenderWindow( ) );
193     dlg->addRenderWindow( this->m_MPR->GetZRenderWindow( ) );
194     dlg->addRenderWindow( this->m_MPR->GetWRenderWindow( ) );
195     this->_unBlock( );
196     dlg->exec( );
197
198   } // fi
199 }
200
201 // -------------------------------------------------------------------------
202 void cpBaseQtApplication::MainWindow::
203 _block( )
204 {
205   if( this->m_Application != NULL )
206   {
207     this->m_Application->setOverrideCursor( Qt::WaitCursor );
208     this->m_Application->installEventFilter( &( this->m_Blocker ) );
209
210   } // fi
211 }
212
213 // -------------------------------------------------------------------------
214 void cpBaseQtApplication::MainWindow::
215 _unBlock( )
216 {
217   if( this->m_Application != NULL )
218   {
219     while( this->m_Application->overrideCursor( ) )
220       this->m_Application->restoreOverrideCursor( );
221     this->m_Application->removeEventFilter( &( this->m_Blocker ) );
222
223   } // fi
224 }
225
226 // -------------------------------------------------------------------------
227 void cpBaseQtApplication::MainWindow::
228 _configure( PluginsNavigator* nav, TMPR* mpr, Editor* edt )
229 {
230   this->m_Navigator = nav;
231   this->m_MPR = mpr;
232   this->m_Editor = edt;
233   this->_updatePlugins( );
234   if( this->m_Editor != NULL )
235     this->m_Editor->setWorkspace( this->m_Workspace );
236
237   // Associate interactors
238   if( this->m_MPR != NULL )
239   {
240     this->m_Workspace->AddInteractor( this->m_MPR->GetXInteractor( ) );
241     this->m_Workspace->AddInteractor( this->m_MPR->GetYInteractor( ) );
242     this->m_Workspace->AddInteractor( this->m_MPR->GetZInteractor( ) );
243     this->m_Workspace->AddInteractor( this->m_MPR->GetWInteractor( ) );
244
245   } // fi
246 }
247
248 // -------------------------------------------------------------------------
249 void cpBaseQtApplication::MainWindow::
250 _updatePlugins( )
251 {
252   if( this->m_Navigator == NULL )
253     return;
254   this->_block( );
255   this->m_Navigator->Update( );
256   this->_unBlock( );
257 }
258
259 // -------------------------------------------------------------------------
260 void cpBaseQtApplication::MainWindow::
261 _loadWorkspace( const std::string& filename )
262 {
263   try
264   {
265     this->m_Workspace->Load( filename );
266   }
267   catch( std::exception& err )
268   {
269     QMessageBox::critical(
270       this,
271       QMessageBox::tr( "Error loading workspace" ),
272       QMessageBox::tr( err.what( ) )
273       );
274
275   } // yrt
276   if( this->m_Editor != NULL )
277     this->m_Editor->redrawWorkspace( );
278 }
279
280 // -------------------------------------------------------------------------
281 void cpBaseQtApplication::MainWindow::
282 _loadWorkspace( )
283 {
284   QFileDialog dlg( this );
285   dlg.setFileMode( QFileDialog::ExistingFile );
286   dlg.setDirectory( "." );
287   dlg.setNameFilter(
288     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
289     );
290   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
291   if( !( dlg.exec( ) ) )
292     return;
293   this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
294 }
295
296 // -------------------------------------------------------------------------
297 void cpBaseQtApplication::MainWindow::
298 _saveWorkspace( const std::string& filename )
299 {
300   try
301   {
302     this->m_Workspace->Save( filename );
303   }
304   catch( std::exception& err )
305   {
306     QMessageBox::critical(
307       this,
308       QMessageBox::tr( "Error saving workspace" ),
309       QMessageBox::tr( err.what( ) )
310       );
311
312   } // yrt
313 }
314
315 // -------------------------------------------------------------------------
316 void cpBaseQtApplication::MainWindow::
317 _saveWorkspace( )
318 {
319   QFileDialog dlg( this );
320   dlg.setFileMode( QFileDialog::AnyFile );
321   dlg.setDirectory( "." );
322   dlg.setAcceptMode( QFileDialog::AcceptSave );
323   dlg.setNameFilter(
324     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
325     );
326   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
327   if( !( dlg.exec( ) ) )
328     return;
329   this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
330 }
331
332 // -------------------------------------------------------------------------
333 void cpBaseQtApplication::MainWindow::
334 _showPlugins( )
335 {
336   std::stringstream info;
337   this->m_Plugins->Print( info );
338   QMessageBox::information(
339     this,
340     "Loaded libraries and plugins information",
341     info.str( ).c_str( )
342     );
343 }
344
345 // -------------------------------------------------------------------------
346 void cpBaseQtApplication::MainWindow::
347 _addEnvironmentPaths( const std::string& envs )
348 {
349   try { this->m_Plugins->AddEnvironments( envs ); } catch( ... ) { }
350   try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { }
351   this->updateEnvironment( );
352   this->_updatePlugins( );
353 }
354
355 // -------------------------------------------------------------------------
356 void cpBaseQtApplication::MainWindow::
357 _addEnvironmentPaths( )
358 {
359   PathsDialog dlg( this );
360   dlg.addPaths( this->m_Plugins->GetPaths( ) );
361   if( dlg.exec( ) )
362   {
363     auto paths = dlg.getPaths( );
364     std::stringstream envs;
365     for( auto p = paths.begin( ); p != paths.end( ); ++p )
366       envs << *p << cpPlugins_ENV_SEPARATOR;
367     this->_addEnvironmentPaths( envs.str( ) );
368
369   } // fi
370 }
371
372 // -------------------------------------------------------------------------
373 void cpBaseQtApplication::MainWindow::
374 _loadPlugins( const std::string& filename )
375 {
376   try
377   {
378     this->m_Plugins->LoadFile( filename );
379     this->_updatePlugins( );
380   }
381   catch( std::exception& err )
382   {
383     QMessageBox::critical(
384       this,
385       "Error loading plugins path",
386       err.what( )
387       );
388
389   } // yrt
390 }
391
392 // -------------------------------------------------------------------------
393 void cpBaseQtApplication::MainWindow::
394 _loadPlugins( )
395 {
396   QFileDialog dlg( this );
397   dlg.setFileMode( QFileDialog::ExistingFiles );
398
399   std::stringstream filter;
400   std::string suffix = std::string( cpPlugins_LIB_EXT );
401   filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
402   dlg.setNameFilter( filter.str( ).c_str( ) );
403   dlg.setDefaultSuffix( suffix.c_str( ) );
404   if( !( dlg.exec( ) ) )
405     return;
406   QStringList names = dlg.selectedFiles( );
407   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
408     this->_loadPlugins( qIt->toStdString( ) );
409 }
410
411 // -------------------------------------------------------------------------
412 void cpBaseQtApplication::MainWindow::
413 _loadPluginsFromPath( const std::string& path )
414 {
415   try
416   {
417     this->m_Plugins->LoadDirectory( path );
418     this->_updatePlugins( );
419   }
420   catch( std::exception& err )
421   {
422     QMessageBox::critical(
423       this,
424       "Error loading plugins path",
425       err.what( )
426       );
427
428   } // yrt
429 }
430
431 // -------------------------------------------------------------------------
432 void cpBaseQtApplication::MainWindow::
433 _loadPluginsFromPath( )
434 {
435   QFileDialog d( this );
436   d.setFileMode( QFileDialog::DirectoryOnly );
437   if( !( d.exec( ) ) )
438     return;
439   this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
440 }
441
442 // -------------------------------------------------------------------------
443 void cpBaseQtApplication::MainWindow::
444 _actorsProperties( )
445 {
446   if( this->m_MPR != NULL )
447   {
448     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
449     dlg->setData( this->m_MPR );
450     dlg->exec( );
451
452   } // fi
453 }
454
455 // -------------------------------------------------------------------------
456 /* TODO
457    void cpBaseQtApplication::MainWindow::
458    _ClearWorkspace( )
459    {
460    if( this->m_Editor != NULL )
461    this->m_Editor->clear( );
462    this->m_Workspace->Clear( );
463    }
464 */
465
466 // eof - $RCSfile$