1 #include <cpPipelineEditor/BaseQtMainWindow.h>
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpPipelineEditor/Editor.h>
5 #include <QApplication>
10 #include <QTreeWidget>
12 // -------------------------------------------------------------------------
13 bool cpPipelineEditor::BaseQtMainWindow::_TBlocker::
14 eventFilter( QObject* obj, QEvent* event )
16 return( true ); // -> Block all events
17 /* NOTE: correct implementation:
18 switch( event->type( ) )
20 //list event you want to prevent here ...
21 case QEvent::KeyPress:
22 case QEvent::KeyRelease:
23 case QEvent::MouseButtonRelease:
24 case QEvent::MouseButtonPress:
25 case QEvent::MouseButtonDblClick:
29 return( this->QObject::eventFilter( obj, event ) );
33 // -------------------------------------------------------------------------
34 cpPipelineEditor::BaseQtMainWindow::
36 int argc, char* argv[],
40 : Superclass( parent ),
46 this->m_Interface.GuessAccesiblePlugins( );
48 QFileInfo info( argv[ 0 ] );
51 auto exec_dir = info.canonicalPath( ).toStdString( );
52 this->_LoadPluginsFromPath( exec_dir );
55 this->_UpdateLoadedPlugins( );
58 this->m_Workspace.SetInterface( &( this->m_Interface ) );
59 this->m_Workspace.PrintExecutionOn( );
62 // -------------------------------------------------------------------------
63 cpPipelineEditor::BaseQtMainWindow::
66 this->m_Interface.UnloadAll( );
69 // -------------------------------------------------------------------------
70 void cpPipelineEditor::BaseQtMainWindow::
73 cpExtensions::QT::SimpleMPRWidget* mpr,
74 cpPipelineEditor::Editor* editor
77 this->m_TreeWidget = tree;
78 if( this->m_TreeWidget != NULL )
79 this->_UpdateLoadedPlugins( );
80 this->m_Editor = editor;
81 if( this->m_Editor != NULL )
82 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
84 this->m_Workspace.SetMPRViewer( mpr );
87 // -------------------------------------------------------------------------
88 void cpPipelineEditor::BaseQtMainWindow::
89 _LoadPlugins( const std::string& filename )
93 this->m_Interface.LoadPluginFile( filename );
94 this->_UpdateLoadedPlugins( );
96 catch( std::exception& err )
98 QMessageBox::critical(
100 "Error loading plugins path",
107 // -------------------------------------------------------------------------
108 void cpPipelineEditor::BaseQtMainWindow::
109 _LoadPluginsFromPath( const std::string& path )
113 this->m_Interface.LoadPluginDir( path );
114 this->_UpdateLoadedPlugins( );
116 catch( std::exception& err )
118 QMessageBox::critical(
120 "Error loading plugins path",
127 // -------------------------------------------------------------------------
128 void cpPipelineEditor::BaseQtMainWindow::
129 _UpdateLoadedPlugins( )
132 auto filters = this->m_Interface.GetFilters( );
133 if( filters.size( ) == 0 )
136 QMessageBox::critical(
138 "Error loading default plugins",
139 "No plugins loaded: remember to load some!!!"
145 if( this->m_TreeWidget != NULL )
147 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
149 // Create or get category
150 QList< QTreeWidgetItem* > cat_items =
151 this->m_TreeWidget->findItems(
152 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
154 QTreeWidgetItem* cat = NULL;
155 if( cat_items.size( ) == 0 )
157 cat = new QTreeWidgetItem(
158 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
160 this->m_TreeWidget->addTopLevelItem( cat );
163 cat = cat_items[ 0 ];
166 auto fIt = cIt->second.begin( );
167 for( ; fIt != cIt->second.end( ); ++fIt )
169 QList< QTreeWidgetItem* > filter_items =
170 this->m_TreeWidget->findItems(
171 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
173 auto fiIt = filter_items.begin( );
174 auto found_fiIt = filter_items.end( );
175 for( ; fiIt != filter_items.end( ); ++fiIt )
176 if( ( *fiIt )->parent( ) == cat )
180 if( found_fiIt == filter_items.end( ) )
181 QTreeWidgetItem* filter = new QTreeWidgetItem(
182 cat, QStringList( fIt->c_str( ) )
191 this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
194 // -------------------------------------------------------------------------
195 void cpPipelineEditor::BaseQtMainWindow::
198 this->m_Application->setOverrideCursor( Qt::WaitCursor );
199 this->m_Application->installEventFilter( &( this->m_Blocker ) );
202 // -------------------------------------------------------------------------
203 void cpPipelineEditor::BaseQtMainWindow::
206 while( this->m_Application->overrideCursor( ) )
207 this->m_Application->restoreOverrideCursor( );
208 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
211 // -------------------------------------------------------------------------
212 void cpPipelineEditor::BaseQtMainWindow::
213 _LoadWorkspace( const std::string& filename )
215 std::string err = this->m_Workspace.LoadWorkspace( filename );
218 QMessageBox::critical(
220 QMessageBox::tr( "Error loading workspace" ),
221 QMessageBox::tr( err.c_str( ) )
226 if( this->m_Editor != NULL )
227 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
232 // -------------------------------------------------------------------------
233 void cpPipelineEditor::BaseQtMainWindow::
234 _SaveWorkspace( const std::string& filename )
236 std::string err = this->m_Workspace.SaveWorkspace( filename );
238 QMessageBox::critical(
240 QMessageBox::tr( "Error saving workspace" ),
241 QMessageBox::tr( err.c_str( ) )
245 // -------------------------------------------------------------------------
246 void cpPipelineEditor::BaseQtMainWindow::
247 _InteractiveLoadPlugins( )
249 QFileDialog dlg( this );
250 dlg.setFileMode( QFileDialog::ExistingFiles );
251 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
253 std::stringstream name_filter;
254 std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
256 << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
257 dlg.setNameFilter( name_filter.str( ).c_str( ) );
258 dlg.setDefaultSuffix( suffix.c_str( ) );
259 if( !( dlg.exec( ) ) )
262 QStringList names = dlg.selectedFiles( );
263 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
264 this->_LoadPlugins( qIt->toStdString( ) );
267 // -------------------------------------------------------------------------
268 void cpPipelineEditor::BaseQtMainWindow::
269 _InteractiveLoadPluginsFromPath( )
271 QFileDialog dlg( this );
272 dlg.setFileMode( QFileDialog::DirectoryOnly );
273 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
274 if( !( dlg.exec( ) ) )
276 this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
279 // -------------------------------------------------------------------------
280 void cpPipelineEditor::BaseQtMainWindow::
281 _InteractiveLoadWorkspace( )
283 QFileDialog dlg( this );
284 dlg.setFileMode( QFileDialog::ExistingFile );
285 dlg.setDirectory( "." );
287 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
289 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
290 if( !( dlg.exec( ) ) )
292 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
295 // -------------------------------------------------------------------------
296 void cpPipelineEditor::BaseQtMainWindow::
297 _InteractiveSaveWorkspace( )
299 QFileDialog dlg( this );
300 dlg.setFileMode( QFileDialog::AnyFile );
301 dlg.setDirectory( "." );
302 dlg.setAcceptMode( QFileDialog::AcceptSave );
304 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
306 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
307 if( !( dlg.exec( ) ) )
309 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
312 // -------------------------------------------------------------------------
313 void cpPipelineEditor::BaseQtMainWindow::
314 _ExecFilter( const std::string& filter_name )
319 this->m_Workspace.Execute( filter_name );
322 catch( itk::ExceptionObject& err1 )
325 QMessageBox::critical(
327 QMessageBox::tr( "Error executing filter" ),
328 QMessageBox::tr( err1.GetDescription( ) )
331 catch( std::exception& err2 )
334 QMessageBox::critical(
336 QMessageBox::tr( "Error executing filter" ),
337 QMessageBox::tr( err2.what( ) )
343 QMessageBox::critical(
345 QMessageBox::tr( "Error executing filter" ),
346 QMessageBox::tr( "Unknown error" )