Newer
Older
PhaseMeasure / SetConfig.cpp
tanyue on 30 Jun 2023 5 KB 20230630 适配串口
#include "SetConfig.h"
#include "ui_SetConfig.h"

#include <QFile>
#include <QFileDialog>
#include <QDesktopServices>
#include <QPushButton>
#include <QStandardItemModel>
#include <QDebug>

#include "AppConstant.h"

extern bool running;

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

    // 加载样式表
    QFile file(QApplication::applicationDirPath() + "/qss/dlgdark.css");
    if (file.open(QFile::ReadOnly)) {
        QString qss = QLatin1String(file.readAll());
        this->setStyleSheet(qss);
        file.close();
    }

    init();

    connect(ui->logInput, &DoubleClickInput::doubleClicked, this, &SetConfig::onLogPathInputDoubleClicked);
    connect(ui->logInput, &DoubleClickInput::textChanged, this, &SetConfig::onLogPathInputDoubleClicked);
}

void SetConfig::init()
{
    setAttribute(Qt::WA_DeleteOnClose); // 窗口关闭后释放本对象

    setWindowFlags(Qt::Window | Qt::FramelessWindowHint
                   | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint
                   | Qt::WindowMaximizeButtonHint);

    this->setProperty("dialog", true);

    // 修改名称
    ui->buttonBox->button(QDialogButtonBox::Ok)->setText("确定");
    ui->buttonBox->button(QDialogButtonBox::Cancel)->setText("取消");

    ui->comTypeSelect->addItem("串口", ENUM_NET_TYPE::SERIAL);
//    ui->comTypeSelect->addItem("UDP Server", ENUM_NET_TYPE::UDPServer);

    // 设置下拉框的样式
    QStandardItemModel * model = qobject_cast<QStandardItemModel*>(ui->comTypeSelect->model());
    for (int i = 0; i < model->rowCount(); ++i)
    {
        QStandardItem * item = model->item(i);
        item->setSizeHint({ 0, 40 });

        if (ui->comTypeSelect->itemData(i).toInt() == SettingConfig::getInstance().NET_TYPE) {
            ui->comTypeSelect->setCurrentIndex(i);
        }
    }

    ui->logInput->setText(SettingConfig::getInstance().BASE_LOG_PATH);

    ui->netIpInput->setText(SettingConfig::getInstance().DEVICE_HOST);
    ui->netDevPortInput->setText(QString::number(SettingConfig::getInstance().DEVICE_PORT));
    ui->netListenPortInput->setText(QString::number(SettingConfig::getInstance().LISTEN_PORT));
    ui->comNameInput->setText(SettingConfig::getInstance().PORT_NAME);
    ui->comBaudInput->setText(QString::number(SettingConfig::getInstance().BAUD_RATE));

    if (running == true)
    {
        ui->logInput->setReadOnly(true);
    } else {
        ui->logInput->setReadOnly(false);
    }

    showConfigItemByComType();
}

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

void SetConfig::on_buttonBox_accepted()
{
    SettingConfig::getInstance().setConProperty("dev", "type", ui->comTypeSelect->currentData().toString());
    SettingConfig::getInstance().setConProperty("log", "basePath", ui->logInput->text());

    int currentIndx = ui->comTypeSelect->currentIndex();
    switch (currentIndx) {
    case 0:
        SettingConfig::getInstance().setConProperty("com", "portName", ui->comNameInput->text());
        SettingConfig::getInstance().setConProperty("com", "baudRate", ui->comBaudInput->text());
        break;
    case 1:
        SettingConfig::getInstance().setConProperty("net", "listenPort", ui->netListenPortInput->text());
        break;
    }

    SettingConfig::getInstance().init();
}

void SetConfig::on_comTypeSelect_currentIndexChanged(int index)
{
    showConfigItemByComType();
}

void SetConfig::onLogPathInputDoubleClicked()
{
    disconnect(ui->logInput, &DoubleClickInput::textChanged, this, &SetConfig::onLogPathInputDoubleClicked);

    QString selectDir = QFileDialog::getExistingDirectory(this, "选择日志保存根目录", QDir::currentPath(), QFileDialog::ShowDirsOnly);
    if (selectDir.isEmpty() == false)
    {
        ui->logInput->setText(selectDir + "/");
    }

    connect(ui->logInput, &DoubleClickInput::textChanged, this, &SetConfig::onLogPathInputDoubleClicked);
}

void SetConfig::showConfigItemByComType()
{
    int currentIndx = ui->comTypeSelect->currentIndex();
    switch (currentIndx) {
    case 0:
        ui->labelComName->show();
        ui->labelComBaud->show();
        ui->labelNetIp->hide();
        ui->labelNetListenPort->hide();
        ui->labelNetDevPort->hide();

        ui->comNameInput->show();
        ui->comBaudInput->show();
        ui->netIpInput->hide();
        ui->netListenPortInput->hide();
        ui->netDevPortInput->hide();
        break;

    case 1:
        ui->labelComName->hide();
        ui->labelComBaud->hide();
        ui->labelNetIp->hide();
        ui->labelNetListenPort->show();
        ui->labelNetDevPort->hide();

        ui->comNameInput->hide();
        ui->comBaudInput->hide();
        ui->netIpInput->hide();
        ui->netListenPortInput->show();
        ui->netDevPortInput->hide();
        break;

    default:
        ui->labelComName->show();
        ui->labelComBaud->show();
        ui->labelNetIp->show();
        ui->labelNetListenPort->show();
        ui->labelNetDevPort->show();

        ui->comNameInput->show();
        ui->comBaudInput->show();
        ui->netIpInput->show();
        ui->netListenPortInput->show();
        ui->netDevPortInput->show();
        break;
    }
}