khelpmenu.cpp

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 #include <qhbox.h>
00024 #include <qlabel.h>
00025 #include <qtimer.h>
00026 #include <qtoolbutton.h>
00027 #include <qwhatsthis.h>
00028 #include <qwidget.h>
00029 
00030 #include <kaboutapplication.h>
00031 #include <kaboutdata.h>
00032 #include <kaboutkde.h>
00033 #include <kaction.h>
00034 #include <kapplication.h>
00035 #include <kbugreport.h>
00036 #include <kdialogbase.h>
00037 #include <khelpmenu.h>
00038 #include <kiconloader.h>
00039 #include <klocale.h>
00040 #include <kmessagebox.h>
00041 #include <kpopupmenu.h>
00042 #include <kstdaccel.h>
00043 #include <kstdaction.h>
00044 #include <kprocess.h>
00045 
00046 #include "kswitchlanguagedialog.h"
00047 
00048 #include "config.h"
00049 #include <qxembed.h>
00050 
00051 class KHelpMenuPrivate
00052 {
00053 public:
00054     KHelpMenuPrivate():mSwitchApplicationLanguage(NULL)
00055     {
00056     }
00057     ~KHelpMenuPrivate()
00058     {
00059         delete mSwitchApplicationLanguage;
00060     }
00061 
00062     const KAboutData *mAboutData;
00063     KSwitchLanguageDialog *mSwitchApplicationLanguage;
00064 };
00065 
00066 KHelpMenu::KHelpMenu( QWidget *parent, const QString &aboutAppText,
00067               bool showWhatsThis )
00068   : QObject(parent), mMenu(0), mAboutApp(0), mAboutKDE(0), mBugReport(0),
00069     d(new KHelpMenuPrivate)
00070 {
00071   mParent = parent;
00072   mAboutAppText = aboutAppText;
00073   mShowWhatsThis = showWhatsThis;
00074   d->mAboutData = 0;
00075 }
00076 
00077 KHelpMenu::KHelpMenu( QWidget *parent, const KAboutData *aboutData,
00078               bool showWhatsThis, KActionCollection *actions )
00079   : QObject(parent), mMenu(0), mAboutApp(0), mAboutKDE(0), mBugReport(0),
00080     d(new KHelpMenuPrivate)
00081 {
00082   mParent = parent;
00083   mShowWhatsThis = showWhatsThis;
00084 
00085   d->mAboutData = aboutData;
00086 
00087   if (!aboutData)
00088     mAboutAppText = QString::null;
00089 
00090   if (actions)
00091   {
00092     KStdAction::helpContents(this, SLOT(appHelpActivated()), actions);
00093     if (showWhatsThis)
00094       KStdAction::whatsThis(this, SLOT(contextHelpActivated()), actions);
00095     KStdAction::reportBug(this, SLOT(reportBug()), actions);
00096     KStdAction::aboutApp(this, SLOT(aboutApplication()), actions);
00097     KStdAction::aboutKDE(this, SLOT(aboutKDE()), actions);
00098     KStdAction::kubuntuTranslate(this, SLOT(kubuntuTranslate()), actions);
00099     KStdAction::kubuntuGetHelpOnline(this, SLOT(kubuntuGetHelpOnline()), actions);
00100     KStdAction::switchApplicationLanguage(this, SLOT(switchApplicationLanguage()), actions);
00101   }
00102 }
00103 
00104 KHelpMenu::~KHelpMenu()
00105 {
00106   delete mMenu;
00107   delete mAboutApp;
00108   delete mAboutKDE;
00109   delete mBugReport;
00110   delete d;
00111 }
00112 
00113 
00114 KPopupMenu* KHelpMenu::menu()
00115 {
00116   if( !mMenu )
00117   {
00118     //
00119     // 1999-12-02 Espen Sand:
00120     // I use hardcoded menu id's here. Reason is to stay backward
00121     // compatible.
00122     //
00123     const KAboutData *aboutData = d->mAboutData ? d->mAboutData : KGlobal::instance()->aboutData();
00124     QString appName = (aboutData)? aboutData->programName() : QString::fromLatin1(qApp->name());
00125 
00126     mMenu = new KPopupMenu();
00127     connect( mMenu, SIGNAL(destroyed()), this, SLOT(menuDestroyed()));
00128 
00129     bool need_separator = false;
00130     if (kapp->authorizeKAction("help_contents"))
00131     {
00132       mMenu->insertItem( BarIcon( "contents", KIcon::SizeSmall),
00133                      i18n( "%1 &Handbook" ).arg( appName) ,menuHelpContents );
00134       mMenu->connectItem( menuHelpContents, this, SLOT(appHelpActivated()) );
00135       mMenu->setAccel( KStdAccel::shortcut(KStdAccel::Help), menuHelpContents );
00136       need_separator = true;
00137     }
00138 
00139     if( mShowWhatsThis && kapp->authorizeKAction("help_whats_this") )
00140     {
00141       QToolButton* wtb = QWhatsThis::whatsThisButton(0);
00142       mMenu->insertItem( wtb->iconSet(),i18n( "What's &This" ), menuWhatsThis);
00143       mMenu->connectItem( menuWhatsThis, this, SLOT(contextHelpActivated()) );
00144       delete wtb;
00145       mMenu->setAccel( SHIFT + Key_F1, menuWhatsThis );
00146       need_separator = true;
00147     }
00148 
00149     if (kapp->authorizeKAction("help_report_bug") && aboutData && !aboutData->bugAddress().isEmpty() )
00150     {
00151       if (need_separator)
00152         mMenu->insertSeparator();
00153       mMenu->insertItem( i18n( "&Report Bug..." ), menuReportBug );
00154       mMenu->connectItem( menuReportBug, this, SLOT(reportBug()) );
00155       need_separator = true;
00156     }
00157 
00158     if (kapp->authorizeKAction("switch_application_language"))
00159     {
00160       if (need_separator)
00161         mMenu->insertSeparator();
00162       mMenu->insertItem( i18n( "Switch application &language..." ), menuSwitchLanguage );
00163       mMenu->connectItem( menuSwitchLanguage, this, SLOT(switchApplicationLanguage()) );
00164       need_separator = true;
00165     }
00166     
00167     if (need_separator)
00168       mMenu->insertSeparator();
00169 
00170     mMenu->insertItem( SmallIcon("launchpad"), i18n( "&Get Help Online" ), this, SLOT(kubuntuGetHelpOnline()) );
00171     mMenu->insertItem( SmallIcon("locale"), i18n( "&Translate this Application" ), this, SLOT(kubuntuTranslate()) );
00172     mMenu->insertSeparator();
00173 
00174     if (kapp->authorizeKAction("help_about_app"))
00175     {
00176       mMenu->insertItem( kapp->miniIcon(),
00177         i18n( "&About %1" ).arg(appName), menuAboutApp );
00178       mMenu->connectItem( menuAboutApp, this, SLOT( aboutApplication() ) );
00179     }
00180     
00181     if (kapp->authorizeKAction("help_about_kde"))
00182     {
00183       mMenu->insertItem( SmallIcon("about_kde"), i18n( "About &KDE" ), menuAboutKDE );
00184       mMenu->connectItem( menuAboutKDE, this, SLOT( aboutKDE() ) );
00185     }
00186   }
00187 
00188   return mMenu;
00189 }
00190 
00191 
00192 
00193 void KHelpMenu::appHelpActivated()
00194 {
00195   kapp->invokeHelp();
00196 }
00197 
00198 
00199 void KHelpMenu::aboutApplication()
00200 {
00201   if (d->mAboutData)
00202   {
00203     if( !mAboutApp )
00204     {
00205       mAboutApp = new KAboutApplication( d->mAboutData, mParent, "about", false );
00206       connect( mAboutApp, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00207     }
00208     mAboutApp->show();
00209   }
00210   else if( mAboutAppText.isEmpty() )
00211   {
00212     emit showAboutApplication();
00213   }
00214   else
00215   {
00216     if( !mAboutApp )
00217     {
00218       mAboutApp = new KDialogBase( QString::null, // Caption is defined below
00219                    KDialogBase::Yes, KDialogBase::Yes,
00220                    KDialogBase::Yes, mParent, "about",
00221                    false, true, KStdGuiItem::ok() );
00222       connect( mAboutApp, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00223 
00224       QHBox *hbox = new QHBox( mAboutApp );
00225       mAboutApp->setMainWidget( hbox );
00226       hbox->setSpacing(KDialog::spacingHint()*3);
00227       hbox->setMargin(KDialog::marginHint()*1);
00228 
00229       QLabel *label1 = new QLabel(hbox);
00230       label1->setPixmap( kapp->icon() );
00231       QLabel *label2 = new QLabel(hbox);
00232       label2->setText( mAboutAppText );
00233 
00234       mAboutApp->setPlainCaption( i18n("About %1").arg(kapp->caption()) );
00235       mAboutApp->disableResize();
00236     }
00237 
00238     mAboutApp->show();
00239   }
00240 }
00241 
00242 
00243 void KHelpMenu::kubuntuGetHelpOnline()
00244 {
00245   const KAboutData *aboutData = KGlobal::instance()->aboutData();
00246   QString path = KStandardDirs::findExe( aboutData->appName() );
00247   KProcess *proc = new KProcess;
00248   *proc << "launchpad-integration" << "--file" << path << "--info";
00249   proc->start(KProcess::DontCare);
00250 }
00251 
00252 void KHelpMenu::kubuntuTranslate()
00253 {
00254   const KAboutData *aboutData = KGlobal::instance()->aboutData();
00255   QString path = KStandardDirs::findExe( aboutData->appName() );
00256   KProcess *proc = new KProcess;
00257   *proc << "launchpad-integration" << "--file" << path << "--translate";
00258   proc->start(KProcess::DontCare);
00259 }
00260 
00261 void KHelpMenu::aboutKDE()
00262 {
00263   if( !mAboutKDE )
00264   {
00265     mAboutKDE = new KAboutKDE( mParent, "aboutkde", false );
00266     connect( mAboutKDE, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00267   }
00268   mAboutKDE->show();
00269 }
00270 
00271 
00272 void KHelpMenu::reportBug()
00273 {
00274   if( !mBugReport )
00275   {
00276     mBugReport = new KBugReport( mParent, false, d->mAboutData );
00277     connect( mBugReport, SIGNAL(finished()),this,SLOT( dialogFinished()) );
00278   }
00279   mBugReport->show();
00280 }
00281 
00282 void KHelpMenu::switchApplicationLanguage()
00283 {
00284   if ( !d->mSwitchApplicationLanguage )
00285   {
00286     d->mSwitchApplicationLanguage = new KSwitchLanguageDialog( mParent, "switchlanguagedialog", false );
00287     connect( d->mSwitchApplicationLanguage, SIGNAL(finished()), this, SLOT( dialogFinished()) );
00288   }
00289   d->mSwitchApplicationLanguage->show();
00290 }
00291 
00292 
00293 void KHelpMenu::dialogFinished()
00294 {
00295   QTimer::singleShot( 0, this, SLOT(timerExpired()) );
00296 }
00297 
00298 
00299 void KHelpMenu::timerExpired()
00300 {
00301   if( mAboutKDE && !mAboutKDE->isVisible() )
00302   {
00303     delete mAboutKDE; mAboutKDE = 0;
00304   }
00305 
00306   if( mBugReport && !mBugReport->isVisible() )
00307   {
00308     delete mBugReport; mBugReport = 0;
00309   }
00310 
00311   if( mAboutApp && !mAboutApp->isVisible() )
00312   {
00313     delete mAboutApp; mAboutApp = 0;
00314   }
00315   
00316   if (d->mSwitchApplicationLanguage && !d->mSwitchApplicationLanguage->isVisible())
00317   {
00318     delete d->mSwitchApplicationLanguage; d->mSwitchApplicationLanguage = 0;
00319   }
00320 }
00321 
00322 
00323 void KHelpMenu::menuDestroyed()
00324 {
00325   mMenu = 0;
00326 }
00327 
00328 
00329 void KHelpMenu::contextHelpActivated()
00330 {
00331   QWhatsThis::enterWhatsThisMode();
00332   QWidget* w = QApplication::widgetAt( QCursor::pos(), true );
00333   while ( w && !w->isTopLevel() && !w->inherits("QXEmbed")  )
00334       w = w->parentWidget();
00335 #ifdef Q_WS_X11
00336    if ( w && w->inherits("QXEmbed") )
00337       (( QXEmbed*) w )->enterWhatsThisMode();
00338 #endif
00339 }
00340 
00341 void KHelpMenu::virtual_hook( int, void* )
00342 { /*BASE::virtual_hook( id, data );*/ }
00343 
00344 
00345 #include "khelpmenu.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys