• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • 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         mSwitchApplicationLanguageAction(0)
00065     {
00066         mMenu = 0;
00067         mAboutApp = 0;
00068         mAboutKDE = 0;
00069         mBugReport = 0;
00070         mHandBookAction = 0;
00071         mWhatsThisAction = 0;
00072         mReportBugAction = 0;
00073         mAboutAppAction = 0;
00074         mAboutKDEAction = 0;
00075     }
00076     ~KHelpMenuPrivate()
00077     {
00078         delete mMenu;
00079         delete mAboutApp;
00080         delete mAboutKDE;
00081         delete mBugReport;
00082         delete mSwitchApplicationLanguage;
00083     }
00084 
00085     KMenu *mMenu;
00086     KDialog *mAboutApp;
00087     KAboutKdeDialog *mAboutKDE;
00088     KBugReport *mBugReport;
00089     KSwitchLanguageDialog *mSwitchApplicationLanguage;
00090 
00091 // TODO evaluate if we use static_cast<QWidget*>(parent()) instead of mParent to win that bit of memory
00092     QWidget *mParent;
00093     QString mAboutAppText;
00094 
00095     bool mShowWhatsThis;
00096 
00097     KAction *mHandBookAction, *mWhatsThisAction;
00098     QAction *mReportBugAction, *mSwitchApplicationLanguageAction, *mAboutAppAction, *mAboutKDEAction;
00099 
00100     const KAboutData *mAboutData;
00101 };
00102 
00103 KHelpMenu::KHelpMenu( QWidget *parent, const QString &aboutAppText,
00104               bool showWhatsThis )
00105   : QObject(parent), d(new KHelpMenuPrivate)
00106 {
00107   d->mAboutAppText = aboutAppText;
00108   d->mShowWhatsThis = showWhatsThis;
00109   d->mParent = parent;
00110   d->mAboutData = 0;
00111 }
00112 
00113 KHelpMenu::KHelpMenu( QWidget *parent, const KAboutData *aboutData,
00114               bool showWhatsThis, KActionCollection *actions )
00115   : QObject(parent), d(new KHelpMenuPrivate)
00116 {
00117   d->mShowWhatsThis = showWhatsThis;
00118   d->mParent = parent;
00119   d->mAboutData = aboutData;
00120 
00121   if (actions)
00122   {
00123     actions->addAction(KStandardAction::HelpContents, this, SLOT(appHelpActivated()));
00124     if (showWhatsThis)
00125       actions->addAction(KStandardAction::WhatsThis, this, SLOT(contextHelpActivated()));
00126     actions->addAction(KStandardAction::ReportBug, this, SLOT(reportBug()));
00127     actions->addAction(KStandardAction::SwitchApplicationLanguage, this, SLOT(switchApplicationLanguage()));
00128     actions->addAction(KStandardAction::AboutApp, this, SLOT(aboutApplication()));
00129     actions->addAction(KStandardAction::AboutKDE, this, SLOT(aboutKDE()));
00130   }
00131 }
00132 
00133 KHelpMenu::~KHelpMenu()
00134 {
00135   delete d;
00136 }
00137 
00138 KMenu* KHelpMenu::menu()
00139 {
00140   if( !d->mMenu )
00141   {
00142     const KAboutData *aboutData = d->mAboutData ? d->mAboutData : KGlobal::mainComponent().aboutData();
00143     QString appName = (aboutData)? aboutData->programName() : qApp->applicationName();
00144 
00145     d->mMenu = new KMenu();
00146     connect( d->mMenu, SIGNAL(destroyed()), this, SLOT(menuDestroyed()));
00147 
00148     d->mMenu->setTitle(i18n("&Help"));
00149 
00150     bool need_separator = false;
00151     if (KAuthorized::authorizeKAction("help_contents"))
00152     {
00153       d->mHandBookAction = new KAction(KIcon("help-contents"), i18n("%1 &Handbook", appName), d->mMenu);
00154       d->mHandBookAction->setShortcut(KStandardShortcut::shortcut(KStandardShortcut::Help));
00155       connect(d->mHandBookAction, SIGNAL(triggered(bool)), this, SLOT(appHelpActivated()));
00156       d->mMenu->addAction(d->mHandBookAction);
00157       need_separator = true;
00158     }
00159 
00160     if( d->mShowWhatsThis && KAuthorized::authorizeKAction("help_whats_this") )
00161     {
00162       d->mWhatsThisAction = new KAction(KIcon("help-contextual"), i18n( "What's &This" ), d->mMenu);
00163       d->mWhatsThisAction->setShortcut(Qt::SHIFT + Qt::Key_F1);
00164       connect(d->mWhatsThisAction, SIGNAL(triggered(bool)), this, SLOT(contextHelpActivated()));
00165       need_separator = true;
00166     }
00167 
00168     if (KAuthorized::authorizeKAction("help_report_bug") && aboutData && !aboutData->bugAddress().isEmpty() )
00169     {
00170       if (need_separator)
00171         d->mMenu->addSeparator();
00172       d->mReportBugAction = d->mMenu->addAction( i18n( "&Report Bug..." ), this, SLOT(reportBug()) );
00173       need_separator = true;
00174     }
00175 
00176     if (KAuthorized::authorizeKAction("switch_application_language"))
00177     {
00178       if((KGlobal::dirs()->findAllResources("locale", QString::fromLatin1("*/entry.desktop"))).count() > 1)
00179       {
00180         if (need_separator)
00181           d->mMenu->addSeparator();
00182         d->mSwitchApplicationLanguageAction = d->mMenu->addAction( i18n( "Switch Application &Language..." ), this, SLOT(switchApplicationLanguage()) );
00183         need_separator = true;
00184       }
00185     }
00186 
00187     if (need_separator)
00188       d->mMenu->addSeparator();
00189 
00190     if (KAuthorized::authorizeKAction("help_about_app"))
00191     {
00192       d->mAboutAppAction = d->mMenu->addAction( qApp->windowIcon(),
00193         i18n( "&About %1" , appName), this, SLOT( aboutApplication() ) );
00194     }
00195 
00196     if (KAuthorized::authorizeKAction("help_about_kde"))
00197     {
00198       d->mAboutKDEAction = d->mMenu->addAction( KIcon("kde"), i18n( "About &KDE" ), this, SLOT( aboutKDE() ) );
00199     }
00200   }
00201 
00202   return d->mMenu;
00203 }
00204 
00205 QAction *KHelpMenu::action( MenuId id ) const
00206 {
00207   switch (id)
00208   {
00209     case menuHelpContents:
00210       return d->mHandBookAction;
00211     break;
00212 
00213     case menuWhatsThis:
00214       return d->mWhatsThisAction;
00215     break;
00216 
00217     case menuReportBug:
00218       return d->mReportBugAction;
00219     break;
00220 
00221     case menuSwitchLanguage:
00222       return d->mSwitchApplicationLanguageAction;
00223     break;
00224 
00225     case menuAboutApp:
00226       return d->mAboutAppAction;
00227     break;
00228 
00229     case menuAboutKDE:
00230       return d->mAboutKDEAction;
00231     break;
00232   }
00233 
00234   return 0;
00235 }
00236 
00237 void KHelpMenu::appHelpActivated()
00238 {
00239   KToolInvocation::invokeHelp();
00240 }
00241 
00242 
00243 void KHelpMenu::aboutApplication()
00244 {
00245   if (receivers(SIGNAL(showAboutApplication())) > 0)
00246   {
00247     emit showAboutApplication();
00248   }
00249   else if (d->mAboutData)
00250   {
00251     if( !d->mAboutApp )
00252     {
00253       d->mAboutApp = new KAboutApplicationDialog( d->mAboutData, d->mParent );
00254       connect( d->mAboutApp, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00255     }
00256     d->mAboutApp->show();
00257   }
00258   else
00259   {
00260     if( !d->mAboutApp )
00261     {
00262       d->mAboutApp = new KDialog( d->mParent, Qt::Dialog );
00263       d->mAboutApp->setCaption( i18n("About %1", KGlobal::caption() ) );
00264       d->mAboutApp->setButtons( KDialog::Yes );
00265       d->mAboutApp->setObjectName( "about" );
00266       d->mAboutApp->showButtonSeparator( true );
00267       d->mAboutApp->setButtonText( KDialog::Yes, KStandardGuiItem::ok().text() );
00268       d->mAboutApp->setDefaultButton( KDialog::Yes );
00269       d->mAboutApp->setEscapeButton( KDialog::Yes );
00270       connect( d->mAboutApp, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00271 
00272       KHBox *hbox = new KHBox( d->mAboutApp );
00273       d->mAboutApp->setMainWidget( hbox );
00274       hbox->setSpacing(KDialog::spacingHint()*3);
00275       hbox->setMargin(KDialog::marginHint()*1);
00276 
00277       QLabel *label1 = new QLabel(hbox);
00278 
00279       int size = IconSize(KIconLoader::Dialog);
00280       label1->setPixmap( qApp->windowIcon().pixmap(size,size) );
00281       QLabel *label2 = new QLabel(hbox);
00282       label2->setText( d->mAboutAppText );
00283     }
00284     d->mAboutApp->show();
00285   }
00286 }
00287 
00288 
00289 void KHelpMenu::aboutKDE()
00290 {
00291   if( !d->mAboutKDE )
00292   {
00293     d->mAboutKDE = new KAboutKdeDialog( d->mParent );
00294     connect( d->mAboutKDE, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00295   }
00296   d->mAboutKDE->show();
00297 }
00298 
00299 
00300 void KHelpMenu::reportBug()
00301 {
00302   if( !d->mBugReport )
00303   {
00304     d->mBugReport = new KBugReport( d->mParent, false, d->mAboutData );
00305     connect( d->mBugReport, SIGNAL(finished()),this,SLOT( dialogFinished()) );
00306   }
00307   d->mBugReport->show();
00308 }
00309 
00310 
00311 void KHelpMenu::switchApplicationLanguage()
00312 {
00313   if ( !d->mSwitchApplicationLanguage )
00314   {
00315     d->mSwitchApplicationLanguage = new KSwitchLanguageDialog( d->mParent );
00316     connect( d->mSwitchApplicationLanguage, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00317   }
00318   d->mSwitchApplicationLanguage->show();
00319 }
00320 
00321 
00322 void KHelpMenu::dialogFinished()
00323 {
00324   QTimer::singleShot( 0, this, SLOT(timerExpired()) );
00325 }
00326 
00327 
00328 void KHelpMenu::timerExpired()
00329 {
00330   if( d->mAboutKDE && !d->mAboutKDE->isVisible() )
00331   {
00332     delete d->mAboutKDE; d->mAboutKDE = 0;
00333   }
00334 
00335   if( d->mBugReport && !d->mBugReport->isVisible() )
00336   {
00337     delete d->mBugReport; d->mBugReport = 0;
00338   }
00339 
00340   if ( d->mSwitchApplicationLanguage && !d->mSwitchApplicationLanguage->isVisible() )
00341   {
00342     delete d->mSwitchApplicationLanguage; d->mSwitchApplicationLanguage = 0;
00343   }
00344 
00345   if( d->mAboutApp && !d->mAboutApp->isVisible() )
00346   {
00347     delete d->mAboutApp; d->mAboutApp = 0;
00348   }
00349 }
00350 
00351 
00352 void KHelpMenu::menuDestroyed()
00353 {
00354   d->mMenu = 0;
00355 }
00356 
00357 
00358 void KHelpMenu::contextHelpActivated()
00359 {
00360   QWhatsThis::enterWhatsThisMode();
00361   QWidget* w = QApplication::widgetAt( QCursor::pos() );
00362 #ifdef Q_WS_X11
00363   while ( w && !w->isTopLevel() && !qobject_cast<QX11EmbedWidget*>(w)  )
00364       w = w->parentWidget();
00365 #ifdef __GNUC__
00366 #warning how to enter whats this mode for a QX11EmbedWidget?
00367 #endif
00368 //   if ( w && qobject_cast<QX11EmbedWidget*>(w) )
00369 //    (( QX11EmbedWidget*) w )->enterWhatsThisMode();
00370 #endif
00371 }
00372 
00373 
00374 #include "khelpmenu.moc"

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