]> Creatis software - cpPlugins.git/blob - lib/cpBaseQtApplication/MainWindow.cxx
60cdc20ad5913d68ee0f9da041bd38410dc5bf37
[cpPlugins.git] / lib / cpBaseQtApplication / MainWindow.cxx
1 #include <cpBaseQtApplication/MainWindow.h>
2
3 #include <QApplication>
4 #include <QDir>
5 #include <QFileDialog>
6 #include <QFileInfo>
7 #include <QMessageBox>
8
9 #include <cpExtensions/QT/SimpleMPRWidget.h>
10 #include <cpExtensions/QT/ConfigurationChooser.h>
11
12 #include <vtkDataSet.h>
13
14 #include <cpBaseQtApplication/Editor.h>
15 #include <cpBaseQtApplication/PathsDialog.h>
16 #include <cpBaseQtApplication/PluginsNavigator.h>
17 #include <cpBaseQtApplication/ActorPropertiesQDialog.h>
18
19 /* TODO
20    #include <cpPlugins/Interface/Plugins.h>
21    #include <cpPlugins/BaseObjects/Widget.h>
22    #include <vtkImageData.h>
23    #include <vtkPolyData.h>
24    #include <vtkRenderer.h>
25    #include <QColorDialog>
26 */
27
28 // -------------------------------------------------------------------------
29 bool cpBaseQtApplication::MainWindow::_TBlocker::
30 eventFilter( QObject* obj, QEvent* event )
31 {
32   return( true ); // -> Block all events
33   /* NOTE: this should be the correct implementation:
34      switch( event->type( ) )
35      {
36      //list event you want to prevent here ...
37      case QEvent::KeyPress:
38      case QEvent::KeyRelease:
39      case QEvent::MouseButtonRelease:
40      case QEvent::MouseButtonPress:
41      case QEvent::MouseButtonDblClick:
42      //...
43      return( true );
44      } // hctiws
45      return( this->QObject::eventFilter( obj, event ) );
46   */
47 }
48
49 // -------------------------------------------------------------------------
50 cpBaseQtApplication::MainWindow::
51 MainWindow( int argc, char* argv[], QApplication* app, QWidget* parent )
52   : Superclass( parent ),
53     m_Application( app ),
54     m_Navigator( NULL ),
55     m_Editor( NULL ),
56     m_MPR( NULL )
57 {
58   // Use some evident paths
59   QFileInfo e_path( argv[ 0 ] );
60   QDir r_path( "." );
61   std::set< std::string > paths;
62   paths.insert( e_path.canonicalPath( ).toStdString( ) );
63   paths.insert( r_path.canonicalPath( ).toStdString( ) );
64   std::stringstream env;
65   for( auto p = paths.begin( ); p != paths.end( ); ++p )
66     env << *p << cpPlugins_ENV_SEPARATOR;
67
68   // Some variables
69   this->m_RunPath = r_path.canonicalPath( ).toStdString( );
70
71   // Get the plugins interface
72   this->m_Plugins = TPlugins::New( );
73   try { this->m_Plugins->AddEnvironments( env.str( ) ); } catch( ... ) { }
74   try { this->m_Plugins->LoadPaths( this->m_RunPath ); } catch( ... ) { }
75   this->updateEnvironment( );
76   try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { }
77
78   // Create local workspace
79   this->m_Workspace = TWorkspace::New( );
80 }
81
82 // -------------------------------------------------------------------------
83 cpBaseQtApplication::MainWindow::
84 ~MainWindow( )
85 {
86 }
87
88 // -------------------------------------------------------------------------
89 void cpBaseQtApplication::MainWindow::
90 updateEnvironment( )
91 {
92   try
93   {
94     this->m_Plugins->LoadEnvironments( );
95     this->m_Plugins->GuessPlugins( );
96   }
97   catch( std::exception& err )
98   {
99     QMessageBox::critical(
100       this, "Error loading required libraries", err.what( )
101       );
102     return;
103
104   } // yrt
105 }
106
107 // -------------------------------------------------------------------------
108 void cpBaseQtApplication::MainWindow::
109 updateFilter( const std::string& name )
110 {
111   auto filter = this->m_Workspace->GetFilter( name );
112   if( filter != NULL )
113     cpBaseQtApplication_Execute( filter->Update( ) );
114 }
115
116 // -------------------------------------------------------------------------
117 void cpBaseQtApplication::MainWindow::
118 showData( const std::string& name, const std::string& port )
119 {
120   this->updateFilter( name );
121   auto filter = this->m_Workspace->GetFilter( name );
122   if( filter != NULL )
123     this->showData(
124       filter->GetOutput( port ), port + std::string( "@" ) + name
125       );
126   else
127     QMessageBox::critical(
128       this,
129       QMessageBox::tr( "Error showing data" ),
130       QMessageBox::tr( "Unknown filter." )
131       );
132 }
133
134 // -------------------------------------------------------------------------
135 void cpBaseQtApplication::MainWindow::
136 showData( cpPlugins::BaseObjects::DataObject* data, const std::string& name )
137 {
138   if( this->m_MPR == NULL || data == NULL )
139     return;
140
141   // Associate visual data
142   this->_block( );
143   bool success = this->m_MPR->Add( data->GetVTK< vtkDataSet >( ), name );
144   this->_unBlock( );
145
146   // Show data or show an error
147   if( success )
148     this->m_MPR->Render( );
149   else
150     QMessageBox::critical(
151       this,
152       QMessageBox::tr( "Error showing data" ),
153       QMessageBox::tr( "Unknown conversion to a \"vtkProp\" object." )
154       );
155 }
156
157 // -------------------------------------------------------------------------
158 /* TODO
159    void cpBaseQtApplication::MainWindow::
160    hideData( const std::string& name, const std::string& port )
161    {
162    this->hideData( port + std::string( "@" ) + name );
163    }
164
165    // -------------------------------------------------------------------------
166    void cpBaseQtApplication::MainWindow::
167    hideData( const std::string& name )
168    {
169    }
170 */
171
172 // -------------------------------------------------------------------------
173 void cpBaseQtApplication::MainWindow::
174 dataProperties( const std::string& name, const std::string& port )
175 {
176   this->dataProperties( port + std::string( "@" ) + name );
177 }
178
179 // -------------------------------------------------------------------------
180 void cpBaseQtApplication::MainWindow::
181 dataProperties( const std::string& name )
182 {
183   if( this->m_MPR == NULL )
184     return;
185
186   auto props = this->m_MPR->GetViewProps( name );
187   if( props.size( ) > 0 )
188   {
189     this->_block( );
190     auto dlg = new cpBaseQtApplication::ActorPropertiesQDialog( NULL );
191     dlg->setProps( props );
192     dlg->addRenderWindow( this->m_MPR->GetXRenderWindow( ) );
193     dlg->addRenderWindow( this->m_MPR->GetYRenderWindow( ) );
194     dlg->addRenderWindow( this->m_MPR->GetZRenderWindow( ) );
195     dlg->addRenderWindow( this->m_MPR->GetWRenderWindow( ) );
196     this->_unBlock( );
197     dlg->exec( );
198
199   } // fi
200 }
201
202 // -------------------------------------------------------------------------
203 void cpBaseQtApplication::MainWindow::
204 _block( )
205 {
206   if( this->m_Application != NULL )
207   {
208     this->m_Application->setOverrideCursor( Qt::WaitCursor );
209     this->m_Application->installEventFilter( &( this->m_Blocker ) );
210
211   } // fi
212 }
213
214 // -------------------------------------------------------------------------
215 void cpBaseQtApplication::MainWindow::
216 _unBlock( )
217 {
218   if( this->m_Application != NULL )
219   {
220     while( this->m_Application->overrideCursor( ) )
221       this->m_Application->restoreOverrideCursor( );
222     this->m_Application->removeEventFilter( &( this->m_Blocker ) );
223
224   } // fi
225 }
226
227 // -------------------------------------------------------------------------
228 void cpBaseQtApplication::MainWindow::
229 _configure( PluginsNavigator* nav, TMPR* mpr, Editor* edt )
230 {
231   this->m_Navigator = nav;
232   this->m_MPR = mpr;
233   this->m_Editor = edt;
234   this->_updatePlugins( );
235   if( this->m_Editor != NULL )
236     this->m_Editor->setWorkspace( this->m_Workspace );
237
238   // Associate interactors
239   if( this->m_MPR != NULL )
240   {
241     this->m_Workspace->AddInteractor( this->m_MPR->GetXInteractor( ) );
242     this->m_Workspace->AddInteractor( this->m_MPR->GetYInteractor( ) );
243     this->m_Workspace->AddInteractor( this->m_MPR->GetZInteractor( ) );
244     this->m_Workspace->AddInteractor( this->m_MPR->GetWInteractor( ) );
245
246   } // fi
247 }
248
249 // -------------------------------------------------------------------------
250 void cpBaseQtApplication::MainWindow::
251 _updatePlugins( )
252 {
253   if( this->m_Navigator == NULL )
254     return;
255   this->_block( );
256   this->m_Navigator->Update( );
257   this->_unBlock( );
258 }
259
260 // -------------------------------------------------------------------------
261 void cpBaseQtApplication::MainWindow::
262 _loadWorkspace( const std::string& filename )
263 {
264   try
265   {
266     this->m_Workspace->Load( filename );
267   }
268   catch( std::exception& err )
269   {
270     QMessageBox::critical(
271       this,
272       QMessageBox::tr( "Error loading workspace" ),
273       QMessageBox::tr( err.what( ) )
274       );
275
276   } // yrt
277   if( this->m_Editor != NULL )
278     this->m_Editor->redrawWorkspace( );
279 }
280
281 // -------------------------------------------------------------------------
282 void cpBaseQtApplication::MainWindow::
283 _loadWorkspace( )
284 {
285   QFileDialog dlg( this );
286   dlg.setFileMode( QFileDialog::ExistingFile );
287   dlg.setDirectory( "." );
288   dlg.setNameFilter(
289     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
290     );
291   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
292   if( !( dlg.exec( ) ) )
293     return;
294   this->_loadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
295 }
296
297 // -------------------------------------------------------------------------
298 void cpBaseQtApplication::MainWindow::
299 _saveWorkspace( const std::string& filename )
300 {
301   try
302   {
303     this->m_Workspace->Save( filename );
304   }
305   catch( std::exception& err )
306   {
307     QMessageBox::critical(
308       this,
309       QMessageBox::tr( "Error saving workspace" ),
310       QMessageBox::tr( err.what( ) )
311       );
312
313   } // yrt
314 }
315
316 // -------------------------------------------------------------------------
317 void cpBaseQtApplication::MainWindow::
318 _saveWorkspace( )
319 {
320   QFileDialog dlg( this );
321   dlg.setFileMode( QFileDialog::AnyFile );
322   dlg.setDirectory( "." );
323   dlg.setAcceptMode( QFileDialog::AcceptSave );
324   dlg.setNameFilter(
325     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
326     );
327   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
328   if( !( dlg.exec( ) ) )
329     return;
330   this->_saveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
331 }
332
333 // -------------------------------------------------------------------------
334 void cpBaseQtApplication::MainWindow::
335 _showPlugins( )
336 {
337   std::stringstream info;
338   this->m_Plugins->Print( info );
339   QMessageBox::information(
340     this,
341     "Loaded libraries and plugins information",
342     info.str( ).c_str( )
343     );
344 }
345
346 // -------------------------------------------------------------------------
347 void cpBaseQtApplication::MainWindow::
348 _addEnvironmentPaths( const std::string& envs )
349 {
350   try { this->m_Plugins->AddEnvironments( envs ); } catch( ... ) { }
351   try { this->m_Plugins->SavePaths( this->m_RunPath ); } catch( ... ) { }
352   this->updateEnvironment( );
353   this->_updatePlugins( );
354 }
355
356 // -------------------------------------------------------------------------
357 void cpBaseQtApplication::MainWindow::
358 _addEnvironmentPaths( )
359 {
360   PathsDialog dlg( this );
361   dlg.addPaths( this->m_Plugins->GetPaths( ) );
362   if( dlg.exec( ) )
363   {
364     auto paths = dlg.getPaths( );
365     std::stringstream envs;
366     for( auto p = paths.begin( ); p != paths.end( ); ++p )
367       envs << *p << cpPlugins_ENV_SEPARATOR;
368     this->_addEnvironmentPaths( envs.str( ) );
369
370   } // fi
371 }
372
373 // -------------------------------------------------------------------------
374 void cpBaseQtApplication::MainWindow::
375 _loadPlugins( const std::string& filename )
376 {
377   try
378   {
379     this->m_Plugins->LoadFile( filename );
380     this->_updatePlugins( );
381   }
382   catch( std::exception& err )
383   {
384     QMessageBox::critical(
385       this,
386       "Error loading plugins path",
387       err.what( )
388       );
389
390   } // yrt
391 }
392
393 // -------------------------------------------------------------------------
394 void cpBaseQtApplication::MainWindow::
395 _loadPlugins( )
396 {
397   QFileDialog dlg( this );
398   dlg.setFileMode( QFileDialog::ExistingFiles );
399
400   std::stringstream filter;
401   std::string suffix = std::string( cpPlugins_LIB_EXT );
402   filter << "Plugins file (*" << cpPlugins_LIB_EXT << ");;All files (*)";
403   dlg.setNameFilter( filter.str( ).c_str( ) );
404   dlg.setDefaultSuffix( suffix.c_str( ) );
405   if( !( dlg.exec( ) ) )
406     return;
407   QStringList names = dlg.selectedFiles( );
408   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
409     this->_loadPlugins( qIt->toStdString( ) );
410 }
411
412 // -------------------------------------------------------------------------
413 void cpBaseQtApplication::MainWindow::
414 _loadPluginsFromPath( const std::string& path )
415 {
416   try
417   {
418     this->m_Plugins->LoadDirectory( path );
419     this->_updatePlugins( );
420   }
421   catch( std::exception& err )
422   {
423     QMessageBox::critical(
424       this,
425       "Error loading plugins path",
426       err.what( )
427       );
428
429   } // yrt
430 }
431
432 // -------------------------------------------------------------------------
433 void cpBaseQtApplication::MainWindow::
434 _loadPluginsFromPath( )
435 {
436   QFileDialog d( this );
437   d.setFileMode( QFileDialog::DirectoryOnly );
438   if( !( d.exec( ) ) )
439     return;
440   this->_loadPluginsFromPath( d.selectedFiles( ).begin( )->toStdString( ) );
441 }
442
443 // -------------------------------------------------------------------------
444 void cpBaseQtApplication::MainWindow::
445 _actorsProperties( )
446 {
447   if( this->m_MPR != NULL )
448   {
449     auto dlg = new cpExtensions::QT::ConfigurationChooser( this );
450     dlg->setData( this->m_MPR );
451     dlg->exec( );
452
453   } // fi
454 }
455
456 // -------------------------------------------------------------------------
457 /* TODO
458    void cpBaseQtApplication::MainWindow::
459    _ClearWorkspace( )
460    {
461    if( this->m_Editor != NULL )
462    this->m_Editor->clear( );
463    this->m_Workspace->Clear( );
464    }
465 */
466
467 // eof - $RCSfile$