KDEUI
kxmlguibuilder.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00129 if (element.attribute("deleted").toLower() == "true") {
00130 return 0;
00131 }
00132
00133 const QString tagName = element.tagName().toLower();
00134 if ( tagName == d->tagMainWindow ) {
00135 KMainWindow *mainwindow = qobject_cast<KMainWindow*>( d->m_widget );
00136 return mainwindow;
00137 }
00138
00139 if ( tagName == d->tagMenuBar ) {
00140 KMainWindow *mainWin = qobject_cast<KMainWindow*>( d->m_widget );
00141 KMenuBar *bar = 0;
00142 if (mainWin)
00143 bar = mainWin->menuBar();
00144 if (!bar)
00145 bar = new KMenuBar( d->m_widget );
00146 bar->show();
00147 return bar;
00148 }
00149
00150 if ( tagName == d->tagMenu ) {
00151
00152
00153
00154
00155
00156
00157 QWidget* p = parent;
00158 while ( p && !qobject_cast<KMainWindow*>( p ) )
00159 p = p->parentWidget();
00160
00161 QByteArray name = element.attribute( d->attrName ).toUtf8();
00162
00163 if (!KAuthorized::authorizeKAction(name))
00164 return 0;
00165
00166 KMenu *popup = new KMenu(p);
00167 popup->setObjectName(name);
00168
00169 d->m_menumenuhandler->insertKMenu(popup);
00170
00171 QString i18nText;
00172 QDomElement textElem = element.namedItem( d->attrText1 ).toElement();
00173 if ( textElem.isNull() )
00174 textElem = element.namedItem( d->attrText2 ).toElement();
00175 const QByteArray text = textElem.text().toUtf8();
00176 const QByteArray context = textElem.attribute(d->attrContext).toUtf8();
00177
00178 if ( text.isEmpty() )
00179 i18nText = i18n( "No text" );
00180 else if ( context.isEmpty() )
00181 i18nText = i18n( text );
00182 else
00183 i18nText = i18nc( context, text );
00184
00185 const QString icon = element.attribute( d->attrIcon );
00186 KIcon pix;
00187 if (!icon.isEmpty()) {
00188 pix = KIcon( icon );
00189 }
00190
00191 if ( parent ) {
00192 QAction* act = popup->menuAction();
00193 if ( !icon.isEmpty() )
00194 act->setIcon(pix);
00195 act->setText(i18nText);
00196 if (index == -1 || index >= parent->actions().count())
00197 parent->addAction(act);
00198 else
00199 parent->insertAction(parent->actions().value(index), act);
00200 containerAction = act;
00201 }
00202
00203 return popup;
00204 }
00205
00206 if ( tagName == d->tagToolBar ) {
00207 QByteArray name = element.attribute( d->attrName ).toUtf8();
00208
00209 KToolBar *bar = static_cast<KToolBar*>(d->m_widget->findChild<KToolBar*>( name ));
00210 if( !bar )
00211 {
00212 bar = new KToolBar(name, d->m_widget, false);
00213 }
00214
00215 if ( qobject_cast<KMainWindow*>( d->m_widget ) )
00216 {
00217 if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
00218 bar->setXMLGUIClient( d->m_client );
00219 }
00220
00221 bar->loadState( element );
00222
00223 return bar;
00224 }
00225
00226 if ( tagName == d->tagStatusBar ) {
00227 KMainWindow *mainWin = qobject_cast<KMainWindow *>(d->m_widget);
00228 if ( mainWin ) {
00229 mainWin->statusBar()->show();
00230 return mainWin->statusBar();
00231 }
00232 KStatusBar *bar = new KStatusBar( d->m_widget );
00233 return bar;
00234 }
00235
00236 return 0L;
00237 }
00238
00239 void KXMLGUIBuilder::removeContainer( QWidget *container, QWidget *parent, QDomElement &element, QAction* containerAction )
00240 {
00241
00242
00243 if ( qobject_cast<QMenu*>( container ) )
00244 {
00245 if ( parent ) {
00246 parent->removeAction( containerAction );
00247 }
00248
00249 delete container;
00250 }
00251 else if ( qobject_cast<KToolBar*>( container ) )
00252 {
00253 KToolBar *tb = static_cast<KToolBar *>( container );
00254
00255 tb->saveState( element );
00256 delete tb;
00257 }
00258 else if ( qobject_cast<KMenuBar*>( container ) )
00259 {
00260 KMenuBar *mb = static_cast<KMenuBar *>( container );
00261 mb->hide();
00262
00263
00264
00265
00266 }
00267 else if ( qobject_cast<KStatusBar*>( container ) )
00268 {
00269 if ( qobject_cast<KMainWindow*>( d->m_widget ) )
00270 container->hide();
00271 else
00272 delete static_cast<KStatusBar *>(container);
00273 }
00274 else
00275 kWarning() << "Unhandled container to remove : " << container->metaObject()->className();
00276 }
00277
00278 QStringList KXMLGUIBuilder::customTags() const
00279 {
00280 QStringList res;
00281 res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
00282 return res;
00283 }
00284
00285 QAction* KXMLGUIBuilder::createCustomElement( QWidget *parent, int index, const QDomElement &element )
00286 {
00287 QAction* before = 0L;
00288 if (index > 0 && index < parent->actions().count())
00289 before = parent->actions().at(index);
00290
00291 const QString tagName = element.tagName().toLower();
00292 if (tagName == d->tagSeparator)
00293 {
00294 if ( QMenu *menu = qobject_cast<QMenu*>( parent ) )
00295 {
00296
00297
00298 return menu->insertSeparator( before );
00299 }
00300 else if ( QMenuBar* bar = qobject_cast<QMenuBar*>( parent ) )
00301 {
00302 QAction* separatorAction = new QAction(bar);
00303 separatorAction->setSeparator(true);
00304 bar->insertAction( before, separatorAction );
00305 return separatorAction;
00306 }
00307 else if ( KToolBar *bar = qobject_cast<KToolBar*>( parent ) )
00308 {
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 return bar->insertSeparator( before );
00331 }
00332 }
00333 else if (tagName == d->tagTearOffHandle)
00334 {
00335 static_cast<QMenu *>(parent)->setTearOffEnabled(true);
00336 }
00337 else if (tagName == d->tagMenuTitle)
00338 {
00339 if ( KMenu* m = qobject_cast<KMenu*>( parent ) )
00340 {
00341 QString i18nText;
00342 QByteArray text = element.text().toUtf8();
00343
00344 if ( text.isEmpty() )
00345 i18nText = i18n( "No text" );
00346 else
00347 i18nText = i18n( text );
00348
00349 QString icon = element.attribute( d->attrIcon );
00350 KIcon pix;
00351
00352 if ( !icon.isEmpty() )
00353 {
00354 pix = KIcon( icon );
00355 }
00356
00357 if ( !icon.isEmpty() ) {
00358 return m->addTitle( pix, i18nText, before );
00359 } else {
00360 return m->addTitle( i18nText, before );
00361 }
00362 }
00363 }
00364
00365 QAction* blank = new QAction(parent);
00366 blank->setVisible(false);
00367 parent->insertAction(before, blank);
00368 return blank;
00369 }
00370
00371 void KXMLGUIBuilder::removeCustomElement( QWidget *parent, QAction* action )
00372 {
00373 parent->removeAction(action);
00374 }
00375
00376 KXMLGUIClient *KXMLGUIBuilder::builderClient() const
00377 {
00378 return d->m_client;
00379 }
00380
00381 void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
00382 {
00383 d->m_client = client;
00384 if ( client )
00385 setBuilderComponentData( client->componentData() );
00386 }
00387
00388 KComponentData KXMLGUIBuilder::builderComponentData() const
00389 {
00390 return d->m_componentData;
00391 }
00392
00393 void KXMLGUIBuilder::setBuilderComponentData(const KComponentData &componentData)
00394 {
00395 d->m_componentData = componentData;
00396 }
00397
00398 void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
00399 {
00400 KXmlGuiWindow* window = qobject_cast<KXmlGuiWindow*>(d->m_widget);
00401 if (!window)
00402 return;
00403 #if 0
00404 KToolBar *toolbar = 0;
00405 QListIterator<KToolBar> it( ( (KMainWindow*)d->m_widget )->toolBarIterator() );
00406 while ( ( toolbar = it.current() ) ) {
00407 kDebug(260) << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar;
00408 ++it;
00409 toolbar->positionYourself();
00410 }
00411 #else
00412 window->finalizeGUI( false );
00413 #endif
00414 }
00415
00416 void KXMLGUIBuilder::virtual_hook( int, void* )
00417 { }
00418