KDEUI
kxmlguiwindow.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
00022
00023
00024
00025
00026 #include "kxmlguiwindow.h"
00027 #include "kmainwindow_p.h"
00028 #include "kactioncollection.h"
00029 #include "kmainwindowiface_p.h"
00030 #include "ktoolbarhandler.h"
00031 #include "kwhatsthismanager_p.h"
00032 #include "kxmlguifactory.h"
00033 #include "kcmdlineargs.h"
00034 #include "ktoggleaction.h"
00035 #include "ksessionmanager.h"
00036 #include "kstandardaction.h"
00037
00038 #include <config.h>
00039
00040 #include <QCloseEvent>
00041 #include <QDesktopWidget>
00042 #include <QDockWidget>
00043 #include <QtXml/QDomDocument>
00044 #include <QtGui/QLayout>
00045 #include <QtCore/QObject>
00046 #include <QtGui/QSessionManager>
00047 #include <QtGui/QStyle>
00048 #include <QtCore/QTimer>
00049 #include <QtGui/QWidget>
00050 #include <QtCore/QList>
00051 #include <kaction.h>
00052 #include <kapplication.h>
00053 #include <kauthorized.h>
00054 #include <kconfig.h>
00055 #include <kdebug.h>
00056 #include <kedittoolbar.h>
00057 #include <khelpmenu.h>
00058 #include <klocale.h>
00059 #include <kmenubar.h>
00060 #include <kstandarddirs.h>
00061 #include <kstatusbar.h>
00062 #include <ktoolbar.h>
00063 #include <kwindowsystem.h>
00064 #include <kconfiggroup.h>
00065
00066 #if defined Q_WS_X11
00067 #include <qx11info_x11.h>
00068 #include <netwm.h>
00069 #include <kstartupinfo.h>
00070 #endif
00071
00072 #include <stdlib.h>
00073 #include <ctype.h>
00074 #include <assert.h>
00075
00076 class KXmlGuiWindowPrivate : public KMainWindowPrivate {
00077 public:
00078 void _k_slotFactoryMakingChanges(bool b)
00079 {
00080
00081
00082
00083 letDirtySettings = !b;
00084 }
00085
00086 bool showHelpMenu:1;
00087 QSize defaultSize;
00088
00089 KDEPrivate::ToolBarHandler *toolBarHandler;
00090 KToggleAction *showStatusBarAction;
00091 QPointer<KEditToolBar> toolBarEditor;
00092 KXMLGUIFactory *factory;
00093 };
00094
00095 KXmlGuiWindow::KXmlGuiWindow( QWidget* parent, Qt::WFlags f )
00096 : KMainWindow(*new KXmlGuiWindowPrivate, parent, f), KXMLGUIBuilder( this )
00097 {
00098 K_D(KXmlGuiWindow);
00099 d->showHelpMenu = true;
00100 d->toolBarHandler = 0;
00101 d->showStatusBarAction = 0;
00102 d->factory = 0;
00103 new KMainWindowInterface(this);
00104 }
00105
00106
00107 QAction *KXmlGuiWindow::toolBarMenuAction()
00108 {
00109 K_D(KXmlGuiWindow);
00110 if ( !d->toolBarHandler )
00111 return 0;
00112
00113 return d->toolBarHandler->toolBarMenuAction();
00114 }
00115
00116
00117 void KXmlGuiWindow::setupToolbarMenuActions()
00118 {
00119 K_D(KXmlGuiWindow);
00120 if ( d->toolBarHandler )
00121 d->toolBarHandler->setupActions();
00122 }
00123
00124
00125 KXmlGuiWindow::~KXmlGuiWindow()
00126 {
00127 K_D(KXmlGuiWindow);
00128 delete d->factory;
00129 }
00130
00131 bool KXmlGuiWindow::event( QEvent* ev )
00132 {
00133 bool ret = KMainWindow::event(ev);
00134 if (ev->type()==QEvent::Polish) {
00135 QDBusConnection::sessionBus().registerObject(dbusName() + "/actions", actionCollection(),
00136 QDBusConnection::ExportScriptableSlots |
00137 QDBusConnection::ExportScriptableProperties |
00138 QDBusConnection::ExportNonScriptableSlots |
00139 QDBusConnection::ExportNonScriptableProperties |
00140 QDBusConnection::ExportChildObjects);
00141 }
00142 return ret;
00143 }
00144
00145 void KXmlGuiWindow::setHelpMenuEnabled(bool showHelpMenu)
00146 {
00147 K_D(KXmlGuiWindow);
00148 d->showHelpMenu = showHelpMenu;
00149 }
00150
00151 bool KXmlGuiWindow::isHelpMenuEnabled() const
00152 {
00153 K_D(const KXmlGuiWindow);
00154 return d->showHelpMenu;
00155 }
00156
00157 KXMLGUIFactory *KXmlGuiWindow::guiFactory()
00158 {
00159 K_D(KXmlGuiWindow);
00160 if (!d->factory) {
00161 d->factory = new KXMLGUIFactory( this, this );
00162 connect(d->factory, SIGNAL(makingChanges(bool)),
00163 this, SLOT(_k_slotFactoryMakingChanges(bool)));
00164 }
00165 return d->factory;
00166 }
00167
00168 void KXmlGuiWindow::configureToolbars()
00169 {
00170 K_D(KXmlGuiWindow);
00171 KConfigGroup cg(KGlobal::config(), QString());
00172 saveMainWindowSettings(cg);
00173 if (!d->toolBarEditor) {
00174 d->toolBarEditor = new KEditToolBar(guiFactory(), this);
00175 d->toolBarEditor->setAttribute(Qt::WA_DeleteOnClose);
00176 connect(d->toolBarEditor, SIGNAL(newToolBarConfig()), SLOT(saveNewToolbarConfig()));
00177 }
00178 d->toolBarEditor->show();
00179 }
00180
00181 void KXmlGuiWindow::saveNewToolbarConfig()
00182 {
00183
00184
00185 guiFactory()->removeClient(this);
00186 guiFactory()->addClient(this);
00187
00188 KConfigGroup cg(KGlobal::config(), QString());
00189 applyMainWindowSettings(cg);
00190 }
00191
00192 void KXmlGuiWindow::setupGUI( StandardWindowOptions options, const QString & xmlfile ) {
00193 setupGUI(QSize(), options, xmlfile);
00194 }
00195
00196 void KXmlGuiWindow::setupGUI( const QSize & defaultSize, StandardWindowOptions options, const QString & xmlfile ) {
00197 K_D(KXmlGuiWindow);
00198
00199 if( options & Keys ){
00200 KStandardAction::keyBindings(guiFactory(),
00201 SLOT(configureShortcuts()), actionCollection());
00202 }
00203
00204 if( (options & StatusBar) && statusBar() ){
00205 createStandardStatusBarAction();
00206 }
00207
00208 if( options & ToolBar ){
00209 setStandardToolBarMenuEnabled( true );
00210 KStandardAction::configureToolbars(this,
00211 SLOT(configureToolbars() ), actionCollection());
00212 }
00213
00214 d->defaultSize = defaultSize;
00215
00216 if( options & Create ){
00217 createGUI(xmlfile);
00218 }
00219
00220 if (initialGeometrySet()) {
00221
00222 }
00223 else if (d->defaultSize.isValid()) {
00224 resize(d->defaultSize);
00225 }
00226 else if (isHidden()) {
00227 adjustSize();
00228 }
00229
00230 if( options & Save ){
00231 const KConfigGroup cg(autoSaveConfigGroup());
00232 if (cg.isValid()) {
00233 setAutoSaveSettings(cg);
00234 } else {
00235 setAutoSaveSettings();
00236 }
00237 }
00238 }
00239 void KXmlGuiWindow::createGUI( const QString &xmlfile )
00240 {
00241 K_D(KXmlGuiWindow);
00242
00243
00244
00245
00246 guiFactory()->removeClient( this );
00247
00248
00249 QMenuBar* mb = menuBar();
00250 if ( mb )
00251 mb->clear();
00252
00253 qDeleteAll( toolBars() );
00254
00255
00256 if (d->showHelpMenu) {
00257 delete d->helpMenu;
00258
00259 d->helpMenu = new KHelpMenu(this, componentData().aboutData(), true, actionCollection());
00260 }
00261
00262 const QString windowXmlFile = xmlfile.isNull() ? componentData().componentName() + "ui.rc" : xmlfile;
00263
00264
00265 if (!xmlFile().isEmpty() && xmlFile() != windowXmlFile) {
00266 kWarning() << "You called setXMLFile(" << xmlFile() << ") and then createGUI or setupGUI,"
00267 << "which also calls setXMLFile and will overwrite the file you have previously set.\n"
00268 << "You should call createGUI("<<xmlFile()<<") or setupGUI(<options>,"<<xmlFile()<<") instead.";
00269 }
00270
00271
00272 setXMLFile(KStandardDirs::locate("config", "ui/ui_standards.rc", componentData()));
00273
00274
00275 setXMLFile(windowXmlFile, true);
00276
00277
00278 setXMLGUIBuildDocument( QDomDocument() );
00279
00280
00281 guiFactory()->addClient( this );
00282
00283
00284 }
00285
00286 void KXmlGuiWindow::slotStateChanged(const QString &newstate)
00287 {
00288 stateChanged(newstate, KXMLGUIClient::StateNoReverse);
00289 }
00290
00291 void KXmlGuiWindow::slotStateChanged(const QString &newstate,
00292 bool reverse)
00293 {
00294 stateChanged(newstate,
00295 reverse ? KXMLGUIClient::StateReverse : KXMLGUIClient::StateNoReverse);
00296 }
00297
00298 void KXmlGuiWindow::setStandardToolBarMenuEnabled( bool enable )
00299 {
00300 K_D(KXmlGuiWindow);
00301 if ( enable ) {
00302 if ( d->toolBarHandler )
00303 return;
00304
00305 d->toolBarHandler = new KDEPrivate::ToolBarHandler( this );
00306
00307 if ( factory() )
00308 factory()->addClient( d->toolBarHandler );
00309 } else {
00310 if ( !d->toolBarHandler )
00311 return;
00312
00313 if ( factory() )
00314 factory()->removeClient( d->toolBarHandler );
00315
00316 delete d->toolBarHandler;
00317 d->toolBarHandler = 0;
00318 }
00319 }
00320
00321 bool KXmlGuiWindow::isStandardToolBarMenuEnabled() const
00322 {
00323 K_D(const KXmlGuiWindow);
00324 return ( d->toolBarHandler );
00325 }
00326
00327 void KXmlGuiWindow::createStandardStatusBarAction(){
00328 K_D(KXmlGuiWindow);
00329 if(!d->showStatusBarAction){
00330 d->showStatusBarAction = KStandardAction::showStatusbar(this, SLOT(setSettingsDirty()), actionCollection());
00331 KStatusBar *sb = statusBar();
00332 connect(d->showStatusBarAction, SIGNAL(toggled(bool)), sb, SLOT(setVisible(bool)));
00333 d->showStatusBarAction->setChecked(sb->isHidden());
00334 } else {
00335
00336 KAction *tmpStatusBar = KStandardAction::showStatusbar(NULL, NULL, NULL);
00337 d->showStatusBarAction->setText(tmpStatusBar->text());
00338 d->showStatusBarAction->setWhatsThis(tmpStatusBar->whatsThis());
00339 delete tmpStatusBar;
00340 }
00341 }
00342
00343 void KXmlGuiWindow::finalizeGUI( bool )
00344 {
00345
00346
00347
00348
00349 if (autoSaveSettings() && autoSaveConfigGroup().isValid()) {
00350 applyMainWindowSettings(autoSaveConfigGroup());
00351 }
00352 }
00353
00354 void KXmlGuiWindow::applyMainWindowSettings(const KConfigGroup &config, bool force)
00355 {
00356 K_D(KXmlGuiWindow);
00357 KMainWindow::applyMainWindowSettings(config, force);
00358 KStatusBar *sb = qFindChild<KStatusBar *>(this);
00359 if (sb && d->showStatusBarAction)
00360 d->showStatusBarAction->setChecked(!sb->isHidden());
00361 }
00362
00363
00364
00365 void KXmlGuiWindow::finalizeGUI( KXMLGUIClient *client )
00366 { KXMLGUIBuilder::finalizeGUI( client ); }
00367
00368 #include "kxmlguiwindow.moc"
00369