]> Creatis software - cpPlugins.git/blob - appli/ImageMPR/ImageMPR.cxx
...
[cpPlugins.git] / appli / ImageMPR / ImageMPR.cxx
1 #include "ImageMPR.h"
2 #include "ui_ImageMPR.h"
3
4 #include <QFileInfo>
5 #include <QMessageBox>
6
7 // -------------------------------------------------------------------------
8 #define ImageMPR_ConnectAction( ACTION )                \
9   QObject::connect(                                     \
10     this->m_UI->a##ACTION, SIGNAL( triggered( ) ),      \
11     this, SLOT( _a##ACTION( ) )                         \
12     )
13
14 // -------------------------------------------------------------------------
15 ImageMPR::
16 ImageMPR( QWidget* parent )
17   : QMainWindow( parent ),
18     m_UI( new Ui::ImageMPR )
19 {
20   this->m_UI->setupUi( this );
21   this->m_Plugins.SetWidget( this );
22   this->m_Plugins.SetApplication( this );
23
24   // Connect actions
25   ImageMPR_ConnectAction( OpenImage );
26   ImageMPR_ConnectAction( OpenDICOMSeries );
27   ImageMPR_ConnectAction( OpenSegmentation );
28   ImageMPR_ConnectAction( OpenPolyData );
29   ImageMPR_ConnectAction( SaveImage );
30   ImageMPR_ConnectAction( SavePolyData );
31   ImageMPR_ConnectAction( Undo );
32   ImageMPR_ConnectAction( Redo );
33   ImageMPR_ConnectAction( LoadPlugins );
34   ImageMPR_ConnectAction( ShowPlugins );
35
36   // Associate model with view
37   for( unsigned int i = 0; i < 4; ++i )
38     this->m_Plugins.AddInteractor( this->m_UI->MPR->GetInteractor( i ) );
39
40   // Try to load default plugins
41   QStringList args = QApplication::arguments( );
42   QFileInfo info( args.at( 0 ) );
43   this->m_Plugins.LoadPluginsPath(
44     info.absolutePath( ).toStdString( ), true
45     );
46
47   // Put loaded plugins into menu
48   this->_AssociatePluginsToMenu( );
49 }
50
51 // -------------------------------------------------------------------------
52 ImageMPR::
53 ~ImageMPR( )
54 {
55   delete this->m_UI;
56 }
57
58 // -------------------------------------------------------------------------
59 void ImageMPR::
60 UpdateActualFilter( )
61 {
62   if( !( this->m_Plugins.HasActiveFilter( ) ) )
63     return;
64
65   // Update filter
66   TPlugins::TStringContainer outputs;
67   if(
68     !(
69       this->m_Plugins.UpdateActiveFilter(
70         outputs, this->m_ActiveFilterMainInput
71         )
72       )
73     )
74     return;
75
76   // Show outputs
77   for( auto oIt = outputs.begin( ); oIt != outputs.end( ); ++oIt )
78   {
79     std::string parent = this->m_Plugins.GetParent( *oIt );
80     TDataObject* dobj = this->m_Plugins.GetData< TDataObject >( *oIt );
81     if( this->m_UI->MPR->AddData( dobj, *oIt, parent ) )
82       this->m_UI->MPR->ShowData( *oIt );
83
84   } // rof
85 }
86
87 // -------------------------------------------------------------------------
88 void ImageMPR::
89 _AssociatePluginsToMenu( )
90 {
91   this->m_UI->MenuFilters->clear( );
92
93   TPlugins::TStringContainer categories;
94   this->m_Plugins.GetLoadedCategories( categories );
95   for( auto cIt = categories.begin( ); cIt != categories.end( ); ++cIt )
96   {
97     QMenu* category = this->m_UI->MenuFilters->addMenu( cIt->c_str( ) );
98     const TPlugins::TStringContainer& filters =
99       this->m_Plugins.GetLoadedFilters( *cIt );
100     for( auto fIt = filters.begin( ); fIt != filters.end( ); ++fIt )
101     {
102       QAction* filter = category->addAction( fIt->c_str( ) );
103       this->connect(
104         filter, SIGNAL( triggered( ) ),
105         this, SLOT( _execPlugin( ) )
106         );
107
108     } // rof
109
110   } // rof
111 }
112
113 // -------------------------------------------------------------------------
114 #define ImageMPR_ReadImage( F )                                         \
115   this->m_UI->MPR->DeleteAllData( );                                    \
116   this->m_Plugins.ClearDataObjects( );                                  \
117   try                                                                   \
118   {                                                                     \
119     std::string name = this->m_Plugins.Read##F( "" );                   \
120     if( name == "" )                                                    \
121       return;                                                           \
122     TImage* image = this->m_Plugins.GetData< TImage >( name );          \
123     this->m_UI->MPR->AddData( image, name, "" );                        \
124     this->m_UI->MPR->SetMainImage( name );                              \
125     this->m_UI->MPR->ShowData( name );                                  \
126   }                                                                     \
127   catch( std::exception& err )                                          \
128   {                                                                     \
129     QMessageBox::critical(                                              \
130       this,                                                             \
131       QMessageBox::tr( "Error reading image." ),                        \
132       QMessageBox::tr( err.what( ) )                                    \
133       );                                                                \
134   }
135
136 void ImageMPR::_aOpenImage( )       { ImageMPR_ReadImage( Image ) }
137 void ImageMPR::_aOpenDICOMSeries( ) { ImageMPR_ReadImage( DicomSeries ) }
138
139 // -------------------------------------------------------------------------
140 void ImageMPR::
141 _aOpenSegmentation( )
142 {
143   try
144   {
145     std::string parent = this->m_UI->MPR->GetMainImage( );
146     std::string name = this->m_Plugins.ReadImage( parent );
147     if( name == "" )
148       return;
149     TImage* image = this->m_Plugins.GetData< TImage >( name );
150     this->m_UI->MPR->AddData( image, name, parent );
151     this->m_UI->MPR->ShowData( name );
152   }
153   catch( std::exception& err )
154   {
155     QMessageBox::critical(
156       this,
157       QMessageBox::tr( "Error reading image." ),
158       QMessageBox::tr( err.what( ) )
159       );
160
161   } // yrt
162 }
163
164 // -------------------------------------------------------------------------
165 void ImageMPR::
166 _aOpenPolyData( )
167 {
168 }
169
170 // -------------------------------------------------------------------------
171 void ImageMPR::
172 _aSaveImage( )
173 {
174   this->m_Plugins.WriteDataObject( this->m_UI->MPR->GetSelectedData( ) );
175 }
176
177 // -------------------------------------------------------------------------
178 void ImageMPR::
179 _aSavePolyData( )
180 {
181 }
182
183 // -------------------------------------------------------------------------
184 void ImageMPR::
185 _aUndo( )
186 {
187 }
188
189 // -------------------------------------------------------------------------
190 void ImageMPR::
191 _aRedo( )
192 {
193 }
194
195 // -------------------------------------------------------------------------
196 void ImageMPR::
197 _aLoadPlugins( )
198 {
199   this->m_Plugins.DialogLoadPlugins( );
200   this->_AssociatePluginsToMenu( );
201 }
202
203 // -------------------------------------------------------------------------
204 void ImageMPR::
205 _aShowPlugins( )
206 {
207 }
208
209 // -------------------------------------------------------------------------
210 void ImageMPR::
211 _execPlugin( )
212 {
213   // Get filter's name
214   QAction* action = dynamic_cast< QAction* >( this->sender( ) );
215   if( action == NULL )
216     return;
217   std::string filter_name = action->text( ).toStdString( );
218
219   // Activate filter
220   if( !( this->m_Plugins.ActivateFilter( filter_name ) ) )
221     return;
222
223   // Get IO names
224   TPlugins::TStringContainer inputs;
225   this->m_Plugins.GetActiveFilterInputsNames( inputs );
226
227   // Configure inputs
228   if( inputs.size( ) > 1 )
229   {
230     // TODO
231   }
232   else if( inputs.size( ) == 1 )
233   {
234     this->m_ActiveFilterMainInput = this->m_UI->MPR->GetSelectedData( );
235     this->m_Plugins.ConnectInputInActiveFilter(
236       this->m_ActiveFilterMainInput, *( inputs.begin( ) )
237       );
238
239   } // fi
240
241   // Configure paramereters
242   auto dlg_res = this->m_Plugins.ConfigureActiveFilter( );
243   if( !dlg_res )
244   {
245     // Just deactivate filter, since it was canceled
246     this->m_Plugins.DeactivateFilter( );
247     return;
248   }
249   else
250   {
251     // Execute automatic filter and associate outputs
252     this->UpdateActualFilter( );
253     this->m_Plugins.DeactivateFilter( );
254
255   } // fi
256 }
257
258 // eof - $RCSfile$