]> Creatis software - cpPlugins.git/blob - appli/PipelineEditor/PipelineEditor.cxx
9868abc7340bee0a894286470bc52a45a407ad18
[cpPlugins.git] / appli / PipelineEditor / PipelineEditor.cxx
1 #include "PipelineEditor.h"
2 #include "ui_PipelineEditor.h"
3
4 #include <cpPipelineEditor/Editor.h>
5
6 #include <QFileDialog>
7 #include <QMessageBox>
8
9 #include <vtkImageData.h>
10 #include <vtkPolyData.h>
11
12 #include <cpPlugins/DataObject.h>
13
14 // -------------------------------------------------------------------------
15 #define PipelineEditor_ConnectAction( ACTION )          \
16   this->connect(                                        \
17     this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
18     this, SLOT( _Action##ACTION( ) )                    \
19     )
20
21 // -------------------------------------------------------------------------
22 #define PipelineEditor_ConnectButton( BUTTON )          \
23   this->connect(                                        \
24     this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ),   \
25     this, SLOT( _Button##BUTTON( ) )                    \
26     )
27
28 // -------------------------------------------------------------------------
29 bool PipelineEditor_Blocker::
30 eventFilter( QObject* obj, QEvent* event )
31 {
32   return( true ); // -> Block all events
33   /* NOTE: 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 PipelineEditor::
51 PipelineEditor( int argc, char* argv[], QApplication* app, QWidget* parent )
52   : QMainWindow( parent ),
53     m_UI( new Ui::PipelineEditor ),
54     m_Application( app ),
55     m_Workspace( NULL ),
56     m_PluginsPath( "." )
57 {
58   this->m_UI->setupUi( this );
59
60   // Prepare plugins interface
61   QFileInfo info( argv[ 0 ] );
62   if( info.exists( ) )
63   {
64     if( !( this->m_Interface.LoadConfiguration( cpPlugins_CONFIG_FILE ) ) )
65     {
66       this->m_PluginsPath = info.canonicalPath( ).toStdString( );
67       this->_LoadPluginsFromPath( this->m_PluginsPath );
68     }
69     else
70       this->_UpdateLoadedPlugins( );
71
72   } // fi
73
74   /* TODO
75      this->m_Interface = new cpPlugins::Interface( );
76      this->m_PluginsPath = info.canonicalPath( ).toStdString( );
77      if( !( this->m_Interface->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
78      if( this->m_Interface->LoadFromFolder( this->m_PluginsPath, false ) != "" )
79      if( !( this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
80      QMessageBox::critical(
81      this,
82      "Error creating default plugins configuration",
83      "Could not save default plugins configuration"
84      );
85      this->_UpdateLoadedPlugins( );
86
87      } // fi
88   */
89
90   // Create an empty workspace
91   this->m_Workspace = new cpPlugins::Workspace( );
92   this->m_Workspace->SetInterface( &( this->m_Interface ) );
93   this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
94   this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
95
96   // Connect actions to slots
97   PipelineEditor_ConnectButton( LoadPluginsFile );
98   PipelineEditor_ConnectButton( LoadPluginsPath );
99   PipelineEditor_ConnectAction( OpenWorkspace );
100   PipelineEditor_ConnectAction( SaveWorkspace );
101   this->connect(
102     this->m_UI->Canvas->editor( ),
103     SIGNAL( execFilter( const std::string& ) ),
104     this,
105     SLOT( _ExecFilter( const std::string& ) )
106     );
107   this->connect(
108     this->m_UI->Canvas->editor( ),
109     SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
110     this,
111     SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
112     );
113 }
114
115 // -------------------------------------------------------------------------
116 PipelineEditor::
117 ~PipelineEditor( )
118 {
119   if( this->m_Workspace != NULL )
120     delete this->m_Workspace;
121   delete this->m_UI;
122 }
123
124 // -------------------------------------------------------------------------
125 void PipelineEditor::
126 _LoadPluginsFromPath( const std::string& path )
127 {
128   QDir dir( path.c_str( ) );
129   std::stringstream filters_str;
130   filters_str << "*." << cpPlugins_PLUGIN_EXT;
131   QStringList filters;
132   filters << filters_str.str( ).c_str( );
133   auto files = dir.entryList( filters );
134   for( auto fIt = files.begin( ); fIt != files.end( ); ++fIt )
135   {
136     try
137     {
138       this->m_Interface.LoadPluginFile( fIt->toStdString( ) );
139     }
140     catch( ... )
141     {
142       // Just ignore un-loadable libraries
143     }
144
145   } // yrt
146   this->_UpdateLoadedPlugins( );
147 }
148
149 // -------------------------------------------------------------------------
150 void PipelineEditor::
151 _UpdateLoadedPlugins( )
152 {
153   this->_Block( );
154   auto filters = this->m_Interface.GetFilters( );
155   if( filters.size( ) == 0 )
156   {
157     QMessageBox::critical(
158       this,
159       "Error loading default plugins",
160       "No plugins loaded: remember to load some!!!"
161       );
162     return;
163
164   } // fi
165
166   for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
167   {
168     // Create or get category
169     QList< QTreeWidgetItem* > cat_items =
170       this->m_UI->LoadedPlugins->findItems(
171         cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
172         );
173     QTreeWidgetItem* cat = NULL;
174     if( cat_items.size( ) == 0 )
175     {
176       cat = new QTreeWidgetItem(
177         ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
178         );
179       this->m_UI->LoadedPlugins->addTopLevelItem( cat );
180     }
181     else
182       cat = cat_items[ 0 ];
183
184     // Create filters
185     auto fIt = cIt->second.begin( );
186     for( ; fIt != cIt->second.end( ); ++fIt )
187     {
188       QList< QTreeWidgetItem* > filter_items =
189         this->m_UI->LoadedPlugins->findItems(
190           fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
191           );
192       auto fiIt = filter_items.begin( );
193       auto found_fiIt = filter_items.end( );
194       for( ; fiIt != filter_items.end( ); ++fiIt )
195         if( ( *fiIt )->parent( ) == cat )
196           found_fiIt = fiIt;
197
198       // Add filter
199       if( found_fiIt == filter_items.end( ) )
200         QTreeWidgetItem* filter = new QTreeWidgetItem(
201           cat, QStringList( fIt->c_str( ) )
202           );
203
204     } // rof
205
206   } // rof
207   this->_UnBlock( );
208   this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
209 }
210
211 // -------------------------------------------------------------------------
212 void PipelineEditor::
213 _Block( )
214 {
215   this->m_Application->setOverrideCursor( Qt::WaitCursor );
216   this->m_Application->installEventFilter( &( this->m_Blocker ) );
217 }
218
219 // -------------------------------------------------------------------------
220 void PipelineEditor::
221 _UnBlock( )
222 {
223   while( this->m_Application->overrideCursor( ) )
224     this->m_Application->restoreOverrideCursor( );
225   this->m_Application->removeEventFilter( &( this->m_Blocker ) );
226 }
227
228 // -------------------------------------------------------------------------
229 void PipelineEditor::
230 _ButtonLoadPluginsFile( )
231 {
232   QFileDialog dlg( this );
233   dlg.setFileMode( QFileDialog::ExistingFiles );
234   dlg.setDirectory( "." );
235
236   std::stringstream name_filter;
237   std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
238   name_filter
239     << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
240   dlg.setNameFilter( name_filter.str( ).c_str( ) );
241   dlg.setDefaultSuffix( suffix.c_str( ) );
242
243   if( !( dlg.exec( ) ) )
244     return;
245
246   // Read
247   QStringList names = dlg.selectedFiles( );
248   std::stringstream err_str;
249   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
250   {
251     try
252     {
253       this->m_Interface.LoadPluginFile( qIt->toStdString( ) );
254     }
255     catch( std::exception& err )
256     {
257       err_str << err.what( ) << std::endl;
258
259     } // yrt
260
261   } // rof
262
263   // Show an error message
264   std::string err = err_str.str( );
265   if( err.size( ) > 0 )
266     QMessageBox::critical(
267       this,
268       "Error loading plugins",
269       err.c_str( )
270       );
271
272   // Update view
273   // TODO: this->m_Interface.SaveDefaultConfiguration( this->m_PluginsPath );
274   this->_UpdateLoadedPlugins( );
275 }
276
277 // -------------------------------------------------------------------------
278 void PipelineEditor::
279 _ButtonLoadPluginsPath( )
280 {
281   /*
282     QFileDialog dlg( this );
283     dlg.setFileMode( QFileDialog::DirectoryOnly );
284     dlg.setDirectory( "." );
285     if( !( dlg.exec( ) ) )
286     return;
287
288     // Read
289     std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
290     std::string err = this->m_Interface->LoadFromFolder( dir, false );
291     if( err != "" )
292     QMessageBox::critical(
293     this,
294     "Error loading plugins directory",
295     err.c_str( )
296     );
297
298     // Update view
299     this->m_Interface->SaveDefaultConfiguration( this->m_PluginsPath );
300     this->_UpdateLoadedPlugins( );
301   */
302 }
303
304 // -------------------------------------------------------------------------
305 void PipelineEditor::
306 _ActionOpenWorkspace( )
307 {
308   QFileDialog dlg( this );
309   dlg.setFileMode( QFileDialog::ExistingFile );
310   dlg.setDirectory( "." );
311   dlg.setNameFilter(
312     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
313     );
314   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
315   if( !( dlg.exec( ) ) )
316     return;
317
318   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
319   if( this->m_Workspace != NULL )
320     delete this->m_Workspace;
321   this->m_Workspace = new cpPlugins::Workspace( );
322   this->m_Workspace->SetInterface( &( this->m_Interface ) );
323   this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
324   std::string err = this->m_Workspace->LoadWorkspace( fname );
325   if( err != "" )
326   {
327     delete this->m_Workspace;
328     this->m_Workspace = NULL;
329     QMessageBox::critical(
330       this,
331       QMessageBox::tr( "Error loading workspace" ),
332       QMessageBox::tr( err.c_str( ) )
333       );
334   }
335   else
336     this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
337 }
338
339 // -------------------------------------------------------------------------
340 void PipelineEditor::
341 _ActionSaveWorkspace( )
342 {
343   if( this->m_Workspace == NULL )
344     return;
345
346   QFileDialog dlg( this );
347   dlg.setFileMode( QFileDialog::AnyFile );
348   dlg.setDirectory( "." );
349   dlg.setAcceptMode( QFileDialog::AcceptSave );
350   dlg.setNameFilter(
351     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
352     );
353   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
354   if( !( dlg.exec( ) ) )
355     return;
356   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
357
358   std::string err = this->m_Workspace->SaveWorkspace( fname );
359   if( err != "" )
360     QMessageBox::critical(
361       this,
362       QMessageBox::tr( "Error saving workspace" ),
363       QMessageBox::tr( err.c_str( ) )
364       );
365 }
366
367
368 // -------------------------------------------------------------------------
369 void PipelineEditor::
370 _ExecFilter( const std::string& filter_name )
371 {
372   if( this->m_Workspace != NULL )
373   {
374     // Update filter, if needed
375     this->_Block( );
376     std::string err = this->m_Workspace->Execute( filter_name );
377     this->_UnBlock( );
378     if( err != "" )
379       QMessageBox::critical(
380         this,
381         QMessageBox::tr( "Error executing filter" ),
382         QMessageBox::tr( err.c_str( ) )
383         );
384
385   } // fi
386 }
387
388 // -------------------------------------------------------------------------
389 void PipelineEditor::
390 _ShowFilterOutput(
391   const std::string& filter_name, const std::string& output_name
392   )
393 {
394   typedef cpPlugins::DataObject _TDataObject;
395
396   // Update filter, if needed
397   this->_ExecFilter( filter_name );
398
399   // Get output
400   auto filter = this->m_Workspace->GetFilter( filter_name );
401   if( filter != NULL )
402   {
403     auto output = filter->GetOutputData( output_name );
404     if( output != NULL )
405     {
406       std::string data_name = output_name + "@" + filter_name;
407       auto idata = output->GetVTK< vtkImageData >( );
408       auto mdata = output->GetVTK< vtkPolyData >( );
409       if( idata != NULL )
410       {
411         if( this->m_UI->Viewer->AddData( idata, data_name, "" ) )
412         {
413           if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
414             this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
415           else
416             this->m_UI->Viewer->SetMainImage( data_name );
417           this->_Block( );
418           this->m_UI->Viewer->ShowData( data_name );
419           this->_UnBlock( );
420
421         } // fi
422       }
423       else if( mdata != NULL )
424       {
425       }
426       else
427         QMessageBox::critical(
428           this,
429           QMessageBox::tr( "Error showing data" ),
430           QMessageBox::tr( "No known VTK conversion!" )
431           );
432
433       /* TODO
434          if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
435          {
436          if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
437          this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
438          else
439          this->m_UI->Viewer->SetMainImage( data_name );
440          this->m_UI->Viewer->ShowData( data_name );
441          }
442          else
443       */
444
445     } // fi
446
447   } // fi
448 }
449
450 // eof - $RCSfile$