KDeclarative

keysequencehelper.cpp
1/*
2 SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
3 SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org>
4 SPDX-FileCopyrightText: 1998 Mark Donohoe <donohoe@kde.org>
5 SPDX-FileCopyrightText: 2001 Ellis Whitehead <ellis@kde.org>
6 SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
7
8 SPDX-License-Identifier: LGPL-2.1-or-later
9*/
10
11#include "keysequencehelper.h"
12
13#include <QDebug>
14#include <QHash>
15#include <QPointer>
16#include <QQmlEngine>
17#include <QQuickRenderControl>
18#include <QQuickWindow>
19
20#include <KLocalizedString>
21#include <KStandardShortcut>
22
23#ifndef Q_OS_ANDROID
24#include <KMessageDialog>
25#endif
26
27#include <config-kdeclarative.h>
28#if HAVE_KGLOBALACCEL
29#include <KGlobalAccel>
30#include <KGlobalShortcutInfo>
31#endif
32
33class KeySequenceHelperPrivate
34{
35public:
36 KeySequenceHelperPrivate(KeySequenceHelper *qq);
37
38 // members
39 KeySequenceHelper *const q;
40
41 //! Check the key sequence against KStandardShortcut::find()
42 KeySequenceEnums::ShortcutTypes checkAgainstShortcutTypes;
43};
44
45KeySequenceHelperPrivate::KeySequenceHelperPrivate(KeySequenceHelper *qq)
46 : q(qq)
47 , checkAgainstShortcutTypes(KeySequenceEnums::StandardShortcuts | KeySequenceEnums::GlobalShortcuts)
48{
49}
50
51KeySequenceHelper::KeySequenceHelper(QObject *parent)
52 : KKeySequenceRecorder(nullptr, parent)
53 , d(new KeySequenceHelperPrivate(this))
54{
55}
56
57KeySequenceHelper::~KeySequenceHelper()
58{
59 delete d;
60}
61
62void KeySequenceHelper::updateKeySequence(const QKeySequence &keySequence)
63{
64 setCurrentKeySequence(keySequence);
65}
66
67void KeySequenceHelper::showErrorDialog(const QString &title, const QString &text)
68{
69#ifndef Q_OS_ANDROID
70 auto dialog = new KMessageDialog(KMessageDialog::Error, text);
71 dialog->setIcon(QIcon());
72 dialog->setCaption(title);
73 dialog->setAttribute(Qt::WA_DeleteOnClose);
74 dialog->setWindowModality(Qt::WindowModal);
75 connect(dialog, &KMessageDialog::finished, this, [this]() {
76 Q_EMIT questionDialogRejected();
77 });
78 dialog->show();
79#endif
80}
81
82void KeySequenceHelper::showQuestionDialog(const QString &title, const QString &text)
83{
84#ifndef Q_OS_ANDROID
85 auto dialog = new KMessageDialog(KMessageDialog::QuestionTwoActions, text);
86 dialog->setIcon(QIcon());
87 dialog->setCaption(title);
88 dialog->setButtons(KStandardGuiItem::cont(), KStandardGuiItem::cancel());
89 dialog->setAttribute(Qt::WA_DeleteOnClose);
90 dialog->setWindowModality(Qt::WindowModal);
91 connect(dialog, &KMessageDialog::finished, this, [this](int result) {
92 switch (result) {
95 Q_EMIT questionDialogAccepted();
96 break;
99 Q_EMIT questionDialogRejected();
100 break;
101 }
102 });
103 dialog->show();
104#else
105 Q_EMIT questionDialogAccepted();
106#endif
107}
108
109KeySequenceEnums::ShortcutTypes KeySequenceHelper::checkAgainstShortcutTypes()
110{
111 return d->checkAgainstShortcutTypes;
112}
113
114void KeySequenceHelper::setCheckAgainstShortcutTypes(KeySequenceEnums::ShortcutTypes types)
115{
116 if (d->checkAgainstShortcutTypes != types) {
117 d->checkAgainstShortcutTypes = types;
118 }
119 Q_EMIT checkAgainstShortcutTypesChanged();
120}
121
122bool KeySequenceHelper::keySequenceIsEmpty(const QKeySequence &keySequence)
123{
124 return keySequence.isEmpty();
125}
126
127QString KeySequenceHelper::keySequenceNativeText(const QKeySequence &keySequence)
128{
129 return keySequence.toString(QKeySequence::NativeText);
130}
131
132QWindow *KeySequenceHelper::renderWindow(QQuickWindow *quickWindow)
133{
134 QWindow *renderWindow = QQuickRenderControl::renderWindowFor(quickWindow);
135 auto window = renderWindow ? renderWindow : quickWindow;
136 // If we have CppOwnership, set it explicitly to prevent the engine taking ownership of the window
137 // and crashing on teardown
140 }
141 return window;
142}
143
144#include "moc_keysequencehelper.cpp"
KGuiItem cont()
KGuiItem cancel()
void finished(int result)
ObjectOwnership objectOwnership(QObject *object)
void setObjectOwnership(QObject *object, ObjectOwnership ownership)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QWindow * renderWindowFor(QQuickWindow *win, QPoint *offset)
WA_DeleteOnClose
WindowModal
void keySequence(QWidget *widget, const QKeySequence &keySequence)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 11 2025 11:51:39 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.