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