1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only |
3 | |
4 | |
5 | |
6 | #include "qvoice.h" |
7 | #include "qvoice_p.h" |
8 | #include "qtexttospeech.h" |
9 | |
10 | QT_BEGIN_NAMESPACE |
11 | |
12 | QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QVoicePrivate) |
13 | |
14 | /*! |
15 | \class QVoice |
16 | \brief The QVoice class represents a particular voice. |
17 | \inmodule QtTextToSpeech |
18 | |
19 | To get a voice that is supported by the current text-to-speech engine, |
20 | use \l QTextToSpeech::availableVoices() or \l QTextToSpeech::findVoices(). |
21 | */ |
22 | |
23 | /*! |
24 | \qmltype voice |
25 | \inqmlmodule QtTextToSpeech |
26 | \brief The voice type represents a particular voice. |
27 | |
28 | To get a voice that is supported by the current text-to-speech engine, |
29 | use \l TextToSpeech::availableVoices(). |
30 | */ |
31 | |
32 | /*! |
33 | \enum QVoice::Age |
34 | |
35 | The age of a voice. |
36 | |
37 | \value Child Voice of a child |
38 | \value Teenager Voice of a teenager |
39 | \value Adult Voice of an adult |
40 | \value Senior Voice of a senior |
41 | \value Other Voice of unknown age |
42 | */ |
43 | |
44 | /*! |
45 | \enum QVoice::Gender |
46 | |
47 | The gender of a voice. |
48 | |
49 | \value Male Voice of a male |
50 | \value Female Voice of a female |
51 | \value Unknown Voice of unknown gender |
52 | */ |
53 | |
54 | /*! |
55 | Constructs an empty QVoice. |
56 | |
57 | Application code cannot construct arbitrary voice instances. |
58 | Use \l{QTextToSpeech::availableVoices()} or \l{QTextToSpeech::findVoices()} |
59 | instead to select a supported voice. |
60 | */ |
61 | QVoice::QVoice() |
62 | : d(nullptr) |
63 | { |
64 | } |
65 | |
66 | /*! |
67 | Copy-constructs a QVoice from \a other. |
68 | */ |
69 | QVoice::QVoice(const QVoice &other) noexcept |
70 | : d(other.d) |
71 | {} |
72 | |
73 | /*! |
74 | Destroys the QVoice instance. |
75 | */ |
76 | QVoice::~QVoice() |
77 | {} |
78 | |
79 | /*! |
80 | \fn QVoice::QVoice(QVoice &&other) |
81 | |
82 | Constructs a QVoice object by moving from \a other. |
83 | */ |
84 | |
85 | /*! |
86 | \fn QVoice &QVoice::operator=(QVoice &&other) |
87 | Moves \a other into this QVoice object. |
88 | */ |
89 | |
90 | /*! |
91 | Assigns \a other to this QVoice object. |
92 | */ |
93 | QVoice &QVoice::operator=(const QVoice &other) noexcept |
94 | { |
95 | d = other.d; |
96 | return *this; |
97 | } |
98 | |
99 | /*! |
100 | \internal |
101 | */ |
102 | QVoice::QVoice(const QString &name, const QLocale &locale, Gender gender, |
103 | Age age, const QVariant &data) |
104 | :d(new QVoicePrivate(name, locale, gender, age, data)) |
105 | { |
106 | } |
107 | |
108 | |
109 | /*! |
110 | \internal |
111 | Compares all attributes of this voice with \a other. |
112 | Returns \c true if all of them match. |
113 | */ |
114 | bool QVoice::isEqual(const QVoice &other) const noexcept |
115 | { |
116 | if (d == other.d) |
117 | return true; |
118 | if (!d || !other.d) |
119 | return false; |
120 | |
121 | return d->data == other.d->data |
122 | && d->name == other.d->name |
123 | && d->locale == other.d->locale |
124 | && d->gender == other.d->gender |
125 | && d->age == other.d->age; |
126 | } |
127 | |
128 | /*! |
129 | \fn void QVoice::swap(QVoice &other) noexcept |
130 | \since 6.4 |
131 | |
132 | Swaps \a other with this voice. This operation is very fast and never fails. |
133 | */ |
134 | |
135 | /*! |
136 | \fn bool QVoice::operator==(const QVoice &lhs, const QVoice &rhs) |
137 | \return whether the \a lhs voice and the \a rhs voice are identical. |
138 | |
139 | Two voices are identical if \l name, \l locale, \l gender, and \l age |
140 | are identical, and if they belong to the same text-to-speech engine. |
141 | */ |
142 | |
143 | /*! |
144 | \fn bool QVoice::operator!=(const QVoice &lhs, const QVoice &rhs) |
145 | \return whether the \a lhs voice and the \a rhs voice are different. |
146 | */ |
147 | |
148 | /*! |
149 | \fn QDataStream &QVoice::operator<<(QDataStream &stream, const QVoice &voice) |
150 | \since 6.4 |
151 | |
152 | Serializes \a voice to data stream \a stream. |
153 | |
154 | \sa {Serializing Qt Data Types} |
155 | */ |
156 | |
157 | /*! |
158 | \fn QDataStream &QVoice::operator>>(QDataStream &stream, QVoice &voice) |
159 | \since 6.4 |
160 | |
161 | Deserializes \a voice from data stream \a stream. |
162 | |
163 | \sa {Serializing Qt Data Types} |
164 | */ |
165 | |
166 | /*! |
167 | \qmlproperty string Voice::name |
168 | \brief This property holds the name of the voice. |
169 | */ |
170 | |
171 | /*! |
172 | \property QVoice::name |
173 | \brief the name of a voice |
174 | */ |
175 | QString QVoice::name() const |
176 | { |
177 | return d ? d->name : QString(); |
178 | } |
179 | |
180 | /*! |
181 | \qmlproperty enumerator voice::language |
182 | \brief This property holds the language of the voice. |
183 | \since 6.6 |
184 | |
185 | This is the \l{QLocale::}{language} attribute of the voice's \l locale. |
186 | */ |
187 | |
188 | /*! |
189 | \property QVoice::language |
190 | \brief the language of the voice |
191 | \since 6.6 |
192 | |
193 | This is the \l{QLocale::}{language} attribute of the voice's \l locale. |
194 | */ |
195 | |
196 | /*! |
197 | \qmlproperty locale voice::locale |
198 | \brief This property holds the locale of the voice. |
199 | |
200 | The locale includes the language and the territory (i.e. accent or dialect) |
201 | of the voice. |
202 | |
203 | \a language |
204 | */ |
205 | |
206 | /*! |
207 | \property QVoice::locale |
208 | \brief the locale of the voice |
209 | \since 6.4 |
210 | |
211 | The locale includes the language and the territory (i.e. accent or dialect) |
212 | of the voice. |
213 | */ |
214 | QLocale QVoice::locale() const |
215 | { |
216 | return d ? d->locale : QLocale(); |
217 | } |
218 | |
219 | /*! |
220 | \qmlproperty enumeration voice::gender |
221 | \brief This property holds the gender of the voice. |
222 | |
223 | \sa QVoice::Gender |
224 | */ |
225 | |
226 | /*! |
227 | \property QVoice::gender |
228 | \brief the gender of a voice |
229 | */ |
230 | QVoice::Gender QVoice::gender() const |
231 | { |
232 | return d ? d->gender : QVoice::Unknown; |
233 | } |
234 | |
235 | /*! |
236 | \qmlproperty enumeration Voice::age |
237 | \brief This property holds the age of the voice. |
238 | |
239 | \sa QVoice::Age |
240 | */ |
241 | |
242 | /*! |
243 | \property QVoice::age |
244 | \brief the age of a voice |
245 | */ |
246 | QVoice::Age QVoice::age() const |
247 | { |
248 | return d ? d->age : QVoice::Other; |
249 | } |
250 | |
251 | /*! |
252 | \internal |
253 | */ |
254 | QVariant QVoice::data() const |
255 | { |
256 | return d ? d->data : QVariant(); |
257 | } |
258 | |
259 | /*!̈́ |
260 | Returns the \a gender name of a voice. |
261 | */ |
262 | QString QVoice::genderName(QVoice::Gender gender) |
263 | { |
264 | QString retval; |
265 | switch (gender) { |
266 | case QVoice::Male: |
267 | retval = QTextToSpeech::tr(s: "Male" , c: "Gender of a voice" ); |
268 | break; |
269 | case QVoice::Female: |
270 | retval = QTextToSpeech::tr(s: "Female" , c: "Gender of a voice" ); |
271 | break; |
272 | case QVoice::Unknown: |
273 | retval = QTextToSpeech::tr(s: "Unknown Gender" , c: "Voice gender is unknown" ); |
274 | break; |
275 | } |
276 | return retval; |
277 | } |
278 | |
279 | /*! |
280 | Returns a string representing the \a age class of a voice. |
281 | */ |
282 | QString QVoice::ageName(QVoice::Age age) |
283 | { |
284 | QString retval; |
285 | switch (age) { |
286 | case QVoice::Child: |
287 | retval = QTextToSpeech::tr(s: "Child" , c: "Age of a voice" ); |
288 | break; |
289 | case QVoice::Teenager: |
290 | retval = QTextToSpeech::tr(s: "Teenager" , c: "Age of a voice" ); |
291 | break; |
292 | case QVoice::Adult: |
293 | retval = QTextToSpeech::tr(s: "Adult" , c: "Age of a voice" ); |
294 | break; |
295 | case QVoice::Senior: |
296 | retval = QTextToSpeech::tr(s: "Senior" , c: "Age of a voice" ); |
297 | break; |
298 | case QVoice::Other: |
299 | retval = QTextToSpeech::tr(s: "Other Age" , c: "Unknown age of a voice" ); |
300 | break; |
301 | } |
302 | return retval; |
303 | } |
304 | |
305 | #ifndef QT_NO_DATASTREAM |
306 | QDataStream &QVoice::writeTo(QDataStream &stream) const |
307 | { |
308 | stream << name() << locale() << int(gender()) << int(age()) << data(); |
309 | return stream; |
310 | } |
311 | |
312 | QDataStream &QVoice::readFrom(QDataStream &stream) |
313 | { |
314 | if (!d) |
315 | d.reset(ptr: new QVoicePrivate); |
316 | |
317 | int g, a; |
318 | stream >> d->name >> d->locale >> g >> a >> d->data; |
319 | d->gender = Gender(g); |
320 | d->age = Age(a); |
321 | return stream; |
322 | } |
323 | #endif |
324 | |
325 | #ifndef QT_NO_DEBUG_STREAM |
326 | |
327 | /*! |
328 | \fn QDebug QVoice::operator<<(QDebug debug, const QVoice &voice) |
329 | \since 6.4 |
330 | |
331 | Writes information about \a voice to the \a debug stream. |
332 | |
333 | \sa QDebug |
334 | */ |
335 | QDebug operator<<(QDebug dbg, const QVoice &voice) |
336 | { |
337 | QDebugStateSaver state(dbg); |
338 | dbg.noquote().nospace(); |
339 | dbg << "QVoice(name: " << voice.name() |
340 | << ", locale: " << voice.locale() |
341 | << ", gender: " << QVoice::genderName(gender: voice.gender()) |
342 | << ", age: " << QVoice::ageName(age: voice.age()) |
343 | << "; data: " << voice.data() |
344 | << ")" ; |
345 | return dbg; |
346 | } |
347 | #endif |
348 | |
349 | QT_END_NAMESPACE |
350 | |