1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
6 ** This file is part of the QtQuick module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
32 ****************************************************************************/
34 #include "qquickshortcut_p.h"
36 #include <QtQuick/qquickitem.h>
37 #include <QtQuick/qquickwindow.h>
38 #include <QtGui/private/qguiapplication_p.h>
44 \instantiates QQuickShortcut
47 \ingroup qtquick-input
48 \brief Provides keyboard shortcuts
50 The Shortcut type provides a way of handling keyboard shortcuts. The shortcut can
51 be set to one of the \l{QKeySequence::StandardKey}{standard keyboard shortcuts},
52 or it can be described with a string containing a sequence of up to four key
53 presses that are needed to \l{Shortcut::activated}{activate} the shortcut.
59 property int currentIndex
62 sequence: StandardKey.NextChild
63 onActivated: view.currentIndex++
71 /*! \qmlsignal QtQuick::Shortcut::activated()
73 This signal is emitted when the shortcut is activated.
75 The corresponding handler is \c onActivated.
78 /*! \qmlsignal QtQuick::Shortcut::activatedAmbiguously()
80 This signal is emitted when the shortcut is activated ambigously,
81 meaning that it matches the start of more than one shortcut.
83 The corresponding handler is \c onActivatedAmbiguously.
86 QQuickShortcut::QQuickShortcut(QObject *parent) : QObject(parent), m_id(0),
87 m_enabled(true), m_completed(false), m_autorepeat(true), m_context(Qt::WindowShortcut)
91 QQuickShortcut::~QQuickShortcut()
97 \qmlproperty keysequence QtQuick::Shortcut::sequence
99 This property holds the shortcut's key sequence. The key sequence can be set
100 to one of the \l{QKeySequence::StandardKey}{standard keyboard shortcuts}, or
101 it can be described with a string containing a sequence of up to four key
102 presses that are needed to \l{Shortcut::activated}{activate} the shortcut.
104 The default value is an empty key sequence.
108 sequence: "Ctrl+E,Ctrl+W"
109 onActivated: edit.wrapMode = TextEdit.Wrap
113 QVariant QQuickShortcut::sequence() const
118 void QQuickShortcut::setSequence(const QVariant &sequence)
120 if (sequence == m_sequence)
123 QKeySequence shortcut;
124 if (sequence.type() == QVariant::Int)
125 shortcut = QKeySequence(static_cast<QKeySequence::StandardKey>(sequence.toInt()));
127 shortcut = QKeySequence::fromString(sequence.toString());
129 grabShortcut(shortcut, m_context);
131 m_sequence = sequence;
132 m_shortcut = shortcut;
133 emit sequenceChanged();
137 \qmlproperty string QtQuick::Shortcut::sequenceString
140 This property provides the shortcut's key sequence as a string,
141 for display purposes (tooltips, for example).
145 QString QQuickShortcut::sequenceString() const
147 return m_shortcut.toString(QKeySequence::NativeText);
151 \qmlproperty bool QtQuick::Shortcut::enabled
153 This property holds whether the shortcut is enabled.
155 The default value is \c true.
157 bool QQuickShortcut::isEnabled() const
162 void QQuickShortcut::setEnabled(bool enabled)
164 if (enabled == m_enabled)
168 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutEnabled(enabled, m_id, this);
171 emit enabledChanged();
175 \qmlproperty bool QtQuick::Shortcut::autoRepeat
177 This property holds whether the shortcut can auto repeat.
179 The default value is \c true.
181 bool QQuickShortcut::autoRepeat() const
186 void QQuickShortcut::setAutoRepeat(bool repeat)
188 if (repeat == m_autorepeat)
192 QGuiApplicationPrivate::instance()->shortcutMap.setShortcutAutoRepeat(repeat, m_id, this);
194 m_autorepeat = repeat;
195 emit autoRepeatChanged();
199 \qmlproperty enumeration QtQuick::Shortcut::context
201 This property holds the \l{Qt::ShortcutContext}{shortcut context}.
203 Supported values are:
205 \li \c Qt.WindowShortcut (default) - The shortcut is active when its parent item is in an active top-level window.
206 \li \c Qt.ApplicationShortcut - The shortcut is active when one of the application's windows are active.
211 sequence: StandardKey.Quit
212 context: Qt.ApplicationShortcut
213 onActivated: Qt.quit()
217 Qt::ShortcutContext QQuickShortcut::context() const
222 void QQuickShortcut::setContext(Qt::ShortcutContext context)
224 if (context == m_context)
227 grabShortcut(m_shortcut, context);
230 emit contextChanged();
233 void QQuickShortcut::classBegin()
237 void QQuickShortcut::componentComplete()
240 grabShortcut(m_shortcut, m_context);
243 bool QQuickShortcut::event(QEvent *event)
245 if (m_enabled && event->type() == QEvent::Shortcut) {
246 QShortcutEvent *se = static_cast<QShortcutEvent *>(event);
247 if (se->shortcutId() == m_id && se->key() == m_shortcut){
248 if (se->isAmbiguous())
249 emit activatedAmbiguously();
258 static bool qQuickShortcutContextMatcher(QObject *obj, Qt::ShortcutContext context)
261 case Qt::ApplicationShortcut:
263 case Qt::WindowShortcut:
264 while (obj && !obj->isWindowType()) {
266 if (QQuickItem *item = qobject_cast<QQuickItem *>(obj))
267 obj = item->window();
269 return obj && obj == QGuiApplication::focusWindow();
275 void QQuickShortcut::grabShortcut(const QKeySequence &sequence, Qt::ShortcutContext context)
279 if (m_completed && !sequence.isEmpty()) {
280 QGuiApplicationPrivate *pApp = QGuiApplicationPrivate::instance();
281 m_id = pApp->shortcutMap.addShortcut(this, sequence, context, qQuickShortcutContextMatcher);
283 pApp->shortcutMap.setShortcutEnabled(false, m_id, this);
285 pApp->shortcutMap.setShortcutAutoRepeat(false, m_id, this);
289 void QQuickShortcut::ungrabShortcut()
292 QGuiApplicationPrivate::instance()->shortcutMap.removeShortcut(m_id, this);