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 KeySequenceHelper::ShortcutTypes checkAgainstShortcutTypes;
43};
44
45KeySequenceHelperPrivate::KeySequenceHelperPrivate(KeySequenceHelper *qq)
46 : q(qq)
47 , checkAgainstShortcutTypes(KeySequenceHelper::StandardShortcuts | KeySequenceHelper::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#if HAVE_KGLOBALACCEL
65 if (d->checkAgainstShortcutTypes.testFlag(GlobalShortcuts)) {
67 }
68#endif
69
70 setCurrentKeySequence(keySequence);
71}
72
73void KeySequenceHelper::showErrorDialog(const QString &title, const QString &text)
74{
75#ifndef Q_OS_ANDROID
76 auto dialog = new KMessageDialog(KMessageDialog::Error, text);
77 dialog->setIcon(QIcon());
78 dialog->setCaption(title);
79 dialog->setAttribute(Qt::WA_DeleteOnClose);
80 dialog->setWindowModality(Qt::WindowModal);
81 connect(dialog, &KMessageDialog::finished, this, [this]() {
82 Q_EMIT questionDialogRejected();
83 });
84 dialog->show();
85#endif
86}
87
88void KeySequenceHelper::showQuestionDialog(const QString &title, const QString &text)
89{
90#ifndef Q_OS_ANDROID
92 dialog->setIcon(QIcon());
93 dialog->setCaption(title);
94 dialog->setButtons(KStandardGuiItem::cont(), KStandardGuiItem::cancel());
95 dialog->setAttribute(Qt::WA_DeleteOnClose);
96 dialog->setWindowModality(Qt::WindowModal);
97 connect(dialog, &KMessageDialog::finished, this, [this](int result) {
98 switch (result) {
101 Q_EMIT questionDialogAccepted();
102 break;
105 Q_EMIT questionDialogRejected();
106 break;
107 }
108 });
109 dialog->show();
110#else
111 Q_EMIT questionDialogAccepted();
112#endif
113}
114
115KeySequenceHelper::ShortcutTypes KeySequenceHelper::checkAgainstShortcutTypes()
116{
117 return d->checkAgainstShortcutTypes;
118}
119
120void KeySequenceHelper::setCheckAgainstShortcutTypes(KeySequenceHelper::ShortcutTypes types)
121{
122 if (d->checkAgainstShortcutTypes != types) {
123 d->checkAgainstShortcutTypes = types;
124 }
125 Q_EMIT checkAgainstShortcutTypesChanged();
126}
127
128bool KeySequenceHelper::keySequenceIsEmpty(const QKeySequence &keySequence)
129{
130 return keySequence.isEmpty();
131}
132
133QString KeySequenceHelper::keySequenceNativeText(const QKeySequence &keySequence)
134{
135 return keySequence.toString(QKeySequence::NativeText);
136}
137
138QWindow *KeySequenceHelper::renderWindow(QQuickWindow *quickWindow)
139{
140 QWindow *renderWindow = QQuickRenderControl::renderWindowFor(quickWindow);
141 auto window = renderWindow ? renderWindow : quickWindow;
142 // If we have CppOwnership, set it explicitly to prevent the engine taking ownership of the window
143 // and crashing on teardown
146 }
147 return window;
148}
149
150#include "moc_keysequencehelper.cpp"
static void stealShortcutSystemwide(const QKeySequence &seq)
KGuiItem cont()
KGuiItem cancel()
void finished(int result)
bool testFlag(Enum flag) const const
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-2024 The KDE developers.
Generated on Fri Aug 30 2024 11:50:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.