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