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