| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ****************************************************************************/ |
| 38 | |
| 39 | #include "qinputchord.h" |
| 40 | #include "qinputchord_p.h" |
| 41 | |
| 42 | #include <Qt3DInput/qabstractphysicaldevice.h> |
| 43 | #include <Qt3DCore/qnodecreatedchange.h> |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | namespace Qt3DInput { |
| 48 | |
| 49 | /*! |
| 50 | \class Qt3DInput::QInputChord |
| 51 | \inmodule Qt3DInput |
| 52 | \inherits QAbstractActionInput |
| 53 | \brief QInputChord represents a set of QAbstractActionInput's that must be triggerd at once. |
| 54 | |
| 55 | \since 5.7 |
| 56 | */ |
| 57 | |
| 58 | /*! |
| 59 | \qmltype InputChord |
| 60 | \inqmlmodule Qt3D.Input |
| 61 | \inherits QAbstractActionInput |
| 62 | \instantiates Qt3DInput::QInputChord |
| 63 | \brief QML frontend for the Qt3DInput::QInputChord C++ class. |
| 64 | |
| 65 | Represents a set of QAbstractActionInput's that must be triggerd at once. |
| 66 | |
| 67 | 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. |
| 68 | \qml |
| 69 | InputChord { |
| 70 | interval: 1000 |
| 71 | timeout: 3000 |
| 72 | chords: [ |
| 73 | ActionInput { |
| 74 | sourceDevice: keyboardSourceDevice |
| 75 | keys: [Qt.Key_G] |
| 76 | }, |
| 77 | ActionInput { |
| 78 | sourceDevice: keyboardSourceDevice |
| 79 | keys: [Qt.Key_D] |
| 80 | }, |
| 81 | ActionInput { |
| 82 | sourceDevice: keyboardSourceDevice |
| 83 | keys: [Qt.Key_J] |
| 84 | } |
| 85 | ] |
| 86 | } |
| 87 | \endqml |
| 88 | \since 5.7 |
| 89 | */ |
| 90 | |
| 91 | /*! |
| 92 | \qmlproperty list<AbstractActionInput> Qt3D.Input::InputChord::chords |
| 93 | |
| 94 | The list of AbstractActionInput that must be triggered to trigger this aggregate input. |
| 95 | */ |
| 96 | |
| 97 | /*! |
| 98 | \qmlproperty int Qt3D.Input::InputChord::timeout |
| 99 | */ |
| 100 | |
| 101 | /*! |
| 102 | Constructs a new QInputChord with parent \a parent. |
| 103 | */ |
| 104 | QInputChord::QInputChord(Qt3DCore::QNode *parent) |
| 105 | : Qt3DInput::QAbstractActionInput(*new QInputChordPrivate(), parent) |
| 106 | { |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /*! \internal */ |
| 111 | QInputChord::~QInputChord() |
| 112 | { |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | \property QInputChord::timeout |
| 117 | |
| 118 | The time in which all QAbstractActionInput's in the input chord must triggered within. |
| 119 | The time is in milliseconds. |
| 120 | */ |
| 121 | int QInputChord::timeout() const |
| 122 | { |
| 123 | Q_D(const QInputChord); |
| 124 | return d->m_timeout; |
| 125 | } |
| 126 | |
| 127 | /*! |
| 128 | Sets the time in which all QAbstractActionInput's in the input chord must triggered within to \a timeout. |
| 129 | The time is in milliseconds |
| 130 | */ |
| 131 | void QInputChord::setTimeout(int timeout) |
| 132 | { |
| 133 | Q_D(QInputChord); |
| 134 | if (d->m_timeout != timeout) { |
| 135 | d->m_timeout = timeout; |
| 136 | emit timeoutChanged(timeout); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /*! |
| 141 | Append the QAbstractActionInput \a input to the end of this QInputChord's chord vector. |
| 142 | |
| 143 | \sa removeChord |
| 144 | */ |
| 145 | void QInputChord::addChord(QAbstractActionInput *input) |
| 146 | { |
| 147 | Q_D(QInputChord); |
| 148 | if (!d->m_chords.contains(t: input)) { |
| 149 | d->m_chords.push_back(t: input); |
| 150 | |
| 151 | // Ensures proper bookkeeping |
| 152 | d->registerDestructionHelper(node: input, func: &QInputChord::removeChord, d->m_chords); |
| 153 | |
| 154 | if (!input->parent()) |
| 155 | input->setParent(this); |
| 156 | |
| 157 | d->updateNode(node: input, property: "input" , change: Qt3DCore::PropertyValueAdded); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | Remove the QAbstractActionInput \a input from this QInputChord's chord vector. |
| 163 | |
| 164 | \sa addChord |
| 165 | */ |
| 166 | void QInputChord::removeChord(QAbstractActionInput *input) |
| 167 | { |
| 168 | Q_D(QInputChord); |
| 169 | if (d->m_chords.contains(t: input)) { |
| 170 | d->updateNode(node: input, property: "input" , change: Qt3DCore::PropertyValueRemoved); |
| 171 | |
| 172 | d->m_chords.removeOne(t: input); |
| 173 | |
| 174 | // Remove bookkeeping connection |
| 175 | d->unregisterDestructionHelper(node: input); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /*! |
| 180 | Returns the QInputChord's chord vector. |
| 181 | */ |
| 182 | QVector<QAbstractActionInput *> QInputChord::chords() const |
| 183 | { |
| 184 | Q_D(const QInputChord); |
| 185 | return d->m_chords; |
| 186 | } |
| 187 | |
| 188 | Qt3DCore::QNodeCreatedChangeBasePtr QInputChord::createNodeCreationChange() const |
| 189 | { |
| 190 | auto creationChange = Qt3DCore::QNodeCreatedChangePtr<QInputChordData>::create(arguments: this); |
| 191 | QInputChordData &data = creationChange->data; |
| 192 | |
| 193 | Q_D(const QInputChord); |
| 194 | data.chordIds = qIdsForNodes(nodes: chords()); |
| 195 | data.timeout = d->m_timeout; |
| 196 | |
| 197 | return creationChange; |
| 198 | } |
| 199 | |
| 200 | QInputChordPrivate::QInputChordPrivate() |
| 201 | : QAbstractActionInputPrivate(), |
| 202 | m_timeout(0) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | } // Qt3DInput |
| 207 | |
| 208 | QT_END_NAMESPACE |
| 209 | |