• Skip to content
  • Skip to link menu
KDE 4.3 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       d->mMenu->addAction(d->mWhatsThisAction);
00166       need_separator = true;
00167     }
00168 
00169     if (KAuthorized::authorizeKAction("help_report_bug") && aboutData && !aboutData->bugAddress().isEmpty() )
00170     {
00171       if (need_separator)
00172         d->mMenu->addSeparator();
00173       d->mReportBugAction = d->mMenu->addAction( KIcon("tools-report-bug"), i18n( "&Report Bug..." ), this, SLOT(reportBug()) );
00174       need_separator = true;
00175     }
00176 
00177     if (KAuthorized::authorizeKAction("switch_application_language"))
00178     {
00179       if((KGlobal::dirs()->findAllResources("locale", QString::fromLatin1("*/entry.desktop"))).count() > 1)
00180       {
00181         if (need_separator)
00182           d->mMenu->addSeparator();
00183         d->mSwitchApplicationLanguageAction = d->mMenu->addAction( i18n( "Switch Application &Language..." ), this, SLOT(switchApplicationLanguage()) );
00184         need_separator = true;
00185       }
00186     }
00187 
00188     if (need_separator)
00189       d->mMenu->addSeparator();
00190 
00191     if (KAuthorized::authorizeKAction("help_about_app"))
00192     {
00193       d->mAboutAppAction = d->mMenu->addAction( qApp->windowIcon(),
00194         i18n( "&About %1" , appName), this, SLOT( aboutApplication() ) );
00195     }
00196 
00197     if (KAuthorized::authorizeKAction("help_about_kde"))
00198     {
00199       d->mAboutKDEAction = d->mMenu->addAction( KIcon("kde"), i18n( "About &KDE" ), this, SLOT( aboutKDE() ) );
00200     }
00201   }
00202 
00203   return d->mMenu;
00204 }
00205 
00206 QAction *KHelpMenu::action( MenuId id ) const
00207 {
00208   switch (id)
00209   {
00210     case menuHelpContents:
00211       return d->mHandBookAction;
00212     break;
00213 
00214     case menuWhatsThis:
00215       return d->mWhatsThisAction;
00216     break;
00217 
00218     case menuReportBug:
00219       return d->mReportBugAction;
00220     break;
00221 
00222     case menuSwitchLanguage:
00223       return d->mSwitchApplicationLanguageAction;
00224     break;
00225 
00226     case menuAboutApp:
00227       return d->mAboutAppAction;
00228     break;
00229 
00230     case menuAboutKDE:
00231       return d->mAboutKDEAction;
00232     break;
00233   }
00234 
00235   return 0;
00236 }
00237 
00238 void KHelpMenu::appHelpActivated()
00239 {
00240   KToolInvocation::invokeHelp();
00241 }
00242 
00243 
00244 void KHelpMenu::aboutApplication()
00245 {
00246   if (receivers(SIGNAL(showAboutApplication())) > 0)
00247   {
00248     emit showAboutApplication();
00249   }
00250   else if (d->mAboutData)
00251   {
00252     if( !d->mAboutApp )
00253     {
00254       d->mAboutApp = new KAboutApplicationDialog( d->mAboutData, d->mParent );
00255       connect( d->mAboutApp, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00256     }
00257     d->mAboutApp->show();
00258   }
00259   else
00260   {
00261     if( !d->mAboutApp )
00262     {
00263       d->mAboutApp = new KDialog( d->mParent, Qt::Dialog );
00264       d->mAboutApp->setCaption( i18n("About %1", KGlobal::caption() ) );
00265       d->mAboutApp->setButtons( KDialog::Yes );
00266       d->mAboutApp->setObjectName( "about" );
00267       d->mAboutApp->showButtonSeparator( true );
00268       d->mAboutApp->setButtonText( KDialog::Yes, KStandardGuiItem::ok().text() );
00269       d->mAboutApp->setDefaultButton( KDialog::Yes );
00270       d->mAboutApp->setEscapeButton( KDialog::Yes );
00271       connect( d->mAboutApp, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00272 
00273       KHBox *hbox = new KHBox( d->mAboutApp );
00274       d->mAboutApp->setMainWidget( hbox );
00275       hbox->setSpacing(KDialog::spacingHint()*3);
00276       hbox->setMargin(KDialog::marginHint()*1);
00277 
00278       QLabel *label1 = new QLabel(hbox);
00279 
00280       int size = IconSize(KIconLoader::Dialog);
00281       label1->setPixmap( qApp->windowIcon().pixmap(size,size) );
00282       QLabel *label2 = new QLabel(hbox);
00283       label2->setText( d->mAboutAppText );
00284     }
00285     d->mAboutApp->show();
00286   }
00287 }
00288 
00289 
00290 void KHelpMenu::aboutKDE()
00291 {
00292   if( !d->mAboutKDE )
00293   {
00294     d->mAboutKDE = new KAboutKdeDialog( d->mParent );
00295     connect( d->mAboutKDE, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00296   }
00297   d->mAboutKDE->show();
00298 }
00299 
00300 
00301 void KHelpMenu::reportBug()
00302 {
00303   if( !d->mBugReport )
00304   {
00305     d->mBugReport = new KBugReport( d->mParent, false, d->mAboutData );
00306     connect( d->mBugReport, SIGNAL(finished()),this,SLOT( dialogFinished()) );
00307   }
00308   d->mBugReport->show();
00309 }
00310 
00311 
00312 void KHelpMenu::switchApplicationLanguage()
00313 {
00314   if ( !d->mSwitchApplicationLanguage )
00315   {
00316     d->mSwitchApplicationLanguage = new KSwitchLanguageDialog( d->mParent );
00317     connect( d->mSwitchApplicationLanguage, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00318   }
00319   d->mSwitchApplicationLanguage->show();
00320 }
00321 
00322 
00323 void KHelpMenu::dialogFinished()
00324 {
00325   QTimer::singleShot( 0, this, SLOT(timerExpired()) );
00326 }
00327 
00328 
00329 void KHelpMenu::timerExpired()
00330 {
00331   if( d->mAboutKDE && !d->mAboutKDE->isVisible() )
00332   {
00333     delete d->mAboutKDE; d->mAboutKDE = 0;
00334   }
00335 
00336   if( d->mBugReport && !d->mBugReport->isVisible() )
00337   {
00338     delete d->mBugReport; d->mBugReport = 0;
00339   }
00340 
00341   if ( d->mSwitchApplicationLanguage && !d->mSwitchApplicationLanguage->isVisible() )
00342   {
00343     delete d->mSwitchApplicationLanguage; d->mSwitchApplicationLanguage = 0;
00344   }
00345 
00346   if( d->mAboutApp && !d->mAboutApp->isVisible() )
00347   {
00348     delete d->mAboutApp; d->mAboutApp = 0;
00349   }
00350 }
00351 
00352 
00353 void KHelpMenu::menuDestroyed()
00354 {
00355   d->mMenu = 0;
00356 }
00357 
00358 
00359 void KHelpMenu::contextHelpActivated()
00360 {
00361   QWhatsThis::enterWhatsThisMode();
00362   QWidget* w = QApplication::widgetAt( QCursor::pos() );
00363 #ifdef Q_WS_X11
00364   while ( w && !w->isTopLevel() && !qobject_cast<QX11EmbedWidget*>(w)  )
00365       w = w->parentWidget();
00366 #ifdef __GNUC__
00367 #warning how to enter whats this mode for a QX11EmbedWidget?
00368 #endif
00369 //   if ( w && qobject_cast<QX11EmbedWidget*>(w) )
00370 //    (( QX11EmbedWidget*) w )->enterWhatsThisMode();
00371 #endif
00372 }
00373 
00374 
00375 #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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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