]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/cpPipelineEditor.cxx
...
[cpPlugins.git] / appli / cpPipelineEditor / cpPipelineEditor.cxx
1 #include "cpPipelineEditor.h"
2 #include "ui_cpPipelineEditor.h"
3
4 #include <QFileDialog>
5 #include <QMessageBox>
6 #include <cpPlugins/Interface/Workspace.h>
7
8 // -------------------------------------------------------------------------
9 #define cpPipelineEditor_ConnectAction( ACTION )        \
10   QObject::connect(                                     \
11     this->m_UI->Action##ACTION, SIGNAL( triggered( ) ), \
12     this, SLOT( _Action##ACTION( ) )                    \
13     )
14
15 // -------------------------------------------------------------------------
16 #define cpPipelineEditor_ConnectButton( BUTTON )        \
17   QObject::connect(                                     \
18     this->m_UI->Button##BUTTON, SIGNAL( clicked( ) ),   \
19     this, SLOT( _Button##BUTTON( ) )                    \
20     )
21
22 // -------------------------------------------------------------------------
23 cpPipelineEditor::
24 cpPipelineEditor( QWidget* parent )
25   : QMainWindow( parent ),
26     m_UI( new Ui::cpPipelineEditor ),
27     m_Workspace( NULL )
28 {
29   this->m_UI->setupUi( this );
30
31   // Connect actions to slots
32   cpPipelineEditor_ConnectButton( LoadPluginsFile );
33   cpPipelineEditor_ConnectButton( LoadPluginsPath );
34   cpPipelineEditor_ConnectAction( OpenWorkspace );
35 }
36
37 // -------------------------------------------------------------------------
38 cpPipelineEditor::
39 ~cpPipelineEditor( )
40 {
41   delete this->m_UI;
42   if( this->m_Workspace != NULL )
43     delete this->m_Workspace;
44 }
45
46 // -------------------------------------------------------------------------
47 void cpPipelineEditor::
48 _UpdateLoadedPlugins( )
49 {
50   typedef cpPlugins::Interface::Workspace::TStringContainer _TStrings;
51
52   if( this->m_Workspace == NULL )
53     return;
54
55   _TStrings categories;
56   this->m_Workspace->GetLoadedPluginCategories( categories );
57   for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
58   {
59     // Create or get category
60     QList< QTreeWidgetItem* > cat_items =
61       this->m_UI->LoadedPlugins->findItems(
62         cIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
63         );
64     QTreeWidgetItem* cat = NULL;
65     if( cat_items.size( ) == 0 )
66     {
67       cat = new QTreeWidgetItem(
68         ( QTreeWidgetItem* )( NULL ), QStringList( cIt->c_str( ) )
69         );
70       this->m_UI->LoadedPlugins->addTopLevelItem( cat );
71     }
72     else
73       cat = cat_items[ 0 ];
74
75     // Add filters
76     _TStrings filters = this->m_Workspace->GetLoadedPluginFilters( *cIt );
77     for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
78     {
79       // Find filter
80       QList< QTreeWidgetItem* > filter_items =
81         this->m_UI->LoadedPlugins->findItems(
82           fIt->c_str( ), Qt::MatchExactly | Qt::MatchRecursive
83           );
84       auto fiIt = filter_items.begin( );
85       auto found_fiIt = filter_items.end( );
86       for( ; fiIt != filter_items.end( ); ++fiIt )
87         if( ( *fiIt )->parent( ) == cat )
88           found_fiIt = fiIt;
89
90       // Add filter
91       if( found_fiIt == filter_items.end( ) )
92         QTreeWidgetItem* filter = new QTreeWidgetItem(
93           cat, QStringList( fIt->c_str( ) )
94           );
95
96     } // rof
97
98   } // rof
99 }
100
101 // -------------------------------------------------------------------------
102 void cpPipelineEditor::
103 _ButtonLoadPluginsFile( )
104 {
105   QFileDialog dlg( this );
106   dlg.setFileMode( QFileDialog::ExistingFiles );
107   dlg.setDirectory( "." );
108 #ifdef _WIN32
109   dlg.setNameFilter( "Plugins file (*.dll);;All files (*)" );
110   dlg.setDefaultSuffix( "dll" );
111 #else // _WIN32
112   dlg.setNameFilter( "Plugins file (*.so);;All files (*)" );
113   dlg.setDefaultSuffix( "so" );
114 #endif // _WIN32
115   if( !( dlg.exec( ) ) )
116     return;
117
118   // Create a new workspace, if not ready
119   if( this->m_Workspace == NULL )
120     this->m_Workspace = new cpPlugins::Interface::Workspace( );
121
122   // Read
123   QStringList names = dlg.selectedFiles( );
124   std::stringstream err_str;
125   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
126     if( !( this->m_Workspace->LoadPlugins( qIt->toStdString( ) ) ) )
127       err_str << qIt->toStdString( ) << std::endl;
128
129   // Show an error message
130   std::string err = err_str.str( );
131   if( err.size( ) > 0 )
132     QMessageBox::critical(
133       this,
134       "Error loading plugins",
135       err.c_str( )
136       );
137
138   // Update view
139   this->m_UI->Canvas->setWorkspace( this->m_Workspace );
140   this->_UpdateLoadedPlugins( );
141 }
142
143 // -------------------------------------------------------------------------
144 void cpPipelineEditor::
145 _ButtonLoadPluginsPath( )
146 {
147   QFileDialog dlg( this );
148   dlg.setFileMode( QFileDialog::DirectoryOnly );
149   dlg.setDirectory( "." );
150   if( !( dlg.exec( ) ) )
151     return;
152
153   // Create a new workspace, if not ready
154   if( this->m_Workspace == NULL )
155     this->m_Workspace = new cpPlugins::Interface::Workspace( );
156
157   // Read
158   std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
159   if( !( this->m_Workspace->LoadPluginsPath( dir, false ) ) )
160     QMessageBox::critical(
161       this,
162       "Error loading plugins directory",
163       dir.c_str( )
164       );
165
166   // Update view
167   this->m_UI->Canvas->setWorkspace( this->m_Workspace );
168   this->_UpdateLoadedPlugins( );
169 }
170
171 // -------------------------------------------------------------------------
172 void cpPipelineEditor::
173 _ActionOpenWorkspace( )
174 {
175   QFileDialog dlg( this );
176   dlg.setFileMode( QFileDialog::ExistingFile );
177   dlg.setDirectory( "." );
178   dlg.setNameFilter(
179     QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
180     );
181   dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
182   if( !( dlg.exec( ) ) )
183     return;
184   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
185
186   if( this->m_Workspace != NULL )
187     delete this->m_Workspace;
188   this->m_Workspace = new cpPlugins::Interface::Workspace( );
189   std::string err = this->m_Workspace->LoadWorkspace( fname );
190   if( err == "" )
191   {
192     this->m_UI->Canvas->setWorkspace( this->m_Workspace );
193   }
194   else
195   {
196     delete this->m_Workspace;
197     this->m_Workspace = NULL;
198     QMessageBox::critical(
199       this,
200       QMessageBox::tr( "Error loading workspace" ),
201       QMessageBox::tr( err.c_str( ) )
202       );
203
204   } // fi
205 }
206
207 // eof - $RCSfile$