| # kdeui Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# kxmlguibuilder.cpp

    00001 /* This file is part of the KDE project
    00002    Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>
    00003                       David Faure <faure@kde.org>
    00004
    00005    This library is free software; you can redistribute it and/or
    00006    modify it under the terms of the GNU Library General Public
    00007    License as published by the Free Software Foundation; either
    00008    version 2 of the License, or (at your option) any later version.
    00009
    00010    This library is distributed in the hope that it will be useful,
    00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013    Library General Public License for more details.
    00014
    00015    You should have received a copy of the GNU Library General Public License
    00016    along with this library; see the file COPYING.LIB.  If not, write to
    00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018    Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include "kapplication.h"
    00022 #include "kxmlguibuilder.h"
    00023 #include "kmenubar.h"
    00024 #include "kpopupmenu.h"
    00025 #include "ktoolbar.h"
    00026 #include "kstatusbar.h"
    00027 #include "kmainwindow.h"
    00028 #include "kaction.h"
    00029 #include "kglobalsettings.h"
    00030 #include <klocale.h>
    00031 #include <kiconloader.h>
    00032 #include <kdebug.h>
    00033 #include <qobjectlist.h>
    00034
    00035 class KXMLGUIBuilderPrivate
    00036 {
    00037 public:
    00038     KXMLGUIBuilderPrivate() {
    00039     }
    00040   ~KXMLGUIBuilderPrivate() {
    00041   }
    00042
    00043     QWidget *m_widget;
    00044
    00045     QString tagMainWindow;
    00046     QString tagMenuBar;
    00047     QString tagMenu;
    00048     QString tagToolBar;
    00049     QString tagStatusBar;
    00050
    00051     QString tagSeparator;
    00052     QString tagTearOffHandle;
    00053     QString tagMenuTitle;
    00054
    00055     QString attrName;
    00056     QString attrLineSeparator;
    00057
    00058     QString attrText1;
    00059     QString attrText2;
    00060     QString attrContext;
    00061
    00062     QString attrIcon;
    00063
    00064     KInstance *m_instance;
    00065     KXMLGUIClient *m_client;
    00066 };
    00067
    00068 KXMLGUIBuilder::KXMLGUIBuilder( QWidget *widget )
    00069 {
    00070   d = new KXMLGUIBuilderPrivate;
    00071   d->m_widget = widget;
    00072
    00073   d->tagMainWindow = QString::fromLatin1( "mainwindow" );
    00074   d->tagMenuBar = QString::fromLatin1( "menubar" );
    00075   d->tagMenu = QString::fromLatin1( "menu" );
    00076   d->tagToolBar = QString::fromLatin1( "toolbar" );
    00077   d->tagStatusBar = QString::fromLatin1( "statusbar" );
    00078
    00079   d->tagSeparator = QString::fromLatin1( "separator" );
    00080   d->tagTearOffHandle = QString::fromLatin1( "tearoffhandle" );
    00081   d->tagMenuTitle = QString::fromLatin1( "title" );
    00082
    00083   d->attrName = QString::fromLatin1( "name" );
    00084   d->attrLineSeparator = QString::fromLatin1( "lineseparator" );
    00085
    00086   d->attrText1 = QString::fromLatin1( "text" );
    00087   d->attrText2 = QString::fromLatin1( "Text" );
    00088   d->attrContext = QString::fromLatin1( "context" );
    00089
    00090   d->attrIcon = QString::fromLatin1( "icon" );
    00091
    00092   d->m_instance = 0;
    00093   d->m_client = 0;
    00094 }
    00095
    00096 KXMLGUIBuilder::~KXMLGUIBuilder()
    00097 {
    00098   delete d;
    00099 }
    00100
    00101 QWidget *KXMLGUIBuilder::widget()
    00102 {
    00103   return d->m_widget;
    00104 }
    00105
    00106 QStringList KXMLGUIBuilder::containerTags() const
    00107 {
    00108   QStringList res;
    00109   res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar;
    00110
    00111   return res;
    00112 }
    00113
    00114 QWidget *KXMLGUIBuilder::createContainer( QWidget *parent, int index, const QDomElement &element, int &id )
    00115 {
    00116   id = -1;
    00117   if ( element.tagName().lower() == d->tagMainWindow )
    00118   {
    00119     KMainWindow *mainwindow = 0;
    00120     if ( ::qt_cast<KMainWindow *>( d->m_widget ) )
    00121       mainwindow = static_cast<KMainWindow *>(d->m_widget);
    00122
    00123     return mainwindow;
    00124   }
    00125
    00126   if ( element.tagName().lower() == d->tagMenuBar )
    00127   {
    00128     KMenuBar *bar;
    00129
    00130     if ( ::qt_cast<KMainWindow *>( d->m_widget ) )
    00131       bar = static_cast<KMainWindow *>(d->m_widget)->menuBar();
    00132     else
    00133       bar = new KMenuBar( d->m_widget );
    00134
    00135     bar->show();
    00136     return bar;
    00137   }
    00138
    00139   if ( element.tagName().lower() == d->tagMenu )
    00140   {
    00141     // Look up to see if we are inside a mainwindow. If yes, then
    00142     // use it as parent widget (to get kaction to plug itself into the
    00143     // mainwindow). Don't use a popupmenu as parent widget, otherwise
    00144     // the popup won't be hidden if it is used as a standalone menu as well.
    00145     // And we don't want to set the parent for a standalone popupmenu,
    00146     // otherwise its shortcuts appear.
    00147     QWidget* p = parent;
    00148     while ( p && !::qt_cast<KMainWindow *>( p ) )
    00149         p = p->parentWidget();
    00150
    00151     QCString name = element.attribute( d->attrName ).utf8();
    00152
    00153     if (!kapp->authorizeKAction(name))
    00154        return 0;
    00155
    00156     KPopupMenu *popup = new KPopupMenu( p, name);
    00157
    00158     QString i18nText;
    00159     QDomElement textElem = element.namedItem( d->attrText1 ).toElement();
    00160     if ( textElem.isNull() ) // try with capital T
    00161       textElem = element.namedItem( d->attrText2 ).toElement();
    00162     QCString text = textElem.text().utf8();
    00163     QCString context = textElem.attribute(d->attrContext).utf8();
    00164
    00165     if ( text.isEmpty() ) // still no luck
    00166       i18nText = i18n( "No text!" );
    00167     else if ( context.isEmpty() )
    00168       i18nText = i18n( text );
    00169     else
    00170       i18nText = i18n( context, text );
    00171
    00172     QString icon = element.attribute( d->attrIcon );
    00173     QIconSet pix;
    00174
    00175     if ( !icon.isEmpty() )
    00176     {
    00177       KInstance *instance = d->m_instance;
    00178       if ( !instance )
    00179         instance = KGlobal::instance();
    00180
    00181       pix = SmallIconSet( icon, 16, instance );
    00182     }
    00183
    00184     if ( parent && ::qt_cast<KMenuBar *>( parent ) )
    00185     {
    00186       if ( !icon.isEmpty() )
    00187         id = static_cast<KMenuBar *>(parent)->insertItem( pix, i18nText, popup, -1, index );
    00188       else
    00189         id = static_cast<KMenuBar *>(parent)->insertItem( i18nText, popup, -1, index );
    00190     }
    00191     else if ( parent && ::qt_cast<QPopupMenu *>( parent ) )
    00192     {
    00193       if ( !icon.isEmpty() )
    00194         id = static_cast<QPopupMenu *>(parent)->insertItem( pix, i18nText, popup, -1, index );
    00195       else
    00196         id = static_cast<QPopupMenu *>(parent)->insertItem( i18nText, popup, -1, index );
    00197     }
    00198
    00199     return popup;
    00200   }
    00201
    00202   if ( element.tagName().lower() == d->tagToolBar )
    00203   {
    00204     bool honor = (element.attribute( d->attrName ) == "mainToolBar");
    00205
    00206     QCString name = element.attribute( d->attrName ).utf8();
    00207
    00208     KToolBar *bar = static_cast<KToolBar*>(d->m_widget->child( name, "KToolBar" ));
    00209     if( !bar )
    00210     {
    00211        bar = new KToolBar( d->m_widget, name, honor, false );
    00212     }
    00213
    00214     if ( ::qt_cast<KMainWindow *>( d->m_widget ) )
    00215     {
    00216         if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
    00217             bar->setXMLGUIClient( d->m_client );
    00218     }
    00219
    00220     bar->loadState( element );
    00221
    00222     return bar;
    00223   }
    00224
    00225   if ( element.tagName().lower() == d->tagStatusBar )
    00226   {
    00227       if ( ::qt_cast<KMainWindow *>( d->m_widget ) )
    00228     {
    00229       KMainWindow *mainWin = static_cast<KMainWindow *>(d->m_widget);
    00230       mainWin->statusBar()->show();
    00231       return mainWin->statusBar();
    00232     }
    00233     KStatusBar *bar = new KStatusBar( d->m_widget );
    00234     return bar;
    00235   }
    00236
    00237   return 0L;
    00238 }
    00239
    00240 void KXMLGUIBuilder::removeContainer( QWidget *container, QWidget *parent, QDomElement &element, int id )
    00241 {
    00242   // Warning parent can be 0L
    00243
    00244   if ( ::qt_cast<QPopupMenu *>( container ) )
    00245   {
    00246     if ( parent )
    00247     {
    00248         if ( ::qt_cast<KMenuBar *>( parent ) )
    00249             static_cast<KMenuBar *>(parent)->removeItem( id );
    00250         else if ( ::qt_cast<QPopupMenu *>( parent ) )
    00251             static_cast<QPopupMenu *>(parent)->removeItem( id );
    00252     }
    00253
    00254     delete container;
    00255   }
    00256   else if ( ::qt_cast<KToolBar *>( container ) )
    00257   {
    00258     KToolBar *tb = static_cast<KToolBar *>( container );
    00259
    00260     tb->saveState( element );
    00261     delete tb;
    00262   }
    00263   else if ( ::qt_cast<KMenuBar *>( container ) )
    00264   {
    00265     KMenuBar *mb = static_cast<KMenuBar *>( container );
    00266     mb->hide();
    00267     // Don't delete menubar - it can be reused by createContainer.
    00268     // If you decide that you do need to delete the menubar, make
    00269     // sure that QMainWindow::d->mb does not point to a deleted
    00270     // menubar object.
    00271   }
    00272   else if ( ::qt_cast<KStatusBar *>( container ) )
    00273   {
    00274     if ( ::qt_cast<KMainWindow *>( d->m_widget ) )
    00275         container->hide();
    00276     else
    00277       delete static_cast<KStatusBar *>(container);
    00278   }
    00279   else
    00280      kdWarning() << "Unhandled container to remove : " << container->className() << endl;
    00281 }
    00282
    00283 QStringList KXMLGUIBuilder::customTags() const
    00284 {
    00285   QStringList res;
    00286   res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
    00287   return res;
    00288 }
    00289
    00290 int KXMLGUIBuilder::createCustomElement( QWidget *parent, int index, const QDomElement &element )
    00291 {
    00292   if ( element.tagName().lower() == d->tagSeparator )
    00293   {
    00294     if ( ::qt_cast<QPopupMenu *>( parent ) )
    00295     {
    00296       // Don't insert multiple separators in a row
    00297       QPopupMenu *menu = static_cast<QPopupMenu *>(parent);
    00298       int count = menu->count();
    00299       if (count)
    00300       {
    00301          int previousId = -1;
    00302          if ((index == -1) || (index > count))
    00303             previousId = menu->idAt(count-1);
    00304          else if (index > 0)
    00305             previousId = menu->idAt(index-1);
    00306          if (previousId != -1)
    00307          {
    00308             if (menu->text(previousId).isEmpty() &&
    00309                 !menu->iconSet(previousId) &&
    00310                 !menu->pixmap(previousId))
    00311                return 0;
    00312          }
    00313       }
    00314       // Don't insert a separator at the top of the menu
    00315       if(count == 0)
    00316         return 0;
    00317       else
    00318         return menu->insertSeparator( index );
    00319     }
    00320     else if ( ::qt_cast<QMenuBar *>( parent ) )
    00321        return static_cast<QMenuBar *>(parent)->insertSeparator( index );
    00322     else if ( ::qt_cast<KToolBar *>( parent ) )
    00323     {
    00324       KToolBar *bar = static_cast<KToolBar *>( parent );
    00325
    00326       bool isLineSep = true;
    00327
    00328       QDomNamedNodeMap attributes = element.attributes();
    00329       unsigned int i = 0;
    00330       for (; i < attributes.length(); i++ )
    00331       {
    00332         QDomAttr attr = attributes.item( i ).toAttr();
    00333
    00334         if ( attr.name().lower() == d->attrLineSeparator &&
    00335              attr.value().lower() == QString::fromLatin1("false") )
    00336         {
    00337           isLineSep = false;
    00338           break;
    00339         }
    00340       }
    00341
    00342       int id = KAction::getToolButtonID();
    00343
    00344       if ( isLineSep )
    00345           bar->insertLineSeparator( index, id );
    00346       else
    00347           bar->insertSeparator( index, id );
    00348
    00349       return id;
    00350     }
    00351   }
    00352   else if ( element.tagName().lower() == d->tagTearOffHandle )
    00353   {
    00354     if ( ::qt_cast<QPopupMenu *>( parent )  && KGlobalSettings::insertTearOffHandle())
    00355       return static_cast<QPopupMenu *>(parent)->insertTearOffHandle( -1, index );
    00356   }
    00357   else if ( element.tagName().lower() == d->tagMenuTitle )
    00358   {
    00359     if ( ::qt_cast<KPopupMenu *>( parent ) )
    00360     {
    00361       QString i18nText;
    00362       QCString text = element.text().utf8();
    00363
    00364       if ( text.isEmpty() )
    00365         i18nText = i18n( "No text!" );
    00366       else
    00367         i18nText = i18n( text );
    00368
    00369       QString icon = element.attribute( d->attrIcon );
    00370       QPixmap pix;
    00371
    00372       if ( !icon.isEmpty() )
    00373       {
    00374         KInstance *instance = d->m_instance;
    00375         if ( !instance )
    00376           instance = KGlobal::instance();
    00377
    00378         pix = SmallIcon( icon, instance );
    00379       }
    00380
    00381       if ( !icon.isEmpty() )
    00382         return static_cast<KPopupMenu *>(parent)->insertTitle( pix, i18nText, -1, index );
    00383       else
    00384         return static_cast<KPopupMenu *>(parent)->insertTitle( i18nText, -1, index );
    00385     }
    00386   }
    00387   return 0;
    00388 }
    00389
    00390 void KXMLGUIBuilder::removeCustomElement( QWidget *parent, int id )
    00391 {
    00392   if ( ::qt_cast<QPopupMenu *>( parent ) )
    00393     static_cast<QPopupMenu *>(parent)->removeItem( id );
    00394   else if ( ::qt_cast<QMenuBar *>( parent ) )
    00395     static_cast<QMenuBar *>(parent)->removeItem( id );
    00396   else if ( ::qt_cast<KToolBar *>( parent ) )
    00397     static_cast<KToolBar *>(parent)->removeItemDelayed( id );
    00398 }
    00399
    00400 KXMLGUIClient *KXMLGUIBuilder::builderClient() const
    00401 {
    00402   return d->m_client;
    00403 }
    00404
    00405 void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
    00406 {
    00407   d->m_client = client;
    00408   if ( client )
    00409       setBuilderInstance( client->instance() );
    00410 }
    00411
    00412 KInstance *KXMLGUIBuilder::builderInstance() const
    00413 {
    00414   return d->m_instance;
    00415 }
    00416
    00417 void KXMLGUIBuilder::setBuilderInstance( KInstance *instance )
    00418 {
    00419   d->m_instance = instance;
    00420 }
    00421
    00422 void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
    00423 {
    00424     if ( !d->m_widget || !::qt_cast<KMainWindow *>( d->m_widget ) )
    00425         return;
    00426 #if 0
    00427     KToolBar *toolbar = 0;
    00428     QListIterator<KToolBar> it( ( (KMainWindow*)d->m_widget )->toolBarIterator() );
    00429     while ( ( toolbar = it.current() ) ) {
    00430         kdDebug() << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar << endl;
    00431         ++it;
    00432         toolbar->positionYourself();
    00433     }
    00434 #else
    00435     static_cast<KMainWindow *>(d->m_widget)->finalizeGUI( false );
    00436 #endif
    00437 }
    00438
    00439 void KXMLGUIBuilder::virtual_hook( int, void* )
    00440 { /*BASE::virtual_hook( id, data );*/ }
    00441

This file is part of the documentation for kdeui Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:20:25 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003