1 #include "App_cpPipelineEditor.h"
2 #include "ui_App_cpPipelineEditor.h"
4 #include <cpPipelineEditor/Editor.h>
9 #include <vtkImageData.h>
10 #include <vtkPolyData.h>
12 #include <cpPlugins/Interface/Workspace.h>
13 #include <cpPlugins/Interface/DataObject.h>
15 // -------------------------------------------------------------------------
16 #define App_cpPipelineEditor_ConnectAction( ACTION ) \
18 this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
19 this, SLOT( _Action##ACTION( ) ) \
22 // -------------------------------------------------------------------------
23 #define App_cpPipelineEditor_ConnectButton( BUTTON ) \
25 this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ), \
26 this, SLOT( _Button##BUTTON( ) ) \
29 // -------------------------------------------------------------------------
30 App_cpPipelineEditor::
31 App_cpPipelineEditor( int argc, char* argv[], QWidget* parent )
32 : QMainWindow( parent ),
33 m_UI( new Ui::App_cpPipelineEditor ),
37 this->m_UI->setupUi( this );
39 // Prepare plugins interface
40 this->m_Plugins = new cpPlugins::Interface::Interface( );
41 QFileInfo info( argv[ 0 ] );
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(
50 "Error creating default plugins configuration",
51 "Could not save default plugins configuration"
53 this->_UpdateLoadedPlugins( );
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 );
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 );
69 this->m_UI->Canvas->editor( ),
70 SIGNAL( execFilter( const std::string& ) ),
72 SLOT( _ExecFilter( const std::string& ) )
75 this->m_UI->Canvas->editor( ),
76 SIGNAL( showFilterOutput( const std::string&, const std::string& ) ),
78 SLOT( _ShowFilterOutput( const std::string&, const std::string& ) )
82 // -------------------------------------------------------------------------
83 App_cpPipelineEditor::
84 ~App_cpPipelineEditor( )
87 if( this->m_Workspace != NULL )
88 delete this->m_Workspace;
89 delete this->m_Plugins;
92 // -------------------------------------------------------------------------
93 void App_cpPipelineEditor::
94 _UpdateLoadedPlugins( )
96 auto& classes = this->m_Plugins->GetClasses( );
98 if( classes.size( ) == 0 )
100 QMessageBox::critical(
102 "Error loading default plugins",
103 "No plugins loaded: remember to load some!!!"
109 auto catIt = classes.begin( );
110 for( ; catIt != classes.end( ); ++catIt )
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
117 QTreeWidgetItem* cat = NULL;
118 if( cat_items.size( ) == 0 )
120 cat = new QTreeWidgetItem(
121 ( QTreeWidgetItem* )( NULL ), QStringList( catIt->first.c_str( ) )
123 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
126 cat = cat_items[ 0 ];
129 auto fIt = catIt->second.begin( );
130 for( ; fIt != catIt->second.end( ); ++fIt )
132 QList< QTreeWidgetItem* > filter_items =
133 this->m_UI->LoadedPlugins->findItems(
134 fIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
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 )
143 if( found_fiIt == filter_items.end( ) )
144 QTreeWidgetItem* filter = new QTreeWidgetItem(
145 cat, QStringList( fIt->first.c_str( ) )
152 // -------------------------------------------------------------------------
153 void App_cpPipelineEditor::
154 _ButtonLoadPluginsFile( )
156 QFileDialog dlg( this );
157 dlg.setFileMode( QFileDialog::ExistingFiles );
158 dlg.setDirectory( "." );
160 dlg.setNameFilter( "Plugins file (*.dll);;All files (*)" );
161 dlg.setDefaultSuffix( "dll" );
163 dlg.setNameFilter( "Plugins file (*.so);;All files (*)" );
164 dlg.setDefaultSuffix( "so" );
166 if( !( dlg.exec( ) ) )
170 QStringList names = dlg.selectedFiles( );
171 std::stringstream err_str;
172 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
173 if( !( this->m_Plugins->Load( qIt->toStdString( ) ) ) )
174 err_str << qIt->toStdString( ) << std::endl;
176 // Show an error message
177 std::string err = err_str.str( );
178 if( err.size( ) > 0 )
179 QMessageBox::critical(
181 "Error loading plugins",
186 this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath );
187 this->_UpdateLoadedPlugins( );
190 // -------------------------------------------------------------------------
191 void App_cpPipelineEditor::
192 _ButtonLoadPluginsPath( )
194 QFileDialog dlg( this );
195 dlg.setFileMode( QFileDialog::DirectoryOnly );
196 dlg.setDirectory( "." );
197 if( !( dlg.exec( ) ) )
201 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
202 if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) )
203 QMessageBox::critical(
205 "Error loading plugins directory",
210 this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath );
211 this->_UpdateLoadedPlugins( );
214 // -------------------------------------------------------------------------
215 void App_cpPipelineEditor::
216 _ActionOpenWorkspace( )
218 QFileDialog dlg( this );
219 dlg.setFileMode( QFileDialog::ExistingFile );
220 dlg.setDirectory( "." );
222 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
224 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
225 if( !( dlg.exec( ) ) )
227 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
229 if( this->m_Workspace != NULL )
230 delete this->m_Workspace;
231 this->m_Workspace = new cpPlugins::Interface::Workspace( );
232 this->m_Workspace->SetPlugins( this->m_Plugins );
233 this->m_Workspace->SetMPRViewer( this->m_UI->Viewer );
234 std::string err = this->m_Workspace->LoadWorkspace( fname );
237 delete this->m_Workspace;
238 this->m_Workspace = NULL;
239 QMessageBox::critical(
241 QMessageBox::tr( "Error loading workspace" ),
242 QMessageBox::tr( err.c_str( ) )
246 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
249 // -------------------------------------------------------------------------
250 void App_cpPipelineEditor::
251 _ActionSaveWorkspace( )
253 if( this->m_Workspace == NULL )
256 QFileDialog dlg( this );
257 dlg.setFileMode( QFileDialog::AnyFile );
258 dlg.setDirectory( "." );
259 dlg.setAcceptMode( QFileDialog::AcceptSave );
261 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
263 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
264 if( !( dlg.exec( ) ) )
266 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
268 std::string err = this->m_Workspace->SaveWorkspace( fname );
270 QMessageBox::critical(
272 QMessageBox::tr( "Error saving workspace" ),
273 QMessageBox::tr( err.c_str( ) )
278 // -------------------------------------------------------------------------
279 void App_cpPipelineEditor::
280 _ExecFilter( const std::string& filter_name )
282 if( this->m_Workspace != NULL )
284 // Update filter, if needed
285 std::string err = this->m_Workspace->Execute( filter_name );
287 QMessageBox::critical(
289 QMessageBox::tr( "Error executing filter" ),
290 QMessageBox::tr( err.c_str( ) )
296 // -------------------------------------------------------------------------
297 void App_cpPipelineEditor::
299 const std::string& filter_name, const std::string& output_name
302 typedef cpPlugins::Interface::DataObject _TDataObject;
304 // Update filter, if needed
305 this->_ExecFilter( filter_name );
308 auto filter = this->m_Workspace->GetFilter( filter_name );
311 auto output = filter->GetOutput< _TDataObject >( output_name );
314 std::string data_name = output_name + "@" + filter_name;
315 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
317 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
318 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
320 this->m_UI->Viewer->SetMainImage( data_name );
321 this->m_UI->Viewer->ShowData( data_name );
324 QMessageBox::critical(
326 QMessageBox::tr( "Error showing data" ),
327 QMessageBox::tr( "No known VTK conversion!" )