Newer
Older
PhaseMeasure / PhaseWindow.cpp
#include "PhaseWindow.h"
#include "ui_PhaseWindow.h"

#include <iostream>
#include <QTimer>
#include <QDateTime>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QDesktopWidget>
#include <QThread>
#include <SetConfig.h>
#include <QPixmap>
#include <QDir>
#include <QMessageBox>
#include "common/utils/SettingConfig.h"
#include "common/utils/QLogUtil.h"

QString currentFileName = "";

PhaseWindow::PhaseWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PhaseWindow)
{
    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->settingButt->move(screenRect.width() - 200, 10);
    ui->endButt->move(screenRect.width() - 260, 10);
    ui->startButt->move(screenRect.width() - 320, 10);
    ui->line->setGeometry(0, 89, screenRect.width(), 1);
    ui->pointsList->setGeometry(250,30,screenRect.width()-600,40);
    ui->labelsList->setGeometry(250,60,screenRect.width()-600,30);

    // 设置主体区域的大小和位置
    ui->scrollArea->setGeometry(0, 90, screenRect.width(), screenRect.height() - 90);
    ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);

    ui->tim3->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz"));

    this->generateWidgetForDevice();

    device = new PhaseDevice(this);
    connect(device, &PhaseDevice::sendDataToDraw,
            this, &PhaseWindow::drawPhaseDataOnPage);
}

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

void PhaseWindow::drawPhaseDataOnPage(PhaseDataDto * phaseData)
{
    ui->tim3->setText(phaseData->timestamp);
    updateChannelActive(phaseData->channelActive);

    // 更新所有通道BOX的值
    for (int i = 0; i < phaseData->channelActive.size(); i++)
    {
        // 获取对应的通道BOX
        if (phaseData->channelActive.at(i) == "1")
        {
            ChannelItem * channel = channelItemList.at(i);
            channel->updatePhaseMeasureData(phaseData);
        }
    }
}

void PhaseWindow::generateWidgetForDevice()
{
    // 获取设备数据
    int channelNum = SettingConfig::getInstance().CHANNEL_COUNT;

    QRect screenRect = QApplication::desktop()->screenGeometry();

    ui->scrollContents->setGeometry(0, 90, screenRect.width(), channelNum * 170);

    QVBoxLayout * layout = new QVBoxLayout(ui->scrollContents);
    const QFont labelFont("微软雅黑", 10);

    for (int i = 0; i < channelNum; i++)
    {
        // 通道状态图标默认显示为离线/无效状态
        QLabel* label = ui->pointsList->findChild<QLabel*>(QString("p%1").arg(i+1)); //根据子控件的名称查找子控件
        label->setPixmap(QPixmap(":/points/images/white.png"));

        QGroupBox * group = new QGroupBox(ui->scrollContents);
        group->setTitle(QString("通道 - %1").arg(i + 1));
        group->setFont(QFont("微软雅黑", 12));
        group->setGeometry(20, i * 160 + 10, screenRect.width() - 40, 160);
        this->channelGroupList.append(group);

        QHBoxLayout * vbox = new QHBoxLayout(group);
        ChannelItem * channelItem = new ChannelItem();
        channelItem->setGeometry(10, 0, screenRect.width() - 60, 160);
        channelItem->setIndex(i);
        channelItem->setChannelDelay(SettingConfig::getInstance().DELAY_ARR.at(i).toDouble());
        this->channelItemList.append(channelItem);
        vbox->addWidget(channelItem);
        group->setLayout(vbox);
        layout->addWidget(group);
    }
}

void PhaseWindow::updateChannelActive(QList<QString> channelActive)
{
    for (int i = 0; i < channelActive.size(); i++)
    {
        if (channelActive.at(i) == "1")
        {
            QLabel* label = ui->pointsList->findChild<QLabel*>(QString("p%1").arg(i+1)); //根据子控件的名称查找子控件
            label->setPixmap(QPixmap(":/points/images/green.png"));

            // 显示有效的通道
            this->channelGroupList.at(i)->show();
        } else
        {
            QLabel* label = ui->pointsList->findChild<QLabel*>(QString("p%1").arg(i+1)); //根据子控件的名称查找子控件
            label->setPixmap(QPixmap(":/points/images/white.png"));

            // 隐藏无效的通道
            this->channelGroupList.at(i)->hide();
        }
    }
}


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

void PhaseWindow::on_exitButt_clicked()
{
    // 发送结束指令
    device->stopWork();

    QTimer::singleShot(500, qApp, SLOT(quit()));
}

void PhaseWindow::on_startButt_clicked()
{
    //发送开始指令
    device->startWork();
    createFolder();
}

void PhaseWindow::createFolder()
{
    QDir *folder = new QDir;
    bool exist = folder->exists(SettingConfig::getInstance().BASE_LOG_PATH);
    if(!exist)  // 生成base文件夹
    {
        bool ok = folder->mkdir(SettingConfig::getInstance().BASE_LOG_PATH);
    }

    // 生成当前时间文件夹
    QString fileName = QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss");
    exist = folder->exists(SettingConfig::getInstance().BASE_LOG_PATH + fileName);
    if(!exist)
    {
        bool ok = folder->mkdir(SettingConfig::getInstance().BASE_LOG_PATH + fileName);
        if(ok) {
            currentFileName = fileName;
            QMessageBox::warning(this, tr("开始"), tr("开始成功"));
        }
    }
}

void PhaseWindow::on_endButt_clicked()
{
    currentFileName = "";
    // 清空已经存储的数据
    for (int i = 0; i < phaseVector.size(); i++)
    {
        phaseVector[i].clear();
    }

    // 发送结束指令
    device->stopWork();
}

void PhaseWindow::on_settingButt_clicked()
{
    SetConfig *new_win = new SetConfig();
    new_win->setWindowTitle("配置");
    new_win->show();
}