1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : QAD_XmlHandler.cxx
25 // Author : Nicolas REJNERI
30 #define INCLUDE_MENUITEM_DEF
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"
42 #include <qstringlist.h>
43 #include <qfileinfo.h>
45 map<int,QString> QAD_XmlHandler::_bibmap;
47 static const char* SEPARATOR = ":";
49 static QString findFile( QString resDirs, QString fileName )
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() )
64 QAD_XmlHandler::QAD_XmlHandler()
71 QAD_XmlHandler::~QAD_XmlHandler()
76 Sets \a desktop widget which contains menubar.
78 void QAD_XmlHandler::setMainWindow(QAD_Desktop* desktop)
81 myIdList = TColStd_SequenceOfInteger();
83 myBackPopupMenus.setAutoDelete(false);
87 Returns TRUE if environment variable CSF_"Component"Resources is undefined.
88 Returns FALSE otherwise.
90 bool QAD_XmlHandler::setComponent (const QString& dirs )
92 myPathResources = dirs;
98 Starts parsing of document. Does some initialization
100 Reimplemented from QXmlDefaultHandler.
102 bool QAD_XmlHandler::startDocument()
109 Does different actions depending on the name of the tag and the
110 state you are in document.
112 Reimplemented from QXmlDefaultHandler.
114 bool QAD_XmlHandler::startElement( const QString&, const QString&,
115 const QString& qName,
116 const QXmlAttributes& atts )
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();
130 } else if ( qName == "menu-item" ) { //MENU-ITEM
132 QMenuItem* item = myMenus->myActiveMenuBar->findItem(atts.value( "item-id" ).toInt());
134 QPopupMenu* aMenuItem = item->popup();
135 myMenus->myActivePopupMenu=aMenuItem;
137 QPopupMenu* aMenuItem=new QPopupMenu(myMenus->myActiveMenuBar);
138 myMenus->myActivePopupMenu=aMenuItem;
141 if (atts.value( "pos-id" ).compare(QString(""))==0) {
145 pos = atts.value( "pos-id" ).toInt();
147 pos = myMenus->myActiveMenuBar->count() + pos;
149 id = myMenus->myActiveMenuBar->insertItem( atts.value( "label-id" ),
151 atts.value( "item-id" ).toInt(),
153 myIdList.Append( id );
155 } else if ( qName == "popup-item" ) { //POPUP-ITEM
156 QString aCommandID(atts.value( "item-id" ));
159 if (atts.value( "pos-id" ).compare(QString(""))==0) {
163 pos = atts.value( "pos-id" ).toInt();
165 pos = myMenus->myActivePopupMenu->count() + pos;
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" )),
175 id = myMenus->myActivePopupMenu->insertItem( atts.value( "label-id" ),
176 myDesktop, SLOT (onDispatchTools( int )),
177 QAccel::stringToKey(atts.value( "accel-id" )),
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" )),
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" )),
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()] ) ;
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);
205 if (atts.value( "toggle-id" ).compare(QString("true"))==0) {
206 myMenus->myActivePopupMenu->setItemChecked(id,true);
209 myIdList.Append( id );
210 myMenus->myActivePopupMenu->setItemParameter( id, aCommandID.toInt());
211 } else if ( qName == "submenu" ) { //SUBMENU
212 myBackPopupMenus.push(myMenus->myActivePopupMenu);
215 item = myMenus->myActivePopupMenu->findItem(atts.value( "item-id" ).toInt());
217 QPopupMenu* aSubmenu = new QPopupMenu();
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);
226 aSubmenu=new QPopupMenu(myMenus->myActivePopupMenu);
229 if (atts.value( "pos-id" ).compare(QString(""))==0) {
233 pos = atts.value( "pos-id" ).toInt();
235 pos = myMenus->myActivePopupMenu->count() + pos;
237 id = myMenus->myActivePopupMenu->insertItem(atts.value( "label-id" ),
238 aSubmenu,atts.value( "item-id" ).toInt(),
242 myMenus->myActivePopupMenu=aSubmenu;
243 } else if ( qName == "endsubmenu" ) { //SUBMENU
244 myMenus->myActivePopupMenu = myBackPopupMenus.pop();
245 } else if ( qName == "popupmenu" ) { //POPUP MENU
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" ));
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" ))),
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(),
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") {
284 if (atts.value( "pos-id" ).compare(QString(""))==0) {
288 pos = atts.value( "pos-id" ).toInt();
290 pos = myMenus->myActivePopupMenu->count() + pos;
292 id = myMenus->myActivePopupMenu->insertSeparator( pos );
303 Reimplemented from QXmlDefaultHandler.
305 bool QAD_XmlHandler::endElement( const QString&, const QString&, const QString& )
312 Reimplemented from QXmlDefaultHandler.
314 bool QAD_XmlHandler::characters( const QString& ch )
316 // we are not interested in whitespaces
317 QString ch_simplified = ch.simplifyWhiteSpace();
318 if ( ch_simplified.isEmpty() )
325 Returns the default error string.
327 Reimplemented from QXmlDefaultHandler.
329 QString QAD_XmlHandler::errorString()
331 return "the document is not in the quote file format";
337 Reimplemented from QXmlDefaultHandler.
339 bool QAD_XmlHandler::fatalError( const QXmlParseException& exception )
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() );
346 return QXmlDefaultHandler::fatalError( exception );
350 Returns the error protocol if parsing failed
352 Reimplemented from QXmlDefaultHandler.
354 QString QAD_XmlHandler::errorProtocol()
359 QString QAD_XmlHandler::givebib(const int mykey)
361 return QAD_XmlHandler::_bibmap[mykey];