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

KDEUI

kxmlguiwindow.cpp

Go to the documentation of this file.
00001  /* This file is part of the KDE libraries
00002      Copyright
00003      (C) 2000 Reginald Stadlbauer (reggie@kde.org)
00004      (C) 1997 Stephan Kulow (coolo@kde.org)
00005      (C) 1997-2000 Sven Radej (radej@kde.org)
00006      (C) 1997-2000 Matthias Ettrich (ettrich@kde.org)
00007      (C) 1999 Chris Schlaeger (cs@kde.org)
00008      (C) 2002 Joseph Wenninger (jowenn@kde.org)
00009      (C) 2005-2006 Hamish Rodda (rodda@kde.org)
00010 
00011      This library is free software; you can redistribute it and/or
00012      modify it under the terms of the GNU Library General Public
00013      License version 2 as published by the Free Software Foundation.
00014 
00015      This library is distributed in the hope that it will be useful,
00016      but WITHOUT ANY WARRANTY; without even the implied warranty of
00017      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018      Library General Public License for more details.
00019 
00020      You should have received a copy of the GNU Library General Public License
00021      along with this library; see the file COPYING.LIB.  If not, write to
00022      the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00023      Boston, MA 02110-1301, USA.
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     bool showHelpMenu:1;
00079 
00080     KDEPrivate::ToolBarHandler *toolBarHandler;
00081     KToggleAction *showStatusBarAction;
00082     QPointer<KEditToolBar> toolBarEditor;
00083     KXMLGUIFactory *factory;
00084 };
00085 
00086 KXmlGuiWindow::KXmlGuiWindow( QWidget* parent, Qt::WFlags f )
00087     : KMainWindow(*new KXmlGuiWindowPrivate, parent, f), KXMLGUIBuilder( this )
00088 {
00089     K_D(KXmlGuiWindow);
00090     d->showHelpMenu = true;
00091     d->toolBarHandler = 0;
00092     d->showStatusBarAction = 0;
00093     d->factory = 0;
00094     new KMainWindowInterface(this);
00095 }
00096 
00097 
00098 QAction *KXmlGuiWindow::toolBarMenuAction()
00099 {
00100     K_D(KXmlGuiWindow);
00101     if ( !d->toolBarHandler )
00102     return 0;
00103 
00104     return d->toolBarHandler->toolBarMenuAction();
00105 }
00106 
00107 
00108 void KXmlGuiWindow::setupToolbarMenuActions()
00109 {
00110     K_D(KXmlGuiWindow);
00111     if ( d->toolBarHandler )
00112         d->toolBarHandler->setupActions();
00113 }
00114 
00115 
00116 KXmlGuiWindow::~KXmlGuiWindow()
00117 {
00118 }
00119 
00120 bool KXmlGuiWindow::event( QEvent* ev )
00121 {
00122     bool ret = KMainWindow::event(ev);
00123     if (ev->type()==QEvent::Polish) {
00124         QDBusConnection::sessionBus().registerObject(dbusName() + "/actions", actionCollection(),
00125                                                      QDBusConnection::ExportScriptableSlots |
00126                                                      QDBusConnection::ExportScriptableProperties |
00127                                                      QDBusConnection::ExportNonScriptableSlots |
00128                                                      QDBusConnection::ExportNonScriptableProperties |
00129                                                      QDBusConnection::ExportChildObjects);
00130     }
00131     return ret;
00132 }
00133 
00134 void KXmlGuiWindow::setHelpMenuEnabled(bool showHelpMenu)
00135 {
00136     K_D(KXmlGuiWindow);
00137     d->showHelpMenu = showHelpMenu;
00138 }
00139 
00140 bool KXmlGuiWindow::isHelpMenuEnabled() const
00141 {
00142     K_D(const KXmlGuiWindow);
00143     return d->showHelpMenu;
00144 }
00145 
00146 KXMLGUIFactory *KXmlGuiWindow::guiFactory()
00147 {
00148     K_D(KXmlGuiWindow);
00149     if (!d->factory)
00150         d->factory = new KXMLGUIFactory( this, this );
00151     return d->factory;
00152 }
00153 
00154 void KXmlGuiWindow::configureToolbars()
00155 {
00156     K_D(KXmlGuiWindow);
00157     KConfigGroup cg(KGlobal::config(), QString());
00158     saveMainWindowSettings(cg);
00159     if (!d->toolBarEditor) {
00160       d->toolBarEditor = new KEditToolBar(guiFactory(), this);
00161       d->toolBarEditor->setAttribute(Qt::WA_DeleteOnClose);
00162       connect(d->toolBarEditor, SIGNAL(newToolBarConfig()), SLOT(saveNewToolbarConfig()));
00163     }
00164     d->toolBarEditor->show();
00165 }
00166 
00167 void KXmlGuiWindow::saveNewToolbarConfig()
00168 {
00169     // createGUI(xmlFile()); // this loses any plugged-in guiclients, so we use remove+add instead.
00170 
00171     guiFactory()->removeClient(this);
00172     guiFactory()->addClient(this);
00173 
00174     KConfigGroup cg(KGlobal::config(), QString());
00175     applyMainWindowSettings(cg);
00176 }
00177 
00178 void KXmlGuiWindow::setupGUI( StandardWindowOptions options, const QString & xmlfile ) {
00179     setupGUI(QSize(), options, xmlfile);
00180 }
00181 
00182 void KXmlGuiWindow::setupGUI( const QSize & defaultSize, StandardWindowOptions options, const QString & xmlfile ) {
00183     if( options & Keys ){
00184         KStandardAction::keyBindings(guiFactory(),
00185                     SLOT(configureShortcuts()), actionCollection());
00186     }
00187 
00188     if( (options & StatusBar) && statusBar() ){
00189         createStandardStatusBarAction();
00190     }
00191 
00192     if( options & ToolBar ){
00193         setStandardToolBarMenuEnabled( true );
00194         KStandardAction::configureToolbars(this,
00195                       SLOT(configureToolbars() ), actionCollection());
00196     }
00197 
00198     if( options & Create ){
00199         createGUI(xmlfile);
00200     }
00201 
00202     if( options & Save ){
00203         // setupGUI() is typically called in the constructor before show(),
00204         // so the default window size will be incorrect unless the application
00205         // hard coded the size which they should try not to do (i.e. use
00206         // size hints).
00207         if(initialGeometrySet())
00208         {
00209           // Do nothing...
00210         }
00211         else if(defaultSize.isValid())
00212         {
00213           resize(defaultSize);
00214         }
00215         else if(isHidden())
00216         {
00217           adjustSize();
00218         }
00219         setAutoSaveSettings();
00220     }
00221 
00222 }
00223 
00224 void KXmlGuiWindow::createGUI( const QString &xmlfile )
00225 {
00226     K_D(KXmlGuiWindow);
00227     // disabling the updates prevents unnecessary redraws
00228     //setUpdatesEnabled( false );
00229 
00230     // just in case we are rebuilding, let's remove our old client
00231     guiFactory()->removeClient( this );
00232 
00233     // make sure to have an empty GUI
00234     QMenuBar* mb = menuBar();
00235     if ( mb )
00236         mb->clear();
00237 
00238     qDeleteAll( toolBars() ); // delete all toolbars
00239 
00240     // don't build a help menu unless the user ask for it
00241     if (d->showHelpMenu) {
00242         delete d->helpMenu;
00243         // we always want a help menu
00244         d->helpMenu = new KHelpMenu(this, componentData().aboutData(), true, actionCollection());
00245     }
00246 
00247     // we always want to load in our global standards file
00248     setXMLFile(KStandardDirs::locate("config", "ui/ui_standards.rc", componentData()));
00249 
00250     // now, merge in our local xml file.  if this is null, then that
00251     // means that we will be only using the global file
00252     if ( !xmlfile.isNull() ) {
00253         setXMLFile( xmlfile, true );
00254     } else {
00255         QString auto_file(componentData().componentName() + "ui.rc");
00256         setXMLFile( auto_file, true );
00257     }
00258 
00259     // make sure we don't have any state saved already
00260     setXMLGUIBuildDocument( QDomDocument() );
00261 
00262     // do the actual GUI building
00263     guiFactory()->addClient( this );
00264 
00265     //  setUpdatesEnabled( true );
00266     updateGeometry();
00267 }
00268 
00269 void KXmlGuiWindow::slotStateChanged(const QString &newstate)
00270 {
00271   stateChanged(newstate, KXMLGUIClient::StateNoReverse);
00272 }
00273 
00274 void KXmlGuiWindow::slotStateChanged(const QString &newstate,
00275                                    bool reverse)
00276 {
00277   stateChanged(newstate,
00278                reverse ? KXMLGUIClient::StateReverse : KXMLGUIClient::StateNoReverse);
00279 }
00280 
00281 void KXmlGuiWindow::setStandardToolBarMenuEnabled( bool enable )
00282 {
00283     K_D(KXmlGuiWindow);
00284     if ( enable ) {
00285         if ( d->toolBarHandler )
00286             return;
00287 
00288         d->toolBarHandler = new KDEPrivate::ToolBarHandler( this );
00289 
00290         if ( factory() )
00291             factory()->addClient( d->toolBarHandler );
00292     } else {
00293         if ( !d->toolBarHandler )
00294             return;
00295 
00296         if ( factory() )
00297             factory()->removeClient( d->toolBarHandler );
00298 
00299         delete d->toolBarHandler;
00300         d->toolBarHandler = 0;
00301     }
00302 }
00303 
00304 bool KXmlGuiWindow::isStandardToolBarMenuEnabled() const
00305 {
00306     K_D(const KXmlGuiWindow);
00307     return ( d->toolBarHandler );
00308 }
00309 
00310 void KXmlGuiWindow::createStandardStatusBarAction(){
00311     K_D(KXmlGuiWindow);
00312     if(!d->showStatusBarAction){
00313         d->showStatusBarAction = KStandardAction::showStatusbar(this, SLOT(setSettingsDirty()), actionCollection());
00314         KStatusBar *sb = statusBar(); // Creates statusbar if it doesn't exist already.
00315         connect(d->showStatusBarAction, SIGNAL(toggled(bool)), sb, SLOT(setVisible(bool)));
00316         d->showStatusBarAction->setChecked(sb->isHidden());
00317     } else {
00318         // If the language has changed, we'll need to grab the new text and whatsThis
00319     KAction *tmpStatusBar = KStandardAction::showStatusbar(NULL, NULL, NULL);
00320         d->showStatusBarAction->setText(tmpStatusBar->text());
00321         d->showStatusBarAction->setWhatsThis(tmpStatusBar->whatsThis());
00322     delete tmpStatusBar;
00323     }
00324 }
00325 
00326 void KXmlGuiWindow::finalizeGUI( bool /*force*/ )
00327 {
00328     //kDebug(260) << "KXmlGuiWindow::finalizeGUI force=" << force;
00329     // The whole reason for this is that moveToolBar relies on the indexes
00330     // of the other toolbars, so in theory it should be called only once per
00331     // toolbar, but in increasing order of indexes.
00332     // Since we can't do that immediately, we move them, and _then_
00333     // we call positionYourself again for each of them, but this time
00334     // the toolbariterator should give them in the proper order.
00335     // Both the XMLGUI and applySettings call this, hence "force" for the latter.
00336     /* FIXME KAction port - not needed?
00337     foreach (KToolBar* toolbar, toolBars()) {
00338         toolbar->positionYourself( force );
00339     }*/
00340 
00341     //d->settingsDirty = false;
00342 }
00343 
00344 void KXmlGuiWindow::applyMainWindowSettings(const KConfigGroup &config, bool force)
00345 {
00346     K_D(KXmlGuiWindow);
00347     KMainWindow::applyMainWindowSettings(config, force);
00348     KStatusBar *sb = qFindChild<KStatusBar *>(this);
00349     if (sb && d->showStatusBarAction)
00350         d->showStatusBarAction->setChecked(!sb->isHidden());
00351 }
00352 
00353 // KDE5 TODO: change it to "using KXMLGUIBuilder::finalizeGUI;" in the header
00354 // and remove the reimplementation
00355 void KXmlGuiWindow::finalizeGUI( KXMLGUIClient *client )
00356 { KXMLGUIBuilder::finalizeGUI( client ); }
00357 
00358 #include "kxmlguiwindow.moc"
00359 

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