kdecore Library API Documentation

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., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019         */
00020 
00021 // $Id: kcheckaccelerators.cpp,v 1.8 2003/09/10 00:12:12 wheeler Exp $
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" ), block( false ), drklash(0)
00081 {
00082     parent->installEventFilter( this );
00083     KConfigGroupSaver saver( KGlobal::config(), "Development" );
00084     QString sKey = KGlobal::config()->readEntry( "CheckAccelerators", "F12" ).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", true );
00091     autoCheck = KGlobal::config()->readBoolEntry( "AutoCheckAccelerators", true );
00092     connect( &autoCheckTimer, SIGNAL( timeout()), SLOT( autoCheckSlot()));
00093 }
00094 
00095 bool KCheckAccelerators::eventFilter( QObject * , QEvent * e) {
00096     if ( block )
00097         return false;
00098     if ( e->type() == QEvent::Accel ) {
00099         if ( static_cast<QKeyEvent *>(e)->key() == key ) {
00100             block = true;
00101         checkAccelerators( false );
00102         block = false;
00103         static_cast<QKeyEvent *>(e)->accept();
00104         return true;
00105     }
00106     }
00107     if( autoCheck
00108         && ( e->type() == QEvent::ChildInserted ||
00109              e->type() == QEvent::ChildRemoved ))
00110     {
00111         autoCheckTimer.start( 100, true ); // 100 ms
00112     }
00113     return false;
00114 }
00115 
00116 void KCheckAccelerators::autoCheckSlot()
00117 {
00118     if( block || QWidget::mouseGrabber() ||
00119         QWidget::keyboardGrabber() ||
00120         QApplication::activePopupWidget())
00121     {
00122         autoCheckTimer.start( 100, true );
00123         return;
00124     }
00125     block = true;
00126     checkAccelerators( true );
00127     block = false;
00128 }
00129 
00130 void KCheckAccelerators::createDialog(QWidget *actWin, bool automatic)
00131 {
00132     if ( drklash )
00133         return;
00134 
00135     drklash = new QDialog( actWin, "kapp_accel_check_dlg", false, Qt::WDestructiveClose);
00136     drklash->setCaption( i18n( "Dr. Klash' Accelerator Diagnosis" ));
00137     drklash->resize( 500, 460 );
00138     QVBoxLayout* layout = new QVBoxLayout( drklash, 11, 6 );
00139     layout->setAutoAdd( true );
00140     drklash_view = new QTextView( drklash );
00141     QCheckBox* disableAutoCheck = NULL;
00142     if( automatic )  {
00143         disableAutoCheck = new QCheckBox( i18n( "&Disable automatic checking" ), drklash );
00144         connect(disableAutoCheck, SIGNAL(toggled(bool)), SLOT(slotDisableCheck(bool)));
00145     }
00146     QPushButton* btnClose = new QPushButton( i18n( "&Close" ), drklash );
00147     btnClose->setDefault( true );
00148     connect( btnClose, SIGNAL( clicked() ), drklash, SLOT( close() ) );
00149     if (disableAutoCheck)
00150         disableAutoCheck->setFocus();
00151     else
00152         drklash_view->setFocus();
00153 }
00154 
00155 void KCheckAccelerators::slotDisableCheck(bool on)
00156 {
00157     autoCheck = !on;
00158     if (!on)
00159         autoCheckSlot();
00160 }
00161 
00162 void KCheckAccelerators::checkAccelerators( bool automatic ) {
00163     QWidget* actWin = qApp->activeWindow();
00164     if ( !actWin )
00165         return;
00166 
00167     KAcceleratorManager::manage(actWin);
00168     QString a, c, r;
00169     KAcceleratorManager::last_manage(a, c,  r);
00170     if (c.isEmpty() && r.isEmpty() && (automatic || a.isEmpty()))
00171         return;
00172 
00173     QString s;
00174 
00175     if ( ! c.isEmpty() )  {
00176         s += i18n("<h2>Accelerators changed</h2>");
00177         s += "<table border><tr><th><b>Old Text</b></th><th><b>New Text</b></th></tr>"
00178              + c + "</table>";
00179     }
00180 
00181     if ( ! r.isEmpty() )  {
00182         s += i18n("<h2>Accelerators removed</h2>");
00183         s += "<table border><tr><th><b>Old Text</b></th></tr>" + r + "</table>";
00184     }
00185 
00186     if ( ! a.isEmpty() )  {
00187         s += i18n("<h2>Accelerators added (just for your info)</h2>");
00188         s += "<table border><tr><th><b>New Text</b></th></tr>" + a + "</table>";
00189     }
00190 
00191     createDialog(actWin, automatic);
00192     drklash_view->setText(s);
00193     drklash->show();
00194     drklash->raise();
00195 
00196     // dlg will be destroyed before returning
00197 }
00198 
00199 #include "kcheckaccelerators.moc"
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jan 21 09:57:07 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003