1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part 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 | |
40 | #include "qdeclarativeradio_p.h" |
41 | #include "qdeclarativeradiodata_p.h" |
42 | |
43 | QT_BEGIN_NAMESPACE |
44 | |
45 | /*! |
46 | \qmltype Radio |
47 | \instantiates QDeclarativeRadio |
48 | \inqmlmodule QtMultimedia |
49 | \brief Access radio functionality from a QML application. |
50 | \ingroup multimedia_qml |
51 | \ingroup multimedia_radio_qml |
52 | \inherits Item |
53 | \deprecated |
54 | |
55 | \qml |
56 | Rectangle { |
57 | width: 320 |
58 | height: 480 |
59 | |
60 | Radio { |
61 | id: radio |
62 | band: Radio.FM |
63 | } |
64 | |
65 | MouseArea { |
66 | x: 0; y: 0 |
67 | height: parent.height |
68 | width: parent.width / 2 |
69 | |
70 | onClicked: radio.scanDown() |
71 | } |
72 | |
73 | MouseArea { |
74 | x: parent.width / 2; y: 0 |
75 | height: parent.height |
76 | width: parent.width / 2 |
77 | |
78 | onClicked: radio.scanUp() |
79 | } |
80 | } |
81 | \endqml |
82 | |
83 | You can use \c Radio to tune the radio and get information about the signal. |
84 | You can also use the Radio to get information about tuning, for instance the |
85 | frequency steps supported for tuning. |
86 | |
87 | The corresponding \l RadioData gives RDS information about the |
88 | current radio station. The best way to access the RadioData for |
89 | the current Radio is to use the \c radioData property. |
90 | |
91 | \sa {Radio Overview} |
92 | |
93 | */ |
94 | |
95 | |
96 | QDeclarativeRadio::QDeclarativeRadio(QObject *parent) : |
97 | QObject(parent) |
98 | { |
99 | m_radioTuner = new QRadioTuner(this); |
100 | m_radioData = new QDeclarativeRadioData(m_radioTuner, this); |
101 | |
102 | connect(sender: m_radioTuner, SIGNAL(stateChanged(QRadioTuner::State)), receiver: this, SLOT(_q_stateChanged(QRadioTuner::State))); |
103 | connect(sender: m_radioTuner, SIGNAL(bandChanged(QRadioTuner::Band)), receiver: this, SLOT(_q_bandChanged(QRadioTuner::Band))); |
104 | |
105 | connect(sender: m_radioTuner, SIGNAL(frequencyChanged(int)), receiver: this, SIGNAL(frequencyChanged(int))); |
106 | connect(sender: m_radioTuner, SIGNAL(stereoStatusChanged(bool)), receiver: this, SIGNAL(stereoStatusChanged(bool))); |
107 | connect(sender: m_radioTuner, SIGNAL(searchingChanged(bool)), receiver: this, SIGNAL(searchingChanged(bool))); |
108 | connect(sender: m_radioTuner, SIGNAL(signalStrengthChanged(int)), receiver: this, SIGNAL(signalStrengthChanged(int))); |
109 | connect(sender: m_radioTuner, SIGNAL(volumeChanged(int)), receiver: this, SIGNAL(volumeChanged(int))); |
110 | connect(sender: m_radioTuner, SIGNAL(mutedChanged(bool)), receiver: this, SIGNAL(mutedChanged(bool))); |
111 | connect(sender: m_radioTuner, SIGNAL(stationFound(int,QString)), receiver: this, SIGNAL(stationFound(int,QString))); |
112 | connect(sender: m_radioTuner, SIGNAL(antennaConnectedChanged(bool)), receiver: this, SIGNAL(antennaConnectedChanged(bool))); |
113 | connect(sender: m_radioTuner, SIGNAL(availabilityChanged(QMultimedia::AvailabilityStatus)), receiver: this, SLOT(_q_availabilityChanged(QMultimedia::AvailabilityStatus))); |
114 | |
115 | connect(sender: m_radioTuner, SIGNAL(error(QRadioTuner::Error)), receiver: this, SLOT(_q_error(QRadioTuner::Error))); |
116 | } |
117 | |
118 | QDeclarativeRadio::~QDeclarativeRadio() |
119 | { |
120 | } |
121 | |
122 | /*! |
123 | \qmlproperty enumeration QtMultimedia::Radio::state |
124 | |
125 | This property holds the current state of the Radio. |
126 | |
127 | \table |
128 | \header \li Value \li Description |
129 | \row \li ActiveState |
130 | \li The radio is started and active |
131 | |
132 | \row \li StoppedState |
133 | \li The radio is stopped |
134 | |
135 | \endtable |
136 | |
137 | \sa start, stop |
138 | */ |
139 | QDeclarativeRadio::State QDeclarativeRadio::state() const |
140 | { |
141 | return static_cast<QDeclarativeRadio::State>(m_radioTuner->state()); |
142 | } |
143 | |
144 | /*! |
145 | \qmlproperty enumeration QtMultimedia::Radio::band |
146 | |
147 | This property holds the frequency band used for the radio, which can be specified as |
148 | any one of the values in the table below. |
149 | |
150 | \table |
151 | \header \li Value \li Description |
152 | \row \li AM |
153 | \li 520 to 1610 kHz, 9 or 10kHz channel spacing, extended 1610 to 1710 kHz |
154 | |
155 | \row \li FM |
156 | \li 87.5 to 108.0 MHz, except Japan 76-90 MHz |
157 | |
158 | \row \li SW |
159 | \li 1.711 to 30.0 MHz, divided into 15 bands. 5kHz channel spacing |
160 | |
161 | \row \li LW |
162 | \li 148.5 to 283.5 kHz, 9kHz channel spacing (Europe, Africa, Asia) |
163 | |
164 | \row \li FM2 |
165 | \li range not defined, used when area supports more than one FM range |
166 | |
167 | \endtable |
168 | */ |
169 | QDeclarativeRadio::Band QDeclarativeRadio::band() const |
170 | { |
171 | return static_cast<QDeclarativeRadio::Band>(m_radioTuner->band()); |
172 | } |
173 | |
174 | /*! |
175 | \qmlproperty int QtMultimedia::Radio::frequency |
176 | |
177 | Sets the frequency in Hertz that the radio is tuned to. The frequency must be within the frequency |
178 | range for the current band, otherwise it will be changed to be within the frequency range. |
179 | |
180 | \sa maximumFrequency, minimumFrequency |
181 | */ |
182 | int QDeclarativeRadio::frequency() const |
183 | { |
184 | return m_radioTuner->frequency(); |
185 | } |
186 | |
187 | /*! |
188 | \qmlproperty enumeration QtMultimedia::Radio::stereoMode |
189 | |
190 | This property holds the stereo mode of the radio, which can be set to any one of the |
191 | values in the table below. |
192 | |
193 | \table |
194 | \header \li Value \li Description |
195 | \row \li Auto |
196 | \li Uses stereo mode matching the station |
197 | |
198 | \row \li ForceStereo |
199 | \li Forces the radio to play the station in stereo, converting the sound signal if necessary |
200 | |
201 | \row \li ForceMono |
202 | \li Forces the radio to play the station in mono, converting the sound signal if necessary |
203 | |
204 | \endtable |
205 | */ |
206 | QDeclarativeRadio::StereoMode QDeclarativeRadio::stereoMode() const |
207 | { |
208 | return static_cast<QDeclarativeRadio::StereoMode>(m_radioTuner->stereoMode()); |
209 | } |
210 | |
211 | /*! |
212 | \qmlproperty int QtMultimedia::Radio::volume |
213 | |
214 | Set this property to control the volume of the radio. The valid range of the volume is from 0 to 100. |
215 | */ |
216 | int QDeclarativeRadio::volume() const |
217 | { |
218 | return m_radioTuner->volume(); |
219 | } |
220 | |
221 | /*! |
222 | \qmlproperty bool QtMultimedia::Radio::muted |
223 | |
224 | This property reflects whether the radio is muted or not. |
225 | */ |
226 | bool QDeclarativeRadio::muted() const |
227 | { |
228 | return m_radioTuner->isMuted(); |
229 | } |
230 | |
231 | /*! |
232 | \qmlproperty bool QtMultimedia::Radio::stereo |
233 | |
234 | This property holds whether the radio is receiving a stereo signal or not. If \l stereoMode is |
235 | set to ForceMono the value will always be false. Likewise, it will always be true if stereoMode |
236 | is set to ForceStereo. |
237 | |
238 | \sa stereoMode |
239 | */ |
240 | bool QDeclarativeRadio::stereo() const |
241 | { |
242 | return m_radioTuner->isStereo(); |
243 | } |
244 | |
245 | /*! |
246 | \qmlproperty int QtMultimedia::Radio::signalStrength |
247 | |
248 | The strength of the current radio signal as a percentage where 0% equals no signal, and 100% is a |
249 | very good signal. |
250 | */ |
251 | int QDeclarativeRadio::signalStrength() const |
252 | { |
253 | return m_radioTuner->signalStrength(); |
254 | } |
255 | |
256 | /*! |
257 | \qmlproperty bool QtMultimedia::Radio::searching |
258 | |
259 | This property is true if the radio is currently searching for radio stations, for instance using the \l scanUp, |
260 | \l scanDown, and \l searchAllStations methods. Once the search completes, or if it is cancelled using |
261 | \l cancelScan, this property will be false. |
262 | */ |
263 | bool QDeclarativeRadio::searching() const |
264 | { |
265 | return m_radioTuner->isSearching(); |
266 | } |
267 | |
268 | /*! |
269 | \qmlproperty int QtMultimedia::Radio::frequencyStep |
270 | |
271 | The number of Hertz for each step when tuning the radio manually. The value is for the current \l band. |
272 | */ |
273 | int QDeclarativeRadio::frequencyStep() const |
274 | { |
275 | return m_radioTuner->frequencyStep(band: m_radioTuner->band()); |
276 | } |
277 | |
278 | /*! |
279 | \qmlproperty int QtMultimedia::Radio::minimumFrequency |
280 | |
281 | The minimum frequency for the current \l band. |
282 | */ |
283 | int QDeclarativeRadio::minimumFrequency() const |
284 | { |
285 | return m_radioTuner->frequencyRange(band: m_radioTuner->band()).first; |
286 | } |
287 | |
288 | /*! |
289 | \qmlproperty int QtMultimedia::Radio::maximumFrequency |
290 | |
291 | The maximum frequency for the current \l band. |
292 | */ |
293 | int QDeclarativeRadio::maximumFrequency() const |
294 | { |
295 | return m_radioTuner->frequencyRange(band: m_radioTuner->band()).second; |
296 | } |
297 | |
298 | /*! |
299 | \qmlproperty int QtMultimedia::Radio::antennaConnected |
300 | |
301 | This property is true if there is an antenna connected. Otherwise it will be false. |
302 | */ |
303 | bool QDeclarativeRadio::isAntennaConnected() const |
304 | { |
305 | return m_radioTuner->isAntennaConnected(); |
306 | } |
307 | |
308 | /*! |
309 | \qmlproperty enumeration QtMultimedia::Radio::availability |
310 | |
311 | Returns the availability state of the radio. |
312 | |
313 | This is one of: |
314 | |
315 | \table |
316 | \header \li Value \li Description |
317 | \row \li Available |
318 | \li The radio is available to use |
319 | \row \li Busy |
320 | \li The radio is usually available to use, but is currently busy. |
321 | This can happen when some other process needs to use the audio |
322 | hardware. |
323 | \row \li Unavailable |
324 | \li The radio is not available to use (there may be no radio |
325 | hardware) |
326 | \row \li ResourceMissing |
327 | \li There is one or more resources missing, so the radio cannot |
328 | be used. It may be possible to try again at a later time. This |
329 | can occur if there is no antenna connected - see the \l antennaConnected |
330 | property as well. |
331 | \endtable |
332 | */ |
333 | QDeclarativeRadio::Availability QDeclarativeRadio::availability() const |
334 | { |
335 | return Availability(m_radioTuner->availability()); |
336 | } |
337 | |
338 | void QDeclarativeRadio::setBand(QDeclarativeRadio::Band band) |
339 | { |
340 | m_radioTuner->setBand(static_cast<QRadioTuner::Band>(band)); |
341 | } |
342 | |
343 | void QDeclarativeRadio::setFrequency(int frequency) |
344 | { |
345 | m_radioTuner->setFrequency(frequency); |
346 | } |
347 | |
348 | void QDeclarativeRadio::setStereoMode(QDeclarativeRadio::StereoMode stereoMode) |
349 | { |
350 | m_radioTuner->setStereoMode(static_cast<QRadioTuner::StereoMode>(stereoMode)); |
351 | } |
352 | |
353 | void QDeclarativeRadio::setVolume(int volume) |
354 | { |
355 | m_radioTuner->setVolume(volume); |
356 | } |
357 | |
358 | void QDeclarativeRadio::setMuted(bool muted) |
359 | { |
360 | m_radioTuner->setMuted(muted); |
361 | } |
362 | |
363 | /*! |
364 | \qmlmethod QtMultimedia::Radio::cancelScan() |
365 | |
366 | Cancel the current scan. Will also cancel a search started with \l searchAllStations. |
367 | */ |
368 | void QDeclarativeRadio::cancelScan() |
369 | { |
370 | m_radioTuner->cancelSearch(); |
371 | } |
372 | |
373 | /*! |
374 | \qmlmethod QtMultimedia::Radio::scanDown() |
375 | |
376 | Searches backward in the frequency range for the current band. |
377 | */ |
378 | void QDeclarativeRadio::scanDown() |
379 | { |
380 | m_radioTuner->searchBackward(); |
381 | } |
382 | |
383 | /*! |
384 | \qmlmethod QtMultimedia::Radio::scanUp() |
385 | |
386 | Searches forward in the frequency range for the current band. |
387 | */ |
388 | void QDeclarativeRadio::scanUp() |
389 | { |
390 | m_radioTuner->searchForward(); |
391 | } |
392 | |
393 | /*! |
394 | \qmlmethod QtMultimedia::Radio::searchAllStations(enumeration searchMode) |
395 | |
396 | Start searching the complete frequency range for the current band, and save all the |
397 | radio stations found. The \a searchMode can be either of the values described in the |
398 | table below. |
399 | |
400 | \table |
401 | \header \li Value \li Description |
402 | \row \li SearchFast |
403 | \li Stores each radio station for later retrival and tuning |
404 | |
405 | \row \li SearchGetStationId |
406 | \li Does the same as SearchFast, but also emits the station Id with the \l stationFound signal. |
407 | |
408 | \endtable |
409 | |
410 | The snippet below uses \c searchAllStations with \c SearchGetStationId to receive all the radio |
411 | stations in the current band, and store them in a ListView. The station Id is shown to the user |
412 | and if the user presses a station, the radio is tuned to this station. |
413 | |
414 | \qml |
415 | Item { |
416 | width: 640 |
417 | height: 360 |
418 | |
419 | Radio { |
420 | id: radio |
421 | onStationFound: radioStations.append({"frequency": frequency, "stationId": stationId}) |
422 | } |
423 | |
424 | ListModel { |
425 | id: radioStations |
426 | } |
427 | |
428 | ListView { |
429 | model: radioStations |
430 | delegate: Rectangle { |
431 | MouseArea { |
432 | anchors.fill: parent |
433 | onClicked: radio.frequency = frequency |
434 | } |
435 | |
436 | Text { |
437 | anchors.fill: parent |
438 | text: stationId |
439 | } |
440 | } |
441 | } |
442 | |
443 | Rectangle { |
444 | MouseArea { |
445 | anchors.fill: parent |
446 | onClicked: radio.searchAllStations(Radio.SearchGetStationId) |
447 | } |
448 | } |
449 | } |
450 | \endqml |
451 | */ |
452 | void QDeclarativeRadio::searchAllStations(QDeclarativeRadio::SearchMode searchMode) |
453 | { |
454 | m_radioTuner->searchAllStations(searchMode: static_cast<QRadioTuner::SearchMode>(searchMode)); |
455 | } |
456 | |
457 | /*! |
458 | \qmlmethod QtMultimedia::Radio::tuneDown() |
459 | |
460 | Decrements the frequency by the frequency step for the current band. If the frequency is already set |
461 | to the minimum frequency, calling this function has no effect. |
462 | |
463 | \sa band, frequencyStep, minimumFrequency |
464 | */ |
465 | void QDeclarativeRadio::tuneDown() |
466 | { |
467 | int f = frequency(); |
468 | f = f - frequencyStep(); |
469 | setFrequency(f); |
470 | } |
471 | |
472 | /*! |
473 | \qmlmethod QtMultimedia::Radio::tuneUp() |
474 | |
475 | Increments the frequency by the frequency step for the current band. If the frequency is already set |
476 | to the maximum frequency, calling this function has no effect. |
477 | |
478 | \sa band, frequencyStep, maximumFrequency |
479 | */ |
480 | void QDeclarativeRadio::tuneUp() |
481 | { |
482 | int f = frequency(); |
483 | f = f + frequencyStep(); |
484 | setFrequency(f); |
485 | } |
486 | |
487 | /*! |
488 | \qmlmethod QtMultimedia::Radio::start() |
489 | |
490 | Starts the radio. If the radio is available, as determined by the \l availability property, |
491 | this will result in the \l state becoming \c ActiveState. |
492 | */ |
493 | void QDeclarativeRadio::start() |
494 | { |
495 | m_radioTuner->start(); |
496 | } |
497 | |
498 | /*! |
499 | \qmlmethod QtMultimedia::Radio::stop() |
500 | |
501 | Stops the radio. After calling this method the \l state will be \c StoppedState. |
502 | */ |
503 | void QDeclarativeRadio::stop() |
504 | { |
505 | m_radioTuner->stop(); |
506 | } |
507 | |
508 | void QDeclarativeRadio::_q_stateChanged(QRadioTuner::State state) |
509 | { |
510 | emit stateChanged(state: static_cast<QDeclarativeRadio::State>(state)); |
511 | } |
512 | |
513 | void QDeclarativeRadio::_q_bandChanged(QRadioTuner::Band band) |
514 | { |
515 | emit bandChanged(band: static_cast<QDeclarativeRadio::Band>(band)); |
516 | } |
517 | |
518 | void QDeclarativeRadio::_q_error(QRadioTuner::Error errorCode) |
519 | { |
520 | emit error(errorCode: static_cast<QDeclarativeRadio::Error>(errorCode)); |
521 | emit errorChanged(); |
522 | } |
523 | |
524 | void QDeclarativeRadio::_q_availabilityChanged(QMultimedia::AvailabilityStatus availability) |
525 | { |
526 | emit availabilityChanged(availability: Availability(availability)); |
527 | } |
528 | |
529 | /*! |
530 | \qmlsignal QtMultimedia::Radio::stationFound(int frequency, string stationId) |
531 | |
532 | This signal is emitted when a new radio station is found. This signal is only emitted |
533 | if \l searchAllStations is called with \c SearchGetStationId. |
534 | |
535 | The \a frequency is returned in Hertz, and the \a stationId corresponds to the station Id |
536 | in the \l RadioData for this radio station. |
537 | |
538 | The corresponding handler is \c onStationFound. |
539 | */ |
540 | |
541 | QT_END_NAMESPACE |
542 | |