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