File: ioconfig.cpp

package info (click to toggle)
gqrx-sdr 2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,700 kB
  • ctags: 2,724
  • sloc: cpp: 22,413; ansic: 564; xml: 25; makefile: 4
file content (696 lines) | stat: -rw-r--r-- 22,720 bytes parent folder | download | duplicates (2)
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* -*- c++ -*- */
/*
 * Gqrx SDR: Software defined radio receiver powered by GNU Radio and Qt
 *           http://gqrx.dk/
 *
 * Copyright 2011-2014 Alexandru Csete OZ9AEC.
 *
 * Gqrx is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * Gqrx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gqrx; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */
#include <QDebug>
#include <QFile>
#include <QPushButton>
#include <QRegExp>
#include <QSettings>
#include <QString>
#include <QtGlobal>
#include <QVariant>

#include <boost/foreach.hpp>
#include <osmosdr/device.h>
#include <osmosdr/source.h>
#include <osmosdr/ranges.h>

#ifdef WITH_PULSEAUDIO
#include "pulseaudio/pa_device_list.h"
#elif WITH_PORTAUDIO
#include "portaudio/device_list.h"
#elif defined(GQRX_OS_MACX)
#include "osxaudio/device_list.h"
#endif

#include "qtgui/ioconfig.h"
#include "ui_ioconfig.h"


CIoConfig::CIoConfig(QSettings * settings,
                     std::map<QString, QVariant> &devList,
                     QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CIoConfig),
    m_settings(settings)
{
    unsigned int i=0;
    QString devstr;
    bool cfgmatch=false; //flag to indicate that device from config was found

    ui->setupUi(this);

    QString indev = settings->value("input/device", "").toString();

    // insert the device list in device combo box
    std::map<QString, QVariant>::iterator I = devList.begin();
    i = 0;
    while (I != devList.end())
    {
        devstr = (*I).second.toString();
        ui->inDevCombo->addItem((*I).first, devstr);

        // is this the device stored in config?
        if (indev == devstr)
        {
            ui->inDevCombo->setCurrentIndex(i);
            ui->inDevEdit->setText(devstr);
            cfgmatch = true;
        }
        ++I;
        ++i;
    }


    ui->inDevCombo->addItem(tr("Other..."), QVariant(""));

    // If device string from config is not one of the detected devices
    // it could be that device is not plugged in (in which case we select
    // other) or that this is the first time (select the first detected device).
    if (!cfgmatch)
    {
        if (indev.isEmpty())
        {
            // First time config: select the first detected device
            ui->inDevCombo->setCurrentIndex(0);
            ui->inDevEdit->setText(ui->inDevCombo->itemData(0).toString());
            if (ui->inDevEdit->text().isEmpty())
                ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
        }
        else
        {
            // Select other
            ui->inDevCombo->setCurrentIndex(i);
            ui->inDevEdit->setText(indev);
        }
    }

    // input rate
    updateInputSampleRates(settings->value("input/sample_rate", 0).toInt());

    // decimation
    int idx = decim2idx(settings->value("input/decimation", 0).toInt());
    ui->decimCombo->setCurrentIndex(idx);
    decimationChanged(idx);

    // Analog bandwidth
    ui->bwSpinBox->setValue(1.0e-6*settings->value("input/bandwidth", 0.0).toDouble());

    // LNB LO
    ui->loSpinBox->setValue(1.0e-6 * settings->value("input/lnb_lo", 0.0).toDouble());

    // Output device
    QString outdev = settings->value("output/device", "").toString();

     // get list of audio output devices
#ifdef WITH_PULSEAUDIO
    pa_device_list devices;
    outDevList = devices.get_output_devices();

    qDebug() << __FUNCTION__ << ": Available output devices:";
    for (i = 0; i < outDevList.size(); i++)
    {
        qDebug() << "   " << i << ":" << QString(outDevList[i].get_description().c_str());
        //qDebug() << "     " << QString(outDevList[i].get_name().c_str());
        ui->outDevCombo->addItem(QString(outDevList[i].get_description().c_str()));

        // note that item #i in devlist will be item #(i+1)
        // in combo box due to "default"
        if (outdev == QString(outDevList[i].get_name().c_str()))
            ui->outDevCombo->setCurrentIndex(i+1);
    }
#elif WITH_PORTAUDIO
    portaudio_device_list   devices;

    outDevList = devices.get_output_devices();
    for (i = 0; i < outDevList.size(); i++)
    {
        ui->outDevCombo->addItem(QString(outDevList[i].get_description().c_str()));

        // note that item #i in devlist will be item #(i+1)
        // in combo box due to "default"
        if (outdev == QString(outDevList[i].get_name().c_str()))
            ui->outDevCombo->setCurrentIndex(i+1);
    }
    //ui->outDevCombo->setEditable(true);

#elif defined(GQRX_OS_MACX)
    osxaudio_device_list devices;
    outDevList = devices.get_output_devices();

    qDebug() << __FUNCTION__ << ": Available output devices:";
    for (i = 0; i < outDevList.size(); i++)
    {
        qDebug() << "   " << i << ":" << QString(outDevList[i].get_name().c_str());
        ui->outDevCombo->addItem(QString(outDevList[i].get_name().c_str()));

        // note that item #i in devlist will be item #(i+1)
        // in combo box due to "default"
        if (outdev == QString(outDevList[i].get_name().c_str()))
            ui->outDevCombo->setCurrentIndex(i+1);
    }

#else
    ui->outDevCombo->addItem(settings->value("output/device", "Default").toString(),
                             settings->value("output/device", "Default").toString());
    ui->outDevCombo->setEditable(true);
#endif // WITH_PULSEAUDIO

    // Signals and slots
    connect(this, SIGNAL(accepted()), this, SLOT(saveConfig()));
    connect(ui->inDevCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(inputDeviceSelected(int)));
    connect(ui->inDevEdit, SIGNAL(textChanged(QString)), this, SLOT(inputDevstrChanged(QString)));
    connect(ui->inSrCombo, SIGNAL(editTextChanged(QString)), this, SLOT(inputRateChanged(QString)));
    connect(ui->decimCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(decimationChanged(int)));
}

CIoConfig::~CIoConfig()
{
    delete ui;
}

/**
 * @brief get the list of devices
 */
void CIoConfig::getDeviceList(std::map<QString, QVariant> &devList)
{
    unsigned int i=0;
    QString devstr;
    QString devlabel;

#if defined(GQRX_OS_MACX)
    // automatic discovery of FCD does not work on Mac
    // so we do it ourselves
    osxaudio_device_list devices;
    vector<osxaudio_device> inDevList = devices.get_input_devices();

    string this_dev;
    for (i = 0; i < inDevList.size(); i++)
    {
        this_dev = inDevList[i].get_name();
        if (this_dev.find("FUNcube Dongle V1.0") != string::npos)
        {
            devstr = "fcd,type=1,device='FUNcube Dongle V1.0'";
            devList.insert(std::pair<QString, QVariant>(QString("FUNcube Dongle V1.0"), QVariant(devstr)));
        }
        else if (this_dev.find("FUNcube Dongle V1_0") != string::npos)      // since OS X 10.11.4
        {
            devstr = "fcd,type=1,device='FUNcube Dongle V1_0'";
            devList.insert(std::pair<QString, QVariant>(QString("FUNcube Dongle V1_0"), QVariant(devstr)));
        }
        else if (this_dev.find("FUNcube Dongle V2.0") != string::npos)
        {
            devstr = "fcd,type=2,device='FUNcube Dongle V2.0'";
            devList.insert(std::pair<QString, QVariant>(QString("FUNcube Dongle V2.0"), QVariant(devstr)));
        }
        else if (this_dev.find("FUNcube Dongle V2_0") != string::npos)      // since OS X 10.11.4
        {
            devstr = "fcd,type=2,device='FUNcube Dongle V2_0'";
            devList.insert(std::pair<QString, QVariant>(QString("FUNcube Dongle V2_0"), QVariant(devstr)));
        }
    }
#endif

    // Get list of input devices discovered by gr-osmosdr and store them in
    // the device list together with the device descriptor strings
    osmosdr::devices_t devs = osmosdr::device::find();

    qDebug() << __FUNCTION__ << ": Available input devices:";
    BOOST_FOREACH(osmosdr::device_t &dev, devs)
    {
        if (dev.count("label"))
        {
            devlabel = QString(dev["label"].c_str());
            dev.erase("label");
        }
        else
        {
            devlabel = "Unknown";
        }

        devstr = QString(dev.to_string().c_str());
        devList.insert(std::pair<QString, QVariant>(devlabel, devstr));

        qDebug() << "   " << i << ":"  << devlabel;
        ++i;
    }
}

/** Save configuration. */
void CIoConfig::saveConfig()
{
    int         idx;
    int         int_val;
    bool        conv_ok;

    qDebug() << __FUNCTION__;

    idx = ui->outDevCombo->currentIndex();

#if defined(WITH_PULSEAUDIO) || defined(GQRX_OS_MACX)
    if (idx > 0)
    {
          qDebug() << "Output device" << idx << ":" << QString(outDevList[idx-1].get_name().c_str());
          m_settings->setValue("output/device", QString(outDevList[idx-1].get_name().c_str()));
    }
#else
    if (idx > 0 || ui->outDevCombo->currentText() != "Default")
    {
        qDebug() << "Output device:" << ui->outDevCombo->currentText();
        m_settings->setValue("output/device", ui->outDevCombo->currentText());
    }
#endif
    else
    {
        m_settings->remove("output/device");
    }

    // input settings
    m_settings->setValue("input/device", ui->inDevEdit->text());  // "OK" button disabled if empty

    qint64 value = (qint64)(ui->bwSpinBox->value()*1.e6);
    if (value)
        m_settings->setValue("input/bandwidth", value);
    else
        m_settings->remove("input/bandwidth");

    value = (qint64)(ui->loSpinBox->value()*1.e6);
    if (value)
        m_settings->setValue("input/lnb_lo", value);
    else
        m_settings->remove("input/lnb_lo");

    int_val = ui->inSrCombo->currentText().toInt(&conv_ok);
    if (conv_ok)
        m_settings->setValue("input/sample_rate", int_val);
    else
        m_settings->remove("input/sample_rate");

    idx = ui->decimCombo->currentIndex();
    int_val = idx2decim(idx);
    if (int_val < 2)
        m_settings->remove("input/decimation");
    else
        m_settings->setValue("input/decimation", int_val);
}


/**
 * @brief Update list of sample rates based on selected device.
 * @param rate The current sample rate from the configuration.
 */
void CIoConfig::updateInputSampleRates(int rate)
{
    ui->inSrCombo->clear();

    if (ui->inDevEdit->text().isEmpty())
        return;

    if (ui->inDevEdit->text().contains("fcd"))
    {
        if (ui->inDevCombo->currentText().contains("V2"))   // V2.0 or V2_0
        {
            ui->inSrCombo->addItem("192000");
        }
        else
        {
            ui->inSrCombo->addItem("96000");
        }
    }
    else if (ui->inDevEdit->text().contains("rtl") || ui->inDevEdit->text().contains("rtl_tcp"))
    {
        ui->inSrCombo->addItem("240000");
        ui->inSrCombo->addItem("300000");
        ui->inSrCombo->addItem("960000");
        ui->inSrCombo->addItem("1152000");
        ui->inSrCombo->addItem("1200000");
        ui->inSrCombo->addItem("1440000");
        ui->inSrCombo->addItem("1600000");
        ui->inSrCombo->addItem("1800000");
        ui->inSrCombo->addItem("1920000");
        ui->inSrCombo->addItem("2400000");
        ui->inSrCombo->addItem("2880000");
        ui->inSrCombo->addItem("3200000");
        if (rate > 0)
        {
            ui->inSrCombo->addItem(QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(12);
        }
        else
        {
            ui->inSrCombo->setCurrentIndex(7);
        }
    }
    else if (ui->inDevEdit->text().contains("uhd"))
    {
        if (rate > 0)
            ui->inSrCombo->addItem(QString("%1").arg(rate));
        ui->inSrCombo->addItem("250000");
        ui->inSrCombo->addItem("500000");
        ui->inSrCombo->addItem("2000000");
        ui->inSrCombo->addItem("4000000");
        ui->inSrCombo->addItem("8000000");
    }
    else if (ui->inDevEdit->text().contains("hackrf"))
    {
        if (rate > 0)
            ui->inSrCombo->addItem(QString("%1").arg(rate));
        ui->inSrCombo->addItem("8000000");
        ui->inSrCombo->addItem("10000000");
        ui->inSrCombo->addItem("12500000");
        ui->inSrCombo->addItem("16000000");
        ui->inSrCombo->addItem("20000000");
    }
    else if (ui->inDevEdit->text().contains("bladerf"))
    {
        if (rate > 0)
            ui->inSrCombo->addItem(QString("%1").arg(rate));
        ui->inSrCombo->addItem("160000");
        ui->inSrCombo->addItem("250000");
        ui->inSrCombo->addItem("500000");
        ui->inSrCombo->addItem("1000000");
        ui->inSrCombo->addItem("2000000");
        ui->inSrCombo->addItem("5000000");
        ui->inSrCombo->addItem("8000000");
        ui->inSrCombo->addItem("10000000");
        ui->inSrCombo->addItem("15000000");
        ui->inSrCombo->addItem("20000000");
        ui->inSrCombo->addItem("25000000");
        ui->inSrCombo->addItem("30000000");
        ui->inSrCombo->addItem("35000000");
        ui->inSrCombo->addItem("40000000");
    }
    else if (ui->inDevEdit->text().contains("sdr-iq"))
    {
        ui->inSrCombo->addItem("8138");
        ui->inSrCombo->addItem("16276");
        ui->inSrCombo->addItem("37793");
        ui->inSrCombo->addItem("55556");
        ui->inSrCombo->addItem("111111");
        ui->inSrCombo->addItem("158730");
        ui->inSrCombo->addItem("196078");
        if (rate > 0)
        {
            ui->inSrCombo->insertItem(0, QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(0);
        }
        else
            ui->inSrCombo->setCurrentIndex(4); // select 111.111 kHz
    }
    else if (ui->inDevEdit->text().contains("sdr-ip"))
    {
        ui->inSrCombo->addItem("31250");
        ui->inSrCombo->addItem("32000");
        ui->inSrCombo->addItem("40000");
        ui->inSrCombo->addItem("50000");
        ui->inSrCombo->addItem("62500");
        ui->inSrCombo->addItem("64000");
        ui->inSrCombo->addItem("80000");
        ui->inSrCombo->addItem("100000");
        ui->inSrCombo->addItem("125000");
        ui->inSrCombo->addItem("160000");
        ui->inSrCombo->addItem("200000");
        ui->inSrCombo->addItem("250000");
        ui->inSrCombo->addItem("320000");
        ui->inSrCombo->addItem("400000");
        ui->inSrCombo->addItem("500000");
        ui->inSrCombo->addItem("800000");
        ui->inSrCombo->addItem("1000000");
        ui->inSrCombo->addItem("1600000");
        ui->inSrCombo->addItem("2000000");
        if (rate > 0)
        {
            ui->inSrCombo->insertItem(0, QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(0);
        }
        else
            ui->inSrCombo->setCurrentIndex(11); // select 250 kHz
    }
    else if (ui->inDevEdit->text().contains("netsdr"))
    {
        ui->inSrCombo->addItem("32000");
        ui->inSrCombo->addItem("40000");
        ui->inSrCombo->addItem("50000");
        ui->inSrCombo->addItem("62500");
        ui->inSrCombo->addItem("78125");
        ui->inSrCombo->addItem("80000");
        ui->inSrCombo->addItem("100000");
        ui->inSrCombo->addItem("125000");
        ui->inSrCombo->addItem("156250");
        ui->inSrCombo->addItem("160000");
        ui->inSrCombo->addItem("200000");
        ui->inSrCombo->addItem("250000");
        ui->inSrCombo->addItem("312500");
        ui->inSrCombo->addItem("400000");
        ui->inSrCombo->addItem("500000");
        ui->inSrCombo->addItem("625000");
        ui->inSrCombo->addItem("800000");
        ui->inSrCombo->addItem("1000000");
        ui->inSrCombo->addItem("1250000");
        ui->inSrCombo->addItem("2000000");
        if (rate > 0)
        {
            ui->inSrCombo->insertItem(0, QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(0);
        }
        else
            ui->inSrCombo->setCurrentIndex(11); // select 250 kHz
    }
    else if (ui->inDevEdit->text().contains("cloudiq"))
    {
        ui->inSrCombo->addItem("48000");
        ui->inSrCombo->addItem("61440");
        ui->inSrCombo->addItem("96000");
        ui->inSrCombo->addItem("122880");
        ui->inSrCombo->addItem("240000");
        ui->inSrCombo->addItem("256000");
        ui->inSrCombo->addItem("370120");
        ui->inSrCombo->addItem("495483");
        ui->inSrCombo->addItem("512000");
        ui->inSrCombo->addItem("614400");
        ui->inSrCombo->addItem("1024000");
        ui->inSrCombo->addItem("1228800");
        ui->inSrCombo->addItem("1807058");
        if (rate > 0)
        {
            ui->inSrCombo->insertItem(0, QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(0);
        }
        else
            ui->inSrCombo->setCurrentIndex(4); // select 240 kHz
    }
    else if (ui->inDevEdit->text().contains("airspy"))
    {
        if (rate > 0)
            ui->inSrCombo->addItem(QString("%1").arg(rate));

        ui->inSrCombo->addItem("2500000");
        ui->inSrCombo->addItem("3000000");
        ui->inSrCombo->addItem("6000000");
        ui->inSrCombo->addItem("10000000");
    }
    else if (ui->inDevEdit->text().contains("redpitaya"))
    {
        ui->inSrCombo->addItem("20000");
        ui->inSrCombo->addItem("50000");
        ui->inSrCombo->addItem("100000");
        ui->inSrCombo->addItem("250000");
        ui->inSrCombo->addItem("500000");
        ui->inSrCombo->addItem("1250000");
        if (rate > 0)
        {
            ui->inSrCombo->insertItem(0, QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(0);
        }
        else
            ui->inSrCombo->setCurrentIndex(3); // select 250 kHz
    }
    else if (ui->inDevEdit->text().contains("sdrplay"))
    {
        ui->inSrCombo->addItem("222222");
        ui->inSrCombo->addItem("333333");
        ui->inSrCombo->addItem("428571");
        ui->inSrCombo->addItem("500000");
        ui->inSrCombo->addItem("571429");
        ui->inSrCombo->addItem("750000");
        ui->inSrCombo->addItem("875000");
        ui->inSrCombo->addItem("1000000");
        ui->inSrCombo->addItem("1536000");
        ui->inSrCombo->addItem("2048000");
        ui->inSrCombo->addItem("5000000");
        ui->inSrCombo->addItem("6000000");
        ui->inSrCombo->addItem("7000000");
        ui->inSrCombo->addItem("8000000");
        ui->inSrCombo->addItem("9000000");
        ui->inSrCombo->addItem("10000000");
        ui->inSrCombo->addItem("11000000");
        ui->inSrCombo->addItem("12000000");
        if (rate > 0)
        {
            ui->inSrCombo->insertItem(0, QString("%1").arg(rate));
            ui->inSrCombo->setCurrentIndex(0);
        }
        else
            ui->inSrCombo->setCurrentIndex(9); // select 2048 kHz
    }
    else
    {
        if (rate > 0)
            ui->inSrCombo->addItem(QString("%1").arg(rate));
    }

    updateDecimations();
}

/**
 * @brief Update available decimations according to the current sample rate.
 *
 * This function will repopulate the decimation selector combo box to only
 * include decimations up to a meaningful maximum value, so that the quadrature
 * rate doesn't get below 48 ksps.
 */
void CIoConfig::updateDecimations(void)
{
    bool        ok;
    int         rate;

    // get current sample rate from combo box
    rate= ui->inSrCombo->currentText().toInt(&ok);
    if (!ok || rate < 0)
        return;

    ui->decimCombo->clear();
    ui->decimCombo->addItem("None", 0);
    if (rate >= 96000)
        ui->decimCombo->addItem("2", 0);
    if (rate >= 192000)
        ui->decimCombo->addItem("4", 0);
    if (rate >= 384000)
        ui->decimCombo->addItem("8", 0);
    if (rate >= 768000)
        ui->decimCombo->addItem("16", 0);
    if (rate >= 1536000)
        ui->decimCombo->addItem("32", 0);
    if (rate >= 3072000)
        ui->decimCombo->addItem("64", 0);
    if (rate >= 6144000)
        ui->decimCombo->addItem("128", 0);
//    if (rate >= 12288000)
//        ui->decimCombo->addItem("256", 0);
//    if (rate >= 24576000)
//        ui->decimCombo->addItem("512", 0);

    decimationChanged(0);
}

/**
 * @brief New input device has been selected by the user.
 * @param index The index of the item that has been selected in the combo box.
 */
void CIoConfig::inputDeviceSelected(int index)
{
    qDebug() << "New input device selected:" << index;
    qDebug() << "  Label:" << ui->inDevCombo->itemText(index);
    qDebug() << "  Devstr:" << ui->inDevCombo->itemData(index).toString();

    ui->inDevEdit->setText(ui->inDevCombo->itemData(index).toString());
    updateInputSampleRates(0);
}


/**
 * @brief Input device string has changed
 * @param text THe new device string
 *
 * This slot is activated when the device string in the text edit box has changed
 * either by the user or programatically. We use this to enable/disable the OK
 * button - we allo OK only if there is some text in the text entry.
 */
void CIoConfig::inputDevstrChanged(const QString &text)
{
    if (text.isEmpty())
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
    else
        ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
}

/**
 * @brief Sample changed, either by selection of direct entry.
 * @param text The new sample rate.
 */
void CIoConfig::inputRateChanged(const QString &text)
{
    (void) text;
    updateDecimations();
}

/**
 * @brief New decimation rate selected.
 * @param index The index of the selected item in the combo box.
 *
 * This function calculates the quadrature rate and updates the sample rate
 * label just below the decimation combo box.
 */
void CIoConfig::decimationChanged(int index)
{
    float       quad_rate;
    int         input_rate;
    int         decim;
    bool        ok;

    decim = idx2decim(index);
    input_rate = ui->inSrCombo->currentText().toInt(&ok);
    if (!ok)
        return;

    quad_rate = (float)input_rate / (float)decim;
    if (quad_rate > 1.e6f)
        ui->sampRateLabel->setText(QString(" %1 Msps").
                                   arg(quad_rate * 1.e-6, 0, 'f', 3));
    else
        ui->sampRateLabel->setText(QString(" %1 ksps").
                                   arg(quad_rate * 1.e-3, 0, 'f', 3));
}

/** Convert a combo box index to decimation. */
int CIoConfig::idx2decim(int idx) const
{
    if (idx < 1)
        return 1;

    return (1 << idx);
}

/** Convert a decimation to a combobox index */
int CIoConfig::decim2idx(int decim) const
{
    int         idx;

    if (decim == 0)
        return 0;

    idx = 0;
    while (decim >>= 1)
        ++idx;

    return idx;
}