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