KDEUI
kcheckaccelerators.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define INCLUDE_MENUITEM_DEF
00023
00024 #include "kcheckaccelerators.h"
00025
00026 #include <config.h>
00027
00028 #include <QApplication>
00029 #include <QCheckBox>
00030 #include <QDialog>
00031 #include <QShortcutEvent>
00032 #include <QMouseEvent>
00033 #include <QLayout>
00034 #include <QMenuBar>
00035 #include <QMetaObject>
00036 #include <QPushButton>
00037 #include <QTabBar>
00038
00039 #include <QLabel>
00040 #include <QComboBox>
00041 #include <QGroupBox>
00042 #include <QClipboard>
00043 #include <QProcess>
00044
00045 #include <kconfig.h>
00046 #include <kdebug.h>
00047 #include <kglobal.h>
00048 #include <kcomponentdata.h>
00049 #include <klocale.h>
00050 #include <kshortcut.h>
00051 #include <ktextbrowser.h>
00052
00053 #include "kacceleratormanager.h"
00054 #include <kconfiggroup.h>
00055
00056 void KCheckAccelerators::initiateIfNeeded(QObject* parent)
00057 {
00058 KConfigGroup cg( KGlobal::config(), "Development" );
00059 QString sKey = cg.readEntry( "CheckAccelerators" ).trimmed();
00060 int key=0;
00061 if( !sKey.isEmpty() ) {
00062 KShortcut cuts( sKey );
00063 if( !cuts.isEmpty() )
00064 key = cuts.primary()[0];
00065 }
00066 bool autoCheck = cg.readEntry( "AutoCheckAccelerators", true );
00067 bool copyWidgetText = cg.readEntry( "CopyWidgetText", false );
00068
00069 if (!copyWidgetText && key==0 && !autoCheck)
00070 return;
00071
00072 new KCheckAccelerators(parent, key, autoCheck, copyWidgetText);
00073 }
00074
00075 KCheckAccelerators::KCheckAccelerators(QObject* parent, int key_, bool autoCheck_, bool copyWidgetText_)
00076 : QObject(parent)
00077 , key(key_)
00078 , block(false)
00079 , autoCheck(autoCheck_)
00080 , copyWidgetText(copyWidgetText_)
00081 , drklash(0)
00082 {
00083 setObjectName( "kapp_accel_filter" );
00084
00085 KConfigGroup cg( KGlobal::config(), "Development" );
00086 alwaysShow = cg.readEntry( "AlwaysShowCheckAccelerators", false );
00087 copyWidgetTextCommand = cg.readEntry( "CopyWidgetTextCommand", "" );
00088
00089 parent->installEventFilter( this );
00090 connect( &autoCheckTimer, SIGNAL(timeout()), SLOT(autoCheckSlot()));
00091 }
00092
00093 bool KCheckAccelerators::eventFilter(QObject* obj, QEvent* e)
00094 {
00095 if ( block )
00096 return false;
00097
00098 switch ( e->type() ) {
00099 case QEvent::ShortcutOverride:
00100 if ( key && (static_cast<QKeyEvent*>(e)->key() == key) ) {
00101 block = true;
00102 checkAccelerators( false );
00103 block = false;
00104 e->accept();
00105 return true;
00106 }
00107 break;
00108 case QEvent::ChildAdded:
00109 case QEvent::ChildRemoved:
00110
00111 if ( !static_cast<QChildEvent *>(e)->child()->isWidgetType() )
00112 break;
00113
00114 case QEvent::Resize:
00115 case QEvent::LayoutRequest:
00116 case QEvent::WindowActivate:
00117 case QEvent::WindowDeactivate:
00118 if( autoCheck ) {
00119 autoCheckTimer.setSingleShot( true );
00120 autoCheckTimer.start( 20 );
00121 }
00122 break;
00123
00124 case QEvent::MouseButtonPress:
00125 if ( copyWidgetText && static_cast<QMouseEvent*>(e)->button() == Qt::MidButton ) {
00126
00127 QWidget* w=static_cast<QWidget*>(obj)->childAt(static_cast<QMouseEvent*>(e)->pos());
00128 if (!w)
00129 w=static_cast<QWidget*>(obj);
00130 if (!w)
00131 return false;
00132
00133 QString text;
00134 if (qobject_cast<QLabel*>(w))
00135 text=static_cast<QLabel*>(w)->text();
00136 else if (qobject_cast<QAbstractButton*>(w))
00137 text=static_cast<QAbstractButton*>(w)->text();
00138 else if (qobject_cast<QComboBox*>(w))
00139 text=static_cast<QComboBox*>(w)->currentText();
00140 else if (qobject_cast<QTabBar*>(w))
00141 text=static_cast<QTabBar*>(w)->tabText( static_cast<QTabBar*>(w)->tabAt(static_cast<QMouseEvent*>(e)->pos()) );
00142 else if (qobject_cast<QGroupBox*>(w))
00143 text=static_cast<QGroupBox*>(w)->title();
00144 else if (qobject_cast<QMenu*>(obj))
00145 {
00146 QAction* a=static_cast<QMenu*>(obj)->actionAt(static_cast<QMouseEvent*>(e)->pos());
00147 if (!a)
00148 return false;
00149 text=a->text();
00150 if (text.isEmpty())
00151 text=a->iconText();
00152 }
00153 if (text.isEmpty())
00154 return false;
00155
00156 if (static_cast<QMouseEvent*>(e)->modifiers() == Qt::ControlModifier)
00157 text.remove('&');
00158
00159
00160 if (copyWidgetTextCommand.isEmpty())
00161 {
00162 QClipboard *clipboard = QApplication::clipboard();
00163 clipboard->setText(text);
00164 }
00165 else
00166 {
00167 QProcess* script=new QProcess(this);
00168 script->start(copyWidgetTextCommand.arg(text).arg(KGlobal::activeComponent().catalogName()));
00169 connect(script,SIGNAL(finished(int,QProcess::ExitStatus)),script,SLOT(deleteLater()));
00170 }
00171 e->accept();
00172 return true;
00173
00174
00175 }
00176 return false;
00177 case QEvent::Timer:
00178 case QEvent::MouseMove:
00179 case QEvent::Paint:
00180 return false;
00181 default:
00182
00183 break;
00184 }
00185 return false;
00186 }
00187
00188 void KCheckAccelerators::autoCheckSlot()
00189 {
00190 if( block )
00191 {
00192 autoCheckTimer.setSingleShot( true );
00193 autoCheckTimer.start( 20 );
00194 return;
00195 }
00196 block = true;
00197 checkAccelerators( !alwaysShow );
00198 block = false;
00199 }
00200
00201 void KCheckAccelerators::createDialog(QWidget *actWin, bool automatic)
00202 {
00203 if ( drklash )
00204 return;
00205
00206 drklash = new QDialog( actWin );
00207 drklash->setAttribute( Qt::WA_DeleteOnClose );
00208 drklash->setObjectName( "kapp_accel_check_dlg" );
00209 drklash->setWindowTitle( i18nc("@title:window", "Dr. Klash' Accelerator Diagnosis" ));
00210 drklash->resize( 500, 460 );
00211 QVBoxLayout* layout = new QVBoxLayout( drklash );
00212 layout->setMargin( 11 );
00213 layout->setSpacing( 6 );
00214 drklash_view = new KTextBrowser( drklash );
00215 layout->addWidget( drklash_view);
00216 QCheckBox* disableAutoCheck = NULL;
00217 if( automatic ) {
00218 disableAutoCheck = new QCheckBox( i18nc("@option:check","Disable automatic checking" ), drklash );
00219 connect(disableAutoCheck, SIGNAL(toggled(bool)), SLOT(slotDisableCheck(bool)));
00220 layout->addWidget( disableAutoCheck );
00221 }
00222 QPushButton* btnClose = new QPushButton( i18nc("@action:button", "Close" ), drklash );
00223 btnClose->setDefault( true );
00224 layout->addWidget( btnClose );
00225 connect( btnClose, SIGNAL(clicked()), drklash, SLOT(close()) );
00226 if (disableAutoCheck)
00227 disableAutoCheck->setFocus();
00228 else
00229 drklash_view->setFocus();
00230 }
00231
00232 void KCheckAccelerators::slotDisableCheck(bool on)
00233 {
00234 autoCheck = !on;
00235 if (!on)
00236 autoCheckSlot();
00237 }
00238
00239 void KCheckAccelerators::checkAccelerators( bool automatic )
00240 {
00241 QWidget* actWin = qApp->activeWindow();
00242 if ( !actWin )
00243 return;
00244
00245 KAcceleratorManager::manage(actWin);
00246 QString a, c, r;
00247 KAcceleratorManager::last_manage(a, c, r);
00248
00249 if (automatic)
00250 return;
00251
00252 if (c.isEmpty() && r.isEmpty() && (automatic || a.isEmpty()))
00253 return;
00254
00255 QString s;
00256
00257 if ( ! c.isEmpty() ) {
00258 s += i18n("<h2>Accelerators changed</h2>");
00259 s += "<table border><tr><th><b>Old Text</b></th><th><b>New Text</b></th></tr>"
00260 + c + "</table>";
00261 }
00262
00263 if ( ! r.isEmpty() ) {
00264 s += i18n("<h2>Accelerators removed</h2>");
00265 s += "<table border><tr><th><b>Old Text</b></th></tr>" + r + "</table>";
00266 }
00267
00268 if ( ! a.isEmpty() ) {
00269 s += i18n("<h2>Accelerators added (just for your info)</h2>");
00270 s += "<table border><tr><th><b>New Text</b></th></tr>" + a + "</table>";
00271 }
00272
00273 createDialog(actWin, automatic);
00274 drklash_view->setHtml(s);
00275 drklash->show();
00276 drklash->raise();
00277
00278
00279 }
00280
00281 #include "kcheckaccelerators.moc"