Newer
Older
ZXSSCJ / DeviceHub / DeviceHubWindow.cpp
tan yue on 12 Nov 2021 4 KB 20211112 refact device hub
#include "DeviceHubWindow.h"
#include "ui_DeviceHubWindow.h"

#include <QTimer>
#include <QMessageBox>
#include <QDesktopWidget>
#include <QStandardItemModel>

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

    // 无边框
    this->setWindowFlags(Qt::FramelessWindowHint);

    // 窗口大小为占满一屏
    QRect screenRect = QApplication::desktop()->screenGeometry();
    resize(screenRect.width(), screenRect.height());

    // 将窗口移动到左上角
    move(0, 0);
    ui->exitButt->move(screenRect.width() - 80, 10);
    ui->minButt->move(screenRect.width() - 140, 10);
    ui->line->setGeometry(0, 59, screenRect.width(), 1);

    // 设置stackWidget的大小
    ui->stackedWidget->setGeometry(0, 60, screenRect.width(), screenRect.height() - 60);

    // add forms
    freqTunForm = new FrequencyTuningForm(this);
    ui->stackedWidget->addWidget(freqTunForm);

    signGenForm = new SignalGeneratorForm(this);
    ui->stackedWidget->addWidget(signGenForm);


    httpReq = new HttpRequestController(this);
    // 1. 获取访问接口需要的token
    int retCode = this->initHttpToken();
    if (retCode != 200)
    {
        QMessageBox::information(this, "错误", "获取http请求的token失败,程序即将退出");

        QTimer::singleShot(1000, qApp, SLOT(quit()));
    }
    // 2. 获取字典值:设备类型
    retCode = this->initDictDeviceTypes();
    this->initAllDeviceList();
}

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


void DeviceHubWindow::on_minButt_clicked()
{
    setWindowState(Qt::WindowMinimized | windowState());
}

void DeviceHubWindow::on_exitButt_clicked()
{
    QApplication::exit(0);
}

void DeviceHubWindow::on_devTypeSelect_currentIndexChanged(int index)
{
    ui->stackedWidget->setCurrentIndex(index);

    //
    QList<QJsonObject> typeDevList = ConstCache::getInstance().deviceList.value(ui->devTypeSelect->currentData().toString());
    ui->devSelect->clear();
    for (int var = 0; var < typeDevList.size(); ++var) {
        QJsonObject devItem = typeDevList.at(var);
        ui->devSelect->addItem(devItem.find("deviceName")->toString(), devItem);
    }

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

int DeviceHubWindow::initHttpToken()
{
    QJsonObject response = httpReq->getTokenByClientId(SettingConfig::getInstance().CLIENT_ID,
                                                       SettingConfig::getInstance().APP_KEY);
    return response.find("code")->toInt();
}

int DeviceHubWindow::initDictDeviceTypes()
{
    QJsonObject response = httpReq->initDictDeviceType();
    QJsonArray typeArray = response.find("data")->toArray();
    for (int i = 3; i < typeArray.size(); i++)
    {
        QJsonObject typeItem = typeArray.at(i).toObject();
        ui->devTypeSelect->addItem(typeItem.find("name")->toString(), typeItem.find("value")->toString());

        ConstCache::getInstance().deviceTypes.insert(typeItem.find("value")->toString(), typeItem.find("name")->toString());
    }

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

    //
    ui->devTypeSelect->setCurrentIndex(model->rowCount() - 1);

    return response.find("code")->toInt();
}

void DeviceHubWindow::initAllDeviceList()
{
    QMapIterator<QString, QString> it(ConstCache::getInstance().deviceTypes);
    while (it.hasNext())
    {
        it.next();
        QString devType = it.key();
        if (devType == "00" || devType == "01" || devType == "02")
        {
            continue;
        }
        httpReq->initDeviceList(devType);
    }

    ui->devTypeSelect->setCurrentIndex(0);
}