12#include "keysequencevalidator.h"
14#include <KLocalizedString>
15#include <KStandardShortcut>
17#include <config-kdeclarative.h>
19#include <KGlobalAccel>
20#include <KGlobalShortcutInfo>
23KeySequenceValidator::KeySequenceValidator(
QObject *parent)
28QKeySequence KeySequenceValidator::currentKeySequence()
const
30 return m_currentKeySequence;
33void KeySequenceValidator::setCurrentKeySequence(
const QKeySequence &sequence)
35 if (m_currentKeySequence == sequence) {
39 m_currentKeySequence = sequence;
40 Q_EMIT currentKeySequenceChanged();
45 return m_validateTypes;
50 if (m_validateTypes == types) {
54 m_validateTypes = types;
55 Q_EMIT validateTypesChanged();
58void KeySequenceValidator::validateSequence(
const QKeySequence &keySequence)
60 ValidationResult result = ValidationResult::Accept;
61 if (m_validateTypes & KeySequenceHelper::GlobalShortcuts) {
62 result = validateGlobalShortcut(keySequence);
65 if (result == ValidationResult::Reject) {
66 Q_EMIT finished(m_currentKeySequence);
70 if (result == ValidationResult::QuestionPending) {
74 if (m_validateTypes & KeySequenceHelper::StandardShortcuts) {
75 result = validateStandardShortcut(keySequence);
78 if (result == ValidationResult::Reject) {
79 Q_EMIT finished(m_currentKeySequence);
80 }
else if (result == ValidationResult::Accept) {
81 Q_EMIT finished(keySequence);
85void KeySequenceValidator::accept()
90 ValidationResult result = ValidationResult::Accept;
91 if (m_validateGlobalPending) {
92 m_validateGlobalPending =
false;
93 if (m_validateTypes & KeySequenceHelper::StandardShortcuts) {
94 result = validateStandardShortcut(keySequence);
98 if (result == ValidationResult::Reject) {
99 Q_EMIT finished(m_currentKeySequence);
100 }
else if (result == ValidationResult::Accept) {
101 Q_EMIT finished(keySequence);
105void KeySequenceValidator::reject()
108 Q_EMIT finished(m_currentKeySequence);
111KeySequenceValidator::ValidationResult KeySequenceValidator::validateGlobalShortcut(
const QKeySequence &keySequence)
118 "The F12 key is reserved on Windows, so cannot be used for a global shortcut.\n"
119 "Please choose another one.");
121 Q_EMIT error(title, message);
122 return ValidationResult::Reject;
124 return ValidationResult::Accept;
126#elif HAVE_KGLOBALACCEL
143 message +=
i18n(
"The '%1' key combination is shadowed by following global actions:\n").
arg(
keySequence.toString());
145 message +=
i18n(
"Action '%1' in context '%2'\n").
arg(info.friendlyName(), info.contextFriendlyName());
149 message +=
i18n(
"The '%1' key combination shadows following global actions:\n").
arg(
keySequence.toString());
151 message +=
i18n(
"Action '%1' in context '%2'\n").
arg(info.friendlyName(), info.contextFriendlyName());
155 Q_EMIT error(title, message);
156 return ValidationResult::Reject;
160 QString title =
i18nc(
"@dialog:title",
"Found Conflict");
163 if (others.
size() == 1) {
164 auto info = others.
at(0);
165 message =
i18n(
"Shortcut '%1' is already assigned to action '%2' of %3.\nDo you want to reassign it?",
168 info.componentFriendlyName());
170 message =
i18n(
"Shortcut '%1' is already assigned to the following actions:\n");
171 for (
const auto &info : std::as_const(others)) {
172 message +=
i18n(
"Action '%1' of %2\n", info.friendlyName(), info.componentFriendlyName());
174 message +=
i18n(
"Do you want to reassign it?");
178 m_validateGlobalPending =
true;
179 Q_EMIT question(title, message);
180 return ValidationResult::QuestionPending;
183 return ValidationResult::Accept;
186KeySequenceValidator::ValidationResult KeySequenceValidator::validateStandardShortcut(
const QKeySequence &keySequence)
189 if (ssc != KStandardShortcut::AccelNone) {
190 QString title =
i18n(
"Conflict with Standard Application Shortcut");
192 "The '%1' key combination is also used for the standard action "
193 "\"%2\" that some applications use.\n"
194 "Do you really want to use it as a global shortcut as well?",
199 Q_EMIT question(title, message);
200 return ValidationResult::QuestionPending;
202 return ValidationResult::Accept;
206#include "moc_keysequencevalidator.cpp"
static QList< KGlobalShortcutInfo > globalShortcutsByKey(const QKeySequence &seq, MatchType type=Equal)
static bool isGlobalShortcutAvailable(const QKeySequence &seq, const QString &component=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & find()
QString label(StandardShortcut id)
const_reference at(qsizetype i) const const
bool isEmpty() const const
qsizetype size() const const
QString arg(Args &&... args) const const
void keySequence(QWidget *widget, const QKeySequence &keySequence)