statswidget.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "statswidget.h"
  2. #include <QDebug>
  3. StatsWidget::StatsWidget(QWidget *parent)
  4. : QWidget(parent)
  5. , m_statsGroup(nullptr)
  6. , m_totalCountLabel(nullptr)
  7. , m_type0CountLabel(nullptr)
  8. , m_type1CountLabel(nullptr)
  9. , m_type2CountLabel(nullptr)
  10. , m_lastUpdateLabel(nullptr)
  11. , m_totalCount(0)
  12. , m_type0Count(0)
  13. , m_type1Count(0)
  14. , m_type2Count(0)
  15. {
  16. setupUI();
  17. applyStyles();
  18. }
  19. StatsWidget::~StatsWidget()
  20. {
  21. }
  22. void StatsWidget::setupUI()
  23. {
  24. // 创建主布局
  25. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  26. mainLayout->setContentsMargins(5, 5, 5, 5);
  27. mainLayout->setSpacing(5);
  28. // 创建统计信息组
  29. m_statsGroup = new QGroupBox("消息统计", this);
  30. QVBoxLayout *statsLayout = new QVBoxLayout(m_statsGroup);
  31. statsLayout->setContentsMargins(10, 10, 10, 10);
  32. statsLayout->setSpacing(8);
  33. // 总消息数
  34. m_totalCountLabel = new QLabel("总消息数: 0", this);
  35. m_totalCountLabel->setObjectName("totalCountLabel");
  36. statsLayout->addWidget(m_totalCountLabel);
  37. // 创建消息类型统计的水平布局
  38. QHBoxLayout *typeLayout = new QHBoxLayout();
  39. typeLayout->setSpacing(15);
  40. // 类型0消息数
  41. m_type0CountLabel = new QLabel("类型0: 0", this);
  42. m_type0CountLabel->setObjectName("type0CountLabel");
  43. typeLayout->addWidget(m_type0CountLabel);
  44. // 类型1消息数
  45. m_type1CountLabel = new QLabel("类型1: 0", this);
  46. m_type1CountLabel->setObjectName("type1CountLabel");
  47. typeLayout->addWidget(m_type1CountLabel);
  48. // 类型2消息数
  49. m_type2CountLabel = new QLabel("类型2: 0", this);
  50. m_type2CountLabel->setObjectName("type2CountLabel");
  51. typeLayout->addWidget(m_type2CountLabel);
  52. statsLayout->addLayout(typeLayout);
  53. // 最后更新时间
  54. m_lastUpdateLabel = new QLabel("最后更新: --", this);
  55. m_lastUpdateLabel->setObjectName("lastUpdateLabel");
  56. statsLayout->addWidget(m_lastUpdateLabel);
  57. // 添加到主布局
  58. mainLayout->addWidget(m_statsGroup);
  59. mainLayout->addStretch();
  60. setLayout(mainLayout);
  61. setMaximumHeight(150);
  62. }
  63. void StatsWidget::applyStyles()
  64. {
  65. setStyleSheet(
  66. "QGroupBox {"
  67. " font-weight: bold;"
  68. " border: 2px solid #cccccc;"
  69. " border-radius: 5px;"
  70. " margin-top: 1ex;"
  71. " padding-top: 5px;"
  72. "}"
  73. "QGroupBox::title {"
  74. " subcontrol-origin: margin;"
  75. " left: 10px;"
  76. " padding: 0 5px 0 5px;"
  77. "}"
  78. "#totalCountLabel {"
  79. " font-weight: bold;"
  80. " color: #2c3e50;"
  81. " font-size: 14px;"
  82. "}"
  83. "#type0CountLabel, #type1CountLabel, #type2CountLabel {"
  84. " color: #34495e;"
  85. " font-size: 12px;"
  86. "}"
  87. "#lastUpdateLabel {"
  88. " color: #7f8c8d;"
  89. " font-size: 11px;"
  90. "}"
  91. );
  92. }
  93. void StatsWidget::updateStats(const QJsonObject& statsData)
  94. {
  95. qDebug() << "[StatsWidget] 收到统计更新:" << statsData;
  96. // 解析data数组并累加所有用户的统计数据
  97. if (statsData.contains("data") && statsData["data"].isArray()) {
  98. QJsonArray dataArray = statsData["data"].toArray();
  99. // 重置累计数据
  100. m_totalCount = 0;
  101. m_type0Count = 0;
  102. m_type1Count = 0;
  103. m_type2Count = 0;
  104. QDateTime latestUpdate;
  105. // 遍历所有用户数据进行累加
  106. for (const QJsonValue& value : dataArray) {
  107. if (value.isObject()) {
  108. QJsonObject userStats = value.toObject();
  109. // 累加各类型消息数
  110. m_totalCount += userStats["totalCount"].toInt();
  111. m_type0Count += userStats["type0Count"].toInt();
  112. m_type1Count += userStats["type1Count"].toInt();
  113. m_type2Count += userStats["type2Count"].toInt();
  114. // 找到最新的更新时间
  115. QString lastUpdateStr = userStats["lastUpdate"].toString();
  116. QDateTime userUpdate = QDateTime::fromString(lastUpdateStr, Qt::ISODate);
  117. if (userUpdate.isValid() && (!latestUpdate.isValid() || userUpdate > latestUpdate)) {
  118. latestUpdate = userUpdate;
  119. }
  120. }
  121. }
  122. m_lastUpdate = latestUpdate;
  123. // 更新UI显示
  124. m_totalCountLabel->setText(QString("总消息数: %1").arg(m_totalCount));
  125. m_type0CountLabel->setText(QString("类型0: %1").arg(m_type0Count));
  126. m_type1CountLabel->setText(QString("类型1: %1").arg(m_type1Count));
  127. m_type2CountLabel->setText(QString("类型2: %1").arg(m_type2Count));
  128. if (m_lastUpdate.isValid()) {
  129. m_lastUpdateLabel->setText(QString("最后更新: %1")
  130. .arg(m_lastUpdate.toString("yyyy-MM-dd hh:mm:ss")));
  131. } else {
  132. m_lastUpdateLabel->setText("最后更新: --");
  133. }
  134. qDebug() << "[StatsWidget] 全局统计信息已更新 - 总数:" << m_totalCount
  135. << "类型0:" << m_type0Count << "类型1:" << m_type1Count
  136. << "类型2:" << m_type2Count << "用户数:" << dataArray.size();
  137. }
  138. }