diff --git a/component/ZDateEdit/qss/zDateTimeEdit.qss b/component/ZDateEdit/qss/zDateTimeEdit.qss index d733656..74b7e5d 100644 --- a/component/ZDateEdit/qss/zDateTimeEdit.qss +++ b/component/ZDateEdit/qss/zDateTimeEdit.qss @@ -9,7 +9,7 @@ QLable#label_splite{font: bold 14px;} QWidget#widgetTitle{background-color:#061f65;} QWidget#baseWidget{background-color:#EE013B6B;border-radius: 11px;} -QComboBox{background-color: transparent; selection-background-color: transparent; color:#ffffff; font-size: 14px; min-height: 20px; selection-text-align: right;border:0px solid} +QComboBox{background-color: transparent; selection-background-color: transparent; color:#ffffff; font-size: 14px; min-height: 20px; border:0px solid} QComboBox::down-arrow{ image:url(:/component/ZDateEdit/Resources/ComboBoxArrow.png); } QComboBox::down-arrow:on{ top: 1px; left: 1px; } QComboBox::drop-down{ width: 26px; border:none; } QComboBox QAbstractItemView {border: none; outline:0px;} QComboBox QAbstractItemView::item{ height: 26px; min-height:26px;} diff --git a/mainwindow.cpp b/mainwindow.cpp index bb350db..763d32a 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -7,122 +7,175 @@ #include #include -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow) { - ui->setupUi(this); - this->setWindowFlag(Qt::FramelessWindowHint); - this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | - Qt::WindowMinimizeButtonHint); +MainWindow::MainWindow(QWidget* parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + this->setWindowFlag(Qt::FramelessWindowHint); + this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint); - calendarLoopPlay(); + ui->widget_head->installEventFilter(this); - setBeltWarnInfoTextEditStyle(); + m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); - addcoalDistributChart(); + calendarLoopPlay(); - addbeltSpeedChat(); + setSystemTitle(); - addCoalStatisticsChart(); + setBeltWarnInfoTextEditStyle(); - addCoalBeltVideo(); + addcoalDistributChart(); + + addbeltSpeedChat(); + + addCoalStatisticsChart(); + + addCoalBeltVideo(); } MainWindow::~MainWindow() { delete ui; } -void MainWindow::addBeltWarnInfoToTextEdit(const QString &consoleInfo) { - m_textEditConsoleInfo.append(consoleInfo); - QDateTime startDateTime = QDateTime::currentDateTime(); - QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss"); +bool MainWindow::eventFilter(QObject* watched, QEvent* event) +{ + // 处理鼠标按下、释放和移动事件 + if (watched == ui->widget_head) { + QMouseEvent* mouseEvent = static_cast(event); + if (mouseEvent->type() == QEvent::MouseButtonPress) { + if (mouseEvent->button() == Qt::LeftButton) { + m_mousePressed = true; // 启动拖拽 + m_mousePoint = mouseEvent->globalPos() - this->pos(); - if (m_textEditConsoleInfo.isEmpty()) - return; - QString lastConsoleInfo = m_textEditConsoleInfo.last(); + this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint); + this->show(); // 确保窗口显示 - qint64 timeDifferenceInMilliseconds = - m_previousConsleInfoDateTime.secsTo(startDateTime); + return true; + } + } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { + m_mousePressed = false; + return true; + } else if (mouseEvent->type() == QEvent::MouseMove) { + if (m_mousePressed && (mouseEvent->buttons() & Qt::LeftButton)) { + this->move(mouseEvent->globalPos() - m_mousePoint); - if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2) - return; + // 每次移动时设置为最上方 + this->setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint | Qt::Window | Qt::WindowStaysOnTopHint); + this->show(); + return true; + } + } + } +} - QString consoleInfoResult = - QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo); - ui->textEdit_beltWarnInfo->append(consoleInfoResult); +void MainWindow::addBeltWarnInfoToTextEdit(const QString& consoleInfo) +{ + m_textEditConsoleInfo.append(consoleInfo); + QDateTime startDateTime = QDateTime::currentDateTime(); + QString formattedTime = startDateTime.toString("yyyy-MM-dd hh:mm:ss"); - m_previousConsleInfoDateTime = QDateTime::currentDateTime(); + if (m_textEditConsoleInfo.isEmpty()) + return; + QString lastConsoleInfo = m_textEditConsoleInfo.last(); + + qint64 timeDifferenceInMilliseconds = m_previousConsleInfoDateTime.secsTo(startDateTime); + + if ((lastConsoleInfo == consoleInfo) && timeDifferenceInMilliseconds < 2) + return; + + QString consoleInfoResult = QStringLiteral("[%1]%2").arg(formattedTime).arg(consoleInfo); + ui->textEdit_beltWarnInfo->append(consoleInfoResult); + + m_previousConsleInfoDateTime = QDateTime::currentDateTime(); } void MainWindow::on_pushButton_close_clicked() { this->close(); } -void MainWindow::on_pushButton_max_clicked() { - if (isFullScreen()) { - this->showMaximized(); - } else { - this->showFullScreen(); - } +void MainWindow::on_pushButton_max_clicked() +{ + if (isFullScreen()) { + this->showMaximized(); + } else { + this->showFullScreen(); + } } void MainWindow::on_pushButton_min_clicked() { this->showMinimized(); } -void MainWindow::calendarLoopPlay() { - ui->label_calendar->clear(); - QTimer *timer_calendar = new QTimer(this); - connect(timer_calendar, &QTimer::timeout, this, [this]() { - QDateTime time = QDateTime::currentDateTime(); - QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); - ui->label_calendar->setText(str); - }); - timer_calendar->start(1000); //每一秒溢出一次进入槽函数 +void MainWindow::calendarLoopPlay() +{ + ui->label_calendar->clear(); + QTimer* timer_calendar = new QTimer(this); + connect(timer_calendar, &QTimer::timeout, this, [this]() { + QDateTime time = QDateTime::currentDateTime(); + QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd"); + ui->label_calendar->setText(str); + }); + timer_calendar->start(1000); //每一秒溢出一次进入槽函数 } -void MainWindow::setBeltWarnInfoTextEditStyle() { - ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100); - ui->textEdit_beltWarnInfo->setReadOnly(true); - ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction); +void MainWindow::setSystemTitle() +{ + QString titleName = m_systemSetting->value("system/name").toString(); + if (!titleName.isEmpty()) { + ui->label_title->clear(); + ui->label_title->setText(titleName); + } } -void MainWindow::addcoalDistributChart() { - // addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!"); - ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!"); - - CoalDistributBarChartWidget *coalDistributBarChartWidget = - new CoalDistributBarChartWidget(this); - - // 检查布局是否已设置 - if (!ui->widget_coalDistributChart->layout()) { - ui->widget_coalDistributChart->setLayout(new QVBoxLayout); - } - coalDistributBarChartWidget->setContentsMargins(0, 0, 0, 0); - ui->widget_coalDistributChart->layout()->addWidget( - coalDistributBarChartWidget); +void MainWindow::setBeltWarnInfoTextEditStyle() +{ + ui->textEdit_beltWarnInfo->document()->setMaximumBlockCount(100); + ui->textEdit_beltWarnInfo->setReadOnly(true); + ui->textEdit_beltWarnInfo->setTextInteractionFlags(Qt::NoTextInteraction); } -void MainWindow::addbeltSpeedChat() { - ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!"); - BeltSpeedLineWidget *beltSpeedLineWidget = new BeltSpeedLineWidget(this); - // 检查布局是否已设置 - if (!ui->widget_beltSpeedChart->layout()) { - ui->widget_beltSpeedChart->setLayout(new QVBoxLayout); - } - beltSpeedLineWidget->setContentsMargins(0, 0, 0, 0); - ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget); +void MainWindow::addcoalDistributChart() +{ + // addBeltWarnInfoToTextEdit("添加煤流分布窗口成功!"); + ui->textEdit_beltWarnInfo->append("添加煤流分布窗口成功!"); + + CoalDistributBarChartWidget* coalDistributBarChartWidget = new CoalDistributBarChartWidget(this); + + // 检查布局是否已设置 + if (!ui->widget_coalDistributChart->layout()) { + ui->widget_coalDistributChart->setLayout(new QVBoxLayout); + } + coalDistributBarChartWidget->setContentsMargins(0, 0, 0, 0); + ui->widget_coalDistributChart->layout()->addWidget( + coalDistributBarChartWidget); } -void MainWindow::addCoalStatisticsChart() { - ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!"); - CoalStatisticsChartWidget *coalStatisticsChartWidget = - new CoalStatisticsChartWidget(this); - // 检查布局是否已设置 - if (!ui->widget_beltSpeedChart->layout()) { - ui->widget_beltSpeedChart->setLayout(new QVBoxLayout); - } - coalStatisticsChartWidget->setContentsMargins(0, 0, 0, 0); - ui->widget_coalStatisticsChart->layout()->addWidget( - coalStatisticsChartWidget); +void MainWindow::addbeltSpeedChat() +{ + ui->textEdit_beltWarnInfo->append("添加皮带机速度曲线窗口成功!"); + BeltSpeedLineWidget* beltSpeedLineWidget = new BeltSpeedLineWidget(this); + // 检查布局是否已设置 + if (!ui->widget_beltSpeedChart->layout()) { + ui->widget_beltSpeedChart->setLayout(new QVBoxLayout); + } + beltSpeedLineWidget->setContentsMargins(0, 0, 0, 0); + ui->widget_beltSpeedChart->layout()->addWidget(beltSpeedLineWidget); } -void MainWindow::addCoalBeltVideo() { - QString url = "https://stream7.iqilu.com/10339/upload_transcode/202002/18/" - "20200218114723HDu3hhxqIT.mp4"; - ui->widget_beltVideo->setUrl(url); - ui->widget_beltVideo->open(); +void MainWindow::addCoalStatisticsChart() +{ + ui->textEdit_beltWarnInfo->append("添加煤量统计窗口成功!"); + CoalStatisticsChartWidget* coalStatisticsChartWidget = new CoalStatisticsChartWidget(this); + // 检查布局是否已设置 + if (!ui->widget_beltSpeedChart->layout()) { + ui->widget_beltSpeedChart->setLayout(new QVBoxLayout); + } + coalStatisticsChartWidget->setContentsMargins(0, 0, 0, 0); + ui->widget_coalStatisticsChart->layout()->addWidget( + coalStatisticsChartWidget); +} + +void MainWindow::addCoalBeltVideo() +{ + // QString url = "https://stream7.iqilu.com/10339/upload_transcode/202002/18/" + // "20200218114723HDu3hhxqIT.mp4"; + // QString url = "rtsp://admin:1234qwer@192.168.80.82:554/Streaming/Channels/101"; + QString url = m_systemSetting->value("camera/rtsp").toString(); + ui->widget_beltVideo->setUrl(url); + ui->widget_beltVideo->open(); } diff --git a/mainwindow.h b/mainwindow.h index 9d82132..f056073 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,75 +3,93 @@ #include #include +#include namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { - Q_OBJECT + Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); - ~MainWindow(); + explicit MainWindow(QWidget* parent = nullptr); + ~MainWindow(); + +protected: + bool eventFilter(QObject* watched, QEvent* event) override; public slots: - /** + /** * @brief 皮带报警信息 * @param consoleInfo */ - void addBeltWarnInfoToTextEdit(const QString &consoleInfo); + void addBeltWarnInfoToTextEdit(const QString& consoleInfo); private slots: - void on_pushButton_close_clicked(); + void on_pushButton_close_clicked(); - void on_pushButton_max_clicked(); + void on_pushButton_max_clicked(); - void on_pushButton_min_clicked(); + void on_pushButton_min_clicked(); private: - /** + /** * @brief calendarLoopPlay 时间轮播 */ - void calendarLoopPlay(); + void calendarLoopPlay(); - /** + /** + * @brief setSystemTitle 设置系统标题 + */ + void setSystemTitle(); + + /** * @brief setBeltWarnInfoTextEditStyle 调整皮带报警信息编辑框样式 */ - void setBeltWarnInfoTextEditStyle(); + void setBeltWarnInfoTextEditStyle(); - /** + /** * @brief addcoalDistributChart 添加煤流分布柱状图 */ - void addcoalDistributChart(); + void addcoalDistributChart(); - /** + /** * @brief addbeltSpeedChat 添加皮带机速度曲线图 */ - void addbeltSpeedChat(); + void addbeltSpeedChat(); - /** + /** * @brief addCoalStatisticsChart 添加煤量统计图表 */ - void addCoalStatisticsChart(); + void addCoalStatisticsChart(); - /** + /** * @brief addCoalBeltVideo 添加皮带机视频窗口 */ - void addCoalBeltVideo(); + void addCoalBeltVideo(); private: - Ui::MainWindow *ui; + Ui::MainWindow* ui; - /** + /** * @brief m_textEditConsoleInfo 控制台命令列表信息 */ - QList m_textEditConsoleInfo; + QList m_textEditConsoleInfo; - /** + /** * @brief m_previousConsleInfoDateTime 上一条控制台命令发送时间 */ - QDateTime m_previousConsleInfoDateTime; + QDateTime m_previousConsleInfoDateTime; + + /** + * @brief m_systemSetting 系统配置信息 + */ + QSettings* m_systemSetting; + + // 窗口拖拽 + QPoint m_mousePoint; + bool m_mousePressed { false }; }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 159b9f2..677791a 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -14,7 +14,7 @@ MainWindow - + :/Resource/image/logo.png:/Resource/image/logo.png @@ -47,16 +47,15 @@ #widget_head{ - border-image: url(:/Resource/image/title.png); } - + 0 - 0 + 9 0 @@ -78,17 +77,21 @@ - - - #label_title{ + + + + + #label_title{ color:white; font: 24pt "黑体"; } - - - 玉泉煤流检测 - - + + + 玉泉煤流检测 + + + + @@ -104,26 +107,30 @@ - - - - 268 - 0 - - - - #label_calendar{ + + + + + + 268 + 0 + + + + #label_calendar{ color:white; font: 16pt "黑体"; } - - - 2024年10月31日 星期一 - - - Qt::AlignHCenter|Qt::AlignTop - - + + + 2024年10月31日 星期一 + + + Qt::AlignHCenter|Qt::AlignTop + + + + @@ -3293,8 +3300,6 @@ QPushButton:pressed{ 1 - - - + diff --git a/view/coaldistributbarchartwidget.cpp b/view/coaldistributbarchartwidget.cpp index a0e980d..bf44aff 100644 --- a/view/coaldistributbarchartwidget.cpp +++ b/view/coaldistributbarchartwidget.cpp @@ -19,224 +19,230 @@ #include -CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget *parent) - : QWidget(parent), m_tcpSocket(new QTcpSocket(this)) { - initializeChart(); +CoalDistributBarChartWidget::CoalDistributBarChartWidget(QWidget* parent) + : QWidget(parent) + , m_tcpSocket(new QTcpSocket(this)) +{ + initializeChart(); - connectTCPSocket(); + m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); - setupTimer(); + connectTCPSocket(); + + setupTimer(); } -CoalDistributBarChartWidget::~CoalDistributBarChartWidget() { - if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) { - m_tcpSocket->disconnectFromHost(); - m_tcpSocket->waitForDisconnected(); - } - delete m_tcpSocket; +CoalDistributBarChartWidget::~CoalDistributBarChartWidget() +{ + if (m_tcpSocket->state() == QAbstractSocket::ConnectedState) { + m_tcpSocket->disconnectFromHost(); + m_tcpSocket->waitForDisconnected(); + } + delete m_tcpSocket; } -void CoalDistributBarChartWidget::updateChart() { - int radio = getCoalRatioByTCPSocket(); - // if (radio <= 0 || radio > 100) { - // return; - // } +void CoalDistributBarChartWidget::updateChart() +{ + int radio = getCoalRatioByTCPSocket(); + if (radio <= 0 || radio > 100) { + return; + } - int setCount = m_set->count(); + int setCount = m_set->count(); - qDebug() << "setCount" << setCount; - - m_set->remove(setCount - 1); - m_set->insert(0, radio); + m_set->remove(setCount - 1); + m_set->insert(0, radio); } -void CoalDistributBarChartWidget::initializeChart() { - // 创建图标 - m_chart = new QChart(); +void CoalDistributBarChartWidget::initializeChart() +{ + // 创建图标 + m_chart = new QChart(); - m_chart->setBackgroundBrush(Qt::transparent); - m_chart->setMargins(QMargins(0, 0, 0, 0)); - m_chart->legend()->hide(); + m_chart->setBackgroundBrush(Qt::transparent); + m_chart->setMargins(QMargins(0, 0, 0, 0)); + m_chart->legend()->hide(); - // 创建窗口并设置图表 - m_chartView = new QChartView(m_chart); - m_chartView->setBackgroundBrush(Qt::transparent); - m_chartView->setStyleSheet("background: transparent;"); // 使用样式表确保透明 - m_chartView->setRenderHint(QPainter::Antialiasing); - m_chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); + // 创建窗口并设置图表 + m_chartView = new QChartView(m_chart); + m_chartView->setBackgroundBrush(Qt::transparent); + m_chartView->setStyleSheet("background: transparent;"); // 使用样式表确保透明 + m_chartView->setRenderHint(QPainter::Antialiasing); + m_chartView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - // 创建柱状图 - m_series = new QBarSeries(); - m_chart->addSeries(m_series); + // 创建柱状图 + m_series = new QBarSeries(); + m_chart->addSeries(m_series); - // 设置X轴为据煤溜摄像机距离 - QValueAxis *axisX = new QValueAxis; - axisX->setRange(1, 300); - axisX->setTickCount(10); - axisX->setLabelFormat("%d"); - axisX->setLinePen(QPen(Qt::gray)); - axisX->setLabelsColor(Qt::gray); - axisX->setMinorTickCount(0); // 隐藏X轴刻度线 - m_chart->addAxis(axisX, Qt::AlignBottom); - m_series->attachAxis(axisX); + // 设置X轴为据煤溜摄像机距离 + QValueAxis* axisX = new QValueAxis; + axisX->setRange(1, 300); + axisX->setTickCount(10); + axisX->setLabelFormat("%d"); + axisX->setLinePen(QPen(Qt::gray)); + axisX->setLabelsColor(Qt::gray); + axisX->setMinorTickCount(0); // 隐藏X轴刻度线 + m_chart->addAxis(axisX, Qt::AlignBottom); + m_series->attachAxis(axisX); - // 设置Y轴为百分比 - QValueAxis *axisY = new QValueAxis; - axisY->setTitleText("占比(%)"); - axisY->setTitleBrush(QBrush(Qt::gray)); - axisY->setLinePen(QPen(Qt::gray)); - axisY->setLabelsColor(Qt::gray); // 设置标签字体颜色为灰色 - axisY->setRange(0, 100); - axisY->setTitleFont(QFont("Arial", 10)); // 设置标题字体 - axisY->setTitleVisible(true); // 确保标题可见 - m_chart->addAxis(axisY, Qt::AlignLeft); - m_series->attachAxis(axisY); + // 设置Y轴为百分比 + QValueAxis* axisY = new QValueAxis; + axisY->setTitleText("占比(%)"); + axisY->setTitleBrush(QBrush(Qt::gray)); + axisY->setLinePen(QPen(Qt::gray)); + axisY->setLabelsColor(Qt::gray); // 设置标签字体颜色为灰色 + axisY->setRange(0, 100); + axisY->setTitleFont(QFont("Arial", 10)); // 设置标题字体 + axisY->setTitleVisible(true); // 确保标题可见 + m_chart->addAxis(axisY, Qt::AlignLeft); + m_series->attachAxis(axisY); - // 初始化默认数据集 - m_set = new QBarSet("测试1"); - m_set->setColor(QColor(95, 180, 255)); - m_set->setBorderColor(QColor(95, 180, 255)); - QRandomGenerator *generator = QRandomGenerator::global(); - for (int i = 0; i < 300; i++) { - int randomNumber = generator->bounded(10, 51); - *m_set << randomNumber; - } - m_series->append(m_set); + // 初始化默认数据集 + m_set = new QBarSet("测试1"); + m_set->setColor(QColor(95, 180, 255)); + m_set->setBorderColor(QColor(95, 180, 255)); + QRandomGenerator* generator = QRandomGenerator::global(); + for (int i = 0; i < 300; i++) { + int randomNumber = generator->bounded(10, 51); + *m_set << randomNumber; + } + m_series->append(m_set); - QVBoxLayout *layout = new QVBoxLayout(this); - layout->setContentsMargins(0, 0, 0, 0); - layout->setSpacing(0); // 设置间距为0 - layout->addWidget(m_chartView); - setLayout(layout); + QVBoxLayout* layout = new QVBoxLayout(this); + layout->setContentsMargins(0, 0, 0, 0); + layout->setSpacing(0); // 设置间距为0 + layout->addWidget(m_chartView); + setLayout(layout); } -void CoalDistributBarChartWidget::setupTimer() { - // 定时器设置 - m_timer = new QTimer(this); - connect(m_timer, &QTimer::timeout, this, - &CoalDistributBarChartWidget::updateChart); - m_timer->start(1000); // 每秒更新一次 +void CoalDistributBarChartWidget::setupTimer() +{ + // 定时器设置 + m_timer = new QTimer(this); + connect(m_timer, &QTimer::timeout, this, + &CoalDistributBarChartWidget::updateChart); + m_timer->start(1000); // 每秒更新一次 } -void CoalDistributBarChartWidget::connectTCPSocket() { - QString ipAddress = "192.168.80.101"; // 替换为实际 IP 地址 - quint16 port = 3000; // Modbus TCP 端口 +void CoalDistributBarChartWidget::connectTCPSocket() +{ + QString ipAddress = m_systemSetting->value("addr/ip").toString(); + quint16 port = m_systemSetting->value("addr/port").toInt(); - // 连接到 Modbus TCP 服务器 - m_tcpSocket->connectToHost(ipAddress, port); - if (!m_tcpSocket->waitForConnected(5000)) { - qDebug() << "连接失败:" << m_tcpSocket->errorString(); - } + // 连接到 Modbus TCP 服务器 + m_tcpSocket->connectToHost(ipAddress, port); + if (!m_tcpSocket->waitForConnected(5000)) { + qDebug() << "连接失败:" << m_tcpSocket->errorString(); + } } -int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() { - if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) { - qDebug() << "未连接到服务器"; - return -1; // 返回错误值 - } +int CoalDistributBarChartWidget::getCoalRatioByTCPSocket() +{ + if (m_tcpSocket->state() != QAbstractSocket::ConnectedState) { + qDebug() << "未连接到服务器"; + return -1; // 返回错误值 + } - // 构建 Modbus 请求 - const quint8 unitId = 1; // 单元 ID - const quint8 functionCode = 0x03; // 读保持寄存器 - const quint16 startAddress = 699; // 寄存器地址 - const quint16 registerCount = 1; // 读取寄存器数量 + // 构建 Modbus 请求 + const quint8 unitId = 1; // 单元 ID + const quint8 functionCode = 0x03; // 读保持寄存器 + const quint16 startAddress = 699; // 寄存器地址 + const quint16 registerCount = 1; // 读取寄存器数量 - // 构建请求数据 - QByteArray request; - QDataStream stream(&request, QIODevice::WriteOnly); - stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序 + // 构建请求数据 + QByteArray request; + QDataStream stream(&request, QIODevice::WriteOnly); + stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序 - // 添加 Modbus 请求报文各部分 - stream << static_cast(0x0004); // 事务 ID - stream << static_cast(0x0000); // 协议 ID - stream << static_cast(0x0006); // 剩余长度 - stream << unitId; // 单元 ID - stream << functionCode; // 功能码 - stream << static_cast(startAddress); // 寄存器地址(700 = 0x01BB) - stream << registerCount; // 寄存器数量 + // 添加 Modbus 请求报文各部分 + stream << static_cast(0x0004); // 事务 ID + stream << static_cast(0x0000); // 协议 ID + stream << static_cast(0x0006); // 剩余长度 + stream << unitId; // 单元 ID + stream << functionCode; // 功能码 + stream << static_cast(startAddress); // 寄存器地址(700 = 0x01BB) + stream << registerCount; // 寄存器数量 - // 发送请求 - m_tcpSocket->write(request); - if (!m_tcpSocket->waitForBytesWritten(5000)) { - qDebug() << "写入请求失败:" << m_tcpSocket->errorString(); - return -1; // 返回错误值 - } + // 发送请求 + m_tcpSocket->write(request); + if (!m_tcpSocket->waitForBytesWritten(5000)) { + qDebug() << "写入请求失败:" << m_tcpSocket->errorString(); + return -1; // 返回错误值 + } - // 等待响应 - if (!m_tcpSocket->waitForReadyRead(5000)) { - qDebug() << "没有收到响应:" << m_tcpSocket->errorString(); - return -1; // 返回错误值 - } + // 等待响应 + if (!m_tcpSocket->waitForReadyRead(5000)) { + qDebug() << "没有收到响应:" << m_tcpSocket->errorString(); + return -1; // 返回错误值 + } - // 读取响应数据 - QByteArray response = m_tcpSocket->readAll(); - if (response.size() < 9) { - qDebug() << "响应数据不完整"; - return -1; // 返回错误值 - } + // 读取响应数据 + QByteArray response = m_tcpSocket->readAll(); + if (response.size() < 9) { + qDebug() << "响应数据不完整"; + return -1; // 返回错误值 + } - qDebug() << "response" << response.toHex(); + // 解析响应 + QDataStream responseStream(response); + responseStream.setByteOrder(QDataStream::LittleEndian); + quint16 responseTransactionId; + responseStream >> responseTransactionId; // 事务 ID + quint16 responseProtocolId; + responseStream >> responseProtocolId; // 协议 ID + quint16 responseLength; + responseStream >> responseLength; // 剩余长度 + quint8 responseUnitId; + responseStream >> responseUnitId; // 单元 ID + quint8 responseFunctionCode; + responseStream >> responseFunctionCode; // 功能码 + quint8 byteCount; + responseStream >> byteCount; // 字节计数 - // 解析响应 - QDataStream responseStream(response); - responseStream.setByteOrder(QDataStream::LittleEndian); - quint16 responseTransactionId; - responseStream >> responseTransactionId; // 事务 ID - quint16 responseProtocolId; - responseStream >> responseProtocolId; // 协议 ID - quint16 responseLength; - responseStream >> responseLength; // 剩余长度 - quint8 responseUnitId; - responseStream >> responseUnitId; // 单元 ID - quint8 responseFunctionCode; - responseStream >> responseFunctionCode; // 功能码 - quint8 byteCount; - responseStream >> byteCount; // 字节计数 + // 检查功能码和字节计数 + if (responseFunctionCode != functionCode || byteCount != 2) { + qDebug() << "功能码或字节计数不匹配"; + return -1; // 返回错误值 + } - // 检查功能码和字节计数 - if (responseFunctionCode != functionCode || byteCount != 2) { - qDebug() << "功能码或字节计数不匹配"; - return -1; // 返回错误值 - } + // 获取并输出最后四个字节 + if (response.size() < 2) { + qDebug() << "响应数据少于四个字节,无法获取最后四位"; + } - // 获取并输出最后四个字节 - if (response.size() < 2) { - qDebug() << "响应数据少于四个字节,无法获取最后四位"; - } + QByteArray lastFourBytes = response.right(2); // 获取最后2个字节 - QByteArray lastFourBytes = response.right(2); // 获取最后2个字节 + bool isOk; + int decimalValue = lastFourBytes.toHex().toInt(&isOk, 16); - bool isOk; - int decimalValue = lastFourBytes.toHex().toInt(&isOk, 16); - qDebug() << "最后四位:" << decimalValue; - - return decimalValue; // 返回煤量占比 + return decimalValue; // 返回煤量占比 } -QByteArray CoalDistributBarChartWidget::createModbusTcpRequest() { - QByteArray request; - QDataStream stream(&request, QIODevice::WriteOnly); - stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序 +QByteArray CoalDistributBarChartWidget::createModbusTcpRequest() +{ + QByteArray request; + QDataStream stream(&request, QIODevice::WriteOnly); + stream.setByteOrder(QDataStream::BigEndian); // 使用大端字节序 - // 构建 Modbus TCP 请求 - quint16 transactionId = 0x0004; // 事务 ID - quint16 protocolId = 0x0000; // 协议 ID - quint16 length = 0x0006; // 剩余长度 - quint8 unitId = 0x01; // 单元 ID - quint8 functionCode = 0x03; // 功能码 - quint8 byteCount = 0x02; // 字节计数 - quint16 registerAddress = 0x01BB; // 寄存器地址 700 - quint16 registerCount = 0x0001; // 寄存器数量 + // 构建 Modbus TCP 请求 + quint16 transactionId = 0x0004; // 事务 ID + quint16 protocolId = 0x0000; // 协议 ID + quint16 length = 0x0006; // 剩余长度 + quint8 unitId = 0x01; // 单元 ID + quint8 functionCode = 0x03; // 功能码 + quint8 byteCount = 0x02; // 字节计数 + quint16 registerAddress = 0x01BB; // 寄存器地址 700 + quint16 registerCount = 0x0001; // 寄存器数量 - // 写入数据 - stream << transactionId; - stream << protocolId; - stream << length; - stream << unitId; - stream << functionCode; - stream << byteCount; - stream << registerAddress; - stream << registerCount; + // 写入数据 + stream << transactionId; + stream << protocolId; + stream << length; + stream << unitId; + stream << functionCode; + stream << byteCount; + stream << registerAddress; + stream << registerCount; - return request; // 返回生成的请求报文 + return request; // 返回生成的请求报文 } diff --git a/view/coaldistributbarchartwidget.h b/view/coaldistributbarchartwidget.h index 91f2665..abc939d 100644 --- a/view/coaldistributbarchartwidget.h +++ b/view/coaldistributbarchartwidget.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -58,13 +59,9 @@ private: QBarSet* m_set; - int currentIndex; // 当前更新的柱子的索引 - - static const int MaxDataPoints = 300; // 最大数据点数(5分钟,每秒1条) - - QList m_timeLabels; // 存储时间标签 - QTcpSocket* m_tcpSocket; + + QSettings* m_systemSetting; }; #endif // COALDISTRIBUTBARCHARTWIDGET_H diff --git a/view/coalstatisticschartwidget.cpp b/view/coalstatisticschartwidget.cpp index 527982c..d1cbdbb 100644 --- a/view/coalstatisticschartwidget.cpp +++ b/view/coalstatisticschartwidget.cpp @@ -1,6 +1,7 @@ #include "coalstatisticschartwidget.h" #include "ui_coalstatisticschartwidget.h" +#include #include #include #include @@ -15,6 +16,8 @@ CoalStatisticsChartWidget::CoalStatisticsChartWidget(QWidget* parent) { ui->setupUi(this); + m_systemSetting = new QSettings("system.ini", QSettings::IniFormat); + initCoalStatisticsBarChart(); getCoalStatisInfoByHttpRequest("2024-09-01 08", "2024-09-02 08"); @@ -25,6 +28,104 @@ CoalStatisticsChartWidget::~CoalStatisticsChartWidget() delete ui; } +void CoalStatisticsChartWidget::on_pushButton_coalStatisOk_clicked() +{ + // 时间范围 + QString beginTimeStr = ui->dateTimeEdit_startTime->text(); + QString endTimeStr = ui->dateTimeEdit_endTime->text(); + + qDebug() << "beginTimeStr" << beginTimeStr; + qDebug() << "endTimeStr" << endTimeStr; + + // 创建 QDateTime 对象 + QDateTime beginTime = QDateTime::fromString(beginTimeStr, "yyyy/MM/d HH:mm"); + QDateTime endTime = QDateTime::fromString(endTimeStr, "yyyy/MM/d HH:mm"); + + // 判断是否转换成功 + if (!beginTime.isValid() || !endTime.isValid()) { + qDebug() << "Invalid date/time format!"; + return; + } + + int unitIndex = ui->comboBox_unit->currentIndex(); + + // 选择的时间单位 + switch (unitIndex) { + case TimeUnit::Hour: { + // 计算整时 + QList dateTimes; + int minutes = beginTime.time().minute(); // 获取分钟部分 + beginTime = beginTime.addSecs(-minutes * 60); // 向下取整 + + // 遍历时间范围 + while (beginTime <= endTime) { + dateTimes.append(beginTime); + beginTime = beginTime.addSecs(3600); // 增加1小时 + } + + // 输出结果 + for (const QDateTime& dateTime : dateTimes) { + qDebug() << "整小时数" << dateTime.toString("yyyy/MM/d HH:mm"); + } + break; + } + case TimeUnit::Day: { + // 计算整天 + QList dateTimes; + beginTime.setTime(QTime(0, 0)); // 将起始时间设置为当天的00:00:00 + + // 遍历时间范围 + while (beginTime <= endTime) { + dateTimes.append(beginTime); + beginTime = beginTime.addDays(1); // 增加1天 + } + + // 输出结果 + for (const QDateTime& dateTime : dateTimes) { + qDebug() << "整天数" << dateTime.toString("yyyy/MM/d"); + } + break; + } + case TimeUnit::Month: { + // 计算整月 + QList dateTimes; + beginTime.setDate(QDate(beginTime.date().year(), beginTime.date().month(), 1)); // 设置为当月的1号 + + // 遍历时间范围 + while (beginTime <= endTime) { + dateTimes.append(beginTime); + beginTime = beginTime.addMonths(1); // 增加1个月 + } + + // 输出结果 + for (const QDateTime& dateTime : dateTimes) { + qDebug() << "整月数" << dateTime.toString("yyyy/MM"); + } + break; + } + case TimeUnit::Year: { + // 计算整年 + QList dateTimes; + beginTime.setDate(QDate(beginTime.date().year(), 1, 1)); // 设置为当年的1月1日 + + // 遍历时间范围 + while (beginTime <= endTime) { + dateTimes.append(beginTime); + beginTime = beginTime.addYears(1); // 增加1年 + } + + // 输出结果 + for (const QDateTime& dateTime : dateTimes) { + qDebug() << "整年数" << dateTime.toString("yyyy"); + } + break; + } + default: + qDebug() << "无效的时间单位!"; + break; + } +} + void CoalStatisticsChartWidget::initCoalStatisticsBarChart() { // 创建图表 @@ -45,29 +146,35 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart() m_chart->addSeries(m_series); // 设置X轴为时间 - QValueAxis* axisX = new QValueAxis; - axisX->setRange(1, 300); - axisX->setTickCount(10); - axisX->setLabelFormat("%d"); - axisX->setLinePen(QPen(Qt::gray)); - axisX->setLabelsColor(Qt::gray); - axisX->setMinorTickCount(0); - m_chart->addAxis(axisX, Qt::AlignBottom); - m_series->attachAxis(axisX); + m_axisX = new QDateTimeAxis; + m_axisX->setFormat("yyyy/MM/d H"); // 设置日期时间格式 + // m_axisX->setTitleText("时间"); + m_axisX->setTitleBrush(QBrush(Qt::gray)); + m_axisX->setLinePen(QPen(Qt::gray)); + m_axisX->setLabelsColor(Qt::gray); + + // 获取当前时间并设置时间范围为最近六个小时 + QDateTime currentTime = QDateTime::currentDateTime(); + QDateTime startTime = currentTime.addSecs(-6 * 3600); // 当前时间减去六个小时 + + // m_axisX->setLabelsAngle(45); + + // 设置X轴时间范围 + m_axisX->setRange(startTime, currentTime); + m_chart->addAxis(m_axisX, Qt::AlignBottom); + m_series->attachAxis(m_axisX); // 设置Y轴为百分比 - QValueAxis* axisY = new QValueAxis; - axisY->setTitleText("吨"); - axisY->setTitleBrush(QBrush(Qt::gray)); - axisY->setLinePen(QPen(Qt::gray)); - axisY->setLabelsColor(Qt::gray); - axisY->setRange(0, 100); - m_chart->addAxis(axisY, Qt::AlignLeft); - m_series->attachAxis(axisY); - - // 设置Y轴标题位置 - axisY->setTitleFont(QFont("Arial", 10)); - axisY->setTitleVisible(true); + m_axisY = new QValueAxis; + m_axisY->setTitleText("吨"); + m_axisY->setTitleBrush(QBrush(Qt::gray)); + m_axisY->setLinePen(QPen(Qt::gray)); + m_axisY->setLabelsColor(Qt::gray); + m_axisY->setRange(0, 100); + m_chart->addAxis(m_axisY, Qt::AlignLeft); + m_series->attachAxis(m_axisY); + m_axisY->setTitleFont(QFont("Arial", 10)); + m_axisY->setTitleVisible(true); // 获取 widget_coalChart 的布局 QVBoxLayout* layout = static_cast(ui->widget_coalChart->layout()); @@ -89,25 +196,37 @@ void CoalStatisticsChartWidget::initCoalStatisticsBarChart() void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& beginTime, const QString& endTime) { - QNetworkAccessManager* manager = new QNetworkAccessManager(); + // 使用类成员变量 QNetworkAccessManager + QNetworkAccessManager* manager = new QNetworkAccessManager(this); + + QString coalStaticUrl = m_systemSetting->value("coalStaticInterface/url").toString(); // URL - QUrl url("http://192.168.5.205:5002/api/get/coal/statistics/value/"); + QUrl url(coalStaticUrl); QNetworkRequest request(url); // Form data - QUrlQuery params; - params.addQueryItem("begin_time", beginTime); - params.addQueryItem("end_time", endTime); + QHttpMultiPart* multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); - // 设置请求头 - request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); + // begin_time 参数 + QHttpPart beginTimePart; + beginTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"begin_time\"")); + beginTimePart.setBody(beginTime.toUtf8()); + + // end_time 参数 + QHttpPart endTimePart; + endTimePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"end_time\"")); + endTimePart.setBody(endTime.toUtf8()); + + // 将表单字段附加到 multipart 请求体中 + multiPart->append(beginTimePart); + multiPart->append(endTimePart); // 发送 POST 请求 - QNetworkReply* reply = manager->post(request, params.query(QUrl::FullyEncoded).toUtf8()); + QNetworkReply* reply = manager->post(request, multiPart); // 连接信号和槽 - QObject::connect(reply, &QNetworkReply::finished, [reply]() { + QObject::connect(reply, &QNetworkReply::finished, [this, reply, multiPart]() { if (reply->error() == QNetworkReply::NoError) { // 处理成功的响应 QByteArray responseData = reply->readAll(); @@ -117,15 +236,19 @@ void CoalStatisticsChartWidget::getCoalStatisInfoByHttpRequest(const QString& be int code = jsonObj["code"].toInt(); QString message = jsonObj["message"].toString(); QJsonObject data = jsonObj["data"].toObject(); - double totalVolume = data["total_volume"].toDouble(); + double totalVolume = data["total_volum"].toDouble(); qDebug() << "Code:" << code; qDebug() << "Message:" << message; qDebug() << "Total Volume:" << totalVolume; } else { // 处理错误 - qDebug() << "Error:" << reply->errorString(); + QString errorString = reply->errorString(); + qDebug() << "Error:" << errorString; } + + // 清理资源 reply->deleteLater(); + multiPart->deleteLater(); }); } diff --git a/view/coalstatisticschartwidget.h b/view/coalstatisticschartwidget.h index 8bbefa4..22d0c78 100644 --- a/view/coalstatisticschartwidget.h +++ b/view/coalstatisticschartwidget.h @@ -9,6 +9,13 @@ namespace Ui { class CoalStatisticsChartWidget; } +enum TimeUnit { + Hour = 0, // 时 + Day = 1, // 天 + Month = 2, // 月 + Year = 3 // 年 +}; + class CoalStatisticsChartWidget : public QWidget { Q_OBJECT @@ -16,6 +23,13 @@ public: explicit CoalStatisticsChartWidget(QWidget* parent = nullptr); ~CoalStatisticsChartWidget(); +private slots: + + /** + * @brief on_pushButton_coalStatisOk_clicked 点击确定按钮 + */ + void on_pushButton_coalStatisOk_clicked(); + private: /** * @brief initCoalStatisticsBarChart 初始化煤量统计柱状图 @@ -33,7 +47,12 @@ private: QChart* m_chart; QChartView* m_chartView; QBarSeries* m_series; + QDateTimeAxis* m_axisX; + QValueAxis* m_axisY; + QTimer* m_timer; + + QSettings* m_systemSetting; }; #endif // COALSTATISTICSCHARTWIDGET_H diff --git a/view/coalstatisticschartwidget.ui b/view/coalstatisticschartwidget.ui index f1a14da..00fda3c 100644 --- a/view/coalstatisticschartwidget.ui +++ b/view/coalstatisticschartwidget.ui @@ -32,13 +32,13 @@ - 15 + 5 - 10 + 6 - 10 + 6 @@ -204,6 +204,42 @@ QDateTimeEdit::up-button, QDateTimeEdit::down-button { + + + + + 0 + 0 + + + + + 45 + 20 + + + + QPushButton{ + border-image: url(:/Resource/image/labelunSelect.png); + color:white; + font-size: 14px; + border: none; +} + +QPushButton:hover{ + border-image: url(:/Resource/image/labelhover.png); +} + +QPushButton:pressed{ + border-image: url(:/Resource/image/labelSelect.png); +} + + + + 确定 + + +