]> Creatis software - cpPlugins.git/blob - appli/cpPipelineEditor/cpPipelineEditor.cxx
088edbcf999652ec976ddf31bf81b20aa88f3920
[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( int argc, char* argv[], QWidget* parent )
27   : QMainWindow( parent ),
28     m_UI( new Ui::cpPipelineEditor ),
29     m_Workspace( NULL )
30 {
31   this->m_UI->setupUi( this );
32
33   // Prepare plugins interface
34   this->m_Plugins = new cpPlugins::Interface::Interface( );
35   QFileInfo info( argv[ 0 ] );
36   if( info.exists( ) )
37   {
38     std::string path = info.canonicalPath( ).toStdString( );
39     if( !( this->m_Plugins->LoadDefaultConfiguration( path ) ) )
40       if( this->m_Plugins->LoadFromFolder( path, false ) )
41         if( !( this->m_Plugins->SaveDefaultConfiguration( path ) ) )
42           QMessageBox::critical(
43             this,
44             "Error creating default plugins configuration",
45             "Could not save default plugins configuration"
46             );
47     this->_UpdateLoadedPlugins( );
48
49   } // fi
50
51   // Create an empty workspace
52   this->m_Workspace = new cpPlugins::Interface::Workspace( );
53   this->m_Workspace->SetInterface( this->m_Plugins );
54   this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
55
56   // Connect actions to slots
57   cpPipelineEditor_ConnectButton( LoadPluginsFile );
58   cpPipelineEditor_ConnectButton( LoadPluginsPath );
59   cpPipelineEditor_ConnectAction( OpenWorkspace );
60   cpPipelineEditor_ConnectAction( SaveWorkspace );
61 }
62
63 // -------------------------------------------------------------------------
64 cpPipelineEditor::
65 ~cpPipelineEditor( )
66 {
67   delete this->m_UI;
68   if( this->m_Workspace != NULL )
69     delete this->m_Workspace;
70   delete this->m_Plugins;
71 }
72
73 // -------------------------------------------------------------------------
74 void cpPipelineEditor::
75 _UpdateLoadedPlugins( )
76 {
77   auto& classes = this->m_Plugins->GetClasses( );
78
79   if( classes.size( ) == 0 )
80   {
81     QMessageBox::critical(
82       this,
83       "Error loading default plugins",
84       "No plugins loaded: remember to load some!!!"
85       );
86     return;
87
88   } // fi
89
90   auto catIt = classes.begin( );
91   for( ; catIt != classes.end( ); ++catIt )
92   {
93     // Create or get category
94     QList< QTreeWidgetItem* > cat_items =
95       this->m_UI->LoadedPlugins->findItems(
96         catIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
97         );
98     QTreeWidgetItem* cat = NULL;
99     if( cat_items.size( ) == 0 )
100     {
101       cat = new QTreeWidgetItem(
102         ( QTreeWidgetItem* )( NULL ), QStringList( catIt->first.c_str( ) )
103         );
104       this->m_UI->LoadedPlugins->addTopLevelItem( cat );
105     }
106     else
107       cat = cat_items[ 0 ];
108
109     // Create filters
110     auto fIt = catIt->second.begin( );
111     for( ; fIt != catIt->second.end( ); ++fIt )
112     {
113       QList< QTreeWidgetItem* > filter_items =
114         this->m_UI->LoadedPlugins->findItems(
115           fIt->first.c_str( ), Qt::MatchExactly | Qt::MatchRecursive
116           );
117       auto fiIt = filter_items.begin( );
118       auto found_fiIt = filter_items.end( );
119       for( ; fiIt != filter_items.end( ); ++fiIt )
120         if( ( *fiIt )->parent( ) == cat )
121           found_fiIt = fiIt;
122
123       // Add filter
124       if( found_fiIt == filter_items.end( ) )
125         QTreeWidgetItem* filter = new QTreeWidgetItem(
126           cat, QStringList( fIt->first.c_str( ) )
127           );
128     } // rof
129
130   } // rof
131 }
132
133 // -------------------------------------------------------------------------
134 void cpPipelineEditor::
135 _ButtonLoadPluginsFile( )
136 {
137   QFileDialog dlg( this );
138   dlg.setFileMode( QFileDialog::ExistingFiles );
139   dlg.setDirectory( "." );
140 #ifdef _WIN32
141   dlg.setNameFilter( "Plugins file (*.dll);;All files (*)" );
142   dlg.setDefaultSuffix( "dll" );
143 #else // _WIN32
144   dlg.setNameFilter( "Plugins file (*.so);;All files (*)" );
145   dlg.setDefaultSuffix( "so" );
146 #endif // _WIN32
147   if( !( dlg.exec( ) ) )
148     return;
149
150   // Read
151   QStringList names = dlg.selectedFiles( );
152   std::stringstream err_str;
153   for( auto qIt = names.begin( ); qIt != names.end( ); ++qIt )
154     if( !( this->m_Plugins->Load( qIt->toStdString( ) ) ) )
155       err_str << qIt->toStdString( ) << std::endl;
156
157   // Show an error message
158   std::string err = err_str.str( );
159   if( err.size( ) > 0 )
160     QMessageBox::critical(
161       this,
162       "Error loading plugins",
163       err.c_str( )
164       );
165
166   // Update view
167   this->_UpdateLoadedPlugins( );
168 }
169
170 // -------------------------------------------------------------------------
171 void cpPipelineEditor::
172 _ButtonLoadPluginsPath( )
173 {
174   QFileDialog dlg( this );
175   dlg.setFileMode( QFileDialog::DirectoryOnly );
176   dlg.setDirectory( "." );
177   if( !( dlg.exec( ) ) )
178     return;
179
180   // Read
181   std::string dir = dlg.selectedFiles( ).begin( )->toStdString( );
182   if( !( this->m_Plugins->LoadFromFolder( dir, false ) ) )
183     QMessageBox::critical(
184       this,
185       "Error loading plugins directory",
186       dir.c_str( )
187       );
188
189   // Update view
190   this->_UpdateLoadedPlugins( );
191 }
192
193 // -------------------------------------------------------------------------
194 void cpPipelineEditor::
195 _ActionOpenWorkspace( )
196 {
197   QFileDialog dlg( this );
198   dlg.setFileMode( QFileDialog::ExistingFile );
199   dlg.setDirectory( "." );
200   dlg.setNameFilter(
201     QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
202     );
203   dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
204   if( !( dlg.exec( ) ) )
205     return;
206   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
207
208   if( this->m_Workspace != NULL )
209     delete this->m_Workspace;
210   this->m_Workspace = new cpPlugins::Interface::Workspace( );
211   this->m_Workspace->SetInterface( this->m_Plugins );
212   std::string err = this->m_Workspace->LoadWorkspace( fname );
213   if( err != "" )
214   {
215     delete this->m_Workspace;
216     this->m_Workspace = NULL;
217     QMessageBox::critical(
218       this,
219       QMessageBox::tr( "Error loading workspace" ),
220       QMessageBox::tr( err.c_str( ) )
221       );
222   }
223   else
224     this->m_UI->Canvas->editor( )->setWorkspace( this->m_Workspace );
225 }
226
227 // -------------------------------------------------------------------------
228 void cpPipelineEditor::
229 _ActionSaveWorkspace( )
230 {
231   if( this->m_Workspace == NULL )
232     return;
233
234   QFileDialog dlg( this );
235   dlg.setFileMode( QFileDialog::AnyFile );
236   dlg.setDirectory( "." );
237   dlg.setAcceptMode( QFileDialog::AcceptSave );
238   dlg.setNameFilter(
239     QFileDialog::tr( "Workspace file (*.xml);;All files (*)" )
240     );
241   dlg.setDefaultSuffix( QFileDialog::tr( "xml" ) );
242   if( !( dlg.exec( ) ) )
243     return;
244   std::string fname = dlg.selectedFiles( ).at( 0 ).toStdString( );
245
246   this->m_UI->Canvas->editor( )->synchronizeBlockPositions( );
247   std::string err = this->m_Workspace->SaveWorkspace( fname );
248   if( err != "" )
249     QMessageBox::critical(
250       this,
251       QMessageBox::tr( "Error saving workspace" ),
252       QMessageBox::tr( err.c_str( ) )
253       );
254 }
255
256 // eof - $RCSfile$