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