Krita Source Code Documentation
Loading...
Searching...
No Matches
KisKKeySequenceWidget Class Reference

A widget to input a QKeySequence. More...

#include <kkeysequencewidget.h>

+ Inheritance diagram for KisKKeySequenceWidget:

Public Types

enum  Validation { Validate = 0 , NoValidate = 1 }
 

Public Member Functions

 KisKKeySequenceWidget (QWidget *parent=0)
 
 ~KisKKeySequenceWidget () override
 

Properties

ShortcutTypes checkForConflictsAgainst
 
bool modifierlessAllowed
 
bool multiKeyShortcutsAllowed
 

Configuration

Configuration options for the widget.

enum  ShortcutType { None = 0x00 , LocalShortcuts = 0x01 , StandardShortcuts = 0x02 , GlobalShortcuts = 0x04 }
 
class KisKKeySequenceWidgetPrivate
 
KisKKeySequenceWidgetPrivate *const d
 
void setCheckForConflictsAgainst (ShortcutTypes types)
 
ShortcutTypes checkForConflictsAgainst () const
 
void setMultiKeyShortcutsAllowed (bool)
 
bool multiKeyShortcutsAllowed () const
 
void setModifierlessAllowed (bool allow)
 
bool isModifierlessAllowed ()
 
void setClearButtonShown (bool show)
 
bool isKeySequenceAvailable (const QKeySequence &seq) const
 
QKeySequence keySequence () const
 
void setCheckActionCollections (const QList< KisKActionCollection * > &actionCollections)
 
void setComponentName (const QString &componentName)
 
void keySequenceChanged (const QKeySequence &seq)
 
void stealShortcut (const QKeySequence &seq, QAction *action)
 
void captureKeySequence ()
 
void setKeySequence (const QKeySequence &seq, Validation val=NoValidate)
 
void clearKeySequence ()
 
void applyStealShortcut ()
 

Detailed Description

A widget to input a QKeySequence.

This widget lets the user choose a QKeySequence, which is usually used as a shortcut key. The recording is initiated by calling captureKeySequence() or the user clicking into the widget.

The widgets provides support for conflict handling. See setCheckForConflictsAgainst() for more information.

KDE Key Sequence Widget
Author
Mark Donohoe donoh.nosp@m.oe@k.nosp@m.de.or.nosp@m.g

Definition at line 36 of file kkeysequencewidget.h.

Member Enumeration Documentation

◆ ShortcutType

Enumerator
None 

No checking for conflicts.

LocalShortcuts 

Check with local shortcuts.

See also
setCheckActionCollections()
StandardShortcuts 

Check against standard shortcuts.

See also
KStandardShortcut
GlobalShortcuts 

Check against global shortcuts.

See also
KGlobalAccel

Definition at line 84 of file kkeysequencewidget.h.

84 {
85 None = 0x00,
86 LocalShortcuts = 0x01,
87 StandardShortcuts = 0x02,
88 GlobalShortcuts = 0x04
89 };
@ GlobalShortcuts
Check against global shortcuts.
@ StandardShortcuts
Check against standard shortcuts.
@ None
No checking for conflicts.
@ LocalShortcuts
Check with local shortcuts.

◆ Validation

An enum about validation when setting a key sequence.

See also
setKeySequence()
Enumerator
Validate 

Validate key sequence.

NoValidate 

Use key sequence without validation.

Definition at line 60 of file kkeysequencewidget.h.

60 {
62 Validate = 0,
64 NoValidate = 1
65 };
@ Validate
Validate key sequence.
@ NoValidate
Use key sequence without validation.

Constructor & Destructor Documentation

◆ KisKKeySequenceWidget()

KisKKeySequenceWidget::KisKKeySequenceWidget ( QWidget * parent = 0)
explicit

Constructor.

Definition at line 203 of file kkeysequencewidget.cpp.

204 : QWidget(parent),
206{
207 d->init();
208 setFocusProxy(d->keyButton);
209 connect(d->keyButton, SIGNAL(clicked()), this, SLOT(captureKeySequence()));
210 connect(d->clearButton, SIGNAL(clicked()), this, SLOT(clearKeySequence()));
211 connect(&d->modifierlessTimeout, SIGNAL(timeout()), this, SLOT(doneRecording()));
212 //TODO: how to adopt style changes at runtime?
213 /*QFont modFont = d->clearButton->font();
214 modFont.setStyleHint(QFont::TypeWriter);
215 d->clearButton->setFont(modFont);*/
217}
friend class KisKKeySequenceWidgetPrivate
KisKKeySequenceWidgetPrivate *const d

References captureKeySequence(), KisKKeySequenceWidgetPrivate::clearButton, clearKeySequence(), d, KisKKeySequenceWidgetPrivate::init(), KisKKeySequenceWidgetPrivate::keyButton, KisKKeySequenceWidgetPrivate::modifierlessTimeout, and KisKKeySequenceWidgetPrivate::updateShortcutDisplay().

◆ ~KisKKeySequenceWidget()

KisKKeySequenceWidget::~KisKKeySequenceWidget ( )
override

Destructs the widget.

Definition at line 236 of file kkeysequencewidget.cpp.

237{
238 delete d;
239}

References d.

Member Function Documentation

◆ applyStealShortcut

void KisKKeySequenceWidget::applyStealShortcut ( )
slot

Actually remove the shortcut that the user wanted to steal, from the action that was using it. This only applies to actions provided to us by setCheckActionCollections() and setCheckActionList().

Global and Standard Shortcuts have to be stolen immediately when the user gives his consent (technical reasons). That means those changes will be active even if you never call applyStealShortcut().

To be called before you apply your changes. No local shortcuts are stolen until this function is called.

Definition at line 341 of file kkeysequencewidget.cpp.

342{
343 QSet<KisKActionCollection *> changedCollections;
344
345 Q_FOREACH (QAction *stealAction, d->stealActions) {
346
347 // Stealing a shortcut means setting it to an empty one.
348 stealAction->setShortcuts(QList<QKeySequence>());
349
350 // The following code will find the action we are about to
351 // steal from and save its action collection.
352 KisKActionCollection *parentCollection = 0;
353 foreach (KisKActionCollection *collection, d->checkActionCollections) {
354 if (collection->actions().contains(stealAction)) {
355 parentCollection = collection;
356 break;
357 }
358 }
359
360 // Remember the changed collection
361 if (parentCollection) {
362 changedCollections.insert(parentCollection);
363 }
364 }
365
366 Q_FOREACH (KisKActionCollection *col, changedCollections) {
367 col->writeSettings();
368 }
369
370 d->stealActions.clear();
371}
A container for a set of QAction objects.
void writeSettings(KConfigGroup *config=0, bool writeScheme=false, QAction *oneAction=0) const
QList< QAction * > actions() const
QList< KisKActionCollection * > checkActionCollections

References KisKActionCollection::actions(), KisKKeySequenceWidgetPrivate::checkActionCollections, d, KisKKeySequenceWidgetPrivate::stealActions, and KisKActionCollection::writeSettings().

◆ captureKeySequence

void KisKKeySequenceWidget::captureKeySequence ( )
slot

Capture a shortcut from the keyboard. This call will only return once a key sequence has been captured or input was aborted. If a key sequence was input, keySequenceChanged() will be emitted.

See also
setModifierlessAllowed()

Definition at line 309 of file kkeysequencewidget.cpp.

References d, and KisKKeySequenceWidgetPrivate::startRecording().

◆ checkForConflictsAgainst()

KisKKeySequenceWidget::ShortcutTypes KisKKeySequenceWidget::checkForConflictsAgainst ( ) const

The shortcut types we check for conflicts.

See also
setCheckForConflictsAgainst()
Since
4.2

Definition at line 241 of file kkeysequencewidget.cpp.

242{
244}
KisKKeySequenceWidget::ShortcutTypes checkAgainstShortcutTypes
Check the key sequence against KStandardShortcut::find()

References KisKKeySequenceWidgetPrivate::checkAgainstShortcutTypes, and d.

◆ clearKeySequence

void KisKKeySequenceWidget::clearKeySequence ( )
slot

Clear the key sequence.

Definition at line 335 of file kkeysequencewidget.cpp.

336{
337 setKeySequence(QKeySequence());
338}
void setKeySequence(const QKeySequence &seq, Validation val=NoValidate)

References setKeySequence().

◆ isKeySequenceAvailable()

bool KisKKeySequenceWidget::isKeySequenceAvailable ( const QKeySequence & seq) const

Checks whether the key sequence seq is available to grab.

The sequence is checked under the same rules as if it has been typed by the user. This method is useful if you get key sequences from another input source and want to check if it is save to set them.

Since
4.2

Definition at line 271 of file kkeysequencewidget.cpp.

272{
273 if (keySequence.isEmpty()) {
274 // qDebug() << "Key sequence" << keySequence.toString() << "is empty and available.";
275 return true;
276 }
277
278 bool hasConflict = (d->conflictWithLocalShortcuts(keySequence)
281
282 if (hasConflict) {
283 /* qInfo() << "Key sequence" << keySequence.toString() << "has an unresolvable conflict." <<
284 QString("Local conflict: %1. Windows conflict: %2. Standard Shortcut conflict: %3") \
285 .arg(d->conflictWithLocalShortcuts(keySequence)) \
286 .arg(d->conflictWithGlobalShortcuts(keySequence)) \
287 .arg(d->conflictWithStandardShortcuts(keySequence)); */
288 }
289 return !(hasConflict);
290
291}
bool conflictWithStandardShortcuts(const QKeySequence &seq)
bool conflictWithGlobalShortcuts(const QKeySequence &seq)
bool conflictWithLocalShortcuts(const QKeySequence &seq)
QKeySequence keySequence() const

References KisKKeySequenceWidgetPrivate::conflictWithGlobalShortcuts(), KisKKeySequenceWidgetPrivate::conflictWithLocalShortcuts(), KisKKeySequenceWidgetPrivate::conflictWithStandardShortcuts(), d, and keySequence().

◆ isModifierlessAllowed()

bool KisKKeySequenceWidget::isModifierlessAllowed ( )

◆ keySequence()

QKeySequence KisKKeySequenceWidget::keySequence ( ) const

Return the currently selected key sequence.

Definition at line 314 of file kkeysequencewidget.cpp.

315{
316 return d->keySequence;
317}

References d, and KisKKeySequenceWidgetPrivate::keySequence.

◆ keySequenceChanged

void KisKKeySequenceWidget::keySequenceChanged ( const QKeySequence & seq)
signal

This signal is emitted when the current key sequence has changed, be it by user input or programmatically.

◆ multiKeyShortcutsAllowed()

bool KisKKeySequenceWidget::multiKeyShortcutsAllowed ( ) const

◆ setCheckActionCollections()

void KisKKeySequenceWidget::setCheckActionCollections ( const QList< KisKActionCollection * > & actionCollections)

Set a list of action collections to check against for conflicting shortcut.

See also
setCheckForConflictsAgainst()

If a KAction with a conflicting shortcut is found inside this list and its shortcut can be configured (KAction::isShortcutConfigurable() returns true) the user will be prompted whether to steal the shortcut from this action.

Since
4.1

Definition at line 303 of file kkeysequencewidget.cpp.

304{
305 d->checkActionCollections = actionCollections;
306}

References KisKKeySequenceWidgetPrivate::checkActionCollections, and d.

◆ setCheckForConflictsAgainst()

void KisKKeySequenceWidget::setCheckForConflictsAgainst ( ShortcutTypes types)

Configure if the widget should check for conflicts with existing shortcuts.

When capturing a key sequence for local shortcuts you should check against GlobalShortcuts and your other local shortcuts. This is the default.

You have to provide the local actions to check against with setCheckActionCollections().

When capturing a key sequence for a global shortcut you should check against StandardShortcuts, GlobalShortcuts and your local shortcuts.

There are two ways to react to a user agreeing to steal a shortcut:

  1. Listen to the stealShortcut() signal and steal the shortcuts manually. It's your responsibility to save that change later when you think it is appropriate.
  2. Call applyStealShortcut and KisKKeySequenceWidget will steal the shortcut. This will save the actionCollections the shortcut is part of so make sure it doesn't inadvertently save some unwanted changes too. Read its documentation for some limitation when handling global shortcuts.

If you want to do the conflict checking yourself here are some code snippets for global ...

QStringList conflicting = KGlobalAccel::findActionNameSystemwide(keySequence);
if (!conflicting.isEmpty()) {
// Inform and ask the user about the conflict and reassigning
// the keys sequence
if (!KGlobalAccel::promptStealShortcutSystemwide(q, conflicting, keySequence)) {
return true;
}
KGlobalAccel::stealShortcutSystemwide(keySequence);
}

... and standard shortcuts

KStandardShortcut::StandardShortcut ssc = KStandardShortcut::find(keySequence);
if (ssc != KStandardShortcut::AccelNone) {
// We have a conflict
}
Since
4.2

Definition at line 261 of file kkeysequencewidget.cpp.

262{
264}

References KisKKeySequenceWidgetPrivate::checkAgainstShortcutTypes, and d.

◆ setClearButtonShown()

void KisKKeySequenceWidget::setClearButtonShown ( bool show)

Set whether a small button to set an empty key sequence should be displayed next to the main input widget. The default is to show the clear button.

Definition at line 298 of file kkeysequencewidget.cpp.

299{
300 d->clearButton->setVisible(show);
301}

References KisKKeySequenceWidgetPrivate::clearButton, and d.

◆ setComponentName()

void KisKKeySequenceWidget::setComponentName ( const QString & componentName)

If the component using this widget supports shortcuts contexts, it has to set its component name so we can check conflicts correctly.

Definition at line 246 of file kkeysequencewidget.cpp.

247{
248 d->componentName = componentName;
249}

References KisKKeySequenceWidgetPrivate::componentName, and d.

◆ setKeySequence

void KisKKeySequenceWidget::setKeySequence ( const QKeySequence & seq,
Validation val = NoValidate )
slot

Set the key sequence.

If val == Validate, and the call is actually changing the key sequence, conflicting shortcut will be checked.

Definition at line 320 of file kkeysequencewidget.cpp.

321{
322 // oldKeySequence holds the key sequence before recording started, if setKeySequence()
323 // is called while not recording then set oldKeySequence to the existing sequence so
324 // that the keySequenceChanged() signal is emitted if the new and previous key
325 // sequences are different
326 if (!d->isRecording) {
328 }
329
330 d->keySequence = seq;
331 d->doneRecording(validate == Validate);
332}
void doneRecording(bool validate=true)

References d, KisKKeySequenceWidgetPrivate::doneRecording(), KisKKeySequenceWidgetPrivate::isRecording, KisKKeySequenceWidgetPrivate::keySequence, KisKKeySequenceWidgetPrivate::oldKeySequence, and Validate.

◆ setModifierlessAllowed()

void KisKKeySequenceWidget::setModifierlessAllowed ( bool allow)

This only applies to user input, not to setShortcut(). Set whether to accept "plain" keys without modifiers (like Ctrl, Alt, Meta). Plain keys by our definition include letter and symbol keys and text editing keys (Return, Space, Tab, Backspace, Delete). "Special" keys like F1, Cursor keys, Insert, PageDown will always work.

Definition at line 266 of file kkeysequencewidget.cpp.

267{
268 d->allowModifierless = allow;
269}

References KisKKeySequenceWidgetPrivate::allowModifierless, and d.

◆ setMultiKeyShortcutsAllowed()

void KisKKeySequenceWidget::setMultiKeyShortcutsAllowed ( bool allowed)

Allow multikey shortcuts?

Definition at line 256 of file kkeysequencewidget.cpp.

257{
258 d->multiKeyShortcutsAllowed = allowed;
259}

References d, and KisKKeySequenceWidgetPrivate::multiKeyShortcutsAllowed.

◆ stealShortcut

void KisKKeySequenceWidget::stealShortcut ( const QKeySequence & seq,
QAction * action )
signal

This signal is emitted after the user agreed to steal a shortcut from an action. This is only done for local shortcuts. So you can be sure action is one of the actions you provided with setCheckActionList() or setCheckActionCollections().

If you listen to that signal and don't call applyStealShortcut() you are supposed to steal the shortcut and save this change.

Friends And Related Symbol Documentation

◆ KisKKeySequenceWidgetPrivate

friend class KisKKeySequenceWidgetPrivate
friend

Definition at line 281 of file kkeysequencewidget.h.

Member Data Documentation

◆ d

KisKKeySequenceWidgetPrivate* const KisKKeySequenceWidget::d
private

Definition at line 282 of file kkeysequencewidget.h.

Property Documentation

◆ checkForConflictsAgainst

ShortcutTypes KisKKeySequenceWidget::checkForConflictsAgainst
readwrite

Definition at line 47 of file kkeysequencewidget.h.

◆ modifierlessAllowed

bool KisKKeySequenceWidget::modifierlessAllowed
readwrite

Definition at line 52 of file kkeysequencewidget.h.

◆ multiKeyShortcutsAllowed

bool KisKKeySequenceWidget::multiKeyShortcutsAllowed
readwrite

Definition at line 42 of file kkeysequencewidget.h.


The documentation for this class was generated from the following files: