]> Creatis software - cpMesh.git/blob - appli/InteractiveDeformableMeshSegmentation/MainWnd_LoadPlugins.cxx
QuadEdgeMesh ported to cpPlugins/Extensions
[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(
34       clean_line != "sectionplugins"                     &&
35       clean_line != "sectionbase_classes"                &&
36       clean_line != "sectionsegmentation_classes"        &&
37       clean_line != "sectionsegmentation_filter_classes" &&
38       clean_line != "sectionmesh_filter_classes"
39       )
40     {
41       if( clean_line != "" )
42       {
43         if( actual_section == "sectionplugins" )
44         {
45           if( !( this->m_Plugins.Load( clean_line ) ) )
46           {
47             QMessageBox::warning(
48               this,
49               tr( "Ignoring plugin" ),
50               tr( clean_line.c_str( ) )
51               );
52
53           } // fi
54         }
55         else
56         {
57           std::string name =
58             clean_line.substr( clean_line.find_last_of( ":" ) + 1 );
59           if( actual_section == "sectionbase_classes" )
60             this->m_BaseClasses[ name ] = clean_line;
61           else if( actual_section == "sectionsegmentation_classes" )
62             this->m_SegmentationClasses[ name ] = clean_line;
63           else if( actual_section == "sectionsegmentation_filter_classes" )
64             this->m_SegmentationFilterClasses[ name ] = clean_line;
65           else if( actual_section == "sectionmesh_filter_classes" )
66             this->m_MeshFilterClasses[ name ] = clean_line;
67
68         } // fi
69
70       } // fi
71     }
72     else if( clean_line != "" )
73     {
74       if( clean_line[ 0 ] != '#' )
75         actual_section = clean_line;
76
77     } // fi
78
79     std::getline( in, line );
80
81   } // elihw
82   in.close( );
83
84   // Check classes existence
85   if( !( this->_CheckClassesInPlugins( this->m_BaseClasses ) ) )
86   {
87     QMessageBox::critical(
88       this,
89       tr( "Could not load all base plugins." ),
90       tr( "Could not load all base plugins... Closing application!" )
91       );
92     std::exit( 1 );
93
94   } // fi
95
96   // Needed object from plugins
97
98   // Historic objects
99
100
101
102   /*
103     this->m_ImageReaderClassName = "";
104     this->m_ImageSeriesReaderClassName = "";
105     this->m_ImageWriterClassName = "";
106
107   if( in )
108   {
109     std::string plugin;
110     std::getline( in, plugin );
111     while( !( in.eof( ) ) )
112     {
113     if( this->m_Plugins.Load( plugin ) )
114       {
115         TPluginsInterface::TClassesIterator cIt =
116           this->m_Plugins.GetClasses( ).begin( );
117         TPluginsInterface::TClassesIterator end_cIt =
118           this->m_Plugins.GetClasses( ).end( );
119         for( ; cIt != end_cIt; ++cIt )
120         {
121           std::string c_name = cIt->first;
122           c_name = c_name.substr( c_name.find_last_of( ":" ) + 1 );
123           if( c_name == "ImageReader" )
124           {
125             this->m_ImageReaderClassName = cIt->first;
126           }
127           else if( c_name == "ImageSeriesReader" )
128           {
129             this->m_ImageSeriesReaderClassName = cIt->first;
130           }
131           else if( c_name == "ImageWriter" )
132           {
133             this->m_ImageWriterClassName = cIt->first;
134           }
135           else
136           {
137           } // fi
138           
139         } // rof
140
141            TFilterPlugins::TClassesIterator cIt =
142            this->m_Plugins.BeginClasses( );
143            for( ; cIt != this->m_Plugins.EndClasses( ); ++cIt )
144            {
145            TFilterObject* filter =
146            this->m_Plugins.CreateObject( cIt->first );
147            if( filter == NULL )
148            continue;
149            std::string cat = filter->GetCategory( );
150            std::string catType = cat.substr( cat.find_last_of( ":" ) );
151            if( catType == ":BinaryImageToBinaryImageFilter" )
152            {
153            QAction* action = this->m_UI->menuFilterSegmentedImage->
154            addAction( QString( cIt->first.c_str( ) ) );
155            QObject::connect(
156            action, SIGNAL( triggered( ) ),
157            this, SLOT( triggered_aFilterSegmentedImage( ) )
158            );
159            }
160            else if( catType == ":ImageToMeshFilter" )
161            {
162            QAction* action = this->m_UI->menuExtractMesh->
163            addAction( QString( cIt->first.c_str( ) ) );
164            QObject::connect(
165            action, SIGNAL( triggered( ) ),
166            this, SLOT( triggered_aSegmentedImageToMesh( ) )
167            );
168
169            } // fi
170            delete filter;
171            } // rof
172       }
173       else
174       {
175         QMessageBox::warning(
176           this,
177           tr( "Ignoring plugin" ),
178           tr( plugin.c_str( ) )
179           );
180
181       } // fi
182       std::getline( in, plugin );
183
184     } // elihw
185   }
186   else
187   {
188     QMessageBox::critical(
189       this,
190       tr( "No plugins file loaded!" ),
191       tr( this->m_PluginsConfigurationFile.c_str( ) )
192       );
193
194   } // fi
195   in.close( );
196
197   if( this->m_ImageReaderClassName == "" )
198   {
199     QMessageBox::critical(
200       this,
201       tr( "No ImageReader found in plugins!" ),
202       tr( this->m_PluginsConfigurationFile.c_str( ) )
203       );
204
205   } // fi
206
207   if( this->m_ImageWriterClassName == "" )
208   {
209     QMessageBox::critical(
210       this,
211       tr( "No ImageWriter found in plugins!" ),
212       tr( this->m_PluginsConfigurationFile.c_str( ) )
213       );
214
215   } // fi
216   this->_UpdateEnabledFlags( );
217   */
218        }
219
220 // -------------------------------------------------------------------------
221 bool MainWnd::
222 _CheckClassesInPlugins( const TStringMap& classes )
223 {
224   bool r = true;
225   TStringMap::const_iterator cIt = classes.begin( );
226   for( ; cIt != classes.end( ); ++cIt )
227   {
228     TPluginObject* o = this->m_Plugins.CreateObject( cIt->second );
229     if( o != NULL )
230       delete o;
231     else
232       r &= false;
233     
234   } // rof
235   return( r );
236 }
237
238 // eof - $RCSfile$