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       // Don't insert a separator at the top of the menu
00318       if(count == 0)
00319         return 0;
00320       else
00321         return menu->insertSeparator( index );
00322     }
00323     else if ( parent->inherits( "QMenuBar" ) )
00324        return static_cast<QMenuBar *>(parent)->insertSeparator( index );
00325     else if ( parent->inherits( "KToolBar" ) )
00326     {
00327       KToolBar *bar = static_cast<KToolBar *>( parent );
00328 
00329       bool isLineSep = false;
00330 
00331       QDomNamedNodeMap attributes = element.attributes();
00332       unsigned int i = 0;
00333       for (; i < attributes.length(); i++ )
00334       {
00335         QDomAttr attr = attributes.item( i ).toAttr();
00336 
00337         if ( attr.name().lower() == d->attrLineSeparator &&
00338              attr.value().lower() == QString::fromLatin1("true") )
00339         {
00340           isLineSep = true;
00341           break;
00342         }
00343       }
00344 
00345       int id = KAction::getToolButtonID();
00346 
00347       if ( isLineSep )
00348           bar->insertLineSeparator( index, id );
00349       else
00350           bar->insertSeparator( index, id );
00351 
00352       return id;
00353     }
00354   }
00355   else if ( element.tagName().lower() == d->tagTearOffHandle )
00356   {
00357     if ( parent->inherits( "QPopupMenu" )  && KGlobalSettings::insertTearOffHandle())
00358       return static_cast<QPopupMenu *>(parent)->insertTearOffHandle( -1, index );
00359   }
00360   else if ( element.tagName().lower() == d->tagMenuTitle )
00361   {
00362     if ( parent->inherits( "KPopupMenu" ) )
00363     {
00364       QString i18nText;
00365       QCString text = element.text().utf8();
00366 
00367       if ( text.isEmpty() )
00368         i18nText = i18n( "No text!" );
00369       else
00370         i18nText = i18n( text );
00371 
00372       QString icon = element.attribute( d->attrIcon );
00373       QPixmap pix;
00374 
00375       if ( !icon.isEmpty() )
00376       {
00377         KInstance *instance = d->m_instance;
00378         if ( !instance )
00379           instance = KGlobal::instance();
00380 
00381         pix = SmallIcon( icon, instance );
00382       }
00383 
00384       if ( !icon.isEmpty() )
00385         return static_cast<KPopupMenu *>(parent)->insertTitle( pix, i18nText, -1, index );
00386       else
00387         return static_cast<KPopupMenu *>(parent)->insertTitle( i18nText, -1, index );
00388     }
00389   }
00390   return 0;
00391 }
00392 
00393 void KXMLGUIBuilder::removeCustomElement( QWidget *parent, int id )
00394 {
00395   if ( parent->inherits( "QPopupMenu" ) )
00396     static_cast<QPopupMenu *>(parent)->removeItem( id );
00397   else if ( parent->inherits( "QMenuBar" ) )
00398     static_cast<QMenuBar *>(parent)->removeItem( id );
00399   else if ( parent->inherits( "KToolBar" ) )
00400     static_cast<KToolBar *>(parent)->removeItem( id );
00401 }
00402 
00403 KXMLGUIClient *KXMLGUIBuilder::builderClient() const
00404 {
00405   return d->m_client;
00406 }
00407 
00408 void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
00409 {
00410   d->m_client = client;
00411   if ( client )
00412       setBuilderInstance( client->instance() );
00413 }
00414 
00415 KInstance *KXMLGUIBuilder::builderInstance() const
00416 {
00417   return d->m_instance;
00418 }
00419 
00420 void KXMLGUIBuilder::setBuilderInstance( KInstance *instance )
00421 {
00422   d->m_instance = instance;
00423 }
00424 
00425 void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
00426 {
00427     if ( !d->m_widget || !d->m_widget->inherits( "KMainWindow" ) )
00428         return;
00429 #if 0
00430     KToolBar *toolbar = 0;
00431     QListIterator<KToolBar> it( ( (KMainWindow*)d->m_widget )->toolBarIterator() );
00432     while ( ( toolbar = it.current() ) ) {
00433         kdDebug() << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar << endl;
00434         ++it;
00435         toolbar->positionYourself();
00436     }
00437 #else
00438     static_cast<KMainWindow *>(d->m_widget)->finalizeGUI( false );
00439 #endif
00440 }
00441 
00442 void KXMLGUIBuilder::virtual_hook( int, void* )
00443 { /*BASE::virtual_hook( id, data );*/ }
00444 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.3.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Apr 6 22:48:29 2005 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001