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