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