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->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"
KDE 4.6 API Reference