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 }
00128
00129 bool KXmlGuiWindow::event( QEvent* ev )
00130 {
00131 bool ret = KMainWindow::event(ev);
00132 if (ev->type()==QEvent::Polish) {
00133 QDBusConnection::sessionBus().registerObject(dbusName() + "/actions", actionCollection(),
00134 QDBusConnection::ExportScriptableSlots |
00135 QDBusConnection::ExportScriptableProperties |
00136 QDBusConnection::ExportNonScriptableSlots |
00137 QDBusConnection::ExportNonScriptableProperties |
00138 QDBusConnection::ExportChildObjects);
00139 }
00140 return ret;
00141 }
00142
00143 void KXmlGuiWindow::setHelpMenuEnabled(bool showHelpMenu)
00144 {
00145 K_D(KXmlGuiWindow);
00146 d->showHelpMenu = showHelpMenu;
00147 }
00148
00149 bool KXmlGuiWindow::isHelpMenuEnabled() const
00150 {
00151 K_D(const KXmlGuiWindow);
00152 return d->showHelpMenu;
00153 }
00154
00155 KXMLGUIFactory *KXmlGuiWindow::guiFactory()
00156 {
00157 K_D(KXmlGuiWindow);
00158 if (!d->factory) {
00159 d->factory = new KXMLGUIFactory( this, this );
00160 connect(d->factory, SIGNAL(makingChanges(bool)),
00161 this, SLOT(_k_slotFactoryMakingChanges(bool)));
00162 }
00163 return d->factory;
00164 }
00165
00166 void KXmlGuiWindow::configureToolbars()
00167 {
00168 K_D(KXmlGuiWindow);
00169 KConfigGroup cg(KGlobal::config(), QString());
00170 saveMainWindowSettings(cg);
00171 if (!d->toolBarEditor) {
00172 d->toolBarEditor = new KEditToolBar(guiFactory(), this);
00173 d->toolBarEditor->setAttribute(Qt::WA_DeleteOnClose);
00174 connect(d->toolBarEditor, SIGNAL(newToolBarConfig()), SLOT(saveNewToolbarConfig()));
00175 }
00176 d->toolBarEditor->show();
00177 }
00178
00179 void KXmlGuiWindow::saveNewToolbarConfig()
00180 {
00181
00182
00183 guiFactory()->removeClient(this);
00184 guiFactory()->addClient(this);
00185
00186 KConfigGroup cg(KGlobal::config(), QString());
00187 applyMainWindowSettings(cg);
00188 }
00189
00190 void KXmlGuiWindow::setupGUI( StandardWindowOptions options, const QString & xmlfile ) {
00191 setupGUI(QSize(), options, xmlfile);
00192 }
00193
00194 void KXmlGuiWindow::setupGUI( const QSize & defaultSize, StandardWindowOptions options, const QString & xmlfile ) {
00195 K_D(KXmlGuiWindow);
00196
00197 if( options & Keys ){
00198 KStandardAction::keyBindings(guiFactory(),
00199 SLOT(configureShortcuts()), actionCollection());
00200 }
00201
00202 if( (options & StatusBar) && statusBar() ){
00203 createStandardStatusBarAction();
00204 }
00205
00206 if( options & ToolBar ){
00207 setStandardToolBarMenuEnabled( true );
00208 KStandardAction::configureToolbars(this,
00209 SLOT(configureToolbars() ), actionCollection());
00210 }
00211
00212 d->defaultSize = defaultSize;
00213
00214 if( options & Create ){
00215 createGUI(xmlfile);
00216 }
00217
00218 if (initialGeometrySet()) {
00219
00220 }
00221 else if (d->defaultSize.isValid()) {
00222 resize(d->defaultSize);
00223 }
00224 else if (isHidden()) {
00225 adjustSize();
00226 }
00227
00228 if( options & Save ){
00229 const KConfigGroup cg(autoSaveConfigGroup());
00230 if (cg.isValid()) {
00231 setAutoSaveSettings(cg);
00232 } else {
00233 setAutoSaveSettings();
00234 }
00235 }
00236 }
00237 void KXmlGuiWindow::createGUI( const QString &xmlfile )
00238 {
00239 K_D(KXmlGuiWindow);
00240
00241
00242
00243
00244 guiFactory()->removeClient( this );
00245
00246
00247 QMenuBar* mb = menuBar();
00248 if ( mb )
00249 mb->clear();
00250
00251 qDeleteAll( toolBars() );
00252
00253
00254 if (d->showHelpMenu) {
00255 delete d->helpMenu;
00256
00257 d->helpMenu = new KHelpMenu(this, componentData().aboutData(), true, actionCollection());
00258 }
00259
00260 const QString windowXmlFile = xmlfile.isNull() ? componentData().componentName() + "ui.rc" : xmlfile;
00261
00262
00263 if (!xmlFile().isEmpty() && xmlFile() != windowXmlFile) {
00264 kWarning() << "You called setXMLFile(" << xmlFile() << ") and then createGUI or setupGUI,"
00265 << "which also calls setXMLFile and will overwrite the file you have previously set.\n"
00266 << "You should call createGUI("<<xmlFile()<<") or setupGUI(<options>,"<<xmlFile()<<") instead.";
00267 }
00268
00269
00270 setXMLFile(KStandardDirs::locate("config", "ui/ui_standards.rc", componentData()));
00271
00272
00273 setXMLFile(windowXmlFile, true);
00274
00275
00276 setXMLGUIBuildDocument( QDomDocument() );
00277
00278
00279 guiFactory()->addClient( this );
00280
00281
00282 }
00283
00284 void KXmlGuiWindow::slotStateChanged(const QString &newstate)
00285 {
00286 stateChanged(newstate, KXMLGUIClient::StateNoReverse);
00287 }
00288
00289 void KXmlGuiWindow::slotStateChanged(const QString &newstate,
00290 bool reverse)
00291 {
00292 stateChanged(newstate,
00293 reverse ? KXMLGUIClient::StateReverse : KXMLGUIClient::StateNoReverse);
00294 }
00295
00296 void KXmlGuiWindow::setStandardToolBarMenuEnabled( bool enable )
00297 {
00298 K_D(KXmlGuiWindow);
00299 if ( enable ) {
00300 if ( d->toolBarHandler )
00301 return;
00302
00303 d->toolBarHandler = new KDEPrivate::ToolBarHandler( this );
00304
00305 if ( factory() )
00306 factory()->addClient( d->toolBarHandler );
00307 } else {
00308 if ( !d->toolBarHandler )
00309 return;
00310
00311 if ( factory() )
00312 factory()->removeClient( d->toolBarHandler );
00313
00314 delete d->toolBarHandler;
00315 d->toolBarHandler = 0;
00316 }
00317 }
00318
00319 bool KXmlGuiWindow::isStandardToolBarMenuEnabled() const
00320 {
00321 K_D(const KXmlGuiWindow);
00322 return ( d->toolBarHandler );
00323 }
00324
00325 void KXmlGuiWindow::createStandardStatusBarAction(){
00326 K_D(KXmlGuiWindow);
00327 if(!d->showStatusBarAction){
00328 d->showStatusBarAction = KStandardAction::showStatusbar(this, SLOT(setSettingsDirty()), actionCollection());
00329 KStatusBar *sb = statusBar();
00330 connect(d->showStatusBarAction, SIGNAL(toggled(bool)), sb, SLOT(setVisible(bool)));
00331 d->showStatusBarAction->setChecked(sb->isHidden());
00332 } else {
00333
00334 KAction *tmpStatusBar = KStandardAction::showStatusbar(NULL, NULL, NULL);
00335 d->showStatusBarAction->setText(tmpStatusBar->text());
00336 d->showStatusBarAction->setWhatsThis(tmpStatusBar->whatsThis());
00337 delete tmpStatusBar;
00338 }
00339 }
00340
00341 void KXmlGuiWindow::finalizeGUI( bool )
00342 {
00343
00344
00345
00346
00347 if (autoSaveSettings() && autoSaveConfigGroup().isValid()) {
00348 applyMainWindowSettings(autoSaveConfigGroup());
00349 }
00350 }
00351
00352 void KXmlGuiWindow::applyMainWindowSettings(const KConfigGroup &config, bool force)
00353 {
00354 K_D(KXmlGuiWindow);
00355 KMainWindow::applyMainWindowSettings(config, force);
00356 KStatusBar *sb = qFindChild<KStatusBar *>(this);
00357 if (sb && d->showStatusBarAction)
00358 d->showStatusBarAction->setChecked(!sb->isHidden());
00359 }
00360
00361
00362
00363 void KXmlGuiWindow::finalizeGUI( KXMLGUIClient *client )
00364 { KXMLGUIBuilder::finalizeGUI( client ); }
00365
00366 #include "kxmlguiwindow.moc"
00367