#include "PhaseWindow.h" #include "ui_PhaseWindow.h" #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 <QDebug> #include "IconHelper.h" #include "common/utils/SettingConfig.h" #include "common/utils/QLogUtil.h" QString currentFileName = ""; boolean running = false; PhaseWindow::PhaseWindow(QWidget *parent) : QWidget(parent), ui(new Ui::PhaseWindow) { ui->setupUi(this); device = new PhaseDevice(this); this->initForm(); this->generateWidgetForDevice(); } PhaseWindow::~PhaseWindow() { delete ui; } void PhaseWindow::mouseMoveEvent(QMouseEvent *event) { QWidget::mouseMoveEvent(event); QPoint y = event->globalPos();//鼠标相对于桌面左上角的位置,鼠标全局位置 QPoint x = y - this->last; this->move(x); } void PhaseWindow::mousePressEvent(QMouseEvent *event) { QWidget::mousePressEvent(event); QPoint y = event->globalPos();//鼠标相对于桌面左上角的位置,鼠标全局位置 QPoint x = this->geometry().topLeft();//窗口左上角相对于桌面左上角的位置,窗口位置 this->last = y - x;//定值,不变的 } void PhaseWindow::mouseReleaseEvent(QMouseEvent *event) { QWidget::mouseReleaseEvent(event); this->last = QPoint();//鼠标松开的时候,将z值初始化 } 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); QRect screenRect = QApplication::desktop()->availableGeometry(); qint16 screenWidth = screenRect.width(); qint16 screenHeight = screenRect.height(); // 设置窗口位置和大小 move(screenWidth / 2 - 688, 50); // resize(screenWidth, screenHeight); resize(1366, screenHeight - 100); // 设置最小和关闭按钮的样式 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")); 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); 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() { 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); QString valueStr = QString::number(phaseData->channelData.at(i) * 1E12, 'f', 3); ((QGroupBox *) ui->scrollContents->children().at(i + 1))->setTitle(QString("通道 - %1 测量结果:%2 ps").arg(i + 1).arg(valueStr)); } } } void PhaseWindow::generateWidgetForDevice() { // 获取设备数据 int channelNum = SettingConfig::getInstance().CHANNEL_COUNT; QRect screenRect = QApplication::desktop()->screenGeometry(); qDeleteAll(ui->scrollContents->children()); channelGroupList.clear(); channelItemList.clear(); 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); this->channelItemList.append(channelItem); vbox->addWidget(channelItem); group->setLayout(vbox); layout->addWidget(group); } } bool PhaseWindow::createFolder() { QDir *folder = new QDir; bool exist = folder->exists(SettingConfig::getInstance().BASE_LOG_PATH); bool ok = false; if(!exist) // 生成base文件夹 { ok = folder->mkdir(SettingConfig::getInstance().BASE_LOG_PATH); if (ok == false) { return ok; } } // 生成当前时间文件夹 QString fileName = QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"); exist = folder->exists(SettingConfig::getInstance().BASE_LOG_PATH + fileName); if(!exist) { ok = folder->mkdir(SettingConfig::getInstance().BASE_LOG_PATH + fileName); if(ok) { currentFileName = fileName; } } return ok; } 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() { if (running == true) { // 发送结束指令 device->stopWork(); } QTimer::singleShot(500, qApp, SLOT(quit())); } void PhaseWindow::buttonClick() { QToolButton * b = (QToolButton *)sender(); QString name = b->text(); // 如果是开始或者结束测量按钮被点击则切换显示按钮 if (name.contains("开始") || name.contains("停止") || name.contains("测量")) { swiftConntrollerButt(); } } void PhaseWindow::on_startButt_clicked() { this->initDeivce(); // 发送开始指令 bool succ = device->startWork(); if (succ == false) { QMessageBox::warning(this, "开始失败", "开始测量失败,请检查与设备的连接"); return; } else { QMessageBox::information(this, "开始成功", "开始测量成功"); running = true; } succ = createFolder(); if (succ == false) { QMessageBox::warning(this, "记录日志失败", "创建日志文件错误,请检查日志路径配置"); } } void PhaseWindow::on_endButt_clicked() { currentFileName = ""; running = false; // 发送结束指令 device->stopWork(); disconnect(device, &PhaseDevice::sendDataToDraw, this, &PhaseWindow::drawPhaseDataOnPage); this->generateWidgetForDevice(); // 清空已经存储的数据 for (int i = 0; i < phaseVector.size(); i++) { phaseVector[i].clear(); for (int j = 0; j < channelAllan[i].size(); j++) { channelAllan[i][j] = 0; channelAllenSigma[i][j] = 0; } } } void PhaseWindow::on_settingButt_clicked() { SetConfig * settingDlg = new SetConfig(); settingDlg->setModal(true); settingDlg->show(); }