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