• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

khelpmenu.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of the KDE Libraries
00003  * Copyright (C) 1999-2000 Espen Sand (espen@kde.org)
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  *
00020  */
00021 
00022 // I (espen) prefer that header files are included alphabetically
00023 
00024 #include "khelpmenu.h"
00025 
00026 #include <QtCore/QTimer>
00027 #include <QtGui/QLabel>
00028 #include <QtGui/QWidget>
00029 #include <QtGui/QWhatsThis>
00030 
00031 #include <kaboutapplicationdialog.h>
00032 #include <kaboutdata.h>
00033 #include <kaboutkdedialog_p.h>
00034 #include <kaction.h>
00035 #include <kactioncollection.h>
00036 #include <kapplication.h>
00037 #include <kauthorized.h>
00038 #include <kbugreport.h>
00039 #include <kdialog.h>
00040 #include <kguiitem.h>
00041 #include <khbox.h>
00042 #include <kiconloader.h>
00043 #include <klocale.h>
00044 #include <kmenu.h>
00045 #include <kstandardshortcut.h>
00046 #include <kstandardaction.h>
00047 #include <kstandardguiitem.h>
00048 #include <kswitchlanguagedialog_p.h>
00049 #include <ktoolinvocation.h>
00050 #include <kstandarddirs.h>
00051 
00052 #include <config.h>
00053 #ifdef Q_WS_X11
00054 #include <QX11EmbedWidget>
00055 #endif
00056 
00057 using namespace KDEPrivate;
00058 
00059 class KHelpMenuPrivate
00060 {
00061 public:
00062     KHelpMenuPrivate()
00063       : mSwitchApplicationLanguage(0),
00064     mActionsCreated(false),
00065         mSwitchApplicationLanguageAction(0)
00066     {
00067         mMenu = 0;
00068         mAboutApp = 0;
00069         mAboutKDE = 0;
00070         mBugReport = 0;
00071         mHandBookAction = 0;
00072         mWhatsThisAction = 0;
00073         mReportBugAction = 0;
00074         mAboutAppAction = 0;
00075         mAboutKDEAction = 0;
00076     }
00077     ~KHelpMenuPrivate()
00078     {
00079         delete mMenu;
00080         delete mAboutApp;
00081         delete mAboutKDE;
00082         delete mBugReport;
00083         delete mSwitchApplicationLanguage;
00084     }
00085 
00086     void createActions(KHelpMenu* q);
00087 
00088     KMenu *mMenu;
00089     KDialog *mAboutApp;
00090     KAboutKdeDialog *mAboutKDE;
00091     KBugReport *mBugReport;
00092     KSwitchLanguageDialog *mSwitchApplicationLanguage;
00093 
00094 // TODO evaluate if we use static_cast<QWidget*>(parent()) instead of mParent to win that bit of memory
00095     QWidget *mParent;
00096     QString mAboutAppText;
00097 
00098     bool mShowWhatsThis;
00099     bool mActionsCreated;
00100 
00101     KAction *mHandBookAction, *mWhatsThisAction;
00102     QAction *mReportBugAction, *mSwitchApplicationLanguageAction, *mAboutAppAction, *mAboutKDEAction;
00103 
00104     const KAboutData *mAboutData;
00105 };
00106 
00107 KHelpMenu::KHelpMenu( QWidget *parent, const QString &aboutAppText,
00108               bool showWhatsThis )
00109   : QObject(parent), d(new KHelpMenuPrivate)
00110 {
00111   d->mAboutAppText = aboutAppText;
00112   d->mShowWhatsThis = showWhatsThis;
00113   d->mParent = parent;
00114   d->mAboutData = 0;
00115 }
00116 
00117 KHelpMenu::KHelpMenu( QWidget *parent, const KAboutData *aboutData,
00118               bool showWhatsThis, KActionCollection *actions )
00119   : QObject(parent), d(new KHelpMenuPrivate)
00120 {
00121   d->mShowWhatsThis = showWhatsThis;
00122   d->mParent = parent;
00123   d->mAboutData = aboutData;
00124 
00125     if (actions) {
00126         d->createActions(this);
00127         if (d->mHandBookAction)
00128             actions->addAction(d->mHandBookAction->objectName(), d->mHandBookAction);
00129         if (d->mWhatsThisAction)
00130             actions->addAction(d->mWhatsThisAction->objectName(), d->mWhatsThisAction);
00131         if (d->mReportBugAction)
00132             actions->addAction(d->mReportBugAction->objectName(), d->mReportBugAction);
00133         if (d->mSwitchApplicationLanguageAction)
00134             actions->addAction(d->mSwitchApplicationLanguageAction->objectName(), d->mSwitchApplicationLanguageAction);
00135         if (d->mAboutAppAction)
00136             actions->addAction(d->mAboutAppAction->objectName(), d->mAboutAppAction);
00137         if (d->mAboutKDEAction)
00138             actions->addAction(d->mAboutKDEAction->objectName(), d->mAboutKDEAction);
00139     }
00140 }
00141 
00142 KHelpMenu::~KHelpMenu()
00143 {
00144   delete d;
00145 }
00146 
00147 void KHelpMenuPrivate::createActions(KHelpMenu* q)
00148 {
00149     if (mActionsCreated)
00150         return;
00151     mActionsCreated = true;
00152 
00153     if (KAuthorized::authorizeKAction("help_contents")) {
00154         mHandBookAction = KStandardAction::helpContents(q, SLOT(appHelpActivated()), q);
00155     }
00156     if (mShowWhatsThis && KAuthorized::authorizeKAction("help_whats_this")) {
00157         mWhatsThisAction = KStandardAction::whatsThis(q, SLOT(contextHelpActivated()), q);
00158     }
00159 
00160     const KAboutData *aboutData = mAboutData ? mAboutData : KGlobal::mainComponent().aboutData();
00161     if (KAuthorized::authorizeKAction("help_report_bug") && aboutData && !aboutData->bugAddress().isEmpty()) {
00162         mReportBugAction = KStandardAction::reportBug(q, SLOT(reportBug()), q);
00163     }
00164 
00165     if (KAuthorized::authorizeKAction("switch_application_language")) {
00166         if((KGlobal::dirs()->findAllResources("locale", QString::fromLatin1("*/entry.desktop"))).count() > 1) {
00167             mSwitchApplicationLanguageAction = KStandardAction::create(KStandardAction::SwitchApplicationLanguage, q, SLOT(switchApplicationLanguage()), q);
00168         }
00169     }
00170 
00171     if (KAuthorized::authorizeKAction("help_about_app")) {
00172         mAboutAppAction = KStandardAction::aboutApp(q, SLOT(aboutApplication()), q);
00173     }
00174 
00175     if (KAuthorized::authorizeKAction("help_about_kde")) {
00176         mAboutKDEAction = KStandardAction::aboutKDE(q, SLOT(aboutKDE()), q);
00177     }
00178 }
00179 
00180 // Used in the non-xml-gui case, like kfind or ksnapshot's help button.
00181 KMenu* KHelpMenu::menu()
00182 {
00183   if( !d->mMenu )
00184   {
00185     d->mMenu = new KMenu();
00186     connect( d->mMenu, SIGNAL(destroyed()), this, SLOT(menuDestroyed()));
00187 
00188     d->mMenu->setTitle(i18n("&Help"));
00189 
00190     d->createActions(this);
00191 
00192     bool need_separator = false;
00193     if (d->mHandBookAction) {
00194       d->mMenu->addAction(d->mHandBookAction);
00195       need_separator = true;
00196     }
00197 
00198     if (d->mWhatsThisAction) {
00199       d->mMenu->addAction(d->mWhatsThisAction);
00200       need_separator = true;
00201     }
00202 
00203     if (d->mReportBugAction) {
00204       if (need_separator)
00205         d->mMenu->addSeparator();
00206       d->mMenu->addAction(d->mReportBugAction);
00207       need_separator = true;
00208     }
00209 
00210     if (d->mSwitchApplicationLanguageAction) {
00211         if (need_separator)
00212           d->mMenu->addSeparator();
00213         d->mMenu->addAction(d->mSwitchApplicationLanguageAction);
00214         need_separator = true;
00215     }
00216 
00217     if (need_separator)
00218       d->mMenu->addSeparator();
00219 
00220     if (d->mAboutAppAction) {
00221       d->mMenu->addAction(d->mAboutAppAction);
00222     }
00223 
00224     if (d->mAboutKDEAction) {
00225       d->mMenu->addAction(d->mAboutKDEAction);
00226     }
00227   }
00228 
00229   return d->mMenu;
00230 }
00231 
00232 QAction *KHelpMenu::action( MenuId id ) const
00233 {
00234   switch (id)
00235   {
00236     case menuHelpContents:
00237       return d->mHandBookAction;
00238     break;
00239 
00240     case menuWhatsThis:
00241       return d->mWhatsThisAction;
00242     break;
00243 
00244     case menuReportBug:
00245       return d->mReportBugAction;
00246     break;
00247 
00248     case menuSwitchLanguage:
00249       return d->mSwitchApplicationLanguageAction;
00250     break;
00251 
00252     case menuAboutApp:
00253       return d->mAboutAppAction;
00254     break;
00255 
00256     case menuAboutKDE:
00257       return d->mAboutKDEAction;
00258     break;
00259   }
00260 
00261   return 0;
00262 }
00263 
00264 void KHelpMenu::appHelpActivated()
00265 {
00266   KToolInvocation::invokeHelp();
00267 }
00268 
00269 
00270 void KHelpMenu::aboutApplication()
00271 {
00272   if (receivers(SIGNAL(showAboutApplication())) > 0)
00273   {
00274     emit showAboutApplication();
00275   }
00276   else if (d->mAboutData)
00277   {
00278     if( !d->mAboutApp )
00279     {
00280       d->mAboutApp = new KAboutApplicationDialog( d->mAboutData, d->mParent );
00281       connect( d->mAboutApp, SIGNAL(finished()), this, SLOT(dialogFinished()) );
00282     }
00283     d->mAboutApp->show();
00284   }
00285   else
00286   {
00287     if( !d->mAboutApp )
00288     {
00289       d->mAboutApp = new KDialog( d->mParent, Qt::Dialog );
00290       d->mAboutApp->setCaption( i18n("About %1", KGlobal::caption() ) );
00291       d->mAboutApp->setButtons( KDialog::Yes );
00292       d->mAboutApp->setObjectName( "about" );
00293       d->mAboutApp->setButtonText( KDialog::Yes, KStandardGuiItem::ok().text() );
00294       d->mAboutApp->setDefaultButton( KDialog::Yes );
00295       d->mAboutApp->setEscapeButton( KDialog::Yes );
00296       connect( d->mAboutApp, SIGNAL(finished()), this, SLOT(dialogFinished()) );
00297 
00298       KHBox *hbox = new KHBox( d->mAboutApp );
00299       d->mAboutApp->setMainWidget( hbox );
00300       hbox->setSpacing(KDialog::spacingHint()*3);
00301       hbox->setMargin(KDialog::marginHint()*1);
00302 
00303       QLabel *label1 = new QLabel(hbox);
00304 
00305       int size = IconSize(KIconLoader::Dialog);
00306       label1->setPixmap( qApp->windowIcon().pixmap(size,size) );
00307       QLabel *label2 = new QLabel(hbox);
00308       label2->setText( d->mAboutAppText );
00309     }
00310     d->mAboutApp->show();
00311   }
00312 }
00313 
00314 
00315 void KHelpMenu::aboutKDE()
00316 {
00317   if( !d->mAboutKDE )
00318   {
00319     d->mAboutKDE = new KAboutKdeDialog( d->mParent );
00320     connect( d->mAboutKDE, SIGNAL(finished()), this, SLOT(dialogFinished()) );
00321   }
00322   d->mAboutKDE->show();
00323 }
00324 
00325 
00326 void KHelpMenu::reportBug()
00327 {
00328   if( !d->mBugReport )
00329   {
00330     d->mBugReport = new KBugReport( d->mParent, false, d->mAboutData );
00331     connect( d->mBugReport, SIGNAL(finished()),this,SLOT(dialogFinished()) );
00332   }
00333   d->mBugReport->show();
00334 }
00335 
00336 
00337 void KHelpMenu::switchApplicationLanguage()
00338 {
00339   if ( !d->mSwitchApplicationLanguage )
00340   {
00341     d->mSwitchApplicationLanguage = new KSwitchLanguageDialog( d->mParent );
00342     connect( d->mSwitchApplicationLanguage, SIGNAL(finished()), this, SLOT(dialogFinished()) );
00343   }
00344   d->mSwitchApplicationLanguage->show();
00345 }
00346 
00347 
00348 void KHelpMenu::dialogFinished()
00349 {
00350   QTimer::singleShot( 0, this, SLOT(timerExpired()) );
00351 }
00352 
00353 
00354 void KHelpMenu::timerExpired()
00355 {
00356   if( d->mAboutKDE && !d->mAboutKDE->isVisible() )
00357   {
00358     delete d->mAboutKDE; d->mAboutKDE = 0;
00359   }
00360 
00361   if( d->mBugReport && !d->mBugReport->isVisible() )
00362   {
00363     delete d->mBugReport; d->mBugReport = 0;
00364   }
00365 
00366   if ( d->mSwitchApplicationLanguage && !d->mSwitchApplicationLanguage->isVisible() )
00367   {
00368     delete d->mSwitchApplicationLanguage; d->mSwitchApplicationLanguage = 0;
00369   }
00370 
00371   if( d->mAboutApp && !d->mAboutApp->isVisible() )
00372   {
00373     delete d->mAboutApp; d->mAboutApp = 0;
00374   }
00375 }
00376 
00377 
00378 void KHelpMenu::menuDestroyed()
00379 {
00380   d->mMenu = 0;
00381 }
00382 
00383 
00384 void KHelpMenu::contextHelpActivated()
00385 {
00386   QWhatsThis::enterWhatsThisMode();
00387   QWidget* w = QApplication::widgetAt( QCursor::pos() );
00388 #ifdef Q_WS_X11
00389   while ( w && !w->isTopLevel() && !qobject_cast<QX11EmbedWidget*>(w)  )
00390       w = w->parentWidget();
00391 #ifdef __GNUC__
00392 #warning how to enter whats this mode for a QX11EmbedWidget?
00393 #endif
00394 //   if ( w && qobject_cast<QX11EmbedWidget*>(w) )
00395 //    (( QX11EmbedWidget*) w )->enterWhatsThisMode();
00396 #endif
00397 }
00398 
00399 
00400 #include "khelpmenu.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:53:03 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal