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