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 bool honor = (element.attribute( d->attrName ) == "mainToolBar");
00208
00209 QByteArray name = element.attribute( d->attrName ).toUtf8();
00210
00211 KToolBar *bar = static_cast<KToolBar*>(d->m_widget->findChild<KToolBar*>( name ));
00212 if( !bar )
00213 {
00214 bar = new KToolBar( d->m_widget, honor, false );
00215 bar->setObjectName(name);
00216 }
00217
00218 if ( qobject_cast<KMainWindow*>( d->m_widget ) )
00219 {
00220 if ( d->m_client && !d->m_client->xmlFile().isEmpty() )
00221 bar->setXMLGUIClient( d->m_client );
00222 }
00223
00224 bar->loadState( element );
00225
00226 return bar;
00227 }
00228
00229 if ( tagName == d->tagStatusBar ) {
00230 KMainWindow *mainWin = qobject_cast<KMainWindow *>(d->m_widget);
00231 if ( mainWin ) {
00232 mainWin->statusBar()->show();
00233 return mainWin->statusBar();
00234 }
00235 KStatusBar *bar = new KStatusBar( d->m_widget );
00236 return bar;
00237 }
00238
00239 return 0L;
00240 }
00241
00242 void KXMLGUIBuilder::removeContainer( QWidget *container, QWidget *parent, QDomElement &element, QAction* containerAction )
00243 {
00244
00245
00246 if ( qobject_cast<QMenu*>( container ) )
00247 {
00248 if ( parent ) {
00249 parent->removeAction( containerAction );
00250 }
00251
00252 delete container;
00253 }
00254 else if ( qobject_cast<KToolBar*>( container ) )
00255 {
00256 KToolBar *tb = static_cast<KToolBar *>( container );
00257
00258 tb->saveState( element );
00259 delete tb;
00260 }
00261 else if ( qobject_cast<KMenuBar*>( container ) )
00262 {
00263 KMenuBar *mb = static_cast<KMenuBar *>( container );
00264 mb->hide();
00265
00266
00267
00268
00269 }
00270 else if ( qobject_cast<KStatusBar*>( container ) )
00271 {
00272 if ( qobject_cast<KMainWindow*>( d->m_widget ) )
00273 container->hide();
00274 else
00275 delete static_cast<KStatusBar *>(container);
00276 }
00277 else
00278 kWarning() << "Unhandled container to remove : " << container->metaObject()->className();
00279 }
00280
00281 QStringList KXMLGUIBuilder::customTags() const
00282 {
00283 QStringList res;
00284 res << d->tagSeparator << d->tagTearOffHandle << d->tagMenuTitle;
00285 return res;
00286 }
00287
00288 QAction* KXMLGUIBuilder::createCustomElement( QWidget *parent, int index, const QDomElement &element )
00289 {
00290 QAction* before = 0L;
00291 if (index > 0 && index < parent->actions().count())
00292 before = parent->actions().at(index);
00293
00294 if ( element.tagName().toLower() == d->tagSeparator )
00295 {
00296 if ( QMenu *menu = qobject_cast<QMenu*>( parent ) )
00297 {
00298
00299
00300 return menu->insertSeparator( before );
00301 }
00302 else if ( QMenuBar* bar = qobject_cast<QMenuBar*>( parent ) )
00303 {
00304 QAction* separatorAction = new QAction(bar);
00305 separatorAction->setSeparator(true);
00306 bar->insertAction( before, separatorAction );
00307 return separatorAction;
00308 }
00309 else if ( KToolBar *bar = qobject_cast<KToolBar*>( parent ) )
00310 {
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 return bar->insertSeparator( before );
00333 }
00334 }
00335 else if ( element.tagName().toLower() == d->tagTearOffHandle )
00336 {
00337 static_cast<QMenu *>(parent)->setTearOffEnabled(true);
00338 }
00339 else if ( element.tagName().toLower() == d->tagMenuTitle )
00340 {
00341 if ( KMenu* m = qobject_cast<KMenu*>( parent ) )
00342 {
00343 QString i18nText;
00344 QByteArray text = element.text().toUtf8();
00345
00346 if ( text.isEmpty() )
00347 i18nText = i18n( "No text" );
00348 else
00349 i18nText = i18n( text );
00350
00351 QString icon = element.attribute( d->attrIcon );
00352 KIcon pix;
00353
00354 if ( !icon.isEmpty() )
00355 {
00356 pix = KIcon( icon );
00357 }
00358
00359 if ( !icon.isEmpty() ) {
00360 return m->addTitle( pix, i18nText, before );
00361 } else {
00362 return m->addTitle( i18nText, before );
00363 }
00364 }
00365 }
00366
00367 QAction* blank = new QAction(parent);
00368 blank->setVisible(false);
00369 parent->insertAction(before, blank);
00370 return blank;
00371 }
00372
00373 void KXMLGUIBuilder::removeCustomElement( QWidget *parent, QAction* action )
00374 {
00375 parent->removeAction(action);
00376 }
00377
00378 KXMLGUIClient *KXMLGUIBuilder::builderClient() const
00379 {
00380 return d->m_client;
00381 }
00382
00383 void KXMLGUIBuilder::setBuilderClient( KXMLGUIClient *client )
00384 {
00385 d->m_client = client;
00386 if ( client )
00387 setBuilderComponentData( client->componentData() );
00388 }
00389
00390 KComponentData KXMLGUIBuilder::builderComponentData() const
00391 {
00392 return d->m_componentData;
00393 }
00394
00395 void KXMLGUIBuilder::setBuilderComponentData(const KComponentData &componentData)
00396 {
00397 d->m_componentData = componentData;
00398 }
00399
00400 void KXMLGUIBuilder::finalizeGUI( KXMLGUIClient * )
00401 {
00402 KXmlGuiWindow* window = qobject_cast<KXmlGuiWindow*>(d->m_widget);
00403 if (!window)
00404 return;
00405 #if 0
00406 KToolBar *toolbar = 0;
00407 QListIterator<KToolBar> it( ( (KMainWindow*)d->m_widget )->toolBarIterator() );
00408 while ( ( toolbar = it.current() ) ) {
00409 kDebug(260) << "KXMLGUIBuilder::finalizeGUI toolbar=" << (void*)toolbar;
00410 ++it;
00411 toolbar->positionYourself();
00412 }
00413 #else
00414 window->finalizeGUI( false );
00415 #endif
00416 }
00417
00418 void KXMLGUIBuilder::virtual_hook( int, void* )
00419 { }
00420