| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qvoiceselectorattached_p.h" |
| 5 | #include "qdeclarativetexttospeech_p.h" |
| 6 | |
| 7 | #include <QtQml/qqmlinfo.h> |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | using namespace Qt::StringLiterals; |
| 12 | |
| 13 | /*! |
| 14 | \qmltype VoiceSelector |
| 15 | \inqmlmodule QtTextToSpeech |
| 16 | \since 6.6 |
| 17 | \brief Provides attached properties for selecting the voice of a TextToSpeech |
| 18 | element. |
| 19 | |
| 20 | The type provides a set of properties that are by default unset. The TextToSpeech |
| 21 | element will choose the first voice that matches all the set property values. |
| 22 | If no voice is found that matches all criteria, then the voice doesn't change. |
| 23 | |
| 24 | When setting individual properties within this group after the TextToSpeech object |
| 25 | has been initialized, then you have to call the select() method to trigger the |
| 26 | selection of a voice. |
| 27 | |
| 28 | \sa TextToSpeech::voice, TextToSpeech::availableVoices() |
| 29 | */ |
| 30 | |
| 31 | QVoiceSelectorAttached *QVoiceSelectorAttached::qmlAttachedProperties(QObject *obj) |
| 32 | { |
| 33 | QVoiceSelectorAttached *attached = nullptr; |
| 34 | if (QDeclarativeTextToSpeech *tts = qobject_cast<QDeclarativeTextToSpeech *>(object: obj)) { |
| 35 | Q_ASSERT(!tts->m_voiceSelector); |
| 36 | attached = new QVoiceSelectorAttached(tts); |
| 37 | tts->m_voiceSelector = attached; |
| 38 | } else { |
| 39 | qCritical(msg: "A VoiceSelector can only be attached to a TextToSpeech element!"); |
| 40 | } |
| 41 | return attached; |
| 42 | } |
| 43 | |
| 44 | QVoiceSelectorAttached::QVoiceSelectorAttached(QDeclarativeTextToSpeech *tts) |
| 45 | : QObject(tts), m_tts(tts) |
| 46 | {} |
| 47 | |
| 48 | /*! |
| 49 | \qmlmethod void VoiceSelector::select() |
| 50 | |
| 51 | Activates the selection of the voice based on the specified criteria. |
| 52 | |
| 53 | \note This method only needs to be called if the selection criteria are |
| 54 | modified after the TextToSpeech object has been instantiated. |
| 55 | */ |
| 56 | void QVoiceSelectorAttached::select() |
| 57 | { |
| 58 | m_tts->selectVoice(); |
| 59 | } |
| 60 | |
| 61 | /*! |
| 62 | \qmlproperty variant VoiceSelector::name |
| 63 | \brief This property specifies which name the selected voice should have. |
| 64 | |
| 65 | The property can be a string, or a regular expression. |
| 66 | */ |
| 67 | |
| 68 | QVariant QVoiceSelectorAttached::name() const |
| 69 | { |
| 70 | return m_criteria.value(key: u"name"_s); |
| 71 | } |
| 72 | |
| 73 | void QVoiceSelectorAttached::setName(const QVariant &name) |
| 74 | { |
| 75 | if (!name.isValid()) { |
| 76 | m_criteria.remove(key: u"name"_s); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | QVariant &m_name = m_criteria[u"name"_s]; |
| 81 | if (m_name == name) |
| 82 | return; |
| 83 | |
| 84 | m_name = name; |
| 85 | emit nameChanged(); |
| 86 | } |
| 87 | |
| 88 | /*! |
| 89 | \qmlproperty enumerator VoiceSelector::gender |
| 90 | \brief This property specifies which \l{QVoice::Gender}{gender} the selected |
| 91 | voice should have. |
| 92 | */ |
| 93 | |
| 94 | QVoice::Gender QVoiceSelectorAttached::gender() const |
| 95 | { |
| 96 | return m_criteria.value(key: u"gender"_s).value<QVoice::Gender>(); |
| 97 | } |
| 98 | |
| 99 | void QVoiceSelectorAttached::setGender(QVoice::Gender gender) |
| 100 | { |
| 101 | QVariant &m_gender = m_criteria[u"gender"_s]; |
| 102 | if (m_gender == gender) |
| 103 | return; |
| 104 | |
| 105 | m_gender = gender; |
| 106 | emit genderChanged(); |
| 107 | } |
| 108 | |
| 109 | /*! |
| 110 | \qmlproperty enumerator VoiceSelector::age |
| 111 | \brief This property specifies which \l{QVoice::Age}{age} the selected voice |
| 112 | should have. |
| 113 | */ |
| 114 | |
| 115 | QVoice::Age QVoiceSelectorAttached::age() const |
| 116 | { |
| 117 | return m_criteria.value(key: u"age"_s).value<QVoice::Age>(); |
| 118 | } |
| 119 | |
| 120 | void QVoiceSelectorAttached::setAge(QVoice::Age age) |
| 121 | { |
| 122 | QVariant &m_age = m_criteria[u"age"_s]; |
| 123 | if (m_age == age) |
| 124 | return; |
| 125 | m_age = age; |
| 126 | emit ageChanged(); |
| 127 | } |
| 128 | |
| 129 | /*! |
| 130 | \qmlproperty locale VoiceSelector::locale |
| 131 | \brief This property specifies which locale the selected voice should have. |
| 132 | |
| 133 | If this property is set, then both the language and the territory of the |
| 134 | voice need to match. |
| 135 | |
| 136 | \sa language |
| 137 | */ |
| 138 | QLocale QVoiceSelectorAttached::locale() const |
| 139 | { |
| 140 | return m_criteria.value(key: u"locale"_s).toLocale(); |
| 141 | } |
| 142 | |
| 143 | void QVoiceSelectorAttached::setLocale(const QLocale &locale) |
| 144 | { |
| 145 | QVariant &m_locale = m_criteria[u"locale"_s]; |
| 146 | if (m_locale == locale) |
| 147 | return; |
| 148 | |
| 149 | m_locale = locale; |
| 150 | emit localeChanged(); |
| 151 | } |
| 152 | |
| 153 | /*! |
| 154 | \qmlproperty locale VoiceSelector::language |
| 155 | \brief This property specifies which language the selected voice should have. |
| 156 | |
| 157 | The property is of type locale, but only the language component of the locale |
| 158 | will be considered, the territory will be ignored. |
| 159 | |
| 160 | \sa locale |
| 161 | */ |
| 162 | QLocale QVoiceSelectorAttached::language() const |
| 163 | { |
| 164 | const auto &it = m_criteria.find(key: u"language"_s); |
| 165 | if (it == m_criteria.end()) |
| 166 | return locale(); |
| 167 | return (*it).value<QLocale>(); |
| 168 | } |
| 169 | |
| 170 | void QVoiceSelectorAttached::setLanguage(const QLocale &language) |
| 171 | { |
| 172 | QVariant &m_language = m_criteria[u"language"_s]; |
| 173 | if (m_language == language) |
| 174 | return; |
| 175 | |
| 176 | m_language = language; |
| 177 | emit languageChanged(); |
| 178 | } |
| 179 | |
| 180 | QT_END_NAMESPACE |
| 181 |
