<commit_before>/*===================================================================
APM_PLANNER Open Source Ground Control Station

(c) 2013 APM_PLANNER PROJECT <http://www.diydrones.com>

This file is part of the APM_PLANNER project

    APM_PLANNER 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 of the License, or
    (at your option) any later version.

    APM_PLANNER 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 APM_PLANNER. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#include "QsLog.h"
#include "MainWindow.h"
#include "SerialSettingsDialog.h"
#include "Radio3DRConfig.h"
#include "Radio3DRSettings.h"

#include "configuration.h"

#include <QSettings>
#include <QtSerialPort/qserialportinfo.h>
#include <QtSerialPort/qserialport.h>
#include <QTimer>
#include <QMessageBox>

Radio3DRConfig::Radio3DRConfig(QWidget *parent) : QWidget(parent),
    m_radioSettings(NULL),
    m_state(none)
{
    ui.setupUi(this);
    m_settingsDialog = new SettingsDialog;

    ui.settingsButton->setEnabled(true);

    addBaudComboBoxConfig(ui.baudPortComboBox);
    fillPortsInfo(*ui.linkPortComboBox);

    addRadioBaudComboBoxConfig(*ui.baudComboBox);
    addRadioBaudComboBoxConfig(*ui.baudComboBox_remote);
    addRadioAirBaudComboBoxConfig(*ui.airBaudComboBox);
    addRadioAirBaudComboBoxConfig(*ui.airBaudComboBox_remote);
    addTxPowerComboBoxConfig(*ui.txPowerComboBox);
    addTxPowerComboBoxConfig(*ui.txPowerComboBox_remote);
    addRtsCtsComboBoxConfig(*ui.rtsCtsComboBox);
    addRtsCtsComboBoxConfig(*ui.rtsCtsComboBox_remote);

    addMavLinkComboBoxConfig(*ui.mavLinkComboBox_remote);

    loadSavedSerialSettings();

    initConnections();

    //Keep refreshing the serial port list
    m_timer = new QTimer(this);
    connect(m_timer,SIGNAL(timeout()),this,SLOT(populateSerialPorts()));
}

Radio3DRConfig::~Radio3DRConfig()
{
}

void Radio3DRConfig::addBaudComboBoxConfig(QComboBox *comboBox)
{
    comboBox->addItem(QLatin1String("115200"), QSerialPort::Baud115200);
    comboBox->addItem(QLatin1String("57600"), QSerialPort::Baud57600);
    comboBox->addItem(QLatin1String("38400"), QSerialPort::Baud38400);
    comboBox->addItem(QLatin1String("19200"), QSerialPort::Baud19200);
    comboBox->addItem(QLatin1String("9600"), QSerialPort::Baud9600);
    comboBox->addItem(QLatin1String("4800"), QSerialPort::Baud4800);
    comboBox->addItem(QLatin1String("2400"), QSerialPort::Baud2400);
    comboBox->addItem(QLatin1String("1200"), QSerialPort::Baud1200);

    // Set combobox to default baud rate of 57600.
    comboBox->setCurrentIndex(2);
}

void Radio3DRConfig::fillPortsInfo(QComboBox &comboxBox)
{
    QLOG_TRACE() << "3DR Radio fillPortsInfo ";
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QStringList list;
        list << info.portName()
             << info.description()
             << info.manufacturer()
             << info.systemLocation()
             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());

        int found = comboxBox.findData(list);
        if ((found == -1)&& (info.manufacturer().contains("FTDI") || info.manufacturer().contains("Silicon Labs"))) {
            QLOG_INFO() << "Inserting " << list.first();
            comboxBox.insertItem(0,list[0], list);
        } else {
            // Do nothing as the port is already listed
        }
    }}

void Radio3DRConfig::loadSavedSerialSettings()
{
    // Load defaults from settings
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
    settings.sync();
    if (settings.contains("3DRRADIO_COMM_PORT"))
    {
        m_settings.name = settings.value("3DRRADIO_COMM_PORT").toString();
        m_settings.baudRate = settings.value("3DRRADIO_COMM_BAUD").toInt();
        m_settings.parity = static_cast<QSerialPort::Parity>
                (settings.value("3DRRADIO_COMM_PARITY").toInt());
        m_settings.stopBits = static_cast<QSerialPort::StopBits>
                (settings.value("3DRRADIO_COMM_STOPBITS").toInt());
        m_settings.dataBits = static_cast<QSerialPort::DataBits>
                (settings.value("3DRRADIO_COMM_DATABITS").toInt());
        m_settings.flowControl = static_cast<QSerialPort::FlowControl>
                (settings.value("3DRRADIO_COMM_FLOW_CONTROL").toInt());

        ui.linkPortComboBox->setCurrentIndex(ui.linkPortComboBox->findText(m_settings.name));
        ui.baudPortComboBox->setCurrentIndex(ui.baudPortComboBox->findData(m_settings.baudRate));
    } else {
        // init the structure
    }
}

void Radio3DRConfig::saveSerialSettings()
{
    // Store settings
    QSettings settings(QGC::COMPANYNAME, QGC::APPNAME);
    settings.setValue("3DRRADIO_COMM_PORT", m_settings.name);
    settings.setValue("3DRRADIO_COMM_BAUD", m_settings.baudRate);
    settings.setValue("3DRRADIO_COMM_PARITY", m_settings.parity);
    settings.setValue("3DRRADIO_COMM_STOPBITS", m_settings.stopBits);
    settings.setValue("3DRRADIO_COMM_DATABITS", m_settings.dataBits);
    settings.setValue("3DRRADIO_COMM_FLOW_CONTROL", m_settings.flowControl);
    settings.sync();
}

void Radio3DRConfig::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);
    // Start refresh Timer
    QLOG_DEBUG() << "3DR Radio Start Serial Port Scanning";
    m_timer->start(2000);
    loadSavedSerialSettings();

    MainWindow::instance()->toolBar().disableConnectWidget(true);
}

void Radio3DRConfig::hideEvent(QHideEvent *event)
{
    Q_UNUSED(event);
    // Stop the port list refeshing
    QLOG_DEBUG() << "3DR Radio Stop Serial Port Scanning";
    m_timer->stop();
    saveSerialSettings();
    QLOG_DEBUG() << "3DR Radio Remove Conenction to Serial Port";
    delete m_radioSettings;
    MainWindow::instance()->toolBar().disableConnectWidget(false);
}

void Radio3DRConfig::populateSerialPorts()
{
    QLOG_TRACE() << "populateSerialPorts";
    fillPortsInfo(*ui.linkPortComboBox);
}<commit_msg>Radio Config: Fix settings storage and initial values<commit_after>/*===================================================================
APM_PLANNER Open Source Ground Control Station

(c) 2013 APM_PLANNER PROJECT <http://www.diydrones.com>

This file is part of the APM_PLANNER project

    APM_PLANNER 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 of the License, or
    (at your option) any later version.

    APM_PLANNER 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 APM_PLANNER. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

#include "QsLog.h"
#include "MainWindow.h"
#include "SerialSettingsDialog.h"
#include "Radio3DRConfig.h"
#include "Radio3DRSettings.h"

#include "configuration.h"

#include <QSettings>
#include <QtSerialPort/qserialportinfo.h>
#include <QtSerialPort/qserialport.h>
#include <QTimer>
#include <QMessageBox>

Radio3DRConfig::Radio3DRConfig(QWidget *parent) : QWidget(parent),
    m_radioSettings(NULL),
    m_state(none)
{
    ui.setupUi(this);
    m_settingsDialog = new SettingsDialog;

    ui.settingsButton->setEnabled(true);

    addBaudComboBoxConfig(ui.baudPortComboBox);
    fillPortsInfo(*ui.linkPortComboBox);
    m_settings.name = ui.linkPortComboBox->currentText();

    addRadioBaudComboBoxConfig(*ui.baudComboBox);
    addRadioBaudComboBoxConfig(*ui.baudComboBox_remote);
    addRadioAirBaudComboBoxConfig(*ui.airBaudComboBox);
    addRadioAirBaudComboBoxConfig(*ui.airBaudComboBox_remote);
    addTxPowerComboBoxConfig(*ui.txPowerComboBox);
    addTxPowerComboBoxConfig(*ui.txPowerComboBox_remote);
    addRtsCtsComboBoxConfig(*ui.rtsCtsComboBox);
    addRtsCtsComboBoxConfig(*ui.rtsCtsComboBox_remote);

    addMavLinkComboBoxConfig(*ui.mavLinkComboBox_remote);

    loadSavedSerialSettings();

    initConnections();

    //Keep refreshing the serial port list
    m_timer = new QTimer(this);
    connect(m_timer,SIGNAL(timeout()),this,SLOT(populateSerialPorts()));
}

Radio3DRConfig::~Radio3DRConfig()
{
}

void Radio3DRConfig::addBaudComboBoxConfig(QComboBox *comboBox)
{
    comboBox->addItem(QLatin1String("115200"), QSerialPort::Baud115200);
    comboBox->addItem(QLatin1String("57600"), QSerialPort::Baud57600);
    comboBox->addItem(QLatin1String("38400"), QSerialPort::Baud38400);
    comboBox->addItem(QLatin1String("19200"), QSerialPort::Baud19200);
    comboBox->addItem(QLatin1String("9600"), QSerialPort::Baud9600);
    comboBox->addItem(QLatin1String("4800"), QSerialPort::Baud4800);
    comboBox->addItem(QLatin1String("2400"), QSerialPort::Baud2400);
    comboBox->addItem(QLatin1String("1200"), QSerialPort::Baud1200);

    // Set combobox to default baud rate of 57600.
    comboBox->setCurrentIndex(2);
}

void Radio3DRConfig::fillPortsInfo(QComboBox &comboxBox)
{
    QLOG_TRACE() << "3DR Radio fillPortsInfo ";
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        QStringList list;
        list << info.portName()
             << info.description()
             << info.manufacturer()
             << info.systemLocation()
             << (info.vendorIdentifier() ? QString::number(info.vendorIdentifier(), 16) : QString())
             << (info.productIdentifier() ? QString::number(info.productIdentifier(), 16) : QString());

        int found = comboxBox.findData(list);
        if ((found == -1)&& (info.manufacturer().contains("FTDI") || info.manufacturer().contains("Silicon Labs"))) {
            QLOG_INFO() << "Inserting " << list.first();
            comboxBox.insertItem(0,list[0], list);
        } else {
            // Do nothing as the port is already listed
        }
    }}

void Radio3DRConfig::loadSavedSerialSettings()
{
    // Load defaults from settings
    QSettings settings;
    settings.sync();
    settings.beginGroup("3DRRADIO");
    if (settings.contains("COMM_PORT"))
    {
        m_settings.name = settings.value("COMM_PORT").toString();
        m_settings.baudRate = settings.value("COMM_BAUD").toInt();
        m_settings.parity = static_cast<QSerialPort::Parity>
                (settings.value("COMM_PARITY").toInt());
        m_settings.stopBits = static_cast<QSerialPort::StopBits>
                (settings.value("COMM_STOPBITS").toInt());
        m_settings.dataBits = static_cast<QSerialPort::DataBits>
                (settings.value("COMM_DATABITS").toInt());
        m_settings.flowControl = static_cast<QSerialPort::FlowControl>
                (settings.value("COMM_FLOW_CONTROL").toInt());

        ui.linkPortComboBox->setCurrentIndex(ui.linkPortComboBox->findText(m_settings.name));
        ui.baudPortComboBox->setCurrentIndex(ui.baudPortComboBox->findData(m_settings.baudRate));
    } else {
        ui.baudPortComboBox->setCurrentIndex(ui.baudPortComboBox->findData(QSerialPort::Baud57600));
    }
}

void Radio3DRConfig::saveSerialSettings()
{
    // Store settings
    QSettings settings;
    settings.beginGroup("3DRRADIO");
    settings.setValue("COMM_PORT", m_settings.name);
    settings.setValue("COMM_BAUD", m_settings.baudRate);
    settings.setValue("COMM_PARITY", m_settings.parity);
    settings.setValue("COMM_STOPBITS", m_settings.stopBits);
    settings.setValue("COMM_DATABITS", m_settings.dataBits);
    settings.setValue("COMM_FLOW_CONTROL", m_settings.flowControl);
    settings.endGroup();
    settings.sync();
}

void Radio3DRConfig::showEvent(QShowEvent *event)
{
    Q_UNUSED(event);
    // Start refresh Timer
    QLOG_DEBUG() << "3DR Radio Start Serial Port Scanning";
    m_timer->start(2000);
    loadSavedSerialSettings();

    MainWindow::instance()->toolBar().disableConnectWidget(true);
}

void Radio3DRConfig::hideEvent(QHideEvent *event)
{
    Q_UNUSED(event);
    // Stop the port list refeshing
    QLOG_DEBUG() << "3DR Radio Stop Serial Port Scanning";
    m_timer->stop();
    saveSerialSettings();
    QLOG_DEBUG() << "3DR Radio Remove Conenction to Serial Port";
    delete m_radioSettings;
    MainWindow::instance()->toolBar().disableConnectWidget(false);
}

void Radio3DRConfig::populateSerialPorts()
{
    QLOG_TRACE() << "populateSerialPorts";
    fillPortsInfo(*ui.linkPortComboBox);
}
{'fraction_non_alphanumeric': 0.1409617301940333, 'fraction_numerical': 0.020093565457473733, 'mean_word_length': 4.48129466162253, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5614351', 'n_tokens_mistral': 4197, 'n_tokens_neox': 3991, 'n_words': 718}