#include "QWHome.h" #include "common/Spdlogger.h" #include "app/Config.h" #include "common/Utils.h" #include "app/Application.h" #include "app/AppData.h" #include "app/Station.h" #include "protocol/MqttEntity.h" QWHome::QWHome(QWidget* parent) : MyWidget(parent) { this->setObjectName("workspace"); this->setStyleSheet("#workspace { background-color:rgba(100,100,100,50); }"); int x = 10, y = 10; { this->groupSys = MyQUI::GroupBox(this, x, y, 1190, 120, "系统"); auto pw = groupSys.get(); } { x = 10, y += 130; this->groupHttp = MyQUI::GroupBox(this, x, y, 390, 120, "HTTP"); auto pw = groupHttp.get(); MyQUI::PairLine(pw, 20, 20, "服务类型: ", "服务端"); MyQUI::PairLine(pw, 20, 50, "服务端口: ", Utils::toStr(Config::option.http.port)); MyQUI::PairLine(pw, 20, 80, "服务状态: ", "运行"); } { x += 400; this->groupMqtt = MyQUI::GroupBox(this, x, y, 390, 120, "MQTT"); auto pw = groupMqtt.get(); MyQUI::PairLine(pw, 20, 20, "服务类型: ", "客户端"); MyQUI::PairLine(pw, 20, 50, "服务地址: ", Config::option.mqtt.host); MyQUI::PairLine(pw, 20, 80, "服务状态: ", "---"); } { x += 400; this->groupDB = MyQUI::GroupBox(this, x, y, 390, 120, "数据库"); auto pw = groupDB.get(); MyQUI::PairLine(pw, 20, 20, "数据库名: ", Config::option.database.dbname); MyQUI::PairLine(pw, 20, 50, "主机地址: ", Config::option.database.host); MyQUI::PairLine(pw, 20, 80, "用 户 名: ", Config::option.database.user); const std::string QSS_TABLE = // 表格整体样式 "QTableWidget {" " background-color: transparent;" // 背景色 " gridline-color: #C0C0C0;" // 网格线颜色 " border: 1px solid gray;" // 边框 " color: white;" // 文字颜色 "}" // 表头样式 "QHeaderView::section {" " background-color: #404040;" // 表头背景 " padding: 4px;" // 内边距 " border: 1px solid #505050;" // 边框 " min-height: 25px;" // 最小高度 "}" // 单元格样式 "QTableWidget::item {" " padding-left: 5px;" " border-bottom: 1px solid gray;" // 底部边框 "}" // 选中状态 "QTableWidget::item:selected {" " background-color: #B8D6FF;" // 选中背景色 " color: black;" // 选中文字颜色 "}"; table = std::make_shared(this); table->setGeometry(10, y += 130, 1190, 300); table->setStyleSheet(QSS_TABLE.c_str()); table->horizontalHeader()->setStretchLastSection(true); // 最后一列占满 table->verticalHeader()->setVisible(false); // 不显示垂直表头 table->setEditTriggers(QAbstractItemView::NoEditTriggers); // 单元格不可编辑 table->setSelectionMode(QAbstractItemView::SingleSelection); // 设置为单选模式 table->setSelectionBehavior(QAbstractItemView::SelectRows); // 设置为整行选中 table->horizontalHeader()->setFixedHeight(50); table->horizontalHeader()->setDefaultSectionSize(60); QTableWidgetItem* headerItem; QStringList headerText_Row, headerText_Col; headerText_Row << "ID" << "站名" << "编号" << "状态" << "MQTT状态" << "召测(秒)" << "日充电\n电量" << "日放电\n电量" << "总充电\n电量" << "总放电\n电量" << "日充电\n费用" << "日放电\n费用" << "总充电\n费用" << "总放电\n费用" << "日收益" << "总收益" << "--"; // 设置为水平表头 table->setColumnCount(headerText_Row.size()); table->setHorizontalHeaderLabels(headerText_Row); table->setColumnWidth(0, 50); table->setColumnWidth(1, 120); table->setColumnWidth(2, 50); table->setColumnWidth(4, 80); } textLog = std::make_shared(this); textLog->setGeometry(10, y += 310, 1190, 280); textLog->setStyleSheet("background-color: transparent; border: 1px solid gray; font-weight: 400;"); textLog->setReadOnly(true); { // 第二个参数是方法函数名称,即调用 QTextEdit的appeng函数; auto qtSink = std::make_shared(textLog.get(), "append"); spdlog::default_logger()->sinks().push_back(qtSink); } } void QWHome::setTableCell(int row, int col, std::string text, std::string style /*= ""*/) { auto item = table->item(row, col); if (!item) { item = new QTableWidgetItem(); table->setItem(row, col, item); } item->setText(text.c_str()); if (style == "OK") { item->setForeground(QBrush(Qt::green)); } else if (style == "ERR") { item->setForeground(QBrush(Qt::red)); } } void QWHome::onTimer() { auto& appdata = Application().data(); int rowNo = 0; int tsNow = Utils::time(); for (auto& item : appdata.mapStation) { auto& station = item.second; if (rowNo >= table->rowCount()) { table->insertRow(rowNo); } bool isOpen = station->status > 0; bool isConnected = station->mqttCli->isConnected; setTableCell(rowNo, 0, std::to_string(station->stationId)); setTableCell(rowNo, 1, station->name); setTableCell(rowNo, 2, station->code); setTableCell(rowNo, 3, isOpen ? "启用" : "未启用", isOpen ? "OK" : "ERR"); setTableCell(rowNo, 4, isConnected ? "连接成功" : "未连接", isConnected ? "OK" : "ERR"); int tsPolling = station->getPollingTS(); setTableCell(rowNo, 5, tsPolling > 0 ? std::to_string(tsNow - tsPolling) + "/" + std::to_string(Config::option.mqtt.interval) : "--"); setTableCell(rowNo, 6, Utils::toStr(station->statData.dayElectIn, 0)); setTableCell(rowNo, 7, Utils::toStr(station->statData.dayElectOut, 0)); setTableCell(rowNo, 8, Utils::toStr(station->statData.totalElectIn, 0)); setTableCell(rowNo, 9, Utils::toStr(station->statData.totalElectOut, 0)); setTableCell(rowNo, 10, Utils::toStr(station->statData.dayFeeIn, 0)); setTableCell(rowNo, 11, Utils::toStr(station->statData.dayFeeOut, 0)); setTableCell(rowNo, 12, Utils::toStr(station->statData.totalFeeIn, 0)); setTableCell(rowNo, 13, Utils::toStr(station->statData.totalFeeOut, 0)); setTableCell(rowNo, 14, Utils::toStr(station->statData.dayIncome, 0)); setTableCell(rowNo, 15, Utils::toStr(station->statData.totalIncome, 0)); setTableCell(rowNo, 16, station->statData.ts > 0 ? Utils::timeStr(station->statData.ts) : "--"); rowNo++; } }