]> 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->_UpdateLoadedPlugins( );
140 }
141
142 // -------------------------------------------------------------------------
143 void cpPipelineEditor::
144 _ButtonLoadPluginsPath( )
145 {
146   QFileDialog dlg( this );
147   dlg.setFileMode( QFileDialog::DirectoryOnly );
148   dlg.setDirectory( "." );
149   if( !( dlg.exec( ) ) )
150     return;
151
152   // Create a new workspace, if not ready
153   if( this->m_Workspace == NULL )
154     this->m_Workspace = new cpPlugins::Interface::Workspace( );
155
156   // Read
157   std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
158   if( !( this->m_Workspace->LoadPluginsPath( dir, false ) ) )
159     QMessageBox::critical(
160       this,
161       "Error loading plugins directory",
162       dir.c_str( )
163       );
164
165   // Update view
166   this->_UpdateLoadedPlugins( );
167 }
168
169 // -------------------------------------------------------------------------
170 void cpPipelineEditor::
171 _ActionOpenWorkspace( )
172 {
173   QFileDialog dlg( this );
174   dlg.setFileMode( QFileDialog::ExistingFile );
175   dlg.setDirectory( "." );
176   dlg.setNameFilter(
177     QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
178     );
179   dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
180   if( !( dlg.exec( ) ) )
181     return;
182   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
183
184   if( this->m_Workspace != NULL )
185     delete this->m_Workspace;
186   this->m_Workspace = new cpPlugins::Interface::Workspace( );
187   std::string err = this->m_Workspace->LoadWorkspace( fname );
188   if( err == "" )
189   {
190     this->m_UI->Canvas->setWorkspace( this->m_Workspace );
191   }
192   else
193   {
194     delete this->m_Workspace;
195     this->m_Workspace = NULL;
196     QMessageBox::critical(
197       this,
198       QMessageBox::tr( "Error loading workspace" ),
199       QMessageBox::tr( err.c_str( ) )
200       );
201
202   } // fi
203 }
204
205 // eof - $RCSfile$