]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
d70d4c014bca5b19988f95a417bfb165ab5b38b1
[cpPlugins.git] / lib / cpBaseQtApplication / MainWindow.cxx
1 #include <cpBaseQtApplication/MainWindow.h>
2
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
5 #include <cpBaseQtApplication/Editor.h>
6 #include <vtkRenderer.h>
7 #include <QApplication>
8 #include <QColorDialog>
9 #include <QDir>
10 #include <QFileDialog>
11 #include <QFileInfo>
12 #include <QMessageBox>
13 #include <QTreeWidget>
14
15 // -------------------------------------------------------------------------
16 bool cpBaseQtApplication::MainWindow::_TBlocker::
17 eventFilter( QObject* obj, QEvent* event )
18 {
19   return( true ); // -> Block all events
20   /* NOTE: this should be the correct implementation:
21      switch( event->type( ) )
22      {
23      //list event you want to prevent here ...
24      case QEvent::KeyPress:
25      case QEvent::KeyRelease:
26      case QEvent::MouseButtonRelease:
27      case QEvent::MouseButtonPress:
28      case QEvent::MouseButtonDblClick:
29      //...
30      return( true );
31      } // hctiws
32      return( this->QObject::eventFilter( obj, event ) );
33   */
34 }
35
36 // -------------------------------------------------------------------------
37 cpBaseQtApplication::MainWindow::
38 MainWindow(
39   int argc, char* argv[],
40   QApplication* app,
41   QWidget* parent
42   )
43   : Superclass( parent ),
44     m_Application( app ),
45     m_PluginsPath( "." ),
46     m_TreeWidget( NULL ),
47     m_Editor( NULL ),
48     m_MPR( NULL )
49 {
50   QFileInfo i( argv[ 0 ] );
51   if( i.exists( ) )
52   {
53     this->m_ExecutionPath = i.canonicalPath( ).toStdString( );
54     try
55     {
56       this->m_Interface.OpenEnvironments( this->m_ExecutionPath );
57       this->m_Interface.UpdateEnvironments( this->m_ExecutionPath );
58     }
59     catch( ... ) { }
60   }
61   else
62     this->m_ExecutionPath = "";
63   this->m_Workspace.SetInterface( &( this->m_Interface ) );
64 }
65
66 // -------------------------------------------------------------------------
67 cpBaseQtApplication::MainWindow::
68 ~MainWindow( )
69 {
70 }
71
72 // -------------------------------------------------------------------------
73 void cpBaseQtApplication::MainWindow::
74 UpdateEnvironment( )
75 {
76   try
77   {
78     this->m_Interface.LoadEnvironment( );
79     this->m_Interface.GuessPlugins( );
80   }
81   catch( std::exception& err )
82   {
83     QMessageBox::critical(
84       this, "Error loading required libraries", err.what( )
85       );
86     return;
87
88   } // yrt
89 }
90
91 // -------------------------------------------------------------------------
92 void cpBaseQtApplication::MainWindow::
93 _Configure(
94   QTreeWidget* tree,
95   cpExtensions::QT::SimpleMPRWidget* mpr,
96   cpBaseQtApplication::Editor* editor
97   )
98 {
99   this->m_TreeWidget = tree;
100   this->_UpdateLoadedPlugins( );
101   this->m_Editor = editor;
102   this->m_MPR = mpr;
103   if( this->m_Editor != NULL )
104   {
105     this->m_Editor->setWorkspace( &( this->m_Workspace ) );
106
107     // Slots <-> signals
108     this->connect(
109       this->m_Editor,
110       SIGNAL( execFilter( const std::string& ) ),
111       this,
112       SLOT( _ExecFilter( const std::string& ) )
113       );
114     this->connect(
115       this->m_Editor,
116       SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
117       this,
118       SLOT( _ShowData( const std::string&, const std::string& ) )
119       );
120     this->connect(
121       this->m_Editor,
122       SIGNAL( hideFilterOutput( const std::string&, const std::string& ) ),
123       this,
124       SLOT( _HideData( const std::string&, const std::string& ) )
125       );
126     this->connect(
127       this->m_Editor,
128       SIGNAL( visualPropertiesFilterOutput( const std::string&, const std::string& ) ),
129       this,
130       SLOT( _DataProperties( const std::string&, const std::string& ) )
131       );
132
133   } // fi
134   if( this->m_MPR != NULL )
135     this->m_Workspace.SetMPRViewer( this->m_MPR );
136   this->_ClearWorkspace( );
137 }
138
139 // -------------------------------------------------------------------------
140 void cpBaseQtApplication::MainWindow::
141 _ClearWorkspace( )
142 {
143   if( this->m_Editor != NULL )
144     this->m_Editor->clear( );
145   if( this->m_MPR != NULL )
146     this->m_MPR->Clear( );
147   this->m_Workspace.Clear( );
148 }
149
150 // -------------------------------------------------------------------------
151 void cpBaseQtApplication::MainWindow::
152 _LoadPlugins( const std::string& filename )
153 {
154   try
155   {
156     this->m_Interface.LoadFile( filename );
157     this->_UpdateLoadedPlugins( );
158   }
159   catch( std::exception& err )
160   {
161     QMessageBox::critical(
162       this,
163       "Error loading plugins path",
164       err.what( )
165       );
166
167   } // yrt
168 }
169
170 // -------------------------------------------------------------------------
171 void cpBaseQtApplication::MainWindow::
172 _LoadPluginsFromPath( const std::string& path )
173 {
174   try
175   {
176     this->m_Interface.LoadDirectory( path );
177     this->_UpdateLoadedPlugins( );
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 _UpdateLoadedPlugins( )
193 {
194   if( this->m_TreeWidget == NULL )
195     return;
196   this->_Block( );
197   auto categories = this->m_Interface.GetCategories( );
198   unsigned int filter_count = 0;
199   for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
200   {
201     // Create or get category
202     QList< QTreeWidgetItem* > cat_items =
203       this->m_TreeWidget->findItems(
204         cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
205         );
206     QTreeWidgetItem* cat = NULL;
207     if( cat_items.size( ) == 0 )
208     {
209       cat = new QTreeWidgetItem(
210         ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
211         );
212       this->m_TreeWidget->addTopLevelItem( cat );
213     }
214     else
215       cat = cat_items[ 0 ];
216
217     // Create filters
218     auto filters = this->m_Interface.GetFilters( *cIt );
219     filter_count += filters.size( );
220     for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
221     {
222       QList< QTreeWidgetItem* > filter_items =
223         this->m_TreeWidget->findItems(
224           fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
225           );
226       auto fiIt = filter_items.begin( );
227       auto found_fiIt = filter_items.end( );
228       for( ; fiIt != filter_items.end( ); ++fiIt )
229         if( ( *fiIt )->parent( ) == cat )
230           found_fiIt = fiIt;
231
232       // Add filter
233       if( found_fiIt == filter_items.end( ) )
234         QTreeWidgetItem* filter = new QTreeWidgetItem(
235           cat, QStringList( fIt->c_str( ) )
236           );
237
238     } // rof
239
240   } // rof
241   this->m_TreeWidget->expandAll( );
242   this->_UnBlock( );
243
244   if( filter_count == 0 )
245     QMessageBox::critical(
246       this,
247       "Error loading default plugins",
248       "No plugins loaded: remember to load some!!!"
249       );
250 }
251
252 // -------------------------------------------------------------------------
253 void cpBaseQtApplication::MainWindow::
254 _ShowPlugins( )
255 {
256   auto paths = this->m_Interface.GetPaths( );
257   auto plugins = this->m_Interface.GetPlugins( );
258   std::stringstream info;
259   info << "-----   PATHS   -----" << std::endl;
260   for( auto i = paths.begin( ); i != paths.end( ); ++i )
261     info << "   " << *i << std::endl;
262   info << std::endl << "-----  PLUGINS  -----" << std::endl;
263   for( auto i = plugins.begin( ); i != plugins.end( ); ++i )
264     info << "   " << *i << std::endl;
265   QMessageBox::information(
266     this,
267     "Loaded libraries and plugins information",
268     info.str( ).c_str( )
269     );
270 }
271
272 // -------------------------------------------------------------------------
273 void cpBaseQtApplication::MainWindow::
274 _Block( )
275 {
276   this->m_Application->setOverrideCursor( Qt::WaitCursor );
277   this->m_Application->installEventFilter( &( this->m_Blocker ) );
278 }
279
280 // -------------------------------------------------------------------------
281 void cpBaseQtApplication::MainWindow::
282 _UnBlock( )
283 {
284   while( this->m_Application->overrideCursor( ) )
285     this->m_Application->restoreOverrideCursor( );
286   this->m_Application->removeEventFilter( &( this->m_Blocker ) );
287 }
288
289 // -------------------------------------------------------------------------
290 void cpBaseQtApplication::MainWindow::
291 _LoadWorkspace( const std::string& filename )
292 {
293   this->_ClearWorkspace( );
294   std::string err = this->m_Workspace.LoadWorkspace( filename );
295   if( err != "" )
296   {
297     QMessageBox::critical(
298       this,
299       QMessageBox::tr( "Error loading workspace" ),
300       QMessageBox::tr( err.c_str( ) )
301       );
302   }
303   else
304   {
305     /* TODO
306        if( this->m_Editor != NULL )
307        this->m_Editor->setWorkspace( this->m_Workspace );
308     */
309
310   } // fi
311 }
312
313 // -------------------------------------------------------------------------
314 void cpBaseQtApplication::MainWindow::
315 _SaveWorkspace( const std::string& filename )
316 {
317   std::string err = this->m_Workspace.SaveWorkspace( filename );
318   if( err != "" )
319     QMessageBox::critical(
320       this,
321       QMessageBox::tr( "Error saving workspace" ),
322       QMessageBox::tr( err.c_str( ) )
323       );
324 }
325
326 // -------------------------------------------------------------------------
327 void cpBaseQtApplication::MainWindow::
328 _BackgroundProperties( unsigned int i )
329 {
330   if( this->m_MPR == NULL )
331     return;
332
333   QColor color =
334     QColorDialog::getColor(
335       QColor( 0, 0, 0 ),
336       this,
337       "Select Color",
338       QColorDialog::DontUseNativeDialog
339       );
340   if( color.isValid( ) )
341   {
342     double r = double( color.red( ) ) / double( 255 );
343     double g = double( color.green( ) ) / double( 255 );
344     double b = double( color.blue( ) ) / double( 255 );
345     if( i >= 4 )
346     {
347       unsigned int maxId = ( i == 4 )? 3: 4;
348       for( unsigned int j = 0; j < maxId; ++j )
349       {
350         auto ren = this->m_MPR->GetRenderer( j );
351         if( ren != NULL )
352         {
353           ren->SetBackground( r, g, b );
354           ren->Render( );
355
356         } // fi
357
358       } // rof
359     }
360     else
361     {
362       auto ren = this->m_MPR->GetRenderer( i );
363       if( ren != NULL )
364       {
365         ren->SetBackground( r, g, b );
366         ren->Render( );
367
368       } // fi
369
370     } // fi
371
372   } // fi
373 }
374
375 // -------------------------------------------------------------------------
376 void cpBaseQtApplication::MainWindow::
377 _ShowData( const std::string& filter_name, const std::string& output_name )
378 {
379   if( this->m_MPR == NULL )
380     return;
381
382   auto output = this->m_Workspace.GetOutput( filter_name, output_name );
383   if( output != NULL )
384   {
385     this->_Block( );
386     auto actor = output->GetVTKActor( );
387     if( actor != NULL )
388     {
389       this->m_MPR->AddActor(
390         actor, output_name + std::string( "@" ) + filter_name
391         );
392       this->_UnBlock( );
393     }
394     else
395     {
396       this->_UnBlock( );
397       QMessageBox::critical(
398         this,
399         QMessageBox::tr( "Error showing data" ),
400         QMessageBox::tr( "Unknown VTK conversion." )
401         );
402
403     } // fi
404   }
405   else
406     QMessageBox::critical(
407       this,
408       QMessageBox::tr( "Error showing data" ),
409       QMessageBox::tr( "Unknown port name." )
410       );
411 }
412
413 // -------------------------------------------------------------------------
414 void cpBaseQtApplication::MainWindow::
415 _HideData( const std::string& filter, const std::string& output )
416 {
417   std::cout << "MainWindow::HideData" << std::endl;
418   /* TODO
419    */
420 }
421
422 // -------------------------------------------------------------------------
423 void cpBaseQtApplication::MainWindow::
424 _DataProperties(
425   const std::string& filter_name, const std::string& output_name
426   )
427 {
428   if( this->m_MPR == NULL )
429     return;
430
431   auto output = this->m_Workspace.GetOutput( filter_name, output_name );
432   if( output != NULL )
433   {
434     this->_Block( );
435     auto actors = this->m_MPR->GetActors(
436       output_name + std::string( "@" ) + filter_name
437       );
438     auto dlg = new ActorPropertiesQDialog( NULL );
439     for( auto i = actors.begin( ); i != actors.end( ); ++i )
440       dlg->addActor( *i );
441     dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 0 ) );
442     dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 1 ) );
443     dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 2 ) );
444     dlg->addRenderWindow( this->m_MPR->GetRenderWindow( 3 ) );
445     this->_UnBlock( );
446     dlg->exec( );
447   }
448   else
449     QMessageBox::critical(
450       this,
451       QMessageBox::tr( "Error showing data" ),
452       QMessageBox::tr( "Unknown port name." )
453       );
454 }
455
456 // -------------------------------------------------------------------------
457 void cpBaseQtApplication::MainWindow::
458 _InteractiveLoadPlugins( )
459 {
460   QFileDialog dlg( this );
461   dlg.setFileMode( QFileDialog::ExistingFiles );
462   dlg.setDirectory( this->m_PluginsPath.c_str( ) );
463
464   std::stringstream filter;
465   std::string suffix = std::string( cpPlugins_LIB_EXT );
466   filter << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
467   dlg.setNameFilter( filter.str( ).c_str( ) );
468   dlg.setDefaultSuffix( suffix.c_str( ) );
469   if( !( dlg.exec( ) ) )
470     return;
471   QStringList names = dlg.selectedFiles( );
472   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
473     this->_LoadPlugins( qIt->toStdString( ) );
474 }
475
476 // -------------------------------------------------------------------------
477 void cpBaseQtApplication::MainWindow::
478 _InteractiveLoadPluginsFromPath( )
479 {
480   QFileDialog d( this );
481   d.setFileMode( QFileDialog::DirectoryOnly );
482   d.setDirectory( this->m_PluginsPath.c_str( ) );
483   if( !( d.exec( ) ) )
484     return;
485   this->_LoadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
486 }
487
488 // -------------------------------------------------------------------------
489 void cpBaseQtApplication::MainWindow::
490 _InteractiveLoadWorkspace( )
491 {
492   QFileDialog dlg( this );
493   dlg.setFileMode( QFileDialog::ExistingFile );
494   dlg.setDirectory( "." );
495   dlg.setNameFilter(
496     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
497     );
498   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
499   if( !( dlg.exec( ) ) )
500     return;
501   this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
502 }
503
504 // -------------------------------------------------------------------------
505 void cpBaseQtApplication::MainWindow::
506 _InteractiveSaveWorkspace( )
507 {
508   QFileDialog dlg( this );
509   dlg.setFileMode( QFileDialog::AnyFile );
510   dlg.setDirectory( "." );
511   dlg.setAcceptMode( QFileDialog::AcceptSave );
512   dlg.setNameFilter(
513     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
514     );
515   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
516   if( !( dlg.exec( ) ) )
517     return;
518   this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
519 }
520
521 // -------------------------------------------------------------------------
522 void cpBaseQtApplication::MainWindow::
523 _ExecFilter( const std::string& filter_name )
524 {
525   this->_Block( );
526   try
527   {
528     this->m_Workspace.Execute( filter_name );
529     this->_UnBlock( );
530   }
531   catch( itk::ExceptionObject& err1 )
532   {
533     this->_UnBlock( );
534     QMessageBox::critical(
535       this,
536       QMessageBox::tr( "Error executing filter" ),
537       QMessageBox::tr( err1.GetDescription( ) )
538       );
539   }
540   catch( std::exception& err2 )
541   {
542     this->_UnBlock( );
543     QMessageBox::critical(
544       this,
545       QMessageBox::tr( "Error executing filter" ),
546       QMessageBox::tr( err2.what( ) )
547       );
548   }
549   catch( ... )
550   {
551     this->_UnBlock( );
552     QMessageBox::critical(
553       this,
554       QMessageBox::tr( "Error executing filter" ),
555       QMessageBox::tr( "Unknown error" )
556       );
557
558   } // yrt
559 }
560
561 // eof - $RCSfile$