• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kxmlguibuilder.cpp

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kxmlguibuilder.h"
00022 
00023 #include "kapplication.h"
00024 #include "kauthorized.h"
00025 #include "kxmlguiclient.h"
00026 #include "kmenubar.h"
00027 #include "kmenu.h"
00028 #include "ktoolbar.h"
00029 #include "kstatusbar.h"
00030 #include "kmainwindow.h"
00031 #include "kxmlguiwindow.h"
00032 #include "kaction.h"
00033 #include "kglobalsettings.h"
00034 #include <klocale.h>
00035 #include <kiconloader.h>
00036 #include <kdebug.h>
00037 #include <QtXml/QDomElement>
00038 #include <QtCore/QObject>
00039 #include <QtCore/QMutableStringListIterator>
00040 #include "kmenumenuhandler_p.h"
00041 #include <kcomponentdata.h>
00042 
00043 using namespace KDEPrivate;
00044 
00045 class KXMLGUIBuilderPrivate
00046 {
00047   public:
00048     KXMLGUIBuilderPrivate() : m_client(0L)  {}
00049     ~KXMLGUIBuilderPrivate() { }
00050 
00051     QWidget *m_widget;
00052 
00053     QString tagMainWindow;
00054     QString tagMenuBar;
00055     QString tagMenu;
00056     QString tagToolBar;
00057     QString tagStatusBar;
00058 
00059     QString tagSeparator;
00060     QString tagTearOffHandle;
00061     QString tagMenuTitle;
00062 
00063     QString attrName;
00064     QString attrLineSeparator;
00065 
00066     QString attrText1;
00067     QString attrText2;
00068     QString attrContext;
00069 
00070     QString attrIcon;
00071 
00072     KComponentData m_componentData;
00073     KXMLGUIClient *m_client;
00074 
00075     KMenuMenuHandler *m_menumenuhandler;
00076 };
00077 
00078 
00079 KXMLGUIBuilder::KXMLGUIBuilder( QWidget *widget )
00080   : d( new KXMLGUIBuilderPrivate )
00081 {
00082   d->m_widget = widget;
00083 
00084   d->tagMainWindow = QLatin1String( "mainwindow" );
00085   d->tagMenuBar = QLatin1String( "menubar" );
00086   d->tagMenu = QLatin1String( "menu" );
00087   d->tagToolBar = QLatin1String( "toolbar" );
00088   d->tagStatusBar = QLatin1String( "statusbar" );
00089 
00090   d->tagSeparator = QLatin1String( "separator" );
00091   d->tagTearOffHandle = QLatin1String( "tearoffhandle" );
00092   d->tagMenuTitle = QLatin1String( "title" );
00093 
00094   d->attrName = QLatin1String( "name" );
00095   d->attrLineSeparator = QLatin1String( "lineseparator" );
00096 
00097   d->attrText1 = QLatin1String( "text" );
00098   d->attrText2 = QLatin1String( "Text" );
00099   d->attrContext = QLatin1String( "context" );
00100 
00101   d->attrIcon = QLatin1String( "icon" );
00102 
00103   d->m_menumenuhandler=new KMenuMenuHandler(this);
00104 }
00105 
00106 KXMLGUIBuilder::~KXMLGUIBuilder()
00107 {
00108   delete d->m_menumenuhandler;
00109   delete d;
00110 }
00111 
00112 QWidget *KXMLGUIBuilder::widget()
00113 {
00114   return d->m_widget;
00115 }
00116 
00117 QStringList KXMLGUIBuilder::containerTags() const
00118 {
00119   QStringList res;
00120   res << d->tagMenu << d->tagToolBar << d->tagMainWindow << d->tagMenuBar << d->tagStatusBar;
00121 
00122   return res;
00123 }
00124 
00125 QWidget *KXMLGUIBuilder::createContainer( QWidget *parent, int index, const QDomElement &element, QAction*& containerAction )
00126 {
00127     containerAction = 0;
00128     const QString tagName = element.tagName().toLower();
00129     if ( tagName == d->tagMainWindow ) {
00130         KMainWindow *mainwindow = qobject_cast<KMainWindow*>( d->m_widget ); // could be 0
00131         return mainwindow;
00132     }
00133 
00134     if ( tagName == d->tagMenuBar ) {
00135         KMainWindow *mainWin = qobject_cast<KMainWindow*>( d->m_widget );
00136         KMenuBar *bar = 0;
00137         if (mainWin)
00138             bar = mainWin->menuBar();
00139         if (!bar)
00140             bar = new KMenuBar( d->m_widget );
00141         bar->show();
00142         return bar;
00143     }
00144 
00145   if ( tagName == d->tagMenu ) {
00146     // Look up to see if we are inside a mainwindow. If yes, then
00147     // use it as parent widget (to get kaction to plug itself into the
00148     // mainwindow). Don't use a popupmenu as parent widget, otherwise
00149     // the popup won't be hidden if it is used as a standalone menu as well.
00150     // And we don't want to set the parent for a standalone popupmenu,
00151     // otherwise its shortcuts appear.
00152     QWidget* p = parent;
00153     while ( p && !qobject_cast<KMainWindow*>( p ) )
00154         p = p->parentWidget();
00155 
00156     QByteArray name = element.attribute( d->attrName ).toUtf8();
00157 
00158     if (!KAuthorized::authorizeKAction(name))
00159        return 0;
00160 
00161     KMenu *popup = new KMenu(p);
00162     popup->setObjectName(name);
00163 
00164     d->m_menumenuhandler->insertKMenu(popup);
00165 
00166     QString i18nText;
00167     QDomElement textElem = element.namedItem( d->attrText1 ).toElement();
00168     if ( textElem.isNull() ) // try with capital T
00169       textElem = element.namedItem( d->attrText2 ).toElement();
00170     const QByteArray text = textElem.text().toUtf8();
00171     const QByteArray context = textElem.attribute(d->attrContext).toUtf8();
00172 
00173     if ( text.isEmpty() ) // still no luck
00174       i18nText = i18n( "No text" );
00175     else if ( context.isEmpty() )
00176       i18nText = i18n( text );
00177     else
00178       i18nText = i18nc( context, text );
00179 
00180     const QString icon = element.attribute( d->attrIcon );
00181     KIcon pix;
00182     if (!icon.isEmpty()) {
00183         pix = KIcon( icon );
00184     }
00185 
00186     if ( parent ) {
00187         QAction* act = popup->menuAction();
00188         if ( !icon.isEmpty() )
00189             act->setIcon(pix);
00190         act->setText(i18nText);
00191         if (index == -1 || index >= parent->actions().count())
00192             parent->addAction(act);
00193         else
00194             parent->insertAction(parent->actions().value(index), act);
00195         containerAction = act;
00196     }
00197 
00198     return popup;
00199   }
00200 
00201   if ( tagName == d->tagToolBar ) {
00202     bool honor = (element.attribute( d->attrName ) == "mainToolBar");
00203 
00204     QByteArray name = element.attribute( d->attrName ).toUtf8();
00205 
00206     KToolBar *bar = static_cast<KToolBar*>(d->m_widget->findChild<KToolBar*>( name ));
00207     if( !bar )
00208     {
00209        bar = new KToolBar( d->m_widget, honor, false );
00210        bar->setObjectName(name);
00211     }
00212 
00213     if ( qobject_cast<KMainWindow*>( d->m_widget ) )
00214     {
00215         if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
00216             bar->setXMLGUIClient( d->m_client );
00217     }
00218 
00219     bar->loadState( element );
00220 
00221     return bar;
00222   }
00223 
00224   if ( tagName == d->tagStatusBar ) {
00225       KMainWindow *mainWin = qobject_cast<KMainWindow *>(d->m_widget);
00226       if ( mainWin ) {
00227           mainWin->statusBar()->show();
00228           return mainWin->statusBar();
00229       }
00230       KStatusBar *bar = new KStatusBar( d->m_widget );
00231       return bar;
00232   }
00233 
00234   return 0L;
00235 }
00236 
00237 void KXMLGUIBuilder::removeContainer( QWidget *container, QWidget *parent, QDomElement &element, QAction* containerAction )
00238 {
00239   // Warning parent can be 0L
00240 
00241   if ( qobject_cast<QMenu*>( container ) )
00242   {
00243     if ( parent ) {
00244         parent->removeAction( containerAction );
00245     }
00246 
00247     delete container;
00248   }
00249   else if ( qobject_cast<KToolBar*>( container ) )
00250   {
00251     KToolBar *tb = static_cast<KToolBar *>( container );
00252 
00253     tb->saveState( element );
00254     delete tb;
00255   }
00256   else if ( qobject_cast<KMenuBar*>( container ) )
00257   {
00258     KMenuBar *mb = static_cast<KMenuBar *>( container );
00259     mb->hide();
00260     // Don't delete menubar - it can be reused by createContainer.
00261     // If you decide that you do need to delete the menubar, make
00262     // sure that QMainWindow::d->mb does not point to a deleted
00263     // menubar object.
00264   }
00265   else if ( qobject_cast<KStatusBar*>( container ) )
00266   {
00267     if ( qobject_cast<KMainWindow*>( d->m_widget ) )
00268         container->hide();
00269     else
00270       delete static_cast<KStatusBar *>(container);
00271   }
00272   else
00273      kWarning() << "Unhandled container to remove : " << container->metaObject()->className();
00274 }
00275 
00276 QStringList KXMLGUIBuilder::customTags() const
00277 {
00278   QStringList res;
00279   res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
00280   return res;
00281 }
00282 
00283 QAction* KXMLGUIBuilder::createCustomElement( QWidget *parent, int index, const QDomElement &element )
00284 {
00285   QAction* before = 0L;
00286   if (index > 0 && index < parent->actions().count())
00287       before = parent->actions().at(index);
00288 
00289   if ( element.tagName().toLower() == d->tagSeparator )
00290   {
00291     if ( QMenu *menu = qobject_cast<QMenu*>( parent ) )
00292     {
00293       // QMenu already cares for leading/trailing/repeated separators
00294       // no need to check anything
00295       return menu->insertSeparator( before );
00296     }
00297     else if ( QMenuBar* bar = qobject_cast<QMenuBar*>( parent ) )
00298     {
00299        QAction* separatorAction = new QAction(bar);
00300        separatorAction->setSeparator(true);
00301        bar->insertAction( before, separatorAction );
00302        return separatorAction;
00303     }
00304     else if ( KToolBar *bar = qobject_cast<KToolBar*>( parent ) )
00305     {
00306       /* FIXME KAction port - any need to provide a replacement for lineSeparator/normal separator?
00307       bool isLineSep = true;
00308 
00309       QDomNamedNodeMap attributes = element.attributes();
00310       unsigned int i = 0;
00311       for (; i < attributes.length(); i++ )
00312       {
00313         QDomAttr attr = attributes.item( i ).toAttr();
00314 
00315         if ( attr.name().toLower() == d->attrLineSeparator &&
00316              attr.value().toLower() == QLatin1String("false") )
00317         {
00318           isLineSep = false;
00319           break;
00320         }
00321       }
00322 
00323       if ( isLineSep )
00324           return bar->insertSeparator( index ? bar->actions()[index - 1] : 0L );
00325       else*/
00326 
00327       return bar->insertSeparator( before );
00328     }
00329   }
00330   else if ( element.tagName().toLower() == d->tagTearOffHandle )
00331   {
00332     static_cast<QMenu *>(parent)->setTearOffEnabled(true);
00333   }
00334   else if ( element.tagName().toLower() == d->tagMenuTitle )
00335   {
00336     if ( KMenu* m = qobject_cast<KMenu*>( parent ) )
00337     {
00338       QString i18nText;
00339       QByteArray text = element.text().toUtf8();
00340 
00341       if ( text.isEmpty() )
00342         i18nText = i18n( "No text" );
00343       else
00344         i18nText = i18n( text );
00345 
00346       QString icon = element.attribute( d->attrIcon );
00347       KIcon pix;
00348 
00349       if ( !icon.isEmpty() )
00350       {
00351         pix = KIcon( icon );
00352       }
00353 
00354       if ( !icon.isEmpty() ) {
00355         return m->addTitle( pix, i18nText, before );
00356       } else {
00357         return m->addTitle( i18nText, before );
00358       }
00359     }
00360   }
00361 
00362   QAction* blank = new QAction(parent);
00363   blank->setVisible(false);
00364   parent->insertAction(before, blank);
00365   return blank;
00366 }
00367 
00368 void KXMLGUIBuilder::removeCustomElement( QWidget *parent, QAction* action )
00369 {
00370   parent->removeAction(action);
00371 }
00372 
00373 KXMLGUIClient *KXMLGUIBuilder::builderClient() const
00374 {
00375   return d->m_client;
00376 }
00377 
00378 void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
00379 {
00380   d->m_client = client;
00381   if ( client )
00382       setBuilderComponentData( client->componentData() );
00383 }
00384 
00385 KComponentData KXMLGUIBuilder::builderComponentData() const
00386 {
00387   return d->m_componentData;
00388 }
00389 
00390 void KXMLGUIBuilder::setBuilderComponentData(const KComponentData &componentData)
00391 {
00392   d->m_componentData = componentData;
00393 }
00394 
00395 void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
00396 {
00397     KXmlGuiWindow* window = qobject_cast<KXmlGuiWindow*>(d->m_widget);
00398     if (!window)
00399         return;
00400 #if 0
00401     KToolBar *toolbar = 0;
00402     QListIterator<KToolBar> it( ( (KMainWindow*)d->m_widget )->toolBarIterator() );
00403     while ( ( toolbar = it.current() ) ) {
00404         kDebug(260) << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar;
00405         ++it;
00406         toolbar->positionYourself();
00407     }
00408 #else
00409     window->finalizeGUI( false );
00410 #endif
00411 }
00412 
00413 void KXMLGUIBuilder::virtual_hook( int, void* )
00414 { /*BASE::virtual_hook( id, data );*/ }
00415 

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal