summaryrefslogtreecommitdiff
path: root/examples/terminal/settingsdialog.cpp
blob: 08529d87b018e1455ba4ffa2e67fa019728851c8 (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
#include "settingsdialog.h"
#include "ui_settingsdialog.h"

#include <QtAddOnSerialPort/serialportinfo.h>

SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SettingsDialog)
{
    ui->setupUi(this);

    connect(ui->applyButton, SIGNAL(clicked()),
            this, SLOT(apply()));
    connect(ui->portsBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(showPortInfo(int)));

    fillPortsParameters();
    fillPortsInfo();

    updateSettings();
}

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

SettingsDialog::Settings SettingsDialog::settings() const
{
    return currentSettings;
}

void SettingsDialog::showPortInfo(int idx)
{
    if (idx != -1) {
        QStringList list = ui->portsBox->itemData(idx).toStringList();
        ui->descriptionLabel->setText(tr("Description: %1").arg(list.at(1)));
        ui->manufacturerLabel->setText(tr("Manufacturer: %1").arg(list.at(2)));
        ui->locationLabel->setText(tr("Location: %1").arg(list.at(3)));
    }
}

void SettingsDialog::apply()
{
    updateSettings();
    hide();
}

void SettingsDialog::fillPortsParameters()
{
    // fill baud rate (is not the entire list of available values,
    // desired values??, add your independently)
    ui->rateBox->addItem(QLatin1String("9600"), 9600);
    ui->rateBox->addItem(QLatin1String("19200"), 19200);
    ui->rateBox->addItem(QLatin1String("38400"), 38400);
    ui->rateBox->addItem(QLatin1String("115200"), 115200);
    ui->rateBox->addItem(QLatin1String("115200"), 115200);

    // fill data bits
    ui->dataBitsBox->addItem(QLatin1String("5"), static_cast<int>(SerialPort::Data5));
    ui->dataBitsBox->addItem(QLatin1String("6"), static_cast<int>(SerialPort::Data6));
    ui->dataBitsBox->addItem(QLatin1String("7"), static_cast<int>(SerialPort::Data7));
    ui->dataBitsBox->addItem(QLatin1String("8"), static_cast<int>(SerialPort::Data8));

    // fill parity
    ui->parityBox->addItem(QLatin1String("None"), static_cast<int>(SerialPort::NoParity));
    ui->parityBox->addItem(QLatin1String("Even"), static_cast<int>(SerialPort::EvenParity));
    ui->parityBox->addItem(QLatin1String("Odd"), static_cast<int>(SerialPort::OddParity));
    ui->parityBox->addItem(QLatin1String("Mark"), static_cast<int>(SerialPort::MarkParity));
    ui->parityBox->addItem(QLatin1String("Space"), static_cast<int>(SerialPort::SpaceParity));

    // fill stop bits
    ui->stopBitsBox->addItem(QLatin1String("1"), static_cast<int>(SerialPort::OneStop));
#if defined (Q_OS_WIN)
    ui->stopBitsBox->addItem(QLatin1String("1.5"), static_cast<int>(SerialPort::OneAndHalfStop));
#endif
    ui->stopBitsBox->addItem(QLatin1String("2"), static_cast<int>(SerialPort::TwoStop));

    // fill flow control
    ui->flowControlBox->addItem(QLatin1String("None"), static_cast<int>(SerialPort::NoFlowControl));
    ui->flowControlBox->addItem(QLatin1String("RTS/CTS"), static_cast<int>(SerialPort::HardwareControl));
    ui->flowControlBox->addItem(QLatin1String("XON/XOFF"), static_cast<int>(SerialPort::SoftwareControl));
}

void SettingsDialog::fillPortsInfo()
{
    ui->portsBox->clear();
    foreach (const SerialPortInfo &info, SerialPortInfo::availablePorts()) {
        QStringList list;
        list << info.portName() << info.description()
             << info.manufacturer() << info.systemLocation();

        ui->portsBox->addItem(list.at(0), list);
    }
}

void SettingsDialog::updateSettings()
{
    currentSettings.name = ui->portsBox->currentText();

    // Rate
    currentSettings.rate = static_cast<SerialPort::Rate>(
                ui->rateBox->itemData(ui->rateBox->currentIndex()).toInt());
    currentSettings.stringRate = QString::number(currentSettings.rate);

    // Data bits
    currentSettings.dataBits = static_cast<SerialPort::DataBits>(
                ui->dataBitsBox->itemData(ui->dataBitsBox->currentIndex()).toInt());
    currentSettings.stringDataBits = ui->dataBitsBox->currentText();

    // Parity
    currentSettings.parity = static_cast<SerialPort::Parity>(
                ui->parityBox->itemData(ui->parityBox->currentIndex()).toInt());
    currentSettings.stringParity = ui->parityBox->currentText();

    // Stop bits
    currentSettings.stopBits = static_cast<SerialPort::StopBits>(
                ui->stopBitsBox->itemData(ui->stopBitsBox->currentIndex()).toInt());
    currentSettings.stringStopBits = ui->stopBitsBox->currentText();

    // Flow control
    currentSettings.flowcontrol = static_cast<SerialPort::FlowControl>(
                ui->flowControlBox->itemData(ui->flowControlBox->currentIndex()).toInt());
    currentSettings.stringFlowControl = ui->flowControlBox->currentText();
}