kcheckaccelerators.cpp

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 // $Id: kcheckaccelerators.cpp 465272 2005-09-29 09:47:40Z mueller $
00022 
00023 #define INCLUDE_MENUITEM_DEF
00024 #include <qmenudata.h>
00025 
00026 #include "config.h"
00027 
00028 #include "kcheckaccelerators.h"
00029 #include "kaccelmanager.h"
00030 #include <qpopupmenu.h>
00031 #include <qapplication.h>
00032 #include <qdialog.h>
00033 #include <qlayout.h>
00034 #include <qtextview.h>
00035 #include <qobjectlist.h>
00036 #include <qmenubar.h>
00037 #include <qtabbar.h>
00038 #include <qpushbutton.h>
00039 #include <qmetaobject.h>
00040 #include <qcheckbox.h>
00041 
00042 #include <kconfig.h>
00043 #include <kdebug.h>
00044 #include <kglobal.h>
00045 #include <kshortcut.h>
00046 #include <klocale.h>
00047 
00048 /*
00049 
00050  HOWTO:
00051 
00052  This class allows translators (and application developers) to check for accelerator
00053  conflicts in menu and widgets. Put the following in your kdeglobals (or the config
00054  file for the application you're testing):
00055 
00056  [Development]
00057  CheckAccelerators=F12
00058  AutoCheckAccelerators=false
00059  AlwaysShowCheckAccelerators=false
00060 
00061  The checking can be either manual or automatic. To perform manual check, press
00062  the keyboard shortcut set to 'CheckAccelerators' (here F12). If automatic checking
00063  is enabled by setting 'AutoCheckAccelerators' to true, check will be performed every
00064  time the GUI changes. It's possible that in certain cases the check will be
00065  done also when no visible changes in the GUI happen or the check won't be done
00066  even if the GUI changed (in the latter case, use manual check ). Automatic
00067  checks can be anytime disabled by the checkbox in the dialog presenting
00068  the results of the check. If you set 'AlwaysShowCheckAccelerators' to true,
00069  the dialog will be shown even if the automatic check didn't find any conflicts,
00070  and all submenus will be shown, even those without conflicts.
00071 
00072  The dialog first lists the name of the window, then all results for all menus
00073  (if the window has a menubar) and then result for all controls in the active
00074  window (if there are any checkboxes etc.). For every submenu and all controls
00075  there are shown all conflicts grouped by accelerator, and a list of all used
00076  accelerators.
00077 */
00078 
00079 KCheckAccelerators::KCheckAccelerators( QObject* parent )
00080     : QObject( parent, "kapp_accel_filter" ), key(0), block( false ), drklash(0)
00081 {
00082     parent->installEventFilter( this );
00083     KConfigGroupSaver saver( KGlobal::config(), "Development" );
00084     QString sKey = KGlobal::config()->readEntry( "CheckAccelerators" ).stripWhiteSpace();
00085     if( !sKey.isEmpty() ) {
00086       KShortcut cuts( sKey );
00087       if( cuts.count() > 0 )
00088         key = int(cuts.seq(0).qt());
00089     }
00090     alwaysShow = KGlobal::config()->readBoolEntry( "AlwaysShowCheckAccelerators", false );
00091     autoCheck = KGlobal::config()->readBoolEntry( "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::Accel:
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::ChildInserted:
00111     case QEvent::ChildRemoved:
00112     case QEvent::Resize:
00113     case QEvent::LayoutHint:
00114     case QEvent::WindowActivate:
00115     case QEvent::WindowDeactivate:
00116         if( autoCheck )
00117             autoCheckTimer.start( 20, true ); // 20 ms
00118         break;
00119     case QEvent::Timer:
00120     case QEvent::MouseMove:
00121     case QEvent::Paint:
00122         return false;
00123     default:
00124         // kdDebug(125) << "KCheckAccelerators::eventFilter " << e->type() << " " << autoCheck << endl;
00125         break;
00126     }
00127     return false;
00128 }
00129 
00130 void KCheckAccelerators::autoCheckSlot()
00131 {
00132     if( block || QWidget::mouseGrabber() ||
00133         QWidget::keyboardGrabber() ||
00134         QApplication::activePopupWidget())
00135     {
00136         autoCheckTimer.start( 20, true );
00137         return;
00138     }
00139     block = true;
00140     checkAccelerators( !alwaysShow );
00141     block = false;
00142 }
00143 
00144 void KCheckAccelerators::createDialog(QWidget *actWin, bool automatic)
00145 {
00146     if ( drklash )
00147         return;
00148 
00149     drklash = new QDialog( actWin, "kapp_accel_check_dlg", false, Qt::WDestructiveClose);
00150     drklash->setCaption( i18n( "Dr. Klash' Accelerator Diagnosis" ));
00151     drklash->resize( 500, 460 );
00152     QVBoxLayout* layout = new QVBoxLayout( drklash, 11, 6 );
00153     layout->setAutoAdd( true );
00154     drklash_view = new QTextView( drklash );
00155     QCheckBox* disableAutoCheck = NULL;
00156     if( automatic )  {
00157         disableAutoCheck = new QCheckBox( i18n( "&Disable automatic checking" ), drklash );
00158         connect(disableAutoCheck, SIGNAL(toggled(bool)), SLOT(slotDisableCheck(bool)));
00159     }
00160     QPushButton* btnClose = new QPushButton( i18n( "&Close" ), drklash );
00161     btnClose->setDefault( true );
00162     connect( btnClose, SIGNAL( clicked() ), drklash, SLOT( close() ) );
00163     if (disableAutoCheck)
00164         disableAutoCheck->setFocus();
00165     else
00166         drklash_view->setFocus();
00167 }
00168 
00169 void KCheckAccelerators::slotDisableCheck(bool on)
00170 {
00171     autoCheck = !on;
00172     if (!on)
00173         autoCheckSlot();
00174 }
00175 
00176 void KCheckAccelerators::checkAccelerators( bool automatic )
00177 {
00178     QWidget* actWin = qApp->activeWindow();
00179     if ( !actWin )
00180         return;
00181 
00182     KAcceleratorManager::manage(actWin);
00183     QString a, c, r;
00184     KAcceleratorManager::last_manage(a, c,  r);
00185 
00186     if (automatic) // for now we only show dialogs on F12 checks
00187         return;
00188 
00189     if (c.isEmpty() && r.isEmpty() && (automatic || a.isEmpty()))
00190         return;
00191 
00192     QString s;
00193 
00194     if ( ! c.isEmpty() )  {
00195         s += i18n("<h2>Accelerators changed</h2>");
00196         s += "<table border><tr><th><b>Old Text</b></th><th><b>New Text</b></th></tr>"
00197              + c + "</table>";
00198     }
00199 
00200     if ( ! r.isEmpty() )  {
00201         s += i18n("<h2>Accelerators removed</h2>");
00202         s += "<table border><tr><th><b>Old Text</b></th></tr>" + r + "</table>";
00203     }
00204 
00205     if ( ! a.isEmpty() )  {
00206         s += i18n("<h2>Accelerators added (just for your info)</h2>");
00207         s += "<table border><tr><th><b>New Text</b></th></tr>" + a + "</table>";
00208     }
00209 
00210     createDialog(actWin, automatic);
00211     drklash_view->setText(s);
00212     drklash->show();
00213     drklash->raise();
00214 
00215     // dlg will be destroyed before returning
00216 }
00217 
00218 #include "kcheckaccelerators.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys