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 "IconHelper.h"
#include "common/utils/SettingConfig.h"
#include "common/utils/QLogUtil.h"

QString currentFileName = "";
qint16 screenWidth = 0;
qint16 screenHeight = 0;
boolean running = false;

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

    QRect screenRect = QApplication::desktop()->availableGeometry();
    screenWidth = screenRect.width();
    screenHeight = screenRect.height();

    this->initForm();
    this->initDeivce();
    this->generateWidgetForDevice();
}

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

void PhaseWindow::initForm()
{
    this->setProperty("form", true);
    this->setProperty("canMove", false);
    ui->widgetTitle->setProperty("form", "title");
    ui->widgetTop->setProperty("nav", "top");
    this->setWindowFlags(Qt::FramelessWindowHint);

    // 设置窗口位置和大小
    move(0, 0);
    resize(screenWidth, screenHeight);

    // 设置最小和关闭按钮的样式
    IconHelper::Instance()->setIcon(ui->btnMenuMin, QChar(0xf068));
    IconHelper::Instance()->setIcon(ui->btnMenuClose, QChar(0xf00d));

    // 设置标题字体和文字
    QFont titleFont("Microsoft Yahei");
    titleFont.setBold(true);
    titleFont.setPixelSize(24);
    ui->labTitle->setText("比相仪测量软件");
    ui->labTitle->setFont(titleFont);
    this->setWindowTitle(ui->labTitle->text());

    // 设置时间字体和文字
    QFont tmFont("Arial");
    tmFont.setPixelSize(16);
    tmFont.setBold(true);
    ui->labTm->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
    ui->labTm->setFont(tmFont);

    // 设置标题logo
    QPixmap img(":/images/phaseMeasure.png");
    QPixmap logoImg = img.scaled(42, 42);
    ui->labIco->setPixmap(logoImg);

    // 设置通道文字的字体
    QFont chLabelFont("Arial");
    chLabelFont.setPixelSize(14);
    chLabelFont.setBold(true);
    QList<QLabel *> chLabs = ui->labelsList->findChildren<QLabel *>();
    foreach (QLabel * chLabel, chLabs) {
        chLabel->setFont(chLabelFont);
    }

    //设置顶部导航按钮
    QSize icoSize(32, 32);
    int icoWidth = 85;
    QList<QToolButton *> tbtns = ui->widgetTop->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        connect(btn, SIGNAL(clicked()), this, SLOT(buttonClick()));
    }
    ui->endButt->setVisible(running);

    // 设置主体区域的大小和位置
    ui->scrollArea->setGeometry(0, 90, screenWidth, screenHeight - 70);
    ui->scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}

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

void PhaseWindow::drawPhaseDataOnPage(PhaseDataDto * phaseData)
{
    ui->labTm->setText(phaseData->timestamp.left(19));
    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/green.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::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::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::swiftConntrollerButt()
{
    ui->startButt->setVisible(!running);
    ui->endButt->setVisible(running);
}

void PhaseWindow::on_btnMenuMin_clicked()
{
    setWindowState(Qt::WindowMinimized | windowState());
}
void PhaseWindow::on_btnMenuClose_clicked()
{
    // 发送结束指令
    device->stopWork();

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

void PhaseWindow::buttonClick()
{
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();

    QList<QToolButton *> tbtns = ui->widgetTop->findChildren<QToolButton *>();
    foreach (QToolButton *btn, tbtns) {
        if (btn == b) {
            btn->setChecked(true);
        } else {
            btn->setChecked(false);
        }
    }

    // 如果是开始或者结束测量按钮被点击则切换显示按钮
    if (name.contains("开始") || name.contains("停止") || name.contains("测量")) {
        swiftConntrollerButt();
    }
}

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

void PhaseWindow::on_endButt_clicked()
{
    currentFileName = "";

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

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

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