• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kcheckaccelerators.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
00003     Copyright (C) 1998, 1999, 2000 KDE Team
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 #define INCLUDE_MENUITEM_DEF
00022 
00023 #include "kcheckaccelerators.h"
00024 
00025 #include <config.h>
00026 
00027 #include <QApplication>
00028 #include <QCheckBox>
00029 #include <QDialog>
00030 #include <QKeyEvent>
00031 #include <QLayout>
00032 #include <QMenuBar>
00033 #include <QMetaObject>
00034 #include <QPushButton>
00035 #include <QTabBar>
00036 
00037 #include <kconfig.h>
00038 #include <kdebug.h>
00039 #include <kglobal.h>
00040 #include <klocale.h>
00041 #include <kshortcut.h>
00042 #include <ktextbrowser.h>
00043 
00044 #include "kacceleratormanager.h"
00045 #include <kconfiggroup.h>
00046 
00047 /*
00048 
00049  HOWTO:
00050 
00051  This class allows translators (and application developers) to check for accelerator
00052  conflicts in menu and widgets. Put the following in your kdeglobals (or the config
00053  file for the application you're testing):
00054 
00055  [Development]
00056  CheckAccelerators=F12
00057  AutoCheckAccelerators=false
00058  AlwaysShowCheckAccelerators=false
00059 
00060  The checking can be either manual or automatic. To perform manual check, press
00061  the keyboard shortcut set to 'CheckAccelerators' (here F12). If automatic checking
00062  is enabled by setting 'AutoCheckAccelerators' to true, check will be performed every
00063  time the GUI changes. It's possible that in certain cases the check will be
00064  done also when no visible changes in the GUI happen or the check won't be done
00065  even if the GUI changed (in the latter case, use manual check ). Automatic
00066  checks can be anytime disabled by the checkbox in the dialog presenting
00067  the results of the check. If you set 'AlwaysShowCheckAccelerators' to true,
00068  the dialog will be shown even if the automatic check didn't find any conflicts,
00069  and all submenus will be shown, even those without conflicts.
00070 
00071  The dialog first lists the name of the window, then all results for all menus
00072  (if the window has a menubar) and then result for all controls in the active
00073  window (if there are any checkboxes etc.). For every submenu and all controls
00074  there are shown all conflicts grouped by accelerator, and a list of all used
00075  accelerators.
00076 */
00077 
00078 KCheckAccelerators::KCheckAccelerators( QObject* parent )
00079     : QObject( parent ), key(0), block( false ), drklash(0)
00080 {
00081     setObjectName( "kapp_accel_filter" );
00082     parent->installEventFilter( this );
00083     KConfigGroup cg( KGlobal::config(), "Development" );
00084     QString sKey = cg.readEntry( "CheckAccelerators" ).trimmed();
00085     if( !sKey.isEmpty() ) {
00086       KShortcut cuts( sKey );
00087       if( !cuts.isEmpty() )
00088         key = int(cuts.primary()[0]);
00089     }
00090     alwaysShow = cg.readEntry( "AlwaysShowCheckAccelerators", false );
00091     autoCheck = cg.readEntry( "AutoCheckAccelerators", true );
00092     connect( &autoCheckTimer, SIGNAL(timeout()), SLOT(autoCheckSlot()));
00093 }
00094 
00095 bool KCheckAccelerators::eventFilter( QObject * , QEvent * e)
00096 {
00097     if ( block )
00098         return false;
00099 
00100     switch ( e->type() ) { // just simplify debuggin
00101     case QEvent::Shortcut:
00102         if ( key && (static_cast<QKeyEvent *>(e)->key() == key) ) {
00103             block = true;
00104         checkAccelerators( false );
00105         block = false;
00106         static_cast<QKeyEvent *>(e)->accept();
00107         return true;
00108     }
00109         break;
00110     case QEvent::ChildAdded:
00111     case QEvent::ChildRemoved:
00112         // Only care about widgets; this also avoids starting the timer in other threads
00113         if ( !static_cast<QChildEvent *>(e)->child()->isWidgetType() )
00114             break;
00115         // fall-through
00116     case QEvent::Resize:
00117     case QEvent::LayoutRequest:
00118     case QEvent::WindowActivate:
00119     case QEvent::WindowDeactivate:
00120         if( autoCheck ) {
00121             autoCheckTimer.setSingleShot( true );
00122             autoCheckTimer.start( 20 ); // 20 ms
00123         }
00124         break;
00125     case QEvent::Timer:
00126     case QEvent::MouseMove:
00127     case QEvent::Paint:
00128         return false;
00129     default:
00130         // kDebug(125) << "KCheckAccelerators::eventFilter " << e->type() << " " << autoCheck;
00131         break;
00132     }
00133     return false;
00134 }
00135 
00136 void KCheckAccelerators::autoCheckSlot()
00137 {
00138     if( block )
00139     {
00140         autoCheckTimer.setSingleShot( true );
00141         autoCheckTimer.start( 20 );
00142         return;
00143     }
00144     block = true;
00145     checkAccelerators( !alwaysShow );
00146     block = false;
00147 }
00148 
00149 void KCheckAccelerators::createDialog(QWidget *actWin, bool automatic)
00150 {
00151     if ( drklash )
00152         return;
00153 
00154     drklash = new QDialog( actWin );
00155     drklash->setAttribute( Qt::WA_DeleteOnClose );
00156     drklash->setObjectName( "kapp_accel_check_dlg" );
00157     drklash->setWindowTitle( i18nc("@title:window", "Dr. Klash' Accelerator Diagnosis" ));
00158     drklash->resize( 500, 460 );
00159     QVBoxLayout* layout = new QVBoxLayout( drklash );
00160     layout->setMargin( 11 );
00161     layout->setSpacing( 6 );
00162     drklash_view = new KTextBrowser( drklash );
00163     layout->addWidget( drklash );
00164     QCheckBox* disableAutoCheck = NULL;
00165     if( automatic )  {
00166         disableAutoCheck = new QCheckBox( i18nc("@option:check","Disable automatic checking" ), drklash );
00167         connect(disableAutoCheck, SIGNAL(toggled(bool)), SLOT(slotDisableCheck(bool)));
00168         layout->addWidget( disableAutoCheck );
00169     }
00170     QPushButton* btnClose = new QPushButton( i18nc("@action:button", "Close" ), drklash );
00171     btnClose->setDefault( true );
00172     layout->addWidget( btnClose );
00173     connect( btnClose, SIGNAL(clicked()), drklash, SLOT(close()) );
00174     if (disableAutoCheck)
00175         disableAutoCheck->setFocus();
00176     else
00177         drklash_view->setFocus();
00178 }
00179 
00180 void KCheckAccelerators::slotDisableCheck(bool on)
00181 {
00182     autoCheck = !on;
00183     if (!on)
00184         autoCheckSlot();
00185 }
00186 
00187 void KCheckAccelerators::checkAccelerators( bool automatic )
00188 {
00189     QWidget* actWin = qApp->activeWindow();
00190     if ( !actWin )
00191         return;
00192 
00193     KAcceleratorManager::manage(actWin);
00194     QString a, c, r;
00195     KAcceleratorManager::last_manage(a, c,  r);
00196 
00197     if (automatic) // for now we only show dialogs on F12 checks
00198         return;
00199 
00200     if (c.isEmpty() && r.isEmpty() && (automatic || a.isEmpty()))
00201         return;
00202 
00203     QString s;
00204 
00205     if ( ! c.isEmpty() )  {
00206         s += i18n("<h2>Accelerators changed</h2>");
00207         s += "<table border><tr><th><b>Old Text</b></th><th><b>New Text</b></th></tr>"
00208              + c + "</table>";
00209     }
00210 
00211     if ( ! r.isEmpty() )  {
00212         s += i18n("<h2>Accelerators removed</h2>");
00213         s += "<table border><tr><th><b>Old Text</b></th></tr>" + r + "</table>";
00214     }
00215 
00216     if ( ! a.isEmpty() )  {
00217         s += i18n("<h2>Accelerators added (just for your info)</h2>");
00218         s += "<table border><tr><th><b>New Text</b></th></tr>" + a + "</table>";
00219     }
00220 
00221     createDialog(actWin, automatic);
00222     drklash_view->setHtml(s);
00223     drklash->show();
00224     drklash->raise();
00225 
00226     // dlg will be destroyed before returning
00227 }
00228 
00229 #include "kcheckaccelerators.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