]> Creatis software - cpPlugins.git/blob - lib/cpPipelineEditor/BaseQtMainWindow.cxx
...
[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   this->m_Interface.GuessAccesiblePlugins( );
47
48   QFileInfo info( argv[ 0 ] );
49   if( info.exists( ) )
50   {
51     auto exec_dir = info.canonicalPath( ).toStdString( );
52     this->_LoadPluginsFromPath( exec_dir );
53
54   } // fi
55   this->_UpdateLoadedPlugins( );
56
57   // Prepare workspace
58   this->m_Workspace.SetInterface( &( this->m_Interface ) );
59   this->m_Workspace.PrintExecutionOn( );
60 }
61
62 // -------------------------------------------------------------------------
63 cpPipelineEditor::BaseQtMainWindow::
64 ~BaseQtMainWindow( )
65 {
66   this->m_Interface.UnloadAll( );
67 }
68
69 // -------------------------------------------------------------------------
70 void cpPipelineEditor::BaseQtMainWindow::
71 _Configure(
72   QTreeWidget* tree,
73   cpExtensions::QT::SimpleMPRWidget* mpr,
74   cpPipelineEditor::Editor* editor
75   )
76 {
77   this->m_TreeWidget = tree;
78   if( this->m_TreeWidget != NULL )
79     this->_UpdateLoadedPlugins( );
80   this->m_Editor = editor;
81   if( this->m_Editor != NULL )
82     this->m_Editor->setWorkspace( &( this->m_Workspace ) );
83   if( mpr != NULL )
84     this->m_Workspace.SetMPRViewer( mpr );
85 }
86
87 // -------------------------------------------------------------------------
88 void cpPipelineEditor::BaseQtMainWindow::
89 _LoadPlugins( const std::string& filename )
90 {
91   try
92   {
93     this->m_Interface.LoadPluginFile( filename );
94     this->_UpdateLoadedPlugins( );
95   }
96   catch( std::exception& err )
97   {
98     QMessageBox::critical(
99       this,
100       "Error loading plugins path",
101       err.what( )
102       );
103
104   } // yrt
105 }
106
107 // -------------------------------------------------------------------------
108 void cpPipelineEditor::BaseQtMainWindow::
109 _LoadPluginsFromPath( const std::string& path )
110 {
111   try
112   {
113     this->m_Interface.LoadPluginDir( path );
114     this->_UpdateLoadedPlugins( );
115   }
116   catch( std::exception& err )
117   {
118     QMessageBox::critical(
119       this,
120       "Error loading plugins path",
121       err.what( )
122       );
123
124   } // yrt
125 }
126
127 // -------------------------------------------------------------------------
128 void cpPipelineEditor::BaseQtMainWindow::
129 _UpdateLoadedPlugins( )
130 {
131   this->_Block( );
132   auto filters = this->m_Interface.GetFilters( );
133   if( filters.size( ) == 0 )
134   {
135     this->_UnBlock( );
136     QMessageBox::critical(
137       this,
138       "Error loading default plugins",
139       "No plugins loaded: remember to load some!!!"
140       );
141     return;
142
143   } // fi
144
145   if( this->m_TreeWidget != NULL )
146   {
147     for( auto cIt = filters.begin( ); cIt != filters.end( ); ++cIt )
148     {
149       // Create or get category
150       QList< QTreeWidgetItem* > cat_items =
151         this->m_TreeWidget->findItems(
152           cIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
153           );
154       QTreeWidgetItem* cat = NULL;
155       if( cat_items.size( ) == 0 )
156       {
157         cat = new QTreeWidgetItem(
158           ( QTreeWidgetItem* )( NULL ), QStringList( cIt->first.c_str( ) )
159           );
160         this->m_TreeWidget->addTopLevelItem( cat );
161       }
162       else
163         cat = cat_items[ 0 ];
164
165       // Create filters
166       auto fIt = cIt->second.begin( );
167       for( ; fIt != cIt->second.end( ); ++fIt )
168       {
169         QList< QTreeWidgetItem* > filter_items =
170           this->m_TreeWidget->findItems(
171             fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
172             );
173         auto fiIt = filter_items.begin( );
174         auto found_fiIt = filter_items.end( );
175         for( ; fiIt != filter_items.end( ); ++fiIt )
176           if( ( *fiIt )->parent( ) == cat )
177             found_fiIt = fiIt;
178
179         // Add filter
180         if( found_fiIt == filter_items.end( ) )
181           QTreeWidgetItem* filter = new QTreeWidgetItem(
182             cat, QStringList( fIt->c_str( ) )
183             );
184
185       } // rof
186
187     } // rof
188
189   } // fi
190   this->_UnBlock( );
191   this->m_Interface.SaveConfiguration( cpPlugins_CONFIG_FILE );
192 }
193
194 // -------------------------------------------------------------------------
195 void cpPipelineEditor::BaseQtMainWindow::
196 _Block( )
197 {
198   this->m_Application->setOverrideCursor( Qt::WaitCursor );
199   this->m_Application->installEventFilter( &( this->m_Blocker ) );
200 }
201
202 // -------------------------------------------------------------------------
203 void cpPipelineEditor::BaseQtMainWindow::
204 _UnBlock( )
205 {
206   while( this->m_Application->overrideCursor( ) )
207     this->m_Application->restoreOverrideCursor( );
208   this->m_Application->removeEventFilter( &( this->m_Blocker ) );
209 }
210
211 // -------------------------------------------------------------------------
212 void cpPipelineEditor::BaseQtMainWindow::
213 _LoadWorkspace( const std::string& filename )
214 {
215   std::string err = this->m_Workspace.LoadWorkspace( filename );
216   if( err != "" )
217   {
218     QMessageBox::critical(
219       this,
220       QMessageBox::tr( "Error loading workspace" ),
221       QMessageBox::tr( err.c_str( ) )
222       );
223   }
224   else
225   {
226     if( this->m_Editor != NULL )
227       this->m_Editor->setWorkspace( &( this->m_Workspace ) );
228
229   } // fi
230 }
231
232 // -------------------------------------------------------------------------
233 void cpPipelineEditor::BaseQtMainWindow::
234 _SaveWorkspace( const std::string& filename )
235 {
236   std::string err = this->m_Workspace.SaveWorkspace( filename );
237   if( err != "" )
238     QMessageBox::critical(
239       this,
240       QMessageBox::tr( "Error saving workspace" ),
241       QMessageBox::tr( err.c_str( ) )
242       );
243 }
244
245 // -------------------------------------------------------------------------
246 void cpPipelineEditor::BaseQtMainWindow::
247 _InteractiveLoadPlugins( )
248 {
249   QFileDialog dlg( this );
250   dlg.setFileMode( QFileDialog::ExistingFiles );
251   dlg.setDirectory( this->m_PluginsPath.c_str( ) );
252
253   std::stringstream name_filter;
254   std::string suffix = std::string( cpPlugins_PLUGIN_EXT );
255   name_filter
256     << "Plugins file (*." << cpPlugins_PLUGIN_EXT << ");;All files (*)";
257   dlg.setNameFilter( name_filter.str( ).c_str( ) );
258   dlg.setDefaultSuffix( suffix.c_str( ) );
259   if( !( dlg.exec( ) ) )
260     return;
261
262   QStringList names = dlg.selectedFiles( );
263   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
264     this->_LoadPlugins( qIt->toStdString( ) );
265 }
266
267 // -------------------------------------------------------------------------
268 void cpPipelineEditor::BaseQtMainWindow::
269 _InteractiveLoadPluginsFromPath( )
270 {
271   QFileDialog dlg( this );
272   dlg.setFileMode( QFileDialog::DirectoryOnly );
273   dlg.setDirectory( this->m_PluginsPath.c_str( ) );
274   if( !( dlg.exec( ) ) )
275     return;
276   this->_LoadPluginsFromPath( dlg.selectedFiles( ).begin( )->toStdString( ) );
277 }
278
279 // -------------------------------------------------------------------------
280 void cpPipelineEditor::BaseQtMainWindow::
281 _InteractiveLoadWorkspace( )
282 {
283   QFileDialog dlg( this );
284   dlg.setFileMode( QFileDialog::ExistingFile );
285   dlg.setDirectory( "." );
286   dlg.setNameFilter(
287     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
288     );
289   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
290   if( !( dlg.exec( ) ) )
291     return;
292   this->_LoadWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
293 }
294
295 // -------------------------------------------------------------------------
296 void cpPipelineEditor::BaseQtMainWindow::
297 _InteractiveSaveWorkspace( )
298 {
299   QFileDialog dlg( this );
300   dlg.setFileMode( QFileDialog::AnyFile );
301   dlg.setDirectory( "." );
302   dlg.setAcceptMode( QFileDialog::AcceptSave );
303   dlg.setNameFilter(
304     QFileDialog::tr( "Workspace file (*.wxml);;All files (*)" )
305     );
306   dlg.setDefaultSuffix( QFileDialog::tr( "wxml" ) );
307   if( !( dlg.exec( ) ) )
308     return;
309   this->_SaveWorkspace( dlg.selectedFiles( ).begin( )->toStdString( ) );
310 }
311
312 // -------------------------------------------------------------------------
313 void cpPipelineEditor::BaseQtMainWindow::
314 _ExecFilter( const std::string& filter_name )
315 {
316   this->_Block( );
317   try
318   {
319     this->m_Workspace.Execute( filter_name );
320     this->_UnBlock( );
321   }
322   catch( itk::ExceptionObject& err1 )
323   {
324     this->_UnBlock( );
325     QMessageBox::critical(
326       this,
327       QMessageBox::tr( "Error executing filter" ),
328       QMessageBox::tr( err1.GetDescription( ) )
329       );
330   }
331   catch( std::exception& err2 )
332   {
333     this->_UnBlock( );
334     QMessageBox::critical(
335       this,
336       QMessageBox::tr( "Error executing filter" ),
337       QMessageBox::tr( err2.what( ) )
338       );
339   }
340   catch( ... )
341   {
342     this->_UnBlock( );
343     QMessageBox::critical(
344       this,
345       QMessageBox::tr( "Error executing filter" ),
346       QMessageBox::tr( "Unknown error" )
347       );
348
349   } // yrt
350 }
351
352 // eof - $RCSfile$