1 #include <cpPlugins/BaseQtMainWindow.h>
5 #include <cpExtensions/QT/SimpleMPRWidget.h>
6 #include <cpPipelineEditor/Editor.h>
7 #include <QApplication>
11 #include <QMessageBox>
12 #include <QTreeWidget>
14 // -------------------------------------------------------------------------
15 bool cpPlugins::BaseQtMainWindow::_TBlocker::
16 eventFilter( QObject* obj, QEvent* event )
18 return( true ); // -> Block all events
19 /* NOTE: correct implementation:
20 switch( event->type( ) )
22 //list event you want to prevent here ...
23 case QEvent::KeyPress:
24 case QEvent::KeyRelease:
25 case QEvent::MouseButtonRelease:
26 case QEvent::MouseButtonPress:
27 case QEvent::MouseButtonDblClick:
31 return( this->QObject::eventFilter( obj, event ) );
35 // -------------------------------------------------------------------------
36 cpPlugins::BaseQtMainWindow::
38 int argc, char* argv[],
42 : Superclass( parent ),
48 // Prepare plugins interface
49 QFileInfo info( argv[ 0 ] );
52 this->m_Interface.LoadConfiguration( cpPlugins_CONFIG_FILE );
53 this->_LoadPluginsFromPath( this->m_PluginsPath );
54 this->m_PluginsPath = info.canonicalPath( ).toStdString( );
58 if( exec_dir.exists( ) )
60 this->_LoadPluginsFromPath( exec_dir.canonicalPath( ).toStdString( ) );
61 this->m_PluginsPath = exec_dir.canonicalPath( ).toStdString( );
66 this->m_Workspace.SetInterface( &( this->m_Interface ) );
69 // -------------------------------------------------------------------------
70 cpPlugins::BaseQtMainWindow::
73 this->m_Interface.UnloadAll( );
76 // -------------------------------------------------------------------------
77 void cpPlugins::BaseQtMainWindow::
80 cpExtensions::QT::SimpleMPRWidget* mpr,
81 cpPipelineEditor::Editor* editor
84 this->m_TreeWidget = tree;
85 if( this->m_TreeWidget != NULL )
86 this->_UpdateLoadedPlugins( );
87 this->m_Editor = editor;
88 if( this->m_Editor != NULL )
89 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
91 this->m_Workspace.SetMPRViewer( mpr );
94 // -------------------------------------------------------------------------
95 void cpPlugins::BaseQtMainWindow::
96 _LoadPlugins( const std::string& filename )
100 this->m_Interface.LoadPluginFile( filename );
101 this->_UpdateLoadedPlugins( );
103 catch( std::exception& err )
105 QMessageBox::critical(
107 "Error loading plugins path",
114 // -------------------------------------------------------------------------
115 void cpPlugins::BaseQtMainWindow::
116 _LoadPluginsFromPath( const std::string& path )
120 this->m_Interface.LoadPluginDir( path );
121 this->_UpdateLoadedPlugins( );
123 catch( std::exception& err )
125 QMessageBox::critical(
127 "Error loading plugins path",
134 // -------------------------------------------------------------------------
135 void cpPlugins::BaseQtMainWindow::
136 _UpdateLoadedPlugins( )
139 auto filters = this->m_Interface.GetFilters( );
140 if( filters.size( ) == 0 )
143 QMessageBox::critical(
145 "Error loading default plugins",
146 "No plugins loaded: remember to load some!!!"
152 if( this->m_TreeWidget != NULL )
154 for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
156 // Create or get category
157 QList< QTreeWidgetItem* > cat_items =
158 this->m_TreeWidget->findItems(
159 cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
161 QTreeWidgetItem* cat = NULL;
162 if( cat_items.size( ) == 0 )
164 cat = new QTreeWidgetItem(
165 ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
167 this->m_TreeWidget->addTopLevelItem( cat );
170 cat = cat_items[ 0 ];
173 auto fIt = cIt->second.begin( );
174 for( ; fIt != cIt->second.end( ); ++fIt )
176 QList< QTreeWidgetItem* > filter_items =
177 this->m_TreeWidget->findItems(
178 fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
180 auto fiIt = filter_items.begin( );
181 auto found_fiIt = filter_items.end( );
182 for( ; fiIt != filter_items.end( ); ++fiIt )
183 if( ( *fiIt )->parent( ) == cat )
187 if( found_fiIt == filter_items.end( ) )
188 QTreeWidgetItem* filter = new QTreeWidgetItem(
189 cat, QStringList( fIt->c_str( ) )
198 this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
201 // -------------------------------------------------------------------------
202 void cpPlugins::BaseQtMainWindow::
205 this->m_Application->setOverrideCursor( Qt::WaitCursor );
206 this->m_Application->installEventFilter( &( this->m_Blocker ) );
209 // -------------------------------------------------------------------------
210 void cpPlugins::BaseQtMainWindow::
213 while( this->m_Application->overrideCursor( ) )
214 this->m_Application->restoreOverrideCursor( );
215 this->m_Application->removeEventFilter( &( this->m_Blocker ) );
218 // -------------------------------------------------------------------------
219 void cpPlugins::BaseQtMainWindow::
220 _LoadWorkspace( const std::string& filename )
222 std::string err = this->m_Workspace.LoadWorkspace( filename );
225 QMessageBox::critical(
227 QMessageBox::tr( "Error loading workspace" ),
228 QMessageBox::tr( err.c_str( ) )
233 if( this->m_Editor != NULL )
234 this->m_Editor->setWorkspace( &( this->m_Workspace ) );
239 // -------------------------------------------------------------------------
240 void cpPlugins::BaseQtMainWindow::
241 _SaveWorkspace( const std::string& filename )
243 std::string err = this->m_Workspace.SaveWorkspace( filename );
245 QMessageBox::critical(
247 QMessageBox::tr( "Error saving workspace" ),
248 QMessageBox::tr( err.c_str( ) )
252 // -------------------------------------------------------------------------
253 void cpPlugins::BaseQtMainWindow::
254 _InteractiveLoadPlugins( )
256 QFileDialog dlg( this );
257 dlg.setFileMode( QFileDialog::ExistingFiles );
258 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
260 std::stringstream name_filter;
261 std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
263 << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
264 dlg.setNameFilter( name_filter.str( ).c_str( ) );
265 dlg.setDefaultSuffix( suffix.c_str( ) );
266 if( !( dlg.exec( ) ) )
269 QStringList names = dlg.selectedFiles( );
270 for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
271 this->_LoadPlugins( qIt->toStdString( ) );
274 // -------------------------------------------------------------------------
275 void cpPlugins::BaseQtMainWindow::
276 _InteractiveLoadPluginsFromPath( )
278 QFileDialog dlg( this );
279 dlg.setFileMode( QFileDialog::DirectoryOnly );
280 dlg.setDirectory( this->m_PluginsPath.c_str( ) );
281 if( !( dlg.exec( ) ) )
283 this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
286 // -------------------------------------------------------------------------
287 void cpPlugins::BaseQtMainWindow::
288 _InteractiveLoadWorkspace( )
290 QFileDialog dlg( this );
291 dlg.setFileMode( QFileDialog::ExistingFile );
292 dlg.setDirectory( "." );
294 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
296 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
297 if( !( dlg.exec( ) ) )
299 this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
302 // -------------------------------------------------------------------------
303 void cpPlugins::BaseQtMainWindow::
304 _InteractiveSaveWorkspace( )
306 QFileDialog dlg( this );
307 dlg.setFileMode( QFileDialog::AnyFile );
308 dlg.setDirectory( "." );
309 dlg.setAcceptMode( QFileDialog::AcceptSave );
311 QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
313 dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
314 if( !( dlg.exec( ) ) )
316 this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
319 // -------------------------------------------------------------------------
320 void cpPlugins::BaseQtMainWindow::
321 _ExecFilter( const std::string& filter_name )
324 std::string err = this->m_Workspace.Execute( filter_name );
327 QMessageBox::critical(
329 QMessageBox::tr( "Error executing filter" ),
330 QMessageBox::tr( err.c_str( ) )
334 #endif // cpPlugins_QT4