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