Salome HOME
WIN32 compilation of the SMESH module:
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_XmlHandler.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // SMESH SMESHGUI : reading of xml file with list of available hypotheses and algorithms
24 // File   : SMESHGUI_XmlHandler.cxx
25 // Author : Julia DOROVSKIKH, Open CASCADE S.A.S.
26 // SMESH includes
27 //
28 #include "SMESHGUI_XmlHandler.h"
29
30 #include "SMESHGUI.h"
31 #include "SMESHGUI_Hypotheses.h"
32
33 // SALOME GUI includes
34 #include "SUIT_ResourceMgr.h"
35
36 // SALOME KERNEL includes
37 #include <utilities.h>
38
39 #define BAD_HYP_FLAG "NOT_FOUND"
40
41 /*!
42   Constructor
43 */
44 SMESHGUI_XmlHandler::SMESHGUI_XmlHandler()
45 {
46 }
47
48 /*!
49   Destructor
50 */
51 SMESHGUI_XmlHandler::~SMESHGUI_XmlHandler()
52 {
53 }
54
55 /*!
56   Starts parsing of document. Does some initialization
57
58   Reimplemented from QXmlDefaultHandler.
59 */
60 bool SMESHGUI_XmlHandler::startDocument()
61 {
62   myErrorProt = "";
63   return true;
64 }
65
66 /*!
67   Does different actions depending on the name of the tag and the
68   state you are in document.
69
70   Reimplemented from QXmlDefaultHandler.
71 */
72 bool SMESHGUI_XmlHandler::startElement (const QString&, const QString&, 
73                                         const QString& qName, 
74                                         const QXmlAttributes& atts)
75 {
76   if (qName == "meshers")
77   {
78     myHypothesesMap.clear();
79     myAlgorithmsMap.clear();
80   }
81   else if (qName == "meshers-group") // group of hypotheses and algorithms
82   {
83 //    if (atts.value("server-lib") != "")
84     {
85       myPluginName = atts.value("name");
86       myServerLib  = atts.value("server-lib");
87       myClientLib  = atts.value("gui-lib");
88 /* It's Need to tranlate lib name for WIN32 or X platform
89  * (only client lib, because server lib translates in SMESH_Gen_i::createHypothesis
90  *  for normal work of *.py files )
91  */
92       if( !myClientLib.isEmpty() )
93       {
94 #ifdef WIN32
95       //myServerLib += ".dll";
96         myClientLib += ".dll";
97 #else
98       //myServerLib = "lib" + myServerLib + ".so";
99         myClientLib = "lib" + myClientLib + ".so";
100 #endif
101       }
102
103
104       QString aResName = atts.value("resources");
105       if (aResName != "")
106       {
107         MESSAGE("Loading Resources " << aResName.toLatin1().data());
108         SUIT_ResourceMgr* resMgr = SMESHGUI::resourceMgr();
109         QString lang = resMgr->stringValue( resMgr->langSection(), "language", "en" );
110         resMgr->loadTranslator( "resources", QString( "%1_msg_%2.qm" ).arg( aResName, lang ) );
111         resMgr->loadTranslator( "resources", QString( "%1_images.qm" ).arg( aResName, lang ) );
112       }
113     }
114   }
115   else if (qName == "hypotheses") // group of hypotheses
116   {
117   }
118   else if (qName == "algorithms") // group of algorithms
119   {
120   }
121   else if (qName == "hypothesis" || qName == "algorithm") // hypothesis or algorithm
122   {
123     if (atts.value("type") != "")
124     {
125       QString aHypAlType = atts.value("type");
126       QString aLabel = atts.value("label-id");
127       QString anIcon = atts.value("icon-id");
128       bool isAux = atts.value("auxiliary") == "true";
129       bool isNeedGeom = true, isSupportSubmeshes = false;
130       QString aNeedGeom = atts.value("need-geom");
131       if ( !aNeedGeom.isEmpty() )
132         isNeedGeom = (aNeedGeom == "true");
133       QString suppSub = atts.value("support-submeshes");
134       if ( !suppSub.isEmpty() )
135         isSupportSubmeshes = (suppSub == "true");
136
137       QString aDimStr = atts.value("dim");
138       aDimStr = aDimStr.remove( ' ' );
139       QStringList aDimList = aDimStr.split( ',', QString::SkipEmptyParts );
140       QStringList::iterator anIter;
141       bool isOk;
142       QList<int> aDim;
143       for ( anIter = aDimList.begin(); anIter != aDimList.end(); ++anIter )
144       {
145         int aVal = (*anIter).toInt( &isOk );
146         if ( isOk )
147           aDim.append( aVal );
148       }
149
150       // for algo
151       enum { HYPOS = 0, OPT_HYPOS, INPUT, OUTPUT, NB_ATTRIBUTES };
152       const char* name [NB_ATTRIBUTES] = { "hypos", "opt-hypos", "input", "output" };
153       QStringList attr [NB_ATTRIBUTES];
154       for ( int i = 0; i < NB_ATTRIBUTES; ++i ) {
155         QString aStr = atts.value( name[i] );
156         if ( !aStr.isEmpty() ) {
157           aStr.remove( ' ' );
158           attr[ i ] = aStr.split( ',', QString::SkipEmptyParts );
159         }
160       }
161       
162       if ( !aHypAlType.contains( BAD_HYP_FLAG ) ) {
163         HypothesisData* aHypData =
164           new HypothesisData (aHypAlType, myPluginName, myServerLib, myClientLib,
165                               aLabel, anIcon, aDim, isAux,
166                               attr[ HYPOS ], attr[ OPT_HYPOS ], attr[ INPUT ], attr[ OUTPUT ],
167                               isNeedGeom, isSupportSubmeshes );
168
169         if (qName == "algorithm")
170         {
171           myAlgorithmsMap.insert(aHypAlType,aHypData);
172         }
173         else
174         {
175           myHypothesesMap.insert(aHypAlType,aHypData);
176         }
177       }
178     }
179   }
180   else if (qName == "hypotheses-set-group") // group of sets of hypotheses
181   {
182   }
183   else if (qName == "hypotheses-set") // a set of hypotheses
184   {
185     if (atts.value("name") != "")
186     {
187       QString hypos = atts.value("hypos").remove( ' ' );
188       QString algos = atts.value("algos").remove( ' ' );
189       bool badSet = hypos.contains( BAD_HYP_FLAG ) || algos.contains( BAD_HYP_FLAG );
190
191       if ( !badSet )
192         myListOfHypothesesSets.append( new HypothesesSet ( atts.value("name"), 
193                                                            hypos.split( ',', QString::SkipEmptyParts ),
194                                                            algos.split( ',', QString::SkipEmptyParts ) ) );
195     }
196   }
197   else if ( qName == "python-wrap" ||
198             qName == "algo"        ||
199             qName == "hypo"        ||
200             qName == "accumulative-methods")
201   {
202     // elements used in SMESH_2smeshpy
203     return true;
204   }
205   else
206   {
207     // error
208     return false;
209   }
210   return true;
211 }
212
213
214 /*!
215   Reimplemented from QXmlDefaultHandler.
216 */
217 bool SMESHGUI_XmlHandler::endElement (const QString&, const QString&, const QString&)
218 {
219   return true;
220 }
221
222
223 /*!
224   Reimplemented from QXmlDefaultHandler.
225 */
226 bool SMESHGUI_XmlHandler::characters (const QString& ch)
227 {
228   // we are not interested in whitespaces
229   QString ch_simplified = ch.simplified();
230   if ( ch_simplified.isEmpty() )
231     return true;
232   return true;
233 }
234
235
236 /*!
237   Returns the default error string.
238
239   Reimplemented from QXmlDefaultHandler.
240 */
241 QString SMESHGUI_XmlHandler::errorString()
242 {
243   return "the document is not in the quote file format";
244 }
245
246 /*!
247   Returns the error protocol if parsing failed
248
249   Reimplemented from QXmlDefaultHandler.
250 */
251 QString SMESHGUI_XmlHandler::errorProtocol()
252 {
253   return myErrorProt;
254 }
255
256 /*!
257   Returns exception
258
259   Reimplemented from QXmlDefaultHandler.
260 */
261 bool SMESHGUI_XmlHandler::fatalError (const QXmlParseException& exception)
262 {
263   myErrorProt += QString("fatal parsing error: %1 in line %2, column %3\n")
264     .arg(exception.message())
265     .arg(exception.lineNumber())
266     .arg(exception.columnNumber());
267   
268   return QXmlDefaultHandler::fatalError( exception );
269 }