]> Creatis software - cpPlugins.git/blob - lib/cpPipelineEditor/BaseQtMainWindow.cxx
cf5e41f32b07c686436283ff11e302be651892a2
[cpPlugins.git] / lib / cpPipelineEditor / BaseQtMainWindow.cxx
1 #include <cpPipelineEditor/BaseQtMainWindow.h>
2
3 #include <cpExtensions/QT/SimpleMPRWidget.h>
4 #include <cpPipelineEditor/Editor.h>
5 #include <QApplication>
6 #include <QDir>
7 #include <QFileDialog>
8 #include <QFileInfo>
9 #include <QMessageBox>
10 #include <QTreeWidget>
11
12 // -------------------------------------------------------------------------
13 bool cpPipelineEditor::BaseQtMainWindow::_TBlocker::
14 eventFilter( QObject* obj, QEvent* event )
15 {
16   return( true ); // -> Block all events
17   /* NOTE: correct implementation:
18      switch( event->type( ) )
19      {
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:
26      //...
27      return( true );
28      } // hctiws
29      return( this->QObject::eventFilter( obj, event ) );
30   */
31 }
32
33 // -------------------------------------------------------------------------
34 cpPipelineEditor::BaseQtMainWindow::
35 BaseQtMainWindow(
36   int argc, char* argv[],
37   QApplication* app,
38   QWidget* parent
39   )
40   : Superclass( parent ),
41     m_Application( app ),
42     m_PluginsPath( "." ),
43     m_TreeWidget( NULL ),
44     m_Editor( NULL )
45 {
46   // Prepare plugins interface
47   QFileInfo info( argv[ 0 ] );
48   if( info.exists( ) )
49   {
50     this->m_Interface.LoadConfiguration( cpPlugins_CONFIG_FILE );
51     this->_LoadPluginsFromPath( this->m_PluginsPath );
52     this->m_PluginsPath = info.canonicalPath( ).toStdString( );
53
54   } // fi
55   QDir exec_dir( "." );
56   if( exec_dir.exists( ) )
57   {
58     this->_LoadPluginsFromPath( exec_dir.canonicalPath( ).toStdString( ) );
59     this->m_PluginsPath = exec_dir.canonicalPath( ).toStdString( );
60
61   } // fi
62
63   // Prepare workspace
64   this->m_Workspace.SetInterface( &( this->m_Interface ) );
65 }
66
67 // -------------------------------------------------------------------------
68 cpPipelineEditor::BaseQtMainWindow::
69 ~BaseQtMainWindow( )
70 {
71   this->m_Interface.UnloadAll( );
72 }
73
74 // -------------------------------------------------------------------------
75 void cpPipelineEditor::BaseQtMainWindow::
76 _Configure(
77   QTreeWidget* tree,
78   cpExtensions::QT::SimpleMPRWidget* mpr,
79   cpPipelineEditor::Editor* editor
80   )
81 {
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 ) );
88   if( mpr != NULL )
89     this->m_Workspace.SetMPRViewer( mpr );
90 }
91
92 // -------------------------------------------------------------------------
93 void cpPipelineEditor::BaseQtMainWindow::
94 _LoadPlugins( const std::string& filename )
95 {
96   try
97   {
98     this->m_Interface.LoadPluginFile( filename );
99     this->_UpdateLoadedPlugins( );
100   }
101   catch( std::exception& err )
102   {
103     QMessageBox::critical(
104       this,
105       "Error loading plugins path",
106       err.what( )
107       );
108
109   } // yrt
110 }
111
112 // -------------------------------------------------------------------------
113 void cpPipelineEditor::BaseQtMainWindow::
114 _LoadPluginsFromPath( const std::string& path )
115 {
116   try
117   {
118     this->m_Interface.LoadPluginDir( path );
119     this->_UpdateLoadedPlugins( );
120   }
121   catch( std::exception& err )
122   {
123     QMessageBox::critical(
124       this,
125       "Error loading plugins path",
126       err.what( )
127       );
128
129   } // yrt
130 }
131
132 // -------------------------------------------------------------------------
133 void cpPipelineEditor::BaseQtMainWindow::
134 _UpdateLoadedPlugins( )
135 {
136   this->_Block( );
137   auto filters = this->m_Interface.GetFilters( );
138   if( filters.size( ) == 0 )
139   {
140     this->_UnBlock( );
141     QMessageBox::critical(
142       this,
143       "Error loading default plugins",
144       "No plugins loaded: remember to load some!!!"
145       );
146     return;
147
148   } // fi
149
150   if( this->m_TreeWidget != NULL )
151   {
152     for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
153     {
154       // Create or get category
155       QList< QTreeWidgetItem* > cat_items =
156         this->m_TreeWidget->findItems(
157           cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
158           );
159       QTreeWidgetItem* cat = NULL;
160       if( cat_items.size( ) == 0 )
161       {
162         cat = new QTreeWidgetItem(
163           ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
164           );
165         this->m_TreeWidget->addTopLevelItem( cat );
166       }
167       else
168         cat = cat_items[ 0 ];
169
170       // Create filters
171       auto fIt = cIt->second.begin( );
172       for( ; fIt != cIt->second.end( ); ++fIt )
173       {
174         QList< QTreeWidgetItem* > filter_items =
175           this->m_TreeWidget->findItems(
176             fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
177             );
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 )
182             found_fiIt = fiIt;
183
184         // Add filter
185         if( found_fiIt == filter_items.end( ) )
186           QTreeWidgetItem* filter = new QTreeWidgetItem(
187             cat, QStringList( fIt->c_str( ) )
188             );
189
190       } // rof
191
192     } // rof
193
194   } // fi
195   this->_UnBlock( );
196   this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
197 }
198
199 // -------------------------------------------------------------------------
200 void cpPipelineEditor::BaseQtMainWindow::
201 _Block( )
202 {
203   this->m_Application->setOverrideCursor( Qt::WaitCursor );
204   this->m_Application->installEventFilter( &( this->m_Blocker ) );
205 }
206
207 // -------------------------------------------------------------------------
208 void cpPipelineEditor::BaseQtMainWindow::
209 _UnBlock( )
210 {
211   while( this->m_Application->overrideCursor( ) )
212     this->m_Application->restoreOverrideCursor( );
213   this->m_Application->removeEventFilter( &( this->m_Blocker ) );
214 }
215
216 // -------------------------------------------------------------------------
217 void cpPipelineEditor::BaseQtMainWindow::
218 _LoadWorkspace( const std::string& filename )
219 {
220   std::string err = this->m_Workspace.LoadWorkspace( filename );
221   if( err != "" )
222   {
223     QMessageBox::critical(
224       this,
225       QMessageBox::tr( "Error loading workspace" ),
226       QMessageBox::tr( err.c_str( ) )
227       );
228   }
229   else
230   {
231     if( this->m_Editor != NULL )
232       this->m_Editor->setWorkspace( &( this->m_Workspace ) );
233
234   } // fi
235 }
236
237 // -------------------------------------------------------------------------
238 void cpPipelineEditor::BaseQtMainWindow::
239 _SaveWorkspace( const std::string& filename )
240 {
241   std::string err = this->m_Workspace.SaveWorkspace( filename );
242   if( err != "" )
243     QMessageBox::critical(
244       this,
245       QMessageBox::tr( "Error saving workspace" ),
246       QMessageBox::tr( err.c_str( ) )
247       );
248 }
249
250 // -------------------------------------------------------------------------
251 void cpPipelineEditor::BaseQtMainWindow::
252 _InteractiveLoadPlugins( )
253 {
254   QFileDialog dlg( this );
255   dlg.setFileMode( QFileDialog::ExistingFiles );
256   dlg.setDirectory( this->m_PluginsPath.c_str( ) );
257
258   std::stringstream name_filter;
259   std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
260   name_filter
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( ) ) )
265     return;
266
267   QStringList names = dlg.selectedFiles( );
268   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
269     this->_LoadPlugins( qIt->toStdString( ) );
270 }
271
272 // -------------------------------------------------------------------------
273 void cpPipelineEditor::BaseQtMainWindow::
274 _InteractiveLoadPluginsFromPath( )
275 {
276   QFileDialog dlg( this );
277   dlg.setFileMode( QFileDialog::DirectoryOnly );
278   dlg.setDirectory( this->m_PluginsPath.c_str( ) );
279   if( !( dlg.exec( ) ) )
280     return;
281   this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
282 }
283
284 // -------------------------------------------------------------------------
285 void cpPipelineEditor::BaseQtMainWindow::
286 _InteractiveLoadWorkspace( )
287 {
288   QFileDialog dlg( this );
289   dlg.setFileMode( QFileDialog::ExistingFile );
290   dlg.setDirectory( "." );
291   dlg.setNameFilter(
292     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
293     );
294   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
295   if( !( dlg.exec( ) ) )
296     return;
297   this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
298 }
299
300 // -------------------------------------------------------------------------
301 void cpPipelineEditor::BaseQtMainWindow::
302 _InteractiveSaveWorkspace( )
303 {
304   QFileDialog dlg( this );
305   dlg.setFileMode( QFileDialog::AnyFile );
306   dlg.setDirectory( "." );
307   dlg.setAcceptMode( QFileDialog::AcceptSave );
308   dlg.setNameFilter(
309     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
310     );
311   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
312   if( !( dlg.exec( ) ) )
313     return;
314   this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
315 }
316
317 // -------------------------------------------------------------------------
318 void cpPipelineEditor::BaseQtMainWindow::
319 _ExecFilter( const std::string& filter_name )
320 {
321   this->_Block( );
322   std::string err = this->m_Workspace.Execute( filter_name );
323   this->_UnBlock( );
324   if( err != "" )
325     QMessageBox::critical(
326       this,
327       QMessageBox::tr( "Error executing filter" ),
328       QMessageBox::tr( err.c_str( ) )
329       );
330 }
331
332 // eof - $RCSfile$