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 // Prepare plugins interface
47 QFileInfo info( argv[ 0 ] );
50 this->m_Interface.LoadConfiguration( cpPlugins_CONFIG_FILE );
51 this->_LoadPluginsFromPath( this->m_PluginsPath );
52 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
56 if( exec_dir.exists( ) )
58 this->_LoadPluginsFromPath( exec_dir.canonicalPath( ).toStdString( ) );
59 this->m_PluginsPath = exec_dir.canonicalPath( ).toStdString( );
64 this->m_Workspace.SetInterface( &( this->m_Interface ) );
67 // -------------------------------------------------------------------------
68 cpPipelineEditor::BaseQtMainWindow::
71 this->m_Interface.UnloadAll( );
74 // -------------------------------------------------------------------------
75 void cpPipelineEditor::BaseQtMainWindow::
78 cpExtensions::QT::SimpleMPRWidget* mpr,
79 cpPipelineEditor::Editor* editor
82 this->m_TreeWidget = tree;
83 if( this->m_TreeWidget != NULL )
84 this->_UpdateLoadedPlugins( );
85 this->m_Editor = editor;
86 if( this->m_Editor != NULL )
87 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
89 this->m_Workspace.SetMPRViewer( mpr );
92 // -------------------------------------------------------------------------
93 void cpPipelineEditor::BaseQtMainWindow::
94 _LoadPlugins( const std::string& filename )
98 this->m_Interface.LoadPluginFile( filename );
99 this->_UpdateLoadedPlugins( );
101 catch( std::exception& err )
103 QMessageBox::critical(
105 "Error loading plugins path",
112 // -------------------------------------------------------------------------
113 void cpPipelineEditor::BaseQtMainWindow::
114 _LoadPluginsFromPath( const std::string& path )
118 this->m_Interface.LoadPluginDir( path );
119 this->_UpdateLoadedPlugins( );
121 catch( std::exception& err )
123 QMessageBox::critical(
125 "Error loading plugins path",
132 // -------------------------------------------------------------------------
133 void cpPipelineEditor::BaseQtMainWindow::
134 _UpdateLoadedPlugins( )
137 auto filters = this->m_Interface.GetFilters( );
138 if( filters.size( ) == 0 )
141 QMessageBox::critical(
143 "Error loading default plugins",
144 "No plugins loaded: remember to load some!!!"
150 if( this->m_TreeWidget != NULL )
152 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
154 // Create or get category
155 QList< QTreeWidgetItem* > cat_items =
156 this->m_TreeWidget->findItems(
157 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
159 QTreeWidgetItem* cat = NULL;
160 if( cat_items.size( ) == 0 )
162 cat = new QTreeWidgetItem(
163 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
165 this->m_TreeWidget->addTopLevelItem( cat );
168 cat = cat_items[ 0 ];
171 auto fIt = cIt->second.begin( );
172 for( ; fIt != cIt->second.end( ); ++fIt )
174 QList< QTreeWidgetItem* > filter_items =
175 this->m_TreeWidget->findItems(
176 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
178 auto fiIt = filter_items.begin( );
179 auto found_fiIt = filter_items.end( );
180 for( ; fiIt != filter_items.end( ); ++fiIt )
181 if( ( *fiIt )->parent( ) == cat )
185 if( found_fiIt == filter_items.end( ) )
186 QTreeWidgetItem* filter = new QTreeWidgetItem(
187 cat, QStringList( fIt->c_str( ) )
196 this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
199 // -------------------------------------------------------------------------
200 void cpPipelineEditor::BaseQtMainWindow::
203 this->m_Application->setOverrideCursor( Qt::WaitCursor );
204 this->m_Application->installEventFilter( &( this->m_Blocker ) );
207 // -------------------------------------------------------------------------
208 void cpPipelineEditor::BaseQtMainWindow::
211 while( this->m_Application->overrideCursor( ) )
212 this->m_Application->restoreOverrideCursor( );
213 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
216 // -------------------------------------------------------------------------
217 void cpPipelineEditor::BaseQtMainWindow::
218 _LoadWorkspace( const std::string& filename )
220 std::string err = this->m_Workspace.LoadWorkspace( filename );
223 QMessageBox::critical(
225 QMessageBox::tr( "Error loading workspace" ),
226 QMessageBox::tr( err.c_str( ) )
231 if( this->m_Editor != NULL )
232 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
237 // -------------------------------------------------------------------------
238 void cpPipelineEditor::BaseQtMainWindow::
239 _SaveWorkspace( const std::string& filename )
241 std::string err = this->m_Workspace.SaveWorkspace( filename );
243 QMessageBox::critical(
245 QMessageBox::tr( "Error saving workspace" ),
246 QMessageBox::tr( err.c_str( ) )
250 // -------------------------------------------------------------------------
251 void cpPipelineEditor::BaseQtMainWindow::
252 _InteractiveLoadPlugins( )
254 QFileDialog dlg( this );
255 dlg.setFileMode( QFileDialog::ExistingFiles );
256 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
258 std::stringstream name_filter;
259 std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
261 << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
262 dlg.setNameFilter( name_filter.str( ).c_str( ) );
263 dlg.setDefaultSuffix( suffix.c_str( ) );
264 if( !( dlg.exec( ) ) )
267 QStringList names = dlg.selectedFiles( );
268 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
269 this->_LoadPlugins( qIt->toStdString( ) );
272 // -------------------------------------------------------------------------
273 void cpPipelineEditor::BaseQtMainWindow::
274 _InteractiveLoadPluginsFromPath( )
276 QFileDialog dlg( this );
277 dlg.setFileMode( QFileDialog::DirectoryOnly );
278 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
279 if( !( dlg.exec( ) ) )
281 this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
284 // -------------------------------------------------------------------------
285 void cpPipelineEditor::BaseQtMainWindow::
286 _InteractiveLoadWorkspace( )
288 QFileDialog dlg( this );
289 dlg.setFileMode( QFileDialog::ExistingFile );
290 dlg.setDirectory( "." );
292 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
294 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
295 if( !( dlg.exec( ) ) )
297 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
300 // -------------------------------------------------------------------------
301 void cpPipelineEditor::BaseQtMainWindow::
302 _InteractiveSaveWorkspace( )
304 QFileDialog dlg( this );
305 dlg.setFileMode( QFileDialog::AnyFile );
306 dlg.setDirectory( "." );
307 dlg.setAcceptMode( QFileDialog::AcceptSave );
309 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
311 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
312 if( !( dlg.exec( ) ) )
314 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
317 // -------------------------------------------------------------------------
318 void cpPipelineEditor::BaseQtMainWindow::
319 _ExecFilter( const std::string& filter_name )
324 this->m_Workspace.Execute( filter_name );
327 catch( itk::ExceptionObject& err1 )
330 QMessageBox::critical(
332 QMessageBox::tr( "Error executing filter" ),
333 QMessageBox::tr( err1.GetDescription( ) )
336 catch( std::exception& err2 )
339 QMessageBox::critical(
341 QMessageBox::tr( "Error executing filter" ),
342 QMessageBox::tr( err2.what( ) )
348 QMessageBox::critical(
350 QMessageBox::tr( "Error executing filter" ),
351 QMessageBox::tr( "Unknown error" )