]> Creatis software - cpMesh.git/blob - appli/InteractiveDeformableMeshSegmentation/MainWnd_LoadPlugins.cxx
New plugin added
[cpMesh.git] / appli / InteractiveDeformableMeshSegmentation / MainWnd_LoadPlugins.cxx
1 #include "MainWnd.h"
2 #include "ui_MainWnd.h"
3
4 #include <cctype>
5 #include <fstream>
6 #include <QMessageBox>
7
8 // -------------------------------------------------------------------------
9 bool MainWnd::
10 _LoadPlugins( )
11 {
12   // Clear states
13   this->m_Plugins.UnloadAll( );
14   this->m_BaseClasses.clear( );
15   this->m_SegmentationClasses.clear( );
16   this->m_SegmentationFilterClasses.clear( );
17   this->m_MeshFilterClasses.clear( );
18
19   // Read file and load plugins
20   std::ifstream in( this->m_PluginsConfigurationFile.c_str( ) );
21   if( !in )
22     return( false );
23   this->m_LastOpenedFile = this->m_PluginsConfigurationFile;
24   std::string line, actual_section = "";
25   std::getline( in, line );
26   while( !( in.eof( ) ) )
27   {
28     std::string clean_line = line;
29     clean_line.erase(
30       std::remove_if( clean_line.begin( ), clean_line.end( ), isspace ),
31       clean_line.end( )
32       );
33     if( clean_line[ 0 ] != '#' )
34     {
35       if(
36         clean_line != "sectionplugins"                     &&
37         clean_line != "sectionbase_classes"                &&
38         clean_line != "sectionsegmentation_classes"        &&
39         clean_line != "sectionsegmentation_filter_classes" &&
40         clean_line != "sectionmesh_filter_classes"
41         )
42       {
43         if( clean_line != "" )
44         {
45           if( actual_section == "sectionplugins" )
46           {
47             if( !( this->m_Plugins.Load( clean_line ) ) )
48             {
49               QMessageBox::warning(
50                 this,
51                 tr( "Ignoring plugin" ),
52                 tr( clean_line.c_str( ) )
53                 );
54
55             } // fi
56           }
57           else
58           {
59             std::string name =
60               clean_line.substr( clean_line.find_last_of( ":" ) + 1 );
61             if( actual_section == "sectionbase_classes" )
62               this->m_BaseClasses[ name ] = clean_line;
63             else if( actual_section == "sectionsegmentation_classes" )
64               this->m_SegmentationClasses[ name ] = clean_line;
65             else if( actual_section == "sectionsegmentation_filter_classes" )
66               this->m_SegmentationFilterClasses[ name ] = clean_line;
67             else if( actual_section == "sectionmesh_filter_classes" )
68               this->m_MeshFilterClasses[ name ] = clean_line;
69
70           } // fi
71
72         } // fi
73       }
74       else if( clean_line != "" )
75       {
76         if( clean_line[ 0 ] != '#' )
77           actual_section = clean_line;
78
79       } // fi
80
81     } // fi
82     std::getline( in, line );
83
84   } // elihw
85   in.close( );
86
87   // Check classes existence
88   if( !( this->_CheckClassesInPlugins( this->m_BaseClasses ) ) )
89   {
90     QMessageBox::critical(
91       this,
92       tr( "Could not load all base plugins." ),
93       tr( "Could not load all base plugins... Closing application!" )
94       );
95     std::exit( 1 );
96
97   } // fi
98
99   // Needed object from plugins
100   this->_AddPluginActions(
101     this->m_SegmentationClasses, this->m_UI->menuSegmentInputImage,
102     SLOT( _triggered_actionSegmentImage( ) )
103     );
104   this->_AddPluginActions(
105     this->m_SegmentationFilterClasses, this->m_UI->menuFilterSegmentedImage,
106     SLOT( _triggered_actionFilterSegmentation( ) )
107     );
108   this->_AddPluginActions(
109     this->m_MeshFilterClasses, this->m_UI->menuProcessMesh,
110     SLOT( _triggered_actionProcessMesh( ) )
111     );
112
113   // Historic objects
114
115
116
117   /*
118     this->m_ImageReaderClassName = "";
119     this->m_ImageSeriesReaderClassName = "";
120     this->m_ImageWriterClassName = "";
121
122   if( in )
123   {
124     std::string plugin;
125     std::getline( in, plugin );
126     while( !( in.eof( ) ) )
127     {
128     if( this->m_Plugins.Load( plugin ) )
129       {
130         TPluginsInterface::TClassesIterator cIt =
131           this->m_Plugins.GetClasses( ).begin( );
132         TPluginsInterface::TClassesIterator end_cIt =
133           this->m_Plugins.GetClasses( ).end( );
134         for( ; cIt != end_cIt; ++cIt )
135         {
136           std::string c_name = cIt->first;
137           c_name = c_name.substr( c_name.find_last_of( ":" ) + 1 );
138           if( c_name == "ImageReader" )
139           {
140             this->m_ImageReaderClassName = cIt->first;
141           }
142           else if( c_name == "ImageSeriesReader" )
143           {
144             this->m_ImageSeriesReaderClassName = cIt->first;
145           }
146           else if( c_name == "ImageWriter" )
147           {
148             this->m_ImageWriterClassName = cIt->first;
149           }
150           else
151           {
152           } // fi
153           
154         } // rof
155
156            TFilterPlugins::TClassesIterator cIt =
157            this->m_Plugins.BeginClasses( );
158            for( ; cIt != this->m_Plugins.EndClasses( ); ++cIt )
159            {
160            TFilterObject* filter =
161            this->m_Plugins.CreateObject( cIt->first );
162            if( filter == NULL )
163            continue;
164            std::string cat = filter->GetCategory( );
165            std::string catType = cat.substr( cat.find_last_of( ":" ) );
166            if( catType == ":BinaryImageToBinaryImageFilter" )
167            {
168            QAction* action = this->m_UI->menuFilterSegmentedImage->
169            addAction( QString( cIt->first.c_str( ) ) );
170            QObject::connect(
171            action, SIGNAL( triggered( ) ),
172            this, SLOT( triggered_aFilterSegmentedImage( ) )
173            );
174            }
175            else if( catType == ":ImageToMeshFilter" )
176            {
177            QAction* action = this->m_UI->menuExtractMesh->
178            addAction( QString( cIt->first.c_str( ) ) );
179            QObject::connect(
180            action, SIGNAL( triggered( ) ),
181            this, SLOT( triggered_aSegmentedImageToMesh( ) )
182            );
183
184            } // fi
185            delete filter;
186            } // rof
187       }
188       else
189       {
190         QMessageBox::warning(
191           this,
192           tr( "Ignoring plugin" ),
193           tr( plugin.c_str( ) )
194           );
195
196       } // fi
197       std::getline( in, plugin );
198
199     } // elihw
200   }
201   else
202   {
203     QMessageBox::critical(
204       this,
205       tr( "No plugins file loaded!" ),
206       tr( this->m_PluginsConfigurationFile.c_str( ) )
207       );
208
209   } // fi
210   in.close( );
211
212   if( this->m_ImageReaderClassName == "" )
213   {
214     QMessageBox::critical(
215       this,
216       tr( "No ImageReader found in plugins!" ),
217       tr( this->m_PluginsConfigurationFile.c_str( ) )
218       );
219
220   } // fi
221
222   if( this->m_ImageWriterClassName == "" )
223   {
224     QMessageBox::critical(
225       this,
226       tr( "No ImageWriter found in plugins!" ),
227       tr( this->m_PluginsConfigurationFile.c_str( ) )
228       );
229
230   } // fi
231   this->_UpdateEnabledFlags( );
232   */
233        }
234
235 // -------------------------------------------------------------------------
236 bool MainWnd::
237 _CheckClassesInPlugins( const TStringMap& classes )
238 {
239   bool r = true;
240   TStringMap::const_iterator cIt = classes.begin( );
241   for( ; cIt != classes.end( ); ++cIt )
242   {
243     TPluginObject* o = this->m_Plugins.CreateObject( cIt->second );
244     if( o != NULL )
245       delete o;
246     else
247       r &= false;
248     
249   } // rof
250   return( r );
251 }
252
253 // -------------------------------------------------------------------------
254 void MainWnd::
255 _AddPluginActions(
256   const TStringMap& classes, QMenu* menu, const char* method
257   )
258 {
259   TStringMap::const_iterator clIt = classes.begin( );
260   for( ; clIt != classes.end( ); ++clIt )
261   {
262     QAction* action = menu->addAction( QString( clIt->first.c_str( ) ) );
263     QObject::connect( action, SIGNAL( triggered( ) ), this, method );
264
265   } // rof
266 }
267
268 // eof - $RCSfile$