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