kdeui Library API Documentation

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     QString attrFullWidth;
00063     QString attrPosition;
00064     QString attrIndex;
00065     QString attrOffset;
00066     QString attrNewLine;
00067     QString attrIconText;
00068     QString attrIconSize;
00069 
00070     KInstance *m_instance;
00071     KXMLGUIClient *m_client;
00072 };
00073 
00074 KXMLGUIBuilder::KXMLGUIBuilder( QWidget *widget )
00075 {
00076   d = new KXMLGUIBuilderPrivate;
00077   d->m_widget = widget;
00078 
00079   d->tagMainWindow = QString::fromLatin1( "mainwindow" );
00080   d->tagMenuBar = QString::fromLatin1( "menubar" );
00081   d->tagMenu = QString::fromLatin1( "menu" );
00082   d->tagToolBar = QString::fromLatin1( "toolbar" );
00083   d->tagStatusBar = QString::fromLatin1( "statusbar" );
00084 
00085   d->tagSeparator = QString::fromLatin1( "separator" );
00086   d->tagTearOffHandle = QString::fromLatin1( "tearoffhandle" );
00087   d->tagMenuTitle = QString::fromLatin1( "title" );
00088 
00089   d->attrName = QString::fromLatin1( "name" );
00090   d->attrLineSeparator = QString::fromLatin1( "lineseparator" );
00091 
00092   d->attrText1 = QString::fromLatin1( "text" );
00093   d->attrText2 = QString::fromLatin1( "Text" );
00094 
00095   d->attrIcon = QString::fromLatin1( "icon" );
00096   d->attrFullWidth = QString::fromLatin1( "fullWidth" );
00097   d->attrPosition = QString::fromLatin1( "position" );
00098   d->attrIconText = QString::fromLatin1( "iconText" );
00099   d->attrIconSize = QString::fromLatin1( "iconSize" );
00100   d->attrIndex = QString::fromLatin1( "index" );
00101   d->attrOffset = QString::fromLatin1( "offset" );
00102   d->attrNewLine = QString::fromLatin1( "newline" );
00103 
00104   d->m_instance = 0;
00105   d->m_client = 0;
00106 }
00107 
00108 KXMLGUIBuilder::~KXMLGUIBuilder()
00109 {
00110   delete d;
00111 }
00112 
00113 QWidget *KXMLGUIBuilder::widget()
00114 {
00115   return d->m_widget;
00116 }
00117 
00118 QStringList KXMLGUIBuilder::containerTags() const
00119 {
00120   QStringList res;
00121   res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar;
00122 
00123   return res;
00124 }
00125 
00126 QWidget *KXMLGUIBuilder::createContainer( QWidget *parent, int index, const QDomElement &element, int &id )
00127 {
00128   id = -1;
00129   if ( element.tagName().lower() == d->tagMainWindow )
00130   {
00131     KMainWindow *mainwindow = 0;
00132     if ( d->m_widget->inherits( "KMainWindow" ) )
00133       mainwindow = static_cast<KMainWindow *>(d->m_widget);
00134 
00135     return mainwindow;
00136   }
00137 
00138   if ( element.tagName().lower() == d->tagMenuBar )
00139   {
00140     KMenuBar *bar;
00141 
00142     if ( d->m_widget->inherits( "KMainWindow" ) )
00143       bar = static_cast<KMainWindow *>(d->m_widget)->menuBar();
00144     else
00145       bar = new KMenuBar( d->m_widget );
00146 
00147     bar->show();
00148     return bar;
00149   }
00150 
00151   if ( element.tagName().lower() == d->tagMenu )
00152   {
00153     // Look up to see if we are inside a mainwindow. If yes, then
00154     // use it as parent widget (to get kaction to plug itself into the
00155     // mainwindow). Don't use a popupmenu as parent widget, otherwise
00156     // the popup won't be hidden if it is used as a standalone menu as well.
00157     // And we don't want to set the parent for a standalone popupmenu,
00158     // otherwise its shortcuts appear.
00159     QWidget* p = parent;
00160     while ( p && !p->inherits("KMainWindow") )
00161         p = p->parentWidget();
00162 
00163     KPopupMenu *popup = new KPopupMenu( p, element.attribute( d->attrName ).utf8());
00164 
00165     QString i18nText;
00166     QCString text = element.namedItem( d->attrText1 ).toElement().text().utf8();
00167     if ( text.isEmpty() ) // try with capital T
00168       text = element.namedItem( d->attrText2 ).toElement().text().utf8();
00169 
00170     if ( text.isEmpty() ) // still no luck
00171       i18nText = i18n( "No text!" );
00172     else
00173       i18nText = i18n( text );
00174 
00175     QString icon = element.attribute( d->attrIcon );
00176     QIconSet pix;
00177 
00178     if ( !icon.isEmpty() )
00179     {
00180       KInstance *instance = d->m_instance;
00181       if ( !instance )
00182         instance = KGlobal::instance();
00183 
00184       pix = SmallIconSet( icon, 16, instance );
00185     }
00186 
00187     if ( parent && parent->inherits( "KMenuBar" ) )
00188     {
00189       if ( !icon.isEmpty() )
00190         id = static_cast<KMenuBar *>(parent)->insertItem( pix, i18nText, popup, -1, index );
00191       else
00192         id = static_cast<KMenuBar *>(parent)->insertItem( i18nText, popup, -1, index );
00193     }
00194     else if ( parent && parent->inherits( "QPopupMenu" ) )
00195     {
00196       if ( !icon.isEmpty() )
00197         id = static_cast<QPopupMenu *>(parent)->insertItem( pix, i18nText, popup, -1, index );
00198       else
00199         id = static_cast<QPopupMenu *>(parent)->insertItem( i18nText, popup, -1, index );
00200     }
00201 
00202     return popup;
00203   }
00204 
00205   if ( element.tagName().lower() == d->tagToolBar )
00206   {
00207     bool honor = (element.attribute( d->attrName ) == "mainToolBar");
00208 
00209     QCString name = element.attribute( d->attrName ).utf8();
00210 
00211     KToolBar *bar = static_cast<KToolBar*>(d->m_widget->child( name, "KToolBar" ));
00212     if( !bar )
00213     {
00214        bar = new KToolBar( d->m_widget, name, honor, false );
00215     }
00216 
00217     if ( d->m_widget->inherits( "KMainWindow" ) )
00218     {
00219         if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
00220             bar->setXMLGUIClient( d->m_client );
00221     }
00222 
00223     bar->loadState( element );
00224 
00225     return bar;
00226   }
00227 
00228   if ( element.tagName().lower() == d->tagStatusBar )
00229   {
00230     if ( d->m_widget->inherits( "KMainWindow" ) )
00231     {
00232       KMainWindow *mainWin = static_cast<KMainWindow *>(d->m_widget);
00233       mainWin->statusBar()->show();
00234       return mainWin->statusBar();
00235     }
00236     KStatusBar *bar = new KStatusBar( d->m_widget );
00237     return bar;
00238   }
00239 
00240   return 0L;
00241 }
00242 
00243 void KXMLGUIBuilder::removeContainer( QWidget *container, QWidget *parent, QDomElement &element, int id )
00244 {
00245   // Warning parent can be 0L
00246 
00247   if ( container->inherits( "QPopupMenu" ) )
00248   {
00249     if ( parent )
00250     {
00251         if ( parent->inherits( "KMenuBar" ) )
00252             static_cast<KMenuBar *>(parent)->removeItem( id );
00253         else if ( parent->inherits( "QPopupMenu" ) )
00254             static_cast<QPopupMenu *>(parent)->removeItem( id );
00255     }
00256 
00257     delete container;
00258   }
00259   else if ( container->inherits( "KToolBar" ) )
00260   {
00261     KToolBar *tb = static_cast<KToolBar *>( container );
00262 
00263     tb->saveState( element );
00264     delete tb;
00265   }
00266   else if ( container->inherits( "KMenuBar" ) )
00267   {
00268     KMenuBar *mb = static_cast<KMenuBar *>( container );
00269     mb->hide();
00270     // Don't delete menubar - it can be reused by createContainer.
00271     // If you decide that you do need to delete the menubar, make
00272     // sure that QMainWindow::d->mb does not point to a deleted
00273     // menubar object.
00274   }
00275   else if ( container->inherits( "KStatusBar" ) )
00276   {
00277     if ( d->m_widget->inherits( "KMainWindow" ) )
00278         container->hide();
00279     else
00280       delete static_cast<KStatusBar *>(container);
00281   }
00282   else
00283      kdWarning() << "Unhandled container to remove : " << container->className() << endl;
00284 }
00285 
00286 QStringList KXMLGUIBuilder::customTags() const
00287 {
00288   QStringList res;
00289   res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
00290   return res;
00291 }
00292 
00293 int KXMLGUIBuilder::createCustomElement( QWidget *parent, int index, const QDomElement &element )
00294 {
00295   if ( element.tagName().lower() == d->tagSeparator )
00296   {
00297     if ( parent->inherits( "QPopupMenu" ) )
00298     {
00299       // Don't insert multiple separators in a row
00300       QPopupMenu *menu = static_cast<QPopupMenu *>(parent);
00301       int count = menu->count();
00302       if (count)
00303       {
00304          int previousId = -1;
00305          if ((index == -1) || (index > count))
00306             previousId = menu->idAt(count-1);
00307          else if (index > 0)
00308             previousId = menu->idAt(index-1);
00309          if (previousId != -1)
00310          {
00311             if (menu->text(previousId).isEmpty() &&
00312                 !menu->iconSet(previousId) &&
00313                 !menu->pixmap(previousId))
00314                return 0;
00315          }
00316       }
00317       
00318       return menu->insertSeparator( index );
00319     }
00320     else if ( parent->inherits( "QMenuBar" ) )
00321        return static_cast<QMenuBar *>(parent)->insertSeparator( index );
00322     else if ( parent->inherits( "KToolBar" ) )
00323     {
00324       KToolBar *bar = static_cast<KToolBar *>( parent );
00325 
00326       bool isLineSep = false;
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("true") )
00336         {
00337           isLineSep = true;
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 ( parent->inherits( "QPopupMenu" )  && KGlobalSettings::insertTearOffHandle())
00355       return static_cast<QPopupMenu *>(parent)->insertTearOffHandle( -1, index );
00356   }
00357   else if ( element.tagName().lower() == d->tagMenuTitle )
00358   {
00359     if ( parent->inherits( "KPopupMenu" ) )
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 ( parent->inherits( "QPopupMenu" ) )
00393     static_cast<QPopupMenu *>(parent)->removeItem( id );
00394   else if ( parent->inherits( "QMenuBar" ) )
00395     static_cast<QMenuBar *>(parent)->removeItem( id );
00396   else if ( parent->inherits( "KToolBar" ) )
00397     static_cast<KToolBar *>(parent)->removeItem( 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 || !d->m_widget->inherits( "KMainWindow" ) )
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 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:02 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001