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 std::stringstream name_filter;
161 std::string suffix = std::string( PLUGIN_EXT ).substr( 1 );
163 name_filter << "Plugins file (*" << PLUGIN_EXT << ");;All files (*)";
164 dlg.setNameFilter( name_filter.str( ).c_str( ) );
165 dlg.setDefaultSuffix( suffix.c_str( ) );
167 if( !( dlg.exec( ) ) )
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;
177 // Show an error message
178 std::string err = err_str.str( );
179 if( err.size( ) > 0 )
180 QMessageBox::critical(
182 "Error loading plugins",
187 this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath );
188 this->_UpdateLoadedPlugins( );
191 // -------------------------------------------------------------------------
192 void App_cpPipelineEditor::
193 _ButtonLoadPluginsPath( )
195 QFileDialog dlg( this );
196 dlg.setFileMode( QFileDialog::DirectoryOnly );
197 dlg.setDirectory( "." );
198 if( !( dlg.exec( ) ) )
202 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
203 if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) )
204 QMessageBox::critical(
206 "Error loading plugins directory",
211 this->m_Plugins->SaveDefaultConfiguration( this->m_PluginsPath );
212 this->_UpdateLoadedPlugins( );
215 // -------------------------------------------------------------------------
216 void App_cpPipelineEditor::
217 _ActionOpenWorkspace( )
219 QFileDialog dlg( this );
220 dlg.setFileMode( QFileDialog::ExistingFile );
221 dlg.setDirectory( "." );
223 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
225 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
226 if( !( dlg.exec( ) ) )
228 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
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 );
238 delete this->m_Workspace;
239 this->m_Workspace = NULL;
240 QMessageBox::critical(
242 QMessageBox::tr( "Error loading workspace" ),
243 QMessageBox::tr( err.c_str( ) )
247 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
250 // -------------------------------------------------------------------------
251 void App_cpPipelineEditor::
252 _ActionSaveWorkspace( )
254 if( this->m_Workspace == NULL )
257 QFileDialog dlg( this );
258 dlg.setFileMode( QFileDialog::AnyFile );
259 dlg.setDirectory( "." );
260 dlg.setAcceptMode( QFileDialog::AcceptSave );
262 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
264 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
265 if( !( dlg.exec( ) ) )
267 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
269 std::string err = this->m_Workspace->SaveWorkspace( fname );
271 QMessageBox::critical(
273 QMessageBox::tr( "Error saving workspace" ),
274 QMessageBox::tr( err.c_str( ) )
279 // -------------------------------------------------------------------------
280 void App_cpPipelineEditor::
281 _ExecFilter( const std::string& filter_name )
283 if( this->m_Workspace != NULL )
285 // Update filter, if needed
286 std::string err = this->m_Workspace->Execute( filter_name );
288 QMessageBox::critical(
290 QMessageBox::tr( "Error executing filter" ),
291 QMessageBox::tr( err.c_str( ) )
297 // -------------------------------------------------------------------------
298 void App_cpPipelineEditor::
300 const std::string& filter_name, const std::string& output_name
303 typedef cpPlugins::Interface::DataObject _TDataObject;
305 // Update filter, if needed
306 this->_ExecFilter( filter_name );
309 auto filter = this->m_Workspace->GetFilter( filter_name );
312 auto output = filter->GetOutputData< _TDataObject >( output_name );
315 std::string data_name = output_name + "@" + filter_name;
316 if( this->m_UI->Viewer->AddData( output, data_name, "" ) )
318 if( this->m_UI->Viewer->GetNumberOfData( ) > 1 )
319 this->m_UI->Viewer->SetDataColor( data_name, 1, 0, 0 );
321 this->m_UI->Viewer->SetMainImage( data_name );
322 this->m_UI->Viewer->ShowData( data_name );
325 QMessageBox::critical(
327 QMessageBox::tr( "Error showing data" ),
328 QMessageBox::tr( "No known VTK conversion!" )