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