#ifndef MONITOROVOZEZ_H #define MONITOROVOZEZ_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "MonitorTagUI.h" #include "../HTTPClient/client.h" QT_BEGIN_NAMESPACE class MonitorWidget:public QWidget { public: QVBoxLayout *verticalLayout; QHBoxLayout *operationLayout; // QPushButton *exchangeButton; QSpacerItem *horizontalSpacer; QGridLayout *monitorLayout; QHBoxLayout *filterlLayout; QPushButton *interruptedButton; QPushButton *normalButton; QPushButton *standbyButton; QPushButton *hutdownButton; QPushButton *failureButton; QPushButton *runButton; QWidget *fillWidget; int state; QList> tagList; QTimer *timer; void setupUi(QWidget *MonitorWidget) { state = 0; if (MonitorWidget->objectName().isEmpty()) MonitorWidget->setObjectName(QString::fromUtf8("MonitorWidget")); verticalLayout = new QVBoxLayout(MonitorWidget); verticalLayout->setSpacing(0); verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); verticalLayout->setContentsMargins(0, 0, 0, 0); operationLayout = new QHBoxLayout(); operationLayout->setObjectName(QString::fromUtf8("operationLayout")); // exchangeButton = new QPushButton(MonitorWidget); // exchangeButton->setObjectName(QString::fromUtf8("exchangeButton")); // operationLayout->addWidget(exchangeButton); horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); operationLayout->addItem(horizontalSpacer); // verticalLayout->addLayout(operationLayout); monitorLayout = new QGridLayout(); monitorLayout->setObjectName(QString::fromUtf8("monitorLayout")); monitorLayout->setSpacing(30); monitorLayout->setContentsMargins(20, 20, 20, 20); monitorLayout->setColumnStretch(0, 1); monitorLayout->setColumnStretch(1, 1); monitorLayout->setColumnStretch(2, 1); monitorLayout->setColumnStretch(3, 1); monitorLayout->setColumnStretch(4, 1); monitorLayout->setColumnStretch(5, 1); monitorLayout->setColumnStretch(6, 1); fillWidget = new QWidget(); fillWidget->setLayout(monitorLayout); fillWidget->setObjectName(QString::fromUtf8("fillWidget")); // connect(exchangeButton, &QPushButton::clicked, this, [=](){this->changeState();}); QScrollArea *scrollArea = new QScrollArea(); scrollArea->setWidget(fillWidget); scrollArea->setWidgetResizable(true); scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); scrollArea->setObjectName(QString::fromUtf8("scrollArea")); verticalLayout->addWidget(scrollArea); filterlLayout = new QHBoxLayout(); filterlLayout->setObjectName(QString::fromUtf8("filterlLayout")); interruptedButton = new QPushButton(MonitorWidget); interruptedButton->setObjectName(QString::fromUtf8("interruptedButton")); interruptedButton->setIcon(QIcon(":/Static/fanInterrupted.png")); filterlLayout->addWidget(interruptedButton); normalButton = new QPushButton(MonitorWidget); normalButton->setObjectName(QString::fromUtf8("normalButton")); normalButton->setIcon(QIcon(":/Static/fanNormal.png")); filterlLayout->addWidget(normalButton); standbyButton = new QPushButton(MonitorWidget); standbyButton->setObjectName(QString::fromUtf8("standbyButton")); standbyButton->setIcon(QIcon(":/Static/fanStandby.png")); filterlLayout->addWidget(standbyButton); hutdownButton = new QPushButton(MonitorWidget); hutdownButton->setObjectName(QString::fromUtf8("hutdownButton")); hutdownButton->setIcon(QIcon(":/Static/fanHutdown.png")); filterlLayout->addWidget(hutdownButton); failureButton = new QPushButton(MonitorWidget); failureButton->setObjectName(QString::fromUtf8("failureButton")); failureButton->setIcon(QIcon(":/Static/fanError.png")); filterlLayout->addWidget(failureButton); runButton = new QPushButton(MonitorWidget); runButton->setObjectName(QString::fromUtf8("runButton")); runButton->setIcon(QIcon(":/Static/fanRun.png")); filterlLayout->addWidget(runButton); filterlLayout->setContentsMargins(30, 10, 30, 30); filterlLayout->setSpacing(50); verticalLayout->addLayout(filterlLayout); verticalLayout->setStretch(0, 2); verticalLayout->setStretch(1, 30); verticalLayout->setStretch(2, 3); MonitorWidget->setLayout(verticalLayout); QMetaObject::connectSlotsByName(MonitorWidget); retranslateUi(MonitorWidget); addTag(); timer = new QTimer(MonitorWidget); QObject::connect(timer, &QTimer::timeout, [&]() { // 计时器超时时更新标签 addTag(); }); timer->start(5000); interruptedButton->setCheckable(true); normalButton->setCheckable(true); standbyButton->setCheckable(true); hutdownButton->setCheckable(true); failureButton->setCheckable(true); runButton->setCheckable(true); connect(interruptedButton, &QPushButton::clicked, this, [=](){this->addTag();}); connect(normalButton, &QPushButton::clicked, this, [=](){this->addTag();}); connect(standbyButton, &QPushButton::clicked, this, [=](){this->addTag();}); connect(hutdownButton, &QPushButton::clicked, this, [=](){this->addTag();}); connect(failureButton, &QPushButton::clicked, this, [=](){this->addTag();}); connect(runButton, &QPushButton::clicked, this, [=](){this->addTag();}); // connect(exchangeButton, &QPushButton::clicked, this, [=](){this->exchangeClicked();}); } // setupUi void retranslateUi(QWidget *MonitorWidget) { MonitorWidget->setWindowTitle(QCoreApplication::translate("MonitorWidget", "Form", nullptr)); // exchangeButton->setText(QCoreApplication::translate("MonitorWidget", "\345\244\247\345\260\217\345\233\276\346\240\207\345\210\207\346\215\242", nullptr)); } // retranslateUi void exchangeClicked() { int count = monitorLayout->count(); // 遍历布局中的所有子对象 for (int i = 0; i < count; ++i) { // 获取第 i 个子对象的指针 QLayoutItem *item = monitorLayout->itemAt(i); MonitorTag *tag = dynamic_cast(item->widget()); // 判断子对象的类型 if (tag) { if (this->state == 1) { tag->stackedWidget->setCurrentIndex(1); } else if (this->state == 0) { tag->stackedWidget->setCurrentIndex(0); } } } } void changeState() { if (state == 0){ state = 1; } else if (state == 1) { state = 0; } } void addTag() { httpClient.getFanList(); map allFans = fans.allFans; // 获取与 clicked() 信号相关的接收器数量 while (QLayoutItem *item = monitorLayout->takeAt(0)) { if (QWidget *widget = item->widget()) { widget->deleteLater(); // 或使用 delete 关键字删除控件的内存 } delete item; } tagList.clear(); int count = 0; int index = 0; QList tags; for (auto it = allFans.begin(); it != allFans.end(); ++it) { if(!shouldDisplay(it->second.fanCode)){ continue; } if (index == 6){ tagList.append(tags); tags.clear(); index = 0; count++; } MonitorTag *newTag = new MonitorTag(it->second, state); connect(newTag->stateBox, QOverload::of(&QComboBox::currentIndexChanged), [this](int index) {addTag();}); monitorLayout->addWidget(newTag, count, index); tags.append(newTag); index++; } if (allFans.size() < 28){ fillWidget->setFixedHeight(228 * 4); for (int i = 0; i < 28 - allFans.size(); i++) { if (index == 6){ index = 0; count++; } QWidget *w = new QWidget(); monitorLayout->addWidget(w, count, index); index++; } monitorLayout->setRowStretch(0, 1); monitorLayout->setRowStretch(1, 1); monitorLayout->setRowStretch(2, 1); monitorLayout->setRowStretch(3, 1); }else{ fillWidget->setFixedHeight(228 * ceil(allFans.size() / 4)); } fillWidget->setFixedWidth(1900); setButtonMes(); // int c = monitorLayout->count(); // qDebug() << c; } void setButtonMes(){ interruptedButton->setText(QString("通讯中断 ") + QString::number(fans.interruptedFans.size())); normalButton->setText(QString("正常运行 ") + QString::number(fans.normalFans.size())); standbyButton->setText(QString("待机等风 ") + QString::number(fans.standbyFans.size())); hutdownButton->setText(QString("停机维护 ") + QString::number(fans.hutdownFans.size())); failureButton->setText(QString("故障停机 ") + QString::number(fans.interruptedFans.size())); runButton->setText(QString("启动 ") + QString::number(fans.runFans.size())); } bool shouldDisplay(QString FanCode){ vector displayList; if (interruptedButton->isChecked()){displayList.insert(displayList.end(), fans.interruptedFans.begin(), fans.interruptedFans.end());} if (normalButton->isChecked()){displayList.insert(displayList.end(), fans.normalFans.begin(), fans.normalFans.end());} if (standbyButton->isChecked()){displayList.insert(displayList.end(), fans.standbyFans.begin(), fans.standbyFans.end());} if (hutdownButton->isChecked()){displayList.insert(displayList.end(), fans.hutdownFans.begin(), fans.hutdownFans.end());} if (failureButton->isChecked()){displayList.insert(displayList.end(), fans.failureFans.begin(), fans.failureFans.end());} if (runButton->isChecked()){displayList.insert(displayList.end(), fans.runFans.begin(), fans.runFans.end());} if (!interruptedButton->isChecked() && !normalButton->isChecked() && !standbyButton->isChecked() && !hutdownButton->isChecked() && !failureButton->isChecked() && !runButton->isChecked()){ return true; } if (std::find(displayList.begin(), displayList.end(), FanCode) != displayList.end()) { return true; }else{ return false; } } MonitorWidget() { setupUi(this); } }; QT_END_NAMESPACE #endif // MONITOROVOZEZ_H