Salome HOME
sources v1.2
[modules/kernel.git] / src / SALOMEGUI / QAD_XmlHandler.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : QAD_XmlHandler.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #define  INCLUDE_MENUITEM_DEF 
31
32 #include "QAD_Desktop.h"
33 #include "QAD_XmlHandler.h"
34 #include "QAD_ListMenuIdAction.h"
35 #include "QAD_Menus.h"
36 #include "QAD_MessageBox.h"
37 #include "QAD_Tools.h"
38 #include "QAD_Resource.h"
39
40 // QT Include
41 #include <qaccel.h>
42 #include <qstringlist.h>
43 #include <qfileinfo.h>
44
45 map<int,QString>              QAD_XmlHandler::_bibmap;
46
47 static const char* SEPARATOR    = ":";
48
49 static QString findFile( QString resDirs, QString fileName )
50 {
51   QStringList dirList = QStringList::split( SEPARATOR, resDirs, false ); // skip empty entries
52   for ( int i = 0; i < dirList.count(); i++ ) {
53     QString dir = dirList[ i ];
54     QFileInfo fi( QAD_Tools::addSlash( dir ) + fileName );
55     if ( fi.isFile() && fi.exists() )
56       return fi.filePath();
57   }
58   return QString();
59 }
60
61 /*!
62   Constructor
63 */
64 QAD_XmlHandler::QAD_XmlHandler()
65 {
66 }
67
68 /*!
69   Destructor
70 */
71 QAD_XmlHandler::~QAD_XmlHandler()
72 {
73 }
74
75 /*!
76   Sets \a desktop widget which contains menubar.
77 */
78 void QAD_XmlHandler::setMainWindow(QAD_Desktop* desktop)
79 {
80   myDesktop       = desktop;
81   myIdList        = TColStd_SequenceOfInteger();
82   myBackMenu      = true;
83   myBackPopupMenus.setAutoDelete(false);
84 }
85
86 /*!
87   Returns TRUE if environment variable CSF_"Component"Resources is undefined.
88   Returns FALSE otherwise.
89 */
90 bool QAD_XmlHandler::setComponent (const QString& dirs )
91 {
92   myPathResources = dirs;
93   
94   return true ;
95 }
96
97 /*!
98   Starts parsing of document. Does some initialization
99
100   Reimplemented from QXmlDefaultHandler.
101 */
102 bool QAD_XmlHandler::startDocument()
103 {
104   myErrorProt = "";
105   return TRUE;
106 }
107
108 /*!
109   Does different actions depending on the name of the tag and the
110   state you are in document.
111
112   Reimplemented from QXmlDefaultHandler.
113 */
114 bool QAD_XmlHandler::startElement( const QString&, const QString&, 
115                                    const QString& qName, 
116                                    const QXmlAttributes& atts )
117 {
118   char Slot_Name[200];
119
120   if ( qName == "application") {
121     myMenus=new QAD_Menus(myDesktop);
122     myMenusList.append(myMenus);
123   } else if ( qName == "desktop" ) { //DESKTOP
124   } else if ( qName == "menubar" ) { //MENUBAR
125     QMenuBar* aMenuBar = myDesktop->menuBar();  /*new QMenuBar(myMainWindow);*/
126     myMenus->myActiveMenuBar=aMenuBar;
127     myMenus->append(aMenuBar);
128     myMenus->myActiveMenuBar->hide();
129     
130   } else if ( qName == "menu-item" ) { //MENU-ITEM
131     myBackMenu = true;
132     QMenuItem* item = myMenus->myActiveMenuBar->findItem(atts.value( "item-id" ).toInt());
133     if ( item ) {
134       QPopupMenu* aMenuItem = item->popup();
135       myMenus->myActivePopupMenu=aMenuItem;
136     } else {
137       QPopupMenu* aMenuItem=new QPopupMenu(myMenus->myActiveMenuBar);
138       myMenus->myActivePopupMenu=aMenuItem;
139       int id;
140       int pos;
141       if (atts.value( "pos-id" ).compare(QString(""))==0) {
142         pos = -1;
143       }
144       else {
145         pos = atts.value( "pos-id" ).toInt();
146         if ( pos < 0 )
147           pos = myMenus->myActiveMenuBar->count() + pos;
148       }
149       id = myMenus->myActiveMenuBar->insertItem( atts.value( "label-id" ),
150                                                  aMenuItem,
151                                                  atts.value( "item-id" ).toInt(),
152                                                  pos );
153       myIdList.Append( id );
154     }
155   } else if ( qName == "popup-item" ) { //POPUP-ITEM
156     QString aCommandID(atts.value( "item-id" ));        
157     int id;
158     int pos;
159     if (atts.value( "pos-id" ).compare(QString(""))==0) {
160       pos = -1;
161     }
162     else {
163       pos = atts.value( "pos-id" ).toInt();
164       if ( pos < 0 )
165         pos = myMenus->myActivePopupMenu->count() + pos;
166     }
167     if (atts.value( "icon-id").compare(QString(""))==0) 
168       if (atts.value( "execute-action" ).compare(QString(""))==0)
169         id = myMenus->myActivePopupMenu->insertItem( atts.value( "label-id" ),
170                                                      myDesktop, SLOT (onDispatch( int )),
171                                                      QAccel::stringToKey(atts.value( "accel-id" )),
172                                                      aCommandID.toInt(),
173                                                      pos);
174       else
175         id = myMenus->myActivePopupMenu->insertItem( atts.value( "label-id" ),
176                                                      myDesktop, SLOT (onDispatchTools( int )),
177                                                      QAccel::stringToKey(atts.value( "accel-id" )),
178                                                      aCommandID.toInt(),
179                                                      pos);
180     
181     else 
182       if (atts.value( "execute-action" ).compare(QString(""))==0)
183         id = myMenus->myActivePopupMenu->insertItem( QPixmap( findFile( myPathResources, atts.value( "icon-id" ) ) ),
184                                                      atts.value( "label-id" ),
185                                                      myDesktop, SLOT (onDispatch( int )),
186                                                      QAccel::stringToKey(atts.value( "accel-id" )),
187                                                      aCommandID.toInt(),
188                                                      pos );
189       else
190         id = myMenus->myActivePopupMenu->insertItem( QPixmap( findFile( myPathResources, atts.value( "icon-id") ) ),
191                                                      atts.value( "label-id" ),
192                                                      myDesktop, SLOT (onDispatchTools( int )),
193                                                      QAccel::stringToKey(atts.value( "accel-id" )),
194                                                      aCommandID.toInt(),
195                                                      pos);
196     if (atts.value( "chaine")!=NULL) {
197       QAD_XmlHandler::_bibmap[atts.value( "item-id").toInt()]=QString(atts.value( "chaine"));
198 //        SCRUTE(atts.value( "item-id").toInt() ) ;
199 //        SCRUTE(QAD_XmlHandler::_bibmap[atts.value( "item-id").toInt()] ) ;
200     }
201     if (atts.value( "toggle-id" ).compare(QString(""))!=0) {
202       if (atts.value( "toggle-id" ).compare(QString("false"))==0) {
203         myMenus->myActivePopupMenu->setItemChecked(id,false);
204       }
205       if (atts.value( "toggle-id" ).compare(QString("true"))==0) {
206         myMenus->myActivePopupMenu->setItemChecked(id,true);
207       }
208     }
209     myIdList.Append( id );
210     myMenus->myActivePopupMenu->setItemParameter( id, aCommandID.toInt());
211   } else if ( qName == "submenu" ) {    //SUBMENU
212     myBackPopupMenus.push(myMenus->myActivePopupMenu);
213     QMenuItem* item = 0;
214     if ( myBackMenu )
215       item = myMenus->myActivePopupMenu->findItem(atts.value( "item-id" ).toInt());
216       
217     QPopupMenu* aSubmenu = new QPopupMenu();
218     if ( item ) {
219       if (item->popup()) {
220         aSubmenu=item->popup();
221         //Enable submenu only if disable
222         if (!myMenus->myActivePopupMenu->isItemEnabled( atts.value( "item-id" ).toInt()))
223           myMenus->myActivePopupMenu->setItemEnabled( atts.value( "item-id" ).toInt(), true);
224       }
225     } else {      
226       aSubmenu=new QPopupMenu(myMenus->myActivePopupMenu);
227       int id;
228       int pos;
229       if (atts.value( "pos-id" ).compare(QString(""))==0) {
230         pos = -1;
231       }
232       else {
233         pos = atts.value( "pos-id" ).toInt();
234         if ( pos < 0 )
235           pos = myMenus->myActivePopupMenu->count() + pos;
236       }
237       id = myMenus->myActivePopupMenu->insertItem(atts.value( "label-id" ),
238                                                   aSubmenu,atts.value( "item-id" ).toInt(),
239                                                   pos);
240       myIdList.Append(id);
241     }
242     myMenus->myActivePopupMenu=aSubmenu;
243   } else if ( qName == "endsubmenu" ) { //SUBMENU
244     myMenus->myActivePopupMenu = myBackPopupMenus.pop();
245   } else if ( qName == "popupmenu" ) {  //POPUP MENU
246     myBackMenu = false;
247     QPopupMenu* aPopupMenu=new QPopupMenu;
248     myMenus->myActivePopupMenu=aPopupMenu;
249     aPopupMenu->insertItem( atts.value( "label-id" ), aPopupMenu, QAD_TopLabel_Popup_ID);
250     QAD_ListPopupMenu* pmenu = new QAD_ListPopupMenu(myMenus->myActivePopupMenu,
251                                                      atts.value( "context-id" ),
252                                                      atts.value( "parent-id" ),
253                                                      atts.value( "object-id" ));
254     myMenus->append(pmenu);
255   } else if ( qName == "toolbar" ) {    //TOOLBAR
256     QToolBar* aToolBar=new QToolBar(atts.value( "label-id" ),myDesktop);
257     aToolBar->setCloseMode( QDockWindow::Undocked );
258     myMenus->append(aToolBar);
259     myMenus->myActiveToolBar=aToolBar;
260     myMenus->myActiveToolBar->hide();
261   } else if ( qName == "toolbutton-item" ) {    //TOOL BUTTON
262     QString aQStringToolButtonItemId(atts.value( "item-id" ));
263     QActionP* anAction=
264       new QActionP
265       (QObject::tr(atts.value( "label-id" )),
266        QPixmap( findFile( myPathResources, atts.value( "icon-id") ) ),
267        QObject::tr(atts.value( "label-id" )),
268        QAccel::stringToKey(QObject::tr(atts.value( "accel-id" ))),
269        myDesktop);
270     
271     anAction->setStatusTip(QObject::tr(atts.value( "tooltip-id" )));            
272     anAction->setToolTip(QObject::tr(atts.value( "tooltip-id" )));        
273     anAction->addTo(myMenus->myActiveToolBar);
274     QAD_ListMenuIdAction* menu = new QAD_ListMenuIdAction(myMenus->myActivePopupMenu,
275                                                           aQStringToolButtonItemId.toInt(),
276                                                           anAction);
277     myMenus->append(menu);
278     QObject::connect(anAction,SIGNAL(activated()), myDesktop,SLOT(onDispatch( )));
279   } else if (qName == "separatorTB") {
280     myMenus->myActiveToolBar->addSeparator();
281   } else if (qName == "separator") {
282     int id;
283     int pos;
284     if (atts.value( "pos-id" ).compare(QString(""))==0) {
285       pos = -1;
286     }
287     else {
288       pos = atts.value( "pos-id" ).toInt();
289       if ( pos < 0 )
290         pos = myMenus->myActivePopupMenu->count() + pos;
291     }
292     id = myMenus->myActivePopupMenu->insertSeparator( pos );
293     myIdList.Append(id);
294   }else {
295     // error
296     return FALSE;
297   }
298   return TRUE;
299 }
300
301
302 /*!
303   Reimplemented from QXmlDefaultHandler.
304 */
305 bool QAD_XmlHandler::endElement( const QString&, const QString&, const QString& )
306 {
307   return TRUE;
308 }
309
310
311 /*!
312   Reimplemented from QXmlDefaultHandler.
313 */
314 bool QAD_XmlHandler::characters( const QString& ch )
315 {
316   // we are not interested in whitespaces
317   QString ch_simplified = ch.simplifyWhiteSpace();
318   if ( ch_simplified.isEmpty() )
319     return TRUE;
320   return TRUE;
321 }
322
323
324 /*!
325   Returns the default error string.
326
327   Reimplemented from QXmlDefaultHandler.
328 */
329 QString QAD_XmlHandler::errorString()
330 {
331   return "the document is not in the quote file format";
332 }
333
334 /*!
335   Returns exception
336
337   Reimplemented from QXmlDefaultHandler.
338 */
339 bool QAD_XmlHandler::fatalError( const QXmlParseException& exception )
340 {
341   myErrorProt += QString( "fatal parsing error: %1 in line %2, column %3\n" )
342     .arg( exception.message() )
343     .arg( exception.lineNumber() )
344     .arg( exception.columnNumber() );
345   
346   return QXmlDefaultHandler::fatalError( exception );
347 }
348
349 /*!
350   Returns the error protocol if parsing failed
351
352   Reimplemented from QXmlDefaultHandler.
353 */
354 QString QAD_XmlHandler::errorProtocol()
355 {
356   return myErrorProt;
357 }
358
359 QString QAD_XmlHandler::givebib(const int mykey) 
360 {
361   return QAD_XmlHandler::_bibmap[mykey];
362 }