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