]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/App_cpPipelineEditor.cxx
af73df5f21c95b66ef258eb01a51a44d5663abad
[cpPlugins.git] / appli / cpPipelineEditor / App_cpPipelineEditor.cxx
1 #include "App_cpPipelineEditor.h"
2 #include "ui_App_cpPipelineEditor.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/Interface/Workspace.h>
13 #include <cpPlugins/Interface/DataObject.h>
14
15 // -------------------------------------------------------------------------
16 #define App_cpPipelineEditor_ConnectAction( ACTION )    \
17   this->connect(                                        \
18     this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
19     this, SLOT( _Action##ACTION( ) )                    \
20     )
21
22 // -------------------------------------------------------------------------
23 #define App_cpPipelineEditor_ConnectButton( BUTTON )    \
24   this->connect(                                        \
25     this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ),   \
26     this, SLOT( _Button##BUTTON( ) )                    \
27     )
28
29 // -------------------------------------------------------------------------
30 App_cpPipelineEditor::
31 App_cpPipelineEditor( int argc, char* argv[], QWidget* parent )
32   : QMainWindow( parent ),
33     m_UI( new Ui::App_cpPipelineEditor ),
34     m_Workspace( NULL ),
35     m_PluginsPath( "." )
36 {
37   this->m_UI->setupUi( this );
38
39   // Prepare plugins interface
40   this->m_Plugins = new cpPlugins::Interface::Interface( );
41   QFileInfo info( argv[ 0 ] );
42   if( info.exists( ) )
43   {
44     this->m_PluginsPath = info.canonicalPath( ).toStdString( );
45     if( !( this->m_Plugins->LoadDefaultConfiguration( this->m_PluginsPath ) ) )
46       if( this->m_Plugins->LoadFromFolder( this->m_PluginsPath, false ) )
47         if( !( this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath ) ) )
48           QMessageBox::critical(
49             this,
50             "Error creating default plugins configuration",
51             "Could not save default plugins configuration"
52             );
53     this->_UpdateLoadedPlugins( );
54
55   } // fi
56
57   // Create an empty workspace
58   this->m_Workspace = new cpPlugins::Interface::Workspace( );
59   this->m_Workspace->SetPlugins( this->m_Plugins );
60   this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
61   this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
62
63   // Connect actions to slots
64   App_cpPipelineEditor_ConnectButton( LoadPluginsFile );
65   App_cpPipelineEditor_ConnectButton( LoadPluginsPath );
66   App_cpPipelineEditor_ConnectAction( OpenWorkspace );
67   App_cpPipelineEditor_ConnectAction( SaveWorkspace );
68   this->connect(
69     this->m_UI->Canvas->editor( ),
70     SIGNAL( execFilter( const std::string& ) ),
71     this,
72     SLOT( _ExecFilter( const std::string& ) )
73     );
74   this->connect(
75     this->m_UI->Canvas->editor( ),
76     SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
77     this,
78     SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
79     );
80 }
81
82 // -------------------------------------------------------------------------
83 App_cpPipelineEditor::
84 ~App_cpPipelineEditor( )
85 {
86   delete this->m_UI;
87   if( this->m_Workspace != NULL )
88     delete this->m_Workspace;
89   delete this->m_Plugins;
90 }
91
92 // -------------------------------------------------------------------------
93 void App_cpPipelineEditor::
94 _UpdateLoadedPlugins( )
95 {
96   auto& classes = this->m_Plugins->GetClasses( );
97
98   if( classes.size( ) == 0 )
99   {
100     QMessageBox::critical(
101       this,
102       "Error loading default plugins",
103       "No plugins loaded: remember to load some!!!"
104       );
105     return;
106
107   } // fi
108
109   auto catIt = classes.begin( );
110   for( ; catIt != classes.end( ); ++catIt )
111   {
112     // Create or get category
113     QList< QTreeWidgetItem* > cat_items =
114       this->m_UI->LoadedPlugins->findItems(
115         catIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
116         );
117     QTreeWidgetItem* cat = NULL;
118     if( cat_items.size( ) == 0 )
119     {
120       cat = new QTreeWidgetItem(
121         ( QTreeWidgetItem* )( NULL ), QStringList( catIt->first.c_str( ) )
122         );
123       this->m_UI->LoadedPlugins->addTopLevelItem( cat );
124     }
125     else
126       cat = cat_items[ 0 ];
127
128     // Create filters
129     auto fIt = catIt->second.begin( );
130     for( ; fIt != catIt->second.end( ); ++fIt )
131     {
132       QList< QTreeWidgetItem* > filter_items =
133         this->m_UI->LoadedPlugins->findItems(
134           fIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
135           );
136       auto fiIt = filter_items.begin( );
137       auto found_fiIt = filter_items.end( );
138       for( ; fiIt != filter_items.end( ); ++fiIt )
139         if( ( *fiIt )->parent( ) == cat )
140           found_fiIt = fiIt;
141
142       // Add filter
143       if( found_fiIt == filter_items.end( ) )
144         QTreeWidgetItem* filter = new QTreeWidgetItem(
145           cat, QStringList( fIt->first.c_str( ) )
146           );
147     } // rof
148
149   } // rof
150 }
151
152 // -------------------------------------------------------------------------
153 void App_cpPipelineEditor::
154 _ButtonLoadPluginsFile( )
155 {
156   QFileDialog dlg( this );
157   dlg.setFileMode( QFileDialog::ExistingFiles );
158   dlg.setDirectory( "." );
159
160   std::stringstream name_filter;
161   std::string suffix = std::string( PLUGIN_EXT ).substr( 1 );
162
163   name_filter << "Plugins file (*" << PLUGIN_EXT << ");;All files (*)";
164   dlg.setNameFilter( name_filter.str( ).c_str( ) );
165   dlg.setDefaultSuffix( suffix.c_str( ) );
166
167   if( !( dlg.exec( ) ) )
168     return;
169
170   // Read
171   QStringList names = dlg.selectedFiles( );
172   std::stringstream err_str;
173   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
174     if( !( this->m_Plugins->Load( qIt->toStdString( ) ) ) )
175       err_str << qIt->toStdString( ) << std::endl;
176
177   // Show an error message
178   std::string err = err_str.str( );
179   if( err.size( ) > 0 )
180     QMessageBox::critical(
181       this,
182       "Error loading plugins",
183       err.c_str( )
184       );
185
186   // Update view
187   this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath );
188   this->_UpdateLoadedPlugins( );
189 }
190
191 // -------------------------------------------------------------------------
192 void App_cpPipelineEditor::
193 _ButtonLoadPluginsPath( )
194 {
195   QFileDialog dlg( this );
196   dlg.setFileMode( QFileDialog::DirectoryOnly );
197   dlg.setDirectory( "." );
198   if( !( dlg.exec( ) ) )
199     return;
200
201   // Read
202   std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
203   if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) )
204     QMessageBox::critical(
205       this,
206       "Error loading plugins directory",
207       dir.c_str( )
208       );
209
210   // Update view
211   this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath );
212   this->_UpdateLoadedPlugins( );
213 }
214
215 // -------------------------------------------------------------------------
216 void App_cpPipelineEditor::
217 _ActionOpenWorkspace( )
218 {
219   QFileDialog dlg( this );
220   dlg.setFileMode( QFileDialog::ExistingFile );
221   dlg.setDirectory( "." );
222   dlg.setNameFilter(
223     QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
224     );
225   dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
226   if( !( dlg.exec( ) ) )
227     return;
228   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
229
230   if( this->m_Workspace != NULL )
231     delete this->m_Workspace;
232   this->m_Workspace = new cpPlugins::Interface::Workspace( );
233   this->m_Workspace->SetPlugins( this->m_Plugins );
234   this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
235   std::string err = this->m_Workspace->LoadWorkspace( fname );
236   if( err != "" )
237   {
238     delete this->m_Workspace;
239     this->m_Workspace = NULL;
240     QMessageBox::critical(
241       this,
242       QMessageBox::tr( "Error loading workspace" ),
243       QMessageBox::tr( err.c_str( ) )
244       );
245   }
246   else
247     this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
248 }
249
250 // -------------------------------------------------------------------------
251 void App_cpPipelineEditor::
252 _ActionSaveWorkspace( )
253 {
254   if( this->m_Workspace == NULL )
255     return;
256
257   QFileDialog dlg( this );
258   dlg.setFileMode( QFileDialog::AnyFile );
259   dlg.setDirectory( "." );
260   dlg.setAcceptMode( QFileDialog::AcceptSave );
261   dlg.setNameFilter(
262     QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
263     );
264   dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
265   if( !( dlg.exec( ) ) )
266     return;
267   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
268
269   std::string err = this->m_Workspace->SaveWorkspace( fname );
270   if( err != "" )
271     QMessageBox::critical(
272       this,
273       QMessageBox::tr( "Error saving workspace" ),
274       QMessageBox::tr( err.c_str( ) )
275       );
276 }
277
278
279 // -------------------------------------------------------------------------
280 void App_cpPipelineEditor::
281 _ExecFilter( const std::string& filter_name )
282 {
283   if( this->m_Workspace != NULL )
284   {
285     // Update filter, if needed
286     std::string err = this->m_Workspace->Execute( filter_name );
287     if( err != "" )
288       QMessageBox::critical(
289         this,
290         QMessageBox::tr( "Error executing filter" ),
291         QMessageBox::tr( err.c_str( ) )
292         );
293
294   } // fi
295 }
296
297 // -------------------------------------------------------------------------
298 void App_cpPipelineEditor::
299 _ShowFilterOutput(
300   const std::string& filter_name, const std::string& output_name
301   )
302 {
303   typedef cpPlugins::Interface::DataObject _TDataObject;
304
305   // Update filter, if needed
306   this->_ExecFilter( filter_name );
307
308   // Get output
309   auto filter = this->m_Workspace->GetFilter( filter_name );
310   if( filter != NULL )
311   {
312     auto output = filter->GetOutputData( output_name );
313     if( output != NULL )
314     {
315       std::string data_name = output_name + "@" + filter_name;
316       if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
317       {
318         if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
319           this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
320         else
321           this->m_UI->Viewer->SetMainImage( data_name );
322         this->m_UI->Viewer->ShowData( data_name );
323       }
324       else
325         QMessageBox::critical(
326           this,
327           QMessageBox::tr( "Error showing data" ),
328           QMessageBox::tr( "No known VTK conversion!" )
329           );
330
331     } // fi
332
333   } // fi
334 }
335
336 // eof - $RCSfile$