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: this should be the 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 // Try to load plugins from executable dir
49 QFileInfo info( argv[ 0 ] );
51 this->_LoadPluginsFromPath( info.canonicalPath( ).toStdString( ) );
54 this->m_Workspace.SetInterface( &( this->m_Interface ) );
57 // -------------------------------------------------------------------------
58 cpPipelineEditor::BaseQtMainWindow::
61 this->m_Interface.UnloadAll( );
64 // -------------------------------------------------------------------------
65 void cpPipelineEditor::BaseQtMainWindow::
68 cpExtensions::QT::SimpleMPRWidget* mpr,
69 cpPipelineEditor::Editor* editor
72 this->m_TreeWidget = tree;
73 if( this->m_TreeWidget != NULL )
74 this->_UpdateLoadedPlugins( );
75 this->m_Editor = editor;
76 if( this->m_Editor != NULL )
77 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
79 this->m_Workspace.SetMPRViewer( mpr );
82 // -------------------------------------------------------------------------
83 void cpPipelineEditor::BaseQtMainWindow::
84 _LoadPlugins( const std::string& filename )
88 this->m_Interface.LoadPluginFile( filename );
89 this->_UpdateLoadedPlugins( );
91 catch( std::exception& err )
93 QMessageBox::critical(
95 "Error loading plugins path",
102 // -------------------------------------------------------------------------
103 void cpPipelineEditor::BaseQtMainWindow::
104 _LoadPluginsFromPath( const std::string& path )
108 this->m_Interface.LoadPluginDir( path );
109 this->_UpdateLoadedPlugins( );
111 catch( std::exception& err )
113 QMessageBox::critical(
115 "Error loading plugins path",
122 // -------------------------------------------------------------------------
123 void cpPipelineEditor::BaseQtMainWindow::
124 _UpdateLoadedPlugins( )
127 auto filters = this->m_Interface.GetFilters( );
128 if( filters.size( ) == 0 )
131 QMessageBox::critical(
133 "Error loading default plugins",
134 "No plugins loaded: remember to load some!!!"
140 if( this->m_TreeWidget != NULL )
142 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
144 // Create or get category
145 QList< QTreeWidgetItem* > cat_items =
146 this->m_TreeWidget->findItems(
147 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
149 QTreeWidgetItem* cat = NULL;
150 if( cat_items.size( ) == 0 )
152 cat = new QTreeWidgetItem(
153 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
155 this->m_TreeWidget->addTopLevelItem( cat );
158 cat = cat_items[ 0 ];
161 auto fIt = cIt->second.begin( );
162 for( ; fIt != cIt->second.end( ); ++fIt )
164 QList< QTreeWidgetItem* > filter_items =
165 this->m_TreeWidget->findItems(
166 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
168 auto fiIt = filter_items.begin( );
169 auto found_fiIt = filter_items.end( );
170 for( ; fiIt != filter_items.end( ); ++fiIt )
171 if( ( *fiIt )->parent( ) == cat )
175 if( found_fiIt == filter_items.end( ) )
176 QTreeWidgetItem* filter = new QTreeWidgetItem(
177 cat, QStringList( fIt->c_str( ) )
188 // -------------------------------------------------------------------------
189 void cpPipelineEditor::BaseQtMainWindow::
192 this->m_Application->setOverrideCursor( Qt::WaitCursor );
193 this->m_Application->installEventFilter( &( this->m_Blocker ) );
196 // -------------------------------------------------------------------------
197 void cpPipelineEditor::BaseQtMainWindow::
200 while( this->m_Application->overrideCursor( ) )
201 this->m_Application->restoreOverrideCursor( );
202 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
205 // -------------------------------------------------------------------------
206 void cpPipelineEditor::BaseQtMainWindow::
207 _LoadWorkspace( const std::string& filename )
209 std::string err = this->m_Workspace.LoadWorkspace( filename );
212 QMessageBox::critical(
214 QMessageBox::tr( "Error loading workspace" ),
215 QMessageBox::tr( err.c_str( ) )
220 if( this->m_Editor != NULL )
221 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
226 // -------------------------------------------------------------------------
227 void cpPipelineEditor::BaseQtMainWindow::
228 _SaveWorkspace( const std::string& filename )
230 std::string err = this->m_Workspace.SaveWorkspace( filename );
232 QMessageBox::critical(
234 QMessageBox::tr( "Error saving workspace" ),
235 QMessageBox::tr( err.c_str( ) )
239 // -------------------------------------------------------------------------
240 void cpPipelineEditor::BaseQtMainWindow::
241 _InteractiveLoadPlugins( )
243 QFileDialog dlg( this );
244 dlg.setFileMode( QFileDialog::ExistingFiles );
245 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
247 std::stringstream name_filter;
248 std::string suffix = std::string( cpPlugins_LIB_EXT );
250 << "Plugins file (*." << cpPlugins_LIB_EXT << ");;All files (*)";
251 dlg.setNameFilter( name_filter.str( ).c_str( ) );
252 dlg.setDefaultSuffix( suffix.c_str( ) );
253 if( !( dlg.exec( ) ) )
256 QStringList names = dlg.selectedFiles( );
257 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
258 this->_LoadPlugins( qIt->toStdString( ) );
261 // -------------------------------------------------------------------------
262 void cpPipelineEditor::BaseQtMainWindow::
263 _InteractiveLoadPluginsFromPath( )
265 QFileDialog dlg( this );
266 dlg.setFileMode( QFileDialog::DirectoryOnly );
267 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
268 if( !( dlg.exec( ) ) )
270 this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
273 // -------------------------------------------------------------------------
274 void cpPipelineEditor::BaseQtMainWindow::
275 _InteractiveLoadWorkspace( )
277 QFileDialog dlg( this );
278 dlg.setFileMode( QFileDialog::ExistingFile );
279 dlg.setDirectory( "." );
281 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
283 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
284 if( !( dlg.exec( ) ) )
286 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
289 // -------------------------------------------------------------------------
290 void cpPipelineEditor::BaseQtMainWindow::
291 _InteractiveSaveWorkspace( )
293 QFileDialog dlg( this );
294 dlg.setFileMode( QFileDialog::AnyFile );
295 dlg.setDirectory( "." );
296 dlg.setAcceptMode( QFileDialog::AcceptSave );
298 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
300 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
301 if( !( dlg.exec( ) ) )
303 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
306 // -------------------------------------------------------------------------
307 void cpPipelineEditor::BaseQtMainWindow::
308 _ExecFilter( const std::string& filter_name )
313 this->m_Workspace.Execute( filter_name );
316 catch( itk::ExceptionObject& err1 )
319 QMessageBox::critical(
321 QMessageBox::tr( "Error executing filter" ),
322 QMessageBox::tr( err1.GetDescription( ) )
325 catch( std::exception& err2 )
328 QMessageBox::critical(
330 QMessageBox::tr( "Error executing filter" ),
331 QMessageBox::tr( err2.what( ) )
337 QMessageBox::critical(
339 QMessageBox::tr( "Error executing filter" ),
340 QMessageBox::tr( "Unknown error" )