• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • shortcuts
kcheckaccelerators.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
3 Copyright (C) 1998, 1999, 2000 KDE Team
4 Copyright (C) 2008 Nick Shaforostoff <shaforostoff@kde.ru>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20 */
21
22#define INCLUDE_MENUITEM_DEF
23
24#include "kcheckaccelerators.h"
25
26#include <config.h>
27
28#include <QApplication>
29#include <QCheckBox>
30#include <QDialog>
31#include <QShortcutEvent>
32#include <QMouseEvent>
33#include <QLayout>
34#include <QMenuBar>
35#include <QMetaObject>
36#include <QPushButton>
37#include <QTabBar>
38
39#include <QLabel>
40#include <QComboBox>
41#include <QGroupBox>
42#include <QClipboard>
43#include <QProcess>
44
45#include <kconfig.h>
46#include <kdebug.h>
47#include <kglobal.h>
48#include <kcomponentdata.h>
49#include <klocale.h>
50#include <kshortcut.h>
51#include <ktextbrowser.h>
52
53#include "kacceleratormanager.h"
54#include <kconfiggroup.h>
55
56void KCheckAccelerators::initiateIfNeeded(QObject* parent)
57{
58 KConfigGroup cg( KGlobal::config(), "Development" );
59 QString sKey = cg.readEntry( "CheckAccelerators" ).trimmed();
60 int key=0;
61 if( !sKey.isEmpty() ) {
62 KShortcut cuts( sKey );
63 if( !cuts.isEmpty() )
64 key = cuts.primary()[0];
65 }
66 bool autoCheck = cg.readEntry( "AutoCheckAccelerators", true );
67 bool copyWidgetText = cg.readEntry( "CopyWidgetText", false );
68
69 if (!copyWidgetText && key==0 && !autoCheck)
70 return;
71
72 new KCheckAccelerators(parent, key, autoCheck, copyWidgetText);
73}
74
75KCheckAccelerators::KCheckAccelerators(QObject* parent, int key_, bool autoCheck_, bool copyWidgetText_)
76 : QObject(parent)
77 , key(key_)
78 , block(false)
79 , autoCheck(autoCheck_)
80 , copyWidgetText(copyWidgetText_)
81 , drklash(0)
82{
83 setObjectName( "kapp_accel_filter" );
84
85 KConfigGroup cg( KGlobal::config(), "Development" );
86 alwaysShow = cg.readEntry( "AlwaysShowCheckAccelerators", false );
87 copyWidgetTextCommand = cg.readEntry( "CopyWidgetTextCommand", "" );
88
89 parent->installEventFilter( this );
90 connect( &autoCheckTimer, SIGNAL(timeout()), SLOT(autoCheckSlot()));
91}
92
93bool KCheckAccelerators::eventFilter(QObject* obj, QEvent* e)
94{
95 if ( block )
96 return false;
97
98 switch ( e->type() ) { // just simplify debuggin
99 case QEvent::ShortcutOverride:
100 if ( key && (static_cast<QKeyEvent*>(e)->key() == key) ) {
101 block = true;
102 checkAccelerators( false );
103 block = false;
104 e->accept();
105 return true;
106 }
107 break;
108 case QEvent::ChildAdded:
109 case QEvent::ChildRemoved:
110 // Only care about widgets; this also avoids starting the timer in other threads
111 if ( !static_cast<QChildEvent *>(e)->child()->isWidgetType() )
112 break;
113 // fall-through
114 case QEvent::Resize:
115 case QEvent::LayoutRequest:
116 case QEvent::WindowActivate:
117 case QEvent::WindowDeactivate:
118 if( autoCheck ) {
119 autoCheckTimer.setSingleShot( true );
120 autoCheckTimer.start( 20 ); // 20 ms
121 }
122 break;
123 //case QEvent::MouseButtonDblClick:
124 case QEvent::MouseButtonPress:
125 if ( copyWidgetText && static_cast<QMouseEvent*>(e)->button() == Qt::MidButton ) {
126 //kWarning()<<"obj"<<obj;
127 QWidget* w=static_cast<QWidget*>(obj)->childAt(static_cast<QMouseEvent*>(e)->pos());
128 if (!w)
129 w=static_cast<QWidget*>(obj);
130 if (!w)
131 return false;
132 //kWarning()<<"MouseButtonDblClick"<<w;
133 QString text;
134 if (qobject_cast<QLabel*>(w))
135 text=static_cast<QLabel*>(w)->text();
136 else if (qobject_cast<QAbstractButton*>(w))
137 text=static_cast<QAbstractButton*>(w)->text();
138 else if (qobject_cast<QComboBox*>(w))
139 text=static_cast<QComboBox*>(w)->currentText();
140 else if (qobject_cast<QTabBar*>(w))
141 text=static_cast<QTabBar*>(w)->tabText( static_cast<QTabBar*>(w)->tabAt(static_cast<QMouseEvent*>(e)->pos()) );
142 else if (qobject_cast<QGroupBox*>(w))
143 text=static_cast<QGroupBox*>(w)->title();
144 else if (qobject_cast<QMenu*>(obj))
145 {
146 QAction* a=static_cast<QMenu*>(obj)->actionAt(static_cast<QMouseEvent*>(e)->pos());
147 if (!a)
148 return false;
149 text=a->text();
150 if (text.isEmpty())
151 text=a->iconText();
152 }
153 if (text.isEmpty())
154 return false;
155
156 if (static_cast<QMouseEvent*>(e)->modifiers() == Qt::ControlModifier)
157 text.remove('&');
158
159 //kWarning()<<KGlobal::activeComponent().catalogName()<<text;
160 if (copyWidgetTextCommand.isEmpty())
161 {
162 QClipboard *clipboard = QApplication::clipboard();
163 clipboard->setText(text);
164 }
165 else
166 {
167 QProcess* script=new QProcess(this);
168 script->start(copyWidgetTextCommand.arg(text).arg(KGlobal::activeComponent().catalogName()));
169 connect(script,SIGNAL(finished(int,QProcess::ExitStatus)),script,SLOT(deleteLater()));
170 }
171 e->accept();
172 return true;
173
174 //kWarning()<<"MouseButtonDblClick"<<static_cast<QWidget*>(obj)->childAt(static_cast<QMouseEvent*>(e)->globalPos());
175 }
176 return false;
177 case QEvent::Timer:
178 case QEvent::MouseMove:
179 case QEvent::Paint:
180 return false;
181 default:
182 // kDebug(125) << "KCheckAccelerators::eventFilter " << e->type() << " " << autoCheck;
183 break;
184 }
185 return false;
186}
187
188void KCheckAccelerators::autoCheckSlot()
189{
190 if( block )
191 {
192 autoCheckTimer.setSingleShot( true );
193 autoCheckTimer.start( 20 );
194 return;
195 }
196 block = true;
197 checkAccelerators( !alwaysShow );
198 block = false;
199}
200
201void KCheckAccelerators::createDialog(QWidget *actWin, bool automatic)
202{
203 if ( drklash )
204 return;
205
206 drklash = new QDialog( actWin );
207 drklash->setAttribute( Qt::WA_DeleteOnClose );
208 drklash->setObjectName( "kapp_accel_check_dlg" );
209 drklash->setWindowTitle( i18nc("@title:window", "Dr. Klash' Accelerator Diagnosis" ));
210 drklash->resize( 500, 460 );
211 QVBoxLayout* layout = new QVBoxLayout( drklash );
212 drklash_view = new KTextBrowser( drklash );
213 layout->addWidget( drklash_view);
214 QCheckBox* disableAutoCheck = NULL;
215 if( automatic ) {
216 disableAutoCheck = new QCheckBox( i18nc("@option:check","Disable automatic checking" ), drklash );
217 connect(disableAutoCheck, SIGNAL(toggled(bool)), SLOT(slotDisableCheck(bool)));
218 layout->addWidget( disableAutoCheck );
219 }
220 QPushButton* btnClose = new QPushButton( i18nc("@action:button", "Close" ), drklash );
221 btnClose->setDefault( true );
222 layout->addWidget( btnClose );
223 connect( btnClose, SIGNAL(clicked()), drklash, SLOT(close()) );
224 if (disableAutoCheck)
225 disableAutoCheck->setFocus();
226 else
227 drklash_view->setFocus();
228}
229
230void KCheckAccelerators::slotDisableCheck(bool on)
231{
232 autoCheck = !on;
233 if (!on)
234 autoCheckSlot();
235}
236
237void KCheckAccelerators::checkAccelerators( bool automatic )
238{
239 QWidget* actWin = qApp->activeWindow();
240 if ( !actWin )
241 return;
242
243 KAcceleratorManager::manage(actWin);
244 QString a, c, r;
245 KAcceleratorManager::last_manage(a, c, r);
246
247 if (automatic) // for now we only show dialogs on F12 checks
248 return;
249
250 if (c.isEmpty() && r.isEmpty() && (automatic || a.isEmpty()))
251 return;
252
253 QString s;
254
255 if ( ! c.isEmpty() ) {
256 s += i18n("<h2>Accelerators changed</h2>");
257 s += "<table border><tr><th><b>Old Text</b></th><th><b>New Text</b></th></tr>"
258 + c + "</table>";
259 }
260
261 if ( ! r.isEmpty() ) {
262 s += i18n("<h2>Accelerators removed</h2>");
263 s += "<table border><tr><th><b>Old Text</b></th></tr>" + r + "</table>";
264 }
265
266 if ( ! a.isEmpty() ) {
267 s += i18n("<h2>Accelerators added (just for your info)</h2>");
268 s += "<table border><tr><th><b>New Text</b></th></tr>" + a + "</table>";
269 }
270
271 createDialog(actWin, automatic);
272 drklash_view->setHtml(s);
273 drklash->show();
274 drklash->raise();
275
276 // dlg will be destroyed before returning
277}
278
279#include "kcheckaccelerators.moc"
KAcceleratorManager::last_manage
static void last_manage(QString &added, QString &changed, QString &removed)
Definition: kacceleratormanager.cpp:518
KAcceleratorManager::manage
static void manage(QWidget *widget, bool programmers_mode=false)
Manages the accelerators of a widget.
Definition: kacceleratormanager.cpp:509
KCheckAccelerators
Definition: kcheckaccelerators.h:85
KCheckAccelerators::initiateIfNeeded
static void initiateIfNeeded(QObject *parent)
Creates a KCheckAccelerators instance for the given object if this feature is enabled in kdeglobals.
Definition: kcheckaccelerators.cpp:56
KCheckAccelerators::eventFilter
bool eventFilter(QObject *, QEvent *e)
Re-implemented to filter the parent's events.
Definition: kcheckaccelerators.cpp:93
KComponentData::catalogName
QString catalogName() const
KConfigGroup
KConfigGroup::readEntry
QString readEntry(const char *key, const char *aDefault=0) const
KShortcut
Represents a keyboard shortcut.
Definition: kshortcut.h:58
KShortcut::primary
QKeySequence primary() const
Returns the primary key sequence of this shortcut.
Definition: kshortcut.cpp:134
KShortcut::isEmpty
bool isEmpty() const
Returns whether this shortcut contains any nonempty key sequences.
Definition: kshortcut.cpp:144
KTextBrowser
Extended QTextBrowser.
Definition: ktextbrowser.h:52
QAction
QComboBox
QDialog
QGroupBox
QLabel
QMenu
QObject
QProcess
QPushButton
QTabBar
QWidget
kacceleratormanager.h
kcheckaccelerators.h
kcomponentdata.h
kconfig.h
kconfiggroup.h
kdebug.h
kglobal.h
timeout
int timeout
klocale.h
i18n
QString i18n(const char *text)
i18nc
QString i18nc(const char *ctxt, const char *text)
kshortcut.h
Defines platform-independent classes for keyboard shortcut handling.
ktextbrowser.h
KGlobal::config
KSharedConfigPtr config()
KGlobal::activeComponent
KComponentData activeComponent()
KStandardAction::close
KAction * close(const QObject *recvr, const char *slot, QObject *parent)
Close the current document.
Definition: kstandardaction.cpp:269
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal