| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #include "qinputchord.h" |
| 4 | #include "qinputchord_p.h" |
| 5 | |
| 6 | #include <Qt3DInput/qabstractphysicaldevice.h> |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | namespace Qt3DInput { |
| 11 | |
| 12 | /*! |
| 13 | \class Qt3DInput::QInputChord |
| 14 | \inmodule Qt3DInput |
| 15 | \inherits QAbstractActionInput |
| 16 | \brief QInputChord represents a set of QAbstractActionInput's that must be triggerd at once. |
| 17 | |
| 18 | \since 5.7 |
| 19 | */ |
| 20 | |
| 21 | /*! |
| 22 | \qmltype InputChord |
| 23 | \inqmlmodule Qt3D.Input |
| 24 | \inherits AbstractActionInput |
| 25 | \nativetype Qt3DInput::QInputChord |
| 26 | \brief QML frontend for the Qt3DInput::QInputChord C++ class. |
| 27 | |
| 28 | Represents a set of QAbstractActionInput's that must be triggerd at once. |
| 29 | |
| 30 | The following example shows an sequence that will be triggered by pressing the G, D, and J keys in that order with a maximum time between key presses of 1 second and overall maximum input time of 3 seconds. |
| 31 | \qml |
| 32 | InputChord { |
| 33 | interval: 1000 |
| 34 | timeout: 3000 |
| 35 | chords: [ |
| 36 | ActionInput { |
| 37 | sourceDevice: keyboardSourceDevice |
| 38 | keys: [Qt.Key_G] |
| 39 | }, |
| 40 | ActionInput { |
| 41 | sourceDevice: keyboardSourceDevice |
| 42 | keys: [Qt.Key_D] |
| 43 | }, |
| 44 | ActionInput { |
| 45 | sourceDevice: keyboardSourceDevice |
| 46 | keys: [Qt.Key_J] |
| 47 | } |
| 48 | ] |
| 49 | } |
| 50 | \endqml |
| 51 | \since 5.7 |
| 52 | */ |
| 53 | |
| 54 | /*! |
| 55 | \qmlproperty list<AbstractActionInput> Qt3D.Input::InputChord::chords |
| 56 | |
| 57 | The list of AbstractActionInput that must be triggered to trigger this aggregate input. |
| 58 | */ |
| 59 | |
| 60 | /*! |
| 61 | \qmlproperty int Qt3D.Input::InputChord::timeout |
| 62 | */ |
| 63 | |
| 64 | /*! |
| 65 | Constructs a new QInputChord with parent \a parent. |
| 66 | */ |
| 67 | QInputChord::QInputChord(Qt3DCore::QNode *parent) |
| 68 | : Qt3DInput::QAbstractActionInput(*new QInputChordPrivate(), parent) |
| 69 | { |
| 70 | |
| 71 | } |
| 72 | |
| 73 | /*! \internal */ |
| 74 | QInputChord::~QInputChord() |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | /*! |
| 79 | \property Qt3DInput::QInputChord::timeout |
| 80 | |
| 81 | The time in which all QAbstractActionInput's in the input chord must triggered within. |
| 82 | The time is in milliseconds. |
| 83 | */ |
| 84 | int QInputChord::timeout() const |
| 85 | { |
| 86 | Q_D(const QInputChord); |
| 87 | return d->m_timeout; |
| 88 | } |
| 89 | |
| 90 | /*! |
| 91 | Sets the time in which all QAbstractActionInput's in the input chord must triggered within to \a timeout. |
| 92 | The time is in milliseconds |
| 93 | */ |
| 94 | void QInputChord::setTimeout(int timeout) |
| 95 | { |
| 96 | Q_D(QInputChord); |
| 97 | if (d->m_timeout != timeout) { |
| 98 | d->m_timeout = timeout; |
| 99 | emit timeoutChanged(timeout); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /*! |
| 104 | Append the QAbstractActionInput \a input to the end of this QInputChord's chord vector. |
| 105 | |
| 106 | \sa removeChord |
| 107 | */ |
| 108 | void QInputChord::addChord(QAbstractActionInput *input) |
| 109 | { |
| 110 | Q_D(QInputChord); |
| 111 | if (!d->m_chords.contains(t: input)) { |
| 112 | d->m_chords.push_back(t: input); |
| 113 | |
| 114 | // Ensures proper bookkeeping |
| 115 | d->registerDestructionHelper(node: input, func: &QInputChord::removeChord, d->m_chords); |
| 116 | |
| 117 | if (!input->parent()) |
| 118 | input->setParent(this); |
| 119 | |
| 120 | d->update(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /*! |
| 125 | Remove the QAbstractActionInput \a input from this QInputChord's chord vector. |
| 126 | |
| 127 | \sa addChord |
| 128 | */ |
| 129 | void QInputChord::removeChord(QAbstractActionInput *input) |
| 130 | { |
| 131 | Q_D(QInputChord); |
| 132 | if (d->m_chords.contains(t: input)) { |
| 133 | d->update(); |
| 134 | |
| 135 | d->m_chords.removeOne(t: input); |
| 136 | |
| 137 | // Remove bookkeeping connection |
| 138 | d->unregisterDestructionHelper(node: input); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /*! |
| 143 | Returns the QInputChord's chord vector. |
| 144 | */ |
| 145 | QList<QAbstractActionInput *> QInputChord::chords() const |
| 146 | { |
| 147 | Q_D(const QInputChord); |
| 148 | return d->m_chords; |
| 149 | } |
| 150 | |
| 151 | QInputChordPrivate::QInputChordPrivate() |
| 152 | : QAbstractActionInputPrivate(), |
| 153 | m_timeout(0) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | } // Qt3DInput |
| 158 | |
| 159 | QT_END_NAMESPACE |
| 160 | |
| 161 | #include "moc_qinputchord.cpp" |
| 162 | |