]> Creatis software - cpMesh.git/blob - appli/InteractiveDeformableMeshSegmentation/MainWnd_LoadPlugins.cxx
27fbfe2fab30bcaad8ded329f0107fb87856e97b
[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 bool MainWnd::
118 _CheckClassesInPlugins( const TStringMap& classes )
119 {
120   bool r = true;
121   TStringMap::const_iterator cIt = classes.begin( );
122   for( ; cIt != classes.end( ); ++cIt )
123   {
124     TPluginObject* o = this->m_Plugins.CreateObject( cIt->second );
125     if( o != NULL )
126       delete o;
127     else
128       r &= false;
129     
130   } // rof
131   return( r );
132 }
133
134 // -------------------------------------------------------------------------
135 void MainWnd::
136 _AddPluginActions(
137   const TStringMap& classes, QMenu* menu, const char* method
138   )
139 {
140   TStringMap::const_iterator clIt = classes.begin( );
141   for( ; clIt != classes.end( ); ++clIt )
142   {
143     QAction* action = menu->addAction( QString( clIt->first.c_str( ) ) );
144     QObject::connect( action, SIGNAL( triggered( ) ), this, method );
145
146   } // rof
147 }
148
149 // eof - $RCSfile$