# kdeui Library API Documentation

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

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:58:00 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001