| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtFeedback module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 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. |
| 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.LGPLv3 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.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 later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qdeclarativehapticseffect_p.h" |
| 38 | /*! |
| 39 | \qmltype HapticsEffect |
| 40 | \brief The HapticsEffect element represents a custom haptic feedback effect. |
| 41 | \ingroup qml-feedback-api |
| 42 | \inherits FeedbackEffect |
| 43 | |
| 44 | This class closely corresponds to the C++ \l QFeedbackHapticsEffect class. |
| 45 | |
| 46 | \snippet doc/src/snippets/declarative/declarative-feedback.qml Haptics Effect |
| 47 | |
| 48 | \sa Actuator, {QFeedbackHapticsEffect} |
| 49 | */ |
| 50 | QDeclarativeHapticsEffect::QDeclarativeHapticsEffect(QObject *parent) |
| 51 | : QDeclarativeFeedbackEffect(parent), m_actuator(0) |
| 52 | { |
| 53 | d = new QFeedbackHapticsEffect(this); |
| 54 | setFeedbackEffect(d); |
| 55 | |
| 56 | QFeedbackActuator* fa = d->actuator(); |
| 57 | |
| 58 | QList<QFeedbackActuator*> actuators = QFeedbackActuator::actuators(); |
| 59 | foreach (QFeedbackActuator* actuator, actuators) { |
| 60 | QDeclarativeFeedbackActuator* dfa; |
| 61 | dfa = new QDeclarativeFeedbackActuator(this, actuator); |
| 62 | if (fa && *fa == *actuator) { |
| 63 | m_actuator = dfa; |
| 64 | } |
| 65 | m_actuators.push_back(t: dfa); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void QDeclarativeHapticsEffect::setDuration(int msecs) |
| 70 | { |
| 71 | if (msecs != d->duration()) { |
| 72 | d->setDuration(msecs); |
| 73 | emit durationChanged(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | int QDeclarativeHapticsEffect::duration() const |
| 78 | { |
| 79 | return d->duration(); |
| 80 | } |
| 81 | /*! |
| 82 | \qmlproperty double HapticsEffect::intensity |
| 83 | |
| 84 | The intensity of the main part of the haptics effect, from 0.0 to 1.0. |
| 85 | */ |
| 86 | void QDeclarativeHapticsEffect::setIntensity(qreal intensity) |
| 87 | { |
| 88 | if (!qFuzzyCompare(p1: intensity, p2: d->intensity())) { |
| 89 | d->setIntensity(intensity); |
| 90 | emit intensityChanged(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | qreal QDeclarativeHapticsEffect::intensity() const |
| 95 | { |
| 96 | return d->intensity(); |
| 97 | } |
| 98 | |
| 99 | /*! |
| 100 | \qmlproperty int HapticsEffect::attackTime |
| 101 | |
| 102 | The duration of the attack (fade-in) part of the haptics effect. |
| 103 | */ |
| 104 | |
| 105 | void QDeclarativeHapticsEffect::setAttackTime(int msecs) |
| 106 | { |
| 107 | if (msecs != d->attackTime()) { |
| 108 | d->setAttackTime(msecs); |
| 109 | emit attackTimeChanged(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int QDeclarativeHapticsEffect::attackTime() const |
| 114 | { |
| 115 | return d->attackTime(); |
| 116 | } |
| 117 | /*! |
| 118 | \qmlproperty double HapticsEffect::attackIntensity |
| 119 | |
| 120 | The intensity of the attack (fade-in) part of the haptics effect, from 0.0 to 1.0. |
| 121 | */ |
| 122 | void QDeclarativeHapticsEffect::setAttackIntensity(qreal intensity) |
| 123 | { |
| 124 | if (!qFuzzyCompare(p1: intensity, p2: d->attackIntensity())) { |
| 125 | d->setAttackIntensity(intensity); |
| 126 | emit intensityChanged(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | qreal QDeclarativeHapticsEffect::attackIntensity() const |
| 131 | { |
| 132 | return d->attackIntensity(); |
| 133 | } |
| 134 | /*! |
| 135 | \qmlproperty int HapticsEffect::fadeTime |
| 136 | |
| 137 | The duration of the fade-out part of the haptics effect. |
| 138 | */ |
| 139 | |
| 140 | void QDeclarativeHapticsEffect::setFadeTime(int msecs) |
| 141 | { |
| 142 | if (msecs != d->fadeTime()) { |
| 143 | d->setFadeTime(msecs); |
| 144 | emit fadeTimeChanged(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | int QDeclarativeHapticsEffect::fadeTime() const |
| 149 | { |
| 150 | return d->fadeTime(); |
| 151 | } |
| 152 | |
| 153 | void QDeclarativeHapticsEffect::setFadeIntensity(qreal intensity) |
| 154 | { |
| 155 | if (!qFuzzyCompare(p1: intensity, p2: d->fadeIntensity())) { |
| 156 | d->setFadeIntensity(intensity); |
| 157 | emit fadeIntensityChanged(); |
| 158 | } |
| 159 | } |
| 160 | /*! |
| 161 | \qmlproperty double HapticsEffect::fadeIntensity |
| 162 | |
| 163 | The intensity of the fade-out part of the haptics effect, from 0.0 to 1.0. |
| 164 | */ |
| 165 | qreal QDeclarativeHapticsEffect::fadeIntensity() const |
| 166 | { |
| 167 | return d->fadeIntensity(); |
| 168 | } |
| 169 | /*! |
| 170 | \qmlproperty int HapticsEffect::period |
| 171 | |
| 172 | The period of the haptics effect. If the period is zero, the effect will |
| 173 | not repeat. If it is non-zero, the effect will repeat every period milliseconds. |
| 174 | */ |
| 175 | |
| 176 | void QDeclarativeHapticsEffect::setPeriod(int msecs) |
| 177 | { |
| 178 | if (msecs != d->period()) { |
| 179 | d->setPeriod(msecs); |
| 180 | emit periodChanged(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | int QDeclarativeHapticsEffect::period() const |
| 185 | { |
| 186 | return d->period(); |
| 187 | } |
| 188 | |
| 189 | /*! |
| 190 | \qmlproperty Actuator HapticsEffect::actuator |
| 191 | |
| 192 | The actuator that is used for playing this effect. |
| 193 | \sa Actuator |
| 194 | */ |
| 195 | void QDeclarativeHapticsEffect::setActuator(QDeclarativeFeedbackActuator *actuator) |
| 196 | { |
| 197 | if (actuator != m_actuator) { |
| 198 | if (!actuator |
| 199 | || !m_actuator |
| 200 | || !(*(actuator->feedbackActuator()) == *(m_actuator->feedbackActuator()))) { |
| 201 | m_actuator = actuator; |
| 202 | d->setActuator(m_actuator ? m_actuator->feedbackActuator() : 0); |
| 203 | emit actuatorChanged(); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | QDeclarativeFeedbackActuator* QDeclarativeHapticsEffect::actuator() const |
| 209 | { |
| 210 | return m_actuator; |
| 211 | } |
| 212 | /*! |
| 213 | \qmlproperty list<Actuator> HapticsEffect::availableActuators |
| 214 | |
| 215 | This property holds a list of available actuators. |
| 216 | This property is read only. |
| 217 | \sa Actuator |
| 218 | */ |
| 219 | QQmlListProperty<QDeclarativeFeedbackActuator> QDeclarativeHapticsEffect::availableActuators() { |
| 220 | return QQmlListProperty<QDeclarativeFeedbackActuator>(this, |
| 221 | 0, |
| 222 | 0 /*appending actuators are not allowed*/, |
| 223 | actuator_count, |
| 224 | actuator_at, |
| 225 | 0 /*removing actuators are not allowed*/); |
| 226 | } |
| 227 | |
| 228 | int QDeclarativeHapticsEffect::actuator_count(QQmlListProperty<QDeclarativeFeedbackActuator> *prop) |
| 229 | { |
| 230 | return static_cast<QDeclarativeHapticsEffect*>(prop->object)->m_actuators.size(); |
| 231 | } |
| 232 | QDeclarativeFeedbackActuator* QDeclarativeHapticsEffect::actuator_at(QQmlListProperty<QDeclarativeFeedbackActuator> *prop, int index) |
| 233 | { |
| 234 | return static_cast<QDeclarativeHapticsEffect*>(prop->object)->m_actuators.at(i: index); |
| 235 | } |
| 236 | |