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 | |
33 | class KeySequenceHelperPrivate |
34 | { |
35 | public: |
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 | |
45 | KeySequenceHelperPrivate::KeySequenceHelperPrivate(KeySequenceHelper *qq) |
46 | : q(qq) |
47 | , checkAgainstShortcutTypes(KeySequenceEnums::StandardShortcuts | KeySequenceEnums::GlobalShortcuts) |
48 | { |
49 | } |
50 | |
51 | KeySequenceHelper::KeySequenceHelper(QObject *parent) |
52 | : KKeySequenceRecorder(nullptr, parent) |
53 | , d(new KeySequenceHelperPrivate(this)) |
54 | { |
55 | } |
56 | |
57 | KeySequenceHelper::~KeySequenceHelper() |
58 | { |
59 | delete d; |
60 | } |
61 | |
62 | void KeySequenceHelper::updateKeySequence(const QKeySequence &keySequence) |
63 | { |
64 | setCurrentKeySequence(keySequence); |
65 | } |
66 | |
67 | void 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(sender: dialog, signal: &KMessageDialog::finished, context: this, slot: [this]() { |
76 | Q_EMIT questionDialogRejected(); |
77 | }); |
78 | dialog->show(); |
79 | #endif |
80 | } |
81 | |
82 | void 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(primaryAction: KStandardGuiItem::cont(), secondaryAction: KStandardGuiItem::cancel()); |
89 | dialog->setAttribute(Qt::WA_DeleteOnClose); |
90 | dialog->setWindowModality(Qt::WindowModal); |
91 | connect(sender: dialog, signal: &KMessageDialog::finished, context: this, slot: [this](int result) { |
92 | switch (result) { |
93 | case KMessageDialog::PrimaryAction: |
94 | case KMessageDialog::Ok: |
95 | Q_EMIT questionDialogAccepted(); |
96 | break; |
97 | case KMessageDialog::SecondaryAction: |
98 | case KMessageDialog::Cancel: |
99 | Q_EMIT questionDialogRejected(); |
100 | break; |
101 | } |
102 | }); |
103 | dialog->show(); |
104 | #else |
105 | Q_EMIT questionDialogAccepted(); |
106 | #endif |
107 | } |
108 | |
109 | KeySequenceEnums::ShortcutTypes KeySequenceHelper::checkAgainstShortcutTypes() |
110 | { |
111 | return d->checkAgainstShortcutTypes; |
112 | } |
113 | |
114 | void KeySequenceHelper::setCheckAgainstShortcutTypes(KeySequenceEnums::ShortcutTypes types) |
115 | { |
116 | if (d->checkAgainstShortcutTypes != types) { |
117 | d->checkAgainstShortcutTypes = types; |
118 | } |
119 | Q_EMIT checkAgainstShortcutTypesChanged(); |
120 | } |
121 | |
122 | bool KeySequenceHelper::keySequenceIsEmpty(const QKeySequence &keySequence) |
123 | { |
124 | return keySequence.isEmpty(); |
125 | } |
126 | |
127 | QString KeySequenceHelper::keySequenceNativeText(const QKeySequence &keySequence) |
128 | { |
129 | return keySequence.toString(format: QKeySequence::NativeText); |
130 | } |
131 | |
132 | QWindow *KeySequenceHelper::renderWindow(QQuickWindow *quickWindow) |
133 | { |
134 | QWindow *renderWindow = QQuickRenderControl::renderWindowFor(win: 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 |
138 | if (QQmlEngine::objectOwnership(window) == QQmlEngine::CppOwnership) { |
139 | QQmlEngine::setObjectOwnership(window, QQmlEngine::CppOwnership); |
140 | } |
141 | return window; |
142 | } |
143 | |
144 | #include "moc_keysequencehelper.cpp" |
145 | |