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 ) );
61 // -------------------------------------------------------------------------
62 cpPipelineEditor::BaseQtMainWindow::
65 this->m_Interface.UnloadAll( );
68 // -------------------------------------------------------------------------
69 void cpPipelineEditor::BaseQtMainWindow::
72 cpExtensions::QT::SimpleMPRWidget* mpr,
73 cpPipelineEditor::Editor* editor
76 this->m_TreeWidget = tree;
77 if( this->m_TreeWidget != NULL )
78 this->_UpdateLoadedPlugins( );
79 this->m_Editor = editor;
80 if( this->m_Editor != NULL )
81 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
83 this->m_Workspace.SetMPRViewer( mpr );
86 // -------------------------------------------------------------------------
87 void cpPipelineEditor::BaseQtMainWindow::
88 _LoadPlugins( const std::string& filename )
92 this->m_Interface.LoadPluginFile( filename );
93 this->_UpdateLoadedPlugins( );
95 catch( std::exception& err )
97 QMessageBox::critical(
99 "Error loading plugins path",
106 // -------------------------------------------------------------------------
107 void cpPipelineEditor::BaseQtMainWindow::
108 _LoadPluginsFromPath( const std::string& path )
112 this->m_Interface.LoadPluginDir( path );
113 this->_UpdateLoadedPlugins( );
115 catch( std::exception& err )
117 QMessageBox::critical(
119 "Error loading plugins path",
126 // -------------------------------------------------------------------------
127 void cpPipelineEditor::BaseQtMainWindow::
128 _UpdateLoadedPlugins( )
131 auto filters = this->m_Interface.GetFilters( );
132 if( filters.size( ) == 0 )
135 QMessageBox::critical(
137 "Error loading default plugins",
138 "No plugins loaded: remember to load some!!!"
144 if( this->m_TreeWidget != NULL )
146 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
148 // Create or get category
149 QList< QTreeWidgetItem* > cat_items =
150 this->m_TreeWidget->findItems(
151 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
153 QTreeWidgetItem* cat = NULL;
154 if( cat_items.size( ) == 0 )
156 cat = new QTreeWidgetItem(
157 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
159 this->m_TreeWidget->addTopLevelItem( cat );
162 cat = cat_items[ 0 ];
165 auto fIt = cIt->second.begin( );
166 for( ; fIt != cIt->second.end( ); ++fIt )
168 QList< QTreeWidgetItem* > filter_items =
169 this->m_TreeWidget->findItems(
170 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
172 auto fiIt = filter_items.begin( );
173 auto found_fiIt = filter_items.end( );
174 for( ; fiIt != filter_items.end( ); ++fiIt )
175 if( ( *fiIt )->parent( ) == cat )
179 if( found_fiIt == filter_items.end( ) )
180 QTreeWidgetItem* filter = new QTreeWidgetItem(
181 cat, QStringList( fIt->c_str( ) )
190 this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
193 // -------------------------------------------------------------------------
194 void cpPipelineEditor::BaseQtMainWindow::
197 this->m_Application->setOverrideCursor( Qt::WaitCursor );
198 this->m_Application->installEventFilter( &( this->m_Blocker ) );
201 // -------------------------------------------------------------------------
202 void cpPipelineEditor::BaseQtMainWindow::
205 while( this->m_Application->overrideCursor( ) )
206 this->m_Application->restoreOverrideCursor( );
207 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
210 // -------------------------------------------------------------------------
211 void cpPipelineEditor::BaseQtMainWindow::
212 _LoadWorkspace( const std::string& filename )
214 std::string err = this->m_Workspace.LoadWorkspace( filename );
217 QMessageBox::critical(
219 QMessageBox::tr( "Error loading workspace" ),
220 QMessageBox::tr( err.c_str( ) )
225 if( this->m_Editor != NULL )
226 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
231 // -------------------------------------------------------------------------
232 void cpPipelineEditor::BaseQtMainWindow::
233 _SaveWorkspace( const std::string& filename )
235 std::string err = this->m_Workspace.SaveWorkspace( filename );
237 QMessageBox::critical(
239 QMessageBox::tr( "Error saving workspace" ),
240 QMessageBox::tr( err.c_str( ) )
244 // -------------------------------------------------------------------------
245 void cpPipelineEditor::BaseQtMainWindow::
246 _InteractiveLoadPlugins( )
248 QFileDialog dlg( this );
249 dlg.setFileMode( QFileDialog::ExistingFiles );
250 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
252 std::stringstream name_filter;
253 std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
255 << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
256 dlg.setNameFilter( name_filter.str( ).c_str( ) );
257 dlg.setDefaultSuffix( suffix.c_str( ) );
258 if( !( dlg.exec( ) ) )
261 QStringList names = dlg.selectedFiles( );
262 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
263 this->_LoadPlugins( qIt->toStdString( ) );
266 // -------------------------------------------------------------------------
267 void cpPipelineEditor::BaseQtMainWindow::
268 _InteractiveLoadPluginsFromPath( )
270 QFileDialog dlg( this );
271 dlg.setFileMode( QFileDialog::DirectoryOnly );
272 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
273 if( !( dlg.exec( ) ) )
275 this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
278 // -------------------------------------------------------------------------
279 void cpPipelineEditor::BaseQtMainWindow::
280 _InteractiveLoadWorkspace( )
282 QFileDialog dlg( this );
283 dlg.setFileMode( QFileDialog::ExistingFile );
284 dlg.setDirectory( "." );
286 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
288 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
289 if( !( dlg.exec( ) ) )
291 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
294 // -------------------------------------------------------------------------
295 void cpPipelineEditor::BaseQtMainWindow::
296 _InteractiveSaveWorkspace( )
298 QFileDialog dlg( this );
299 dlg.setFileMode( QFileDialog::AnyFile );
300 dlg.setDirectory( "." );
301 dlg.setAcceptMode( QFileDialog::AcceptSave );
303 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
305 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
306 if( !( dlg.exec( ) ) )
308 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
311 // -------------------------------------------------------------------------
312 void cpPipelineEditor::BaseQtMainWindow::
313 _ExecFilter( const std::string& filter_name )
318 this->m_Workspace.Execute( filter_name );
321 catch( itk::ExceptionObject& err1 )
324 QMessageBox::critical(
326 QMessageBox::tr( "Error executing filter" ),
327 QMessageBox::tr( err1.GetDescription( ) )
330 catch( std::exception& err2 )
333 QMessageBox::critical(
335 QMessageBox::tr( "Error executing filter" ),
336 QMessageBox::tr( err2.what( ) )
342 QMessageBox::critical(
344 QMessageBox::tr( "Error executing filter" ),
345 QMessageBox::tr( "Unknown error" )