1 #include "App_cpPipelineEditor.h"
2 #include "ui_App_cpPipelineEditor.h"
4 #include <cpPipelineEditor/Editor.h>
8 #include <cpPlugins/Interface/Workspace.h>
10 // -------------------------------------------------------------------------
11 #define App_cpPipelineEditor_ConnectAction( ACTION ) \
13 this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
14 this, SLOT( _Action##ACTION( ) ) \
17 // -------------------------------------------------------------------------
18 #define App_cpPipelineEditor_ConnectButton( BUTTON ) \
20 this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ), \
21 this, SLOT( _Button##BUTTON( ) ) \
24 // -------------------------------------------------------------------------
25 App_cpPipelineEditor::
26 App_cpPipelineEditor( int argc, char* argv[], QWidget* parent )
27 : QMainWindow( parent ),
28 m_UI( new Ui::App_cpPipelineEditor ),
31 this->m_UI->setupUi( this );
33 // Prepare plugins interface
34 this->m_Plugins = new cpPlugins::Interface::Interface( );
35 QFileInfo info( argv[ 0 ] );
38 std::string path = info.canonicalPath( ).toStdString( );
39 if( !( this->m_Plugins->LoadDefaultConfiguration( path ) ) )
40 if( this->m_Plugins->LoadFromFolder( path, false ) )
41 if( !( this->m_Plugins->SaveDefaultConfiguration( path ) ) )
42 QMessageBox::critical(
44 "Error creating default plugins configuration",
45 "Could not save default plugins configuration"
47 this->_UpdateLoadedPlugins( );
51 // Create an empty workspace
52 this->m_Workspace = new cpPlugins::Interface::Workspace( );
53 this->m_Workspace->SetPlugins( this->m_Plugins );
54 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
56 // Connect actions to slots
57 App_cpPipelineEditor_ConnectButton( LoadPluginsFile );
58 App_cpPipelineEditor_ConnectButton( LoadPluginsPath );
59 App_cpPipelineEditor_ConnectAction( OpenWorkspace );
60 App_cpPipelineEditor_ConnectAction( SaveWorkspace );
63 // -------------------------------------------------------------------------
64 App_cpPipelineEditor::
65 ~App_cpPipelineEditor( )
68 if( this->m_Workspace != NULL )
69 delete this->m_Workspace;
70 delete this->m_Plugins;
73 // -------------------------------------------------------------------------
74 void App_cpPipelineEditor::
75 _UpdateLoadedPlugins( )
77 auto& classes = this->m_Plugins->GetClasses( );
79 if( classes.size( ) == 0 )
81 QMessageBox::critical(
83 "Error loading default plugins",
84 "No plugins loaded: remember to load some!!!"
90 auto catIt = classes.begin( );
91 for( ; catIt != classes.end( ); ++catIt )
93 // Create or get category
94 QList< QTreeWidgetItem* > cat_items =
95 this->m_UI->LoadedPlugins->findItems(
96 catIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
98 QTreeWidgetItem* cat = NULL;
99 if( cat_items.size( ) == 0 )
101 cat = new QTreeWidgetItem(
102 ( QTreeWidgetItem* )( NULL ), QStringList( catIt->first.c_str( ) )
104 this->m_UI->LoadedPlugins->addTopLevelItem( cat );
107 cat = cat_items[ 0 ];
110 auto fIt = catIt->second.begin( );
111 for( ; fIt != catIt->second.end( ); ++fIt )
113 QList< QTreeWidgetItem* > filter_items =
114 this->m_UI->LoadedPlugins->findItems(
115 fIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
117 auto fiIt = filter_items.begin( );
118 auto found_fiIt = filter_items.end( );
119 for( ; fiIt != filter_items.end( ); ++fiIt )
120 if( ( *fiIt )->parent( ) == cat )
124 if( found_fiIt == filter_items.end( ) )
125 QTreeWidgetItem* filter = new QTreeWidgetItem(
126 cat, QStringList( fIt->first.c_str( ) )
133 // -------------------------------------------------------------------------
134 void App_cpPipelineEditor::
135 _ButtonLoadPluginsFile( )
137 QFileDialog dlg( this );
138 dlg.setFileMode( QFileDialog::ExistingFiles );
139 dlg.setDirectory( "." );
141 dlg.setNameFilter( "Plugins file (*.dll);;All files (*)" );
142 dlg.setDefaultSuffix( "dll" );
144 dlg.setNameFilter( "Plugins file (*.so);;All files (*)" );
145 dlg.setDefaultSuffix( "so" );
147 if( !( dlg.exec( ) ) )
151 QStringList names = dlg.selectedFiles( );
152 std::stringstream err_str;
153 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
154 if( !( this->m_Plugins->Load( qIt->toStdString( ) ) ) )
155 err_str << qIt->toStdString( ) << std::endl;
157 // Show an error message
158 std::string err = err_str.str( );
159 if( err.size( ) > 0 )
160 QMessageBox::critical(
162 "Error loading plugins",
167 this->_UpdateLoadedPlugins( );
170 // -------------------------------------------------------------------------
171 void App_cpPipelineEditor::
172 _ButtonLoadPluginsPath( )
174 QFileDialog dlg( this );
175 dlg.setFileMode( QFileDialog::DirectoryOnly );
176 dlg.setDirectory( "." );
177 if( !( dlg.exec( ) ) )
181 std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
182 if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) )
183 QMessageBox::critical(
185 "Error loading plugins directory",
190 this->_UpdateLoadedPlugins( );
193 // -------------------------------------------------------------------------
194 void App_cpPipelineEditor::
195 _ActionOpenWorkspace( )
197 QFileDialog dlg( this );
198 dlg.setFileMode( QFileDialog::ExistingFile );
199 dlg.setDirectory( "." );
201 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
203 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
204 if( !( dlg.exec( ) ) )
206 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
208 if( this->m_Workspace != NULL )
209 delete this->m_Workspace;
210 this->m_Workspace = new cpPlugins::Interface::Workspace( );
211 this->m_Workspace->SetPlugins( this->m_Plugins );
212 std::string err = this->m_Workspace->LoadWorkspace( fname );
215 delete this->m_Workspace;
216 this->m_Workspace = NULL;
217 QMessageBox::critical(
219 QMessageBox::tr( "Error loading workspace" ),
220 QMessageBox::tr( err.c_str( ) )
224 this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
227 // -------------------------------------------------------------------------
228 void App_cpPipelineEditor::
229 _ActionSaveWorkspace( )
231 if( this->m_Workspace == NULL )
234 QFileDialog dlg( this );
235 dlg.setFileMode( QFileDialog::AnyFile );
236 dlg.setDirectory( "." );
237 dlg.setAcceptMode( QFileDialog::AcceptSave );
239 QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
241 dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
242 if( !( dlg.exec( ) ) )
244 std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
246 std::string err = this->m_Workspace->SaveWorkspace( fname );
248 QMessageBox::critical(
250 QMessageBox::tr( "Error saving workspace" ),
251 QMessageBox::tr( err.c_str( ) )