2 // File : QAD_XmlHandler.cxx
3 // Created : Thu Jun 14 13:59:36 2001
4 // Author : Nicolas REJNERI
7 // Copyright : Open CASCADE
10 #define INCLUDE_MENUITEM_DEF
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"
22 #include <qstringlist.h>
23 #include <qfileinfo.h>
25 map<int,QString> QAD_XmlHandler::_bibmap;
27 static const char* SEPARATOR = ":";
29 static QString findFile( QString resDirs, QString fileName )
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() )
44 QAD_XmlHandler::QAD_XmlHandler()
51 QAD_XmlHandler::~QAD_XmlHandler()
56 Sets \a desktop widget which contains menubar.
58 void QAD_XmlHandler::setMainWindow(QAD_Desktop* desktop)
61 myIdList = TColStd_SequenceOfInteger();
63 myBackPopupMenus.setAutoDelete(false);
67 Returns TRUE if environment variable CSF_"Component"Resources is undefined.
68 Returns FALSE otherwise.
70 bool QAD_XmlHandler::setComponent (const QString& dirs )
72 myPathResources = dirs;
78 Starts parsing of document. Does some initialization
80 Reimplemented from QXmlDefaultHandler.
82 bool QAD_XmlHandler::startDocument()
89 Does different actions depending on the name of the tag and the
90 state you are in document.
92 Reimplemented from QXmlDefaultHandler.
94 bool QAD_XmlHandler::startElement( const QString&, const QString&,
96 const QXmlAttributes& atts )
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();
110 } else if ( qName == "menu-item" ) { //MENU-ITEM
112 QMenuItem* item = myMenus->myActiveMenuBar->findItem(atts.value( "item-id" ).toInt());
114 QPopupMenu* aMenuItem = item->popup();
115 myMenus->myActivePopupMenu=aMenuItem;
117 QPopupMenu* aMenuItem=new QPopupMenu(myMenus->myActiveMenuBar);
118 myMenus->myActivePopupMenu=aMenuItem;
121 if (atts.value( "pos-id" ).compare(QString(""))==0) {
125 pos = atts.value( "pos-id" ).toInt();
127 pos = myMenus->myActiveMenuBar->count() + pos;
129 id = myMenus->myActiveMenuBar->insertItem( atts.value( "label-id" ),
131 atts.value( "item-id" ).toInt(),
133 myIdList.Append( id );
135 } else if ( qName == "popup-item" ) { //POPUP-ITEM
136 QString aCommandID(atts.value( "item-id" ));
139 if (atts.value( "pos-id" ).compare(QString(""))==0) {
143 pos = atts.value( "pos-id" ).toInt();
145 pos = myMenus->myActivePopupMenu->count() + pos;
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" )),
155 id = myMenus->myActivePopupMenu->insertItem( atts.value( "label-id" ),
156 myDesktop, SLOT (onDispatchTools( int )),
157 QAccel::stringToKey(atts.value( "accel-id" )),
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" )),
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" )),
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()] ) ;
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);
185 if (atts.value( "toggle-id" ).compare(QString("true"))==0) {
186 myMenus->myActivePopupMenu->setItemChecked(id,true);
189 myIdList.Append( id );
190 myMenus->myActivePopupMenu->setItemParameter( id, aCommandID.toInt());
191 } else if ( qName == "submenu" ) { //SUBMENU
192 myBackPopupMenus.push(myMenus->myActivePopupMenu);
195 item = myMenus->myActivePopupMenu->findItem(atts.value( "item-id" ).toInt());
197 QPopupMenu* aSubmenu = new QPopupMenu();
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);
206 aSubmenu=new QPopupMenu(myMenus->myActivePopupMenu);
209 if (atts.value( "pos-id" ).compare(QString(""))==0) {
213 pos = atts.value( "pos-id" ).toInt();
215 pos = myMenus->myActivePopupMenu->count() + pos;
217 id = myMenus->myActivePopupMenu->insertItem(atts.value( "label-id" ),
218 aSubmenu,atts.value( "item-id" ).toInt(),
222 myMenus->myActivePopupMenu=aSubmenu;
223 } else if ( qName == "endsubmenu" ) { //SUBMENU
224 myMenus->myActivePopupMenu = myBackPopupMenus.pop();
225 } else if ( qName == "popupmenu" ) { //POPUP MENU
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" ));
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" ))),
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(),
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") {
264 if (atts.value( "pos-id" ).compare(QString(""))==0) {
268 pos = atts.value( "pos-id" ).toInt();
270 pos = myMenus->myActivePopupMenu->count() + pos;
272 id = myMenus->myActivePopupMenu->insertSeparator( pos );
283 Reimplemented from QXmlDefaultHandler.
285 bool QAD_XmlHandler::endElement( const QString&, const QString&, const QString& )
292 Reimplemented from QXmlDefaultHandler.
294 bool QAD_XmlHandler::characters( const QString& ch )
296 // we are not interested in whitespaces
297 QString ch_simplified = ch.simplifyWhiteSpace();
298 if ( ch_simplified.isEmpty() )
305 Returns the default error string.
307 Reimplemented from QXmlDefaultHandler.
309 QString QAD_XmlHandler::errorString()
311 return "the document is not in the quote file format";
317 Reimplemented from QXmlDefaultHandler.
319 bool QAD_XmlHandler::fatalError( const QXmlParseException& exception )
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() );
326 return QXmlDefaultHandler::fatalError( exception );
330 Returns the error protocol if parsing failed
332 Reimplemented from QXmlDefaultHandler.
334 QString QAD_XmlHandler::errorProtocol()
339 QString QAD_XmlHandler::givebib(const int mykey)
341 return QAD_XmlHandler::_bibmap[mykey];