summaryrefslogtreecommitdiffstats
path: root/src/imports/multimedia/qdeclarativeradio.cpp
blob: 655b87ca534fd7edf85238b7b35cee702a51d6a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativeradio_p.h"
#include "qdeclarativeradiodata_p.h"

QT_BEGIN_NAMESPACE

/*!
    \qmltype Radio
    \instantiates QDeclarativeRadio
    \inqmlmodule QtMultimedia
    \brief Access radio functionality from a QML application.
    \ingroup multimedia_qml
    \ingroup multimedia_radio_qml
    \inherits Item
    \deprecated

    \qml
    Rectangle {
        width: 320
        height: 480

        Radio {
            id: radio
            band: Radio.FM
        }

        MouseArea {
            x: 0; y: 0
            height: parent.height
            width: parent.width / 2

            onClicked: radio.scanDown()
        }

        MouseArea {
            x: parent.width / 2; y: 0
            height: parent.height
            width: parent.width / 2

            onClicked: radio.scanUp()
        }
    }
    \endqml

    You can use \c Radio to tune the radio and get information about the signal.
    You can also use the Radio to get information about tuning, for instance the
    frequency steps supported for tuning.

    The corresponding \l RadioData gives RDS information about the
    current radio station. The best way to access the RadioData for
    the current Radio is to use the \c radioData property.

    \sa {Radio Overview}

*/


QDeclarativeRadio::QDeclarativeRadio(QObject *parent) :
    QObject(parent)
{
    m_radioTuner = new QRadioTuner(this);
    m_radioData = new QDeclarativeRadioData(m_radioTuner, this);

    connect(m_radioTuner, SIGNAL(stateChanged(QRadioTuner::State)), this, SLOT(_q_stateChanged(QRadioTuner::State)));
    connect(m_radioTuner, SIGNAL(bandChanged(QRadioTuner::Band)), this, SLOT(_q_bandChanged(QRadioTuner::Band)));

    connect(m_radioTuner, SIGNAL(frequencyChanged(int)), this, SIGNAL(frequencyChanged(int)));
    connect(m_radioTuner, SIGNAL(stereoStatusChanged(bool)), this, SIGNAL(stereoStatusChanged(bool)));
    connect(m_radioTuner, SIGNAL(searchingChanged(bool)), this, SIGNAL(searchingChanged(bool)));
    connect(m_radioTuner, SIGNAL(signalStrengthChanged(int)), this, SIGNAL(signalStrengthChanged(int)));
    connect(m_radioTuner, SIGNAL(volumeChanged(int)), this, SIGNAL(volumeChanged(int)));
    connect(m_radioTuner, SIGNAL(mutedChanged(bool)), this, SIGNAL(mutedChanged(bool)));
    connect(m_radioTuner, SIGNAL(stationFound(int,QString)), this, SIGNAL(stationFound(int,QString)));
    connect(m_radioTuner, SIGNAL(antennaConnectedChanged(bool)), this, SIGNAL(antennaConnectedChanged(bool)));
    connect(m_radioTuner, SIGNAL(availabilityChanged(QMultimedia::AvailabilityStatus)), this, SLOT(_q_availabilityChanged(QMultimedia::AvailabilityStatus)));

    connect(m_radioTuner, SIGNAL(error(QRadioTuner::Error)), this, SLOT(_q_error(QRadioTuner::Error)));
}

QDeclarativeRadio::~QDeclarativeRadio()
{
}

/*!
    \qmlproperty enumeration QtMultimedia::Radio::state

    This property holds the current state of the Radio.

    \table
    \header \li Value \li Description
    \row \li ActiveState
        \li The radio is started and active

    \row \li StoppedState
        \li The radio is stopped

    \endtable

    \sa start, stop
*/
QDeclarativeRadio::State QDeclarativeRadio::state() const
{
    return static_cast<QDeclarativeRadio::State>(m_radioTuner->state());
}

/*!
    \qmlproperty enumeration QtMultimedia::Radio::band

    This property holds the frequency band used for the radio, which can be specified as
    any one of the values in the table below.

    \table
    \header \li Value \li Description
    \row \li AM
        \li 520 to 1610 kHz, 9 or 10kHz channel spacing, extended 1610 to 1710 kHz

    \row \li FM
        \li 87.5 to 108.0 MHz, except Japan 76-90 MHz

    \row \li SW
        \li 1.711 to 30.0 MHz, divided into 15 bands. 5kHz channel spacing

    \row \li LW
        \li 148.5 to 283.5 kHz, 9kHz channel spacing (Europe, Africa, Asia)

    \row \li FM2
        \li range not defined, used when area supports more than one FM range

    \endtable
*/
QDeclarativeRadio::Band QDeclarativeRadio::band() const
{
    return static_cast<QDeclarativeRadio::Band>(m_radioTuner->band());
}

/*!
    \qmlproperty int QtMultimedia::Radio::frequency

    Sets the frequency in Hertz that the radio is tuned to. The frequency must be within the frequency
    range for the current band, otherwise it will be changed to be within the frequency range.

    \sa maximumFrequency, minimumFrequency
*/
int QDeclarativeRadio::frequency() const
{
    return m_radioTuner->frequency();
}

/*!
    \qmlproperty enumeration QtMultimedia::Radio::stereoMode

    This property holds the stereo mode of the radio, which can be set to any one of the
    values in the table below.

    \table
    \header \li Value \li Description
    \row \li Auto
        \li Uses stereo mode matching the station

    \row \li ForceStereo
        \li Forces the radio to play the station in stereo, converting the sound signal if necessary

    \row \li ForceMono
        \li Forces the radio to play the station in mono, converting the sound signal if necessary

    \endtable
*/
QDeclarativeRadio::StereoMode QDeclarativeRadio::stereoMode() const
{
    return static_cast<QDeclarativeRadio::StereoMode>(m_radioTuner->stereoMode());
}

/*!
    \qmlproperty int QtMultimedia::Radio::volume

    Set this property to control the volume of the radio. The valid range of the volume is from 0 to 100.
*/
int QDeclarativeRadio::volume() const
{
    return m_radioTuner->volume();
}

/*!
    \qmlproperty bool QtMultimedia::Radio::muted

    This property reflects whether the radio is muted or not.
*/
bool QDeclarativeRadio::muted() const
{
    return m_radioTuner->isMuted();
}

/*!
    \qmlproperty bool QtMultimedia::Radio::stereo

    This property holds whether the radio is receiving a stereo signal or not. If \l stereoMode is
    set to ForceMono the value will always be false. Likewise, it will always be true if stereoMode
    is set to ForceStereo.

    \sa stereoMode
*/
bool QDeclarativeRadio::stereo() const
{
    return m_radioTuner->isStereo();
}

/*!
    \qmlproperty int QtMultimedia::Radio::signalStrength

    The strength of the current radio signal as a percentage where 0% equals no signal, and 100% is a
    very good signal.
*/
int QDeclarativeRadio::signalStrength() const
{
    return m_radioTuner->signalStrength();
}

/*!
    \qmlproperty bool QtMultimedia::Radio::searching

    This property is true if the radio is currently searching for radio stations, for instance using the \l scanUp,
    \l scanDown, and \l searchAllStations methods. Once the search completes, or if it is cancelled using
    \l cancelScan, this property will be false.
*/
bool QDeclarativeRadio::searching() const
{
    return m_radioTuner->isSearching();
}

/*!
    \qmlproperty int QtMultimedia::Radio::frequencyStep

    The number of Hertz for each step when tuning the radio manually. The value is for the current \l band.
 */
int QDeclarativeRadio::frequencyStep() const
{
    return m_radioTuner->frequencyStep(m_radioTuner->band());
}

/*!
    \qmlproperty int QtMultimedia::Radio::minimumFrequency

    The minimum frequency for the current \l band.
 */
int QDeclarativeRadio::minimumFrequency() const
{
    return m_radioTuner->frequencyRange(m_radioTuner->band()).first;
}

/*!
    \qmlproperty int QtMultimedia::Radio::maximumFrequency

    The maximum frequency for the current \l band.
 */
int QDeclarativeRadio::maximumFrequency() const
{
    return m_radioTuner->frequencyRange(m_radioTuner->band()).second;
}

/*!
    \qmlproperty int QtMultimedia::Radio::antennaConnected

    This property is true if there is an antenna connected. Otherwise it will be false.
 */
bool QDeclarativeRadio::isAntennaConnected() const
{
    return m_radioTuner->isAntennaConnected();
}

/*!
    \qmlproperty enumeration QtMultimedia::Radio::availability

    Returns the availability state of the radio.

    This is one of:

    \table
    \header \li Value \li Description
    \row \li Available
        \li The radio is available to use
    \row \li Busy
        \li The radio is usually available to use, but is currently busy.
           This can happen when some other process needs to use the audio
           hardware.
    \row \li Unavailable
        \li The radio is not available to use (there may be no radio
           hardware)
    \row \li ResourceMissing
        \li There is one or more resources missing, so the radio cannot
           be used.  It may be possible to try again at a later time.  This
           can occur if there is no antenna connected - see the \l antennaConnected
           property as well.
    \endtable
 */
QDeclarativeRadio::Availability QDeclarativeRadio::availability() const
{
    return Availability(m_radioTuner->availability());
}

void QDeclarativeRadio::setBand(QDeclarativeRadio::Band band)
{
    m_radioTuner->setBand(static_cast<QRadioTuner::Band>(band));
}

void QDeclarativeRadio::setFrequency(int frequency)
{
    m_radioTuner->setFrequency(frequency);
}

void QDeclarativeRadio::setStereoMode(QDeclarativeRadio::StereoMode stereoMode)
{
    m_radioTuner->setStereoMode(static_cast<QRadioTuner::StereoMode>(stereoMode));
}

void QDeclarativeRadio::setVolume(int volume)
{
    m_radioTuner->setVolume(volume);
}

void QDeclarativeRadio::setMuted(bool muted)
{
    m_radioTuner->setMuted(muted);
}

/*!
    \qmlmethod QtMultimedia::Radio::cancelScan()

    Cancel the current scan. Will also cancel a search started with \l searchAllStations.
 */
void QDeclarativeRadio::cancelScan()
{
    m_radioTuner->cancelSearch();
}

/*!
    \qmlmethod QtMultimedia::Radio::scanDown()

    Searches backward in the frequency range for the current band.
 */
void QDeclarativeRadio::scanDown()
{
    m_radioTuner->searchBackward();
}

/*!
    \qmlmethod QtMultimedia::Radio::scanUp()

    Searches forward in the frequency range for the current band.
 */
void QDeclarativeRadio::scanUp()
{
    m_radioTuner->searchForward();
}

/*!
    \qmlmethod QtMultimedia::Radio::searchAllStations(enumeration searchMode)

    Start searching the complete frequency range for the current band, and save all the
    radio stations found. The \a searchMode can be either of the values described in the
    table below.

    \table
    \header \li Value \li Description
    \row \li SearchFast
        \li Stores each radio station for later retrival and tuning

    \row \li SearchGetStationId
        \li Does the same as SearchFast, but also emits the station Id with the \l stationFound signal.

    \endtable

    The snippet below uses \c searchAllStations with \c SearchGetStationId to receive all the radio
    stations in the current band, and store them in a ListView. The station Id is shown to the user
    and if the user presses a station, the radio is tuned to this station.

    \qml
    Item {
        width: 640
        height: 360

        Radio {
            id: radio
            onStationFound: radioStations.append({"frequency": frequency, "stationId": stationId})
        }

        ListModel {
            id: radioStations
        }

        ListView {
            model: radioStations
            delegate: Rectangle {
                    MouseArea {
                        anchors.fill: parent
                        onClicked: radio.frequency = frequency
                    }

                    Text {
                        anchors.fill: parent
                        text: stationId
                    }
                }
        }

        Rectangle {
            MouseArea {
                anchors.fill: parent
                onClicked: radio.searchAllStations(Radio.SearchGetStationId)
            }
        }
    }
    \endqml
 */
void QDeclarativeRadio::searchAllStations(QDeclarativeRadio::SearchMode searchMode)
{
    m_radioTuner->searchAllStations(static_cast<QRadioTuner::SearchMode>(searchMode));
}

/*!
    \qmlmethod QtMultimedia::Radio::tuneDown()

    Decrements the frequency by the frequency step for the current band. If the frequency is already set
    to the minimum frequency, calling this function has no effect.

    \sa band, frequencyStep, minimumFrequency
 */
void QDeclarativeRadio::tuneDown()
{
    int f = frequency();
    f = f - frequencyStep();
    setFrequency(f);
}

/*!
    \qmlmethod QtMultimedia::Radio::tuneUp()

    Increments the frequency by the frequency step for the current band. If the frequency is already set
    to the maximum frequency, calling this function has no effect.

    \sa band, frequencyStep, maximumFrequency
 */
void QDeclarativeRadio::tuneUp()
{
    int f = frequency();
    f = f + frequencyStep();
    setFrequency(f);
}

/*!
    \qmlmethod QtMultimedia::Radio::start()

    Starts the radio. If the radio is available, as determined by the \l availability property,
    this will result in the \l state becoming \c ActiveState.
 */
void QDeclarativeRadio::start()
{
    m_radioTuner->start();
}

/*!
    \qmlmethod QtMultimedia::Radio::stop()

    Stops the radio. After calling this method the \l state will be \c StoppedState.
 */
void QDeclarativeRadio::stop()
{
    m_radioTuner->stop();
}

void QDeclarativeRadio::_q_stateChanged(QRadioTuner::State state)
{
    emit stateChanged(static_cast<QDeclarativeRadio::State>(state));
}

void QDeclarativeRadio::_q_bandChanged(QRadioTuner::Band band)
{
    emit bandChanged(static_cast<QDeclarativeRadio::Band>(band));
}

void QDeclarativeRadio::_q_error(QRadioTuner::Error errorCode)
{
    emit error(static_cast<QDeclarativeRadio::Error>(errorCode));
    emit errorChanged();
}

void QDeclarativeRadio::_q_availabilityChanged(QMultimedia::AvailabilityStatus availability)
{
    emit availabilityChanged(Availability(availability));
}

/*!
    \qmlsignal QtMultimedia::Radio::stationFound(int frequency, string stationId)

    This signal is emitted when a new radio station is found. This signal is only emitted
    if \l searchAllStations is called with \c SearchGetStationId.

    The \a frequency is returned in Hertz, and the \a stationId corresponds to the station Id
    in the \l RadioData for this radio station.

    The corresponding handler is \c onStationFound.
  */

QT_END_NAMESPACE