From 5891efb41c13eda37296be1bbfec7e903b98c64e Mon Sep 17 00:00:00 2001 From: wangyu Date: Thu, 14 Mar 2024 20:36:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8F=92=E4=BB=B6=E9=87=8C=E5=BB=B6?= =?UTF-8?q?=E8=BF=9F=E8=AF=BB=E5=8F=96=E7=B3=BB=E7=BB=9F=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 插件里延迟读取系统信息 Log: 插件里延迟读取系统信息 --- .../gui/monitor_plugin.cpp | 28 +++++++++++++++---- .../gui/monitor_plugin.h | 4 +++ .../gui/systemmonitortipswidget.cpp | 13 ++++++--- .../gui/systemmonitortipswidget.h | 4 +++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/deepin-system-monitor-plugin/gui/monitor_plugin.cpp b/deepin-system-monitor-plugin/gui/monitor_plugin.cpp index 859c7a3a..2ecf48b2 100644 --- a/deepin-system-monitor-plugin/gui/monitor_plugin.cpp +++ b/deepin-system-monitor-plugin/gui/monitor_plugin.cpp @@ -244,19 +244,23 @@ QIcon MonitorPlugin::icon(const DockPart &dockPart, DGuiApplicationHelper::Color } #endif -void MonitorPlugin::udpateTipsInfo() +void MonitorPlugin::udpateInfo() { // memory qlonglong memory = 0; qlonglong memoryAll = 0; calcMemRate(memory, memoryAll); - m_memStr = QString("%1").arg(memory * 100.0 / memoryAll, 1, 'f', 1, QLatin1Char(' ')); + m_memStr = QString("%1").arg(memory * 100.0 / memoryAll, 1, 'f', 1, QLatin1Char(' ')) + QString("%"); // CPU qlonglong totalCPU = 0; qlonglong availableCPU = 0; calcCpuRate(totalCPU, availableCPU); - m_cpuStr = QString("%1").arg((((totalCPU - m_totalCPU) - (availableCPU - m_availableCPU)) * 100.0 / (totalCPU - m_totalCPU)), 1, 'f', 1, QLatin1Char(' ')); + double cpuPercent = 0.0; + if (totalCPU != m_totalCPU) { + cpuPercent = ((totalCPU - m_totalCPU) - (availableCPU - m_availableCPU)) * 100.0 / (totalCPU - m_totalCPU); + } + m_cpuStr = QString("%1").arg(cpuPercent, 1, 'f', 1, QLatin1Char(' ')) + QString("%"); m_totalCPU = totalCPU; m_availableCPU = availableCPU; @@ -278,7 +282,11 @@ void MonitorPlugin::udpateTipsInfo() m_down = netDownload; m_upload = netUpload; +} +void MonitorPlugin::udpateTipsInfo() +{ + udpateInfo(); m_dataTipsLabel->setSystemMonitorTipsText(QStringList() << m_cpuStr << m_memStr << m_downloadStr << m_uploadStr); } @@ -292,8 +300,18 @@ void MonitorPlugin::loadPlugin() m_dataTipsLabel.reset(new SystemMonitorTipsWidget); m_dataTipsLabel->setObjectName("systemmonitorpluginlabel"); - m_refershTimer->setInterval(2000); - m_refershTimer->start(); + m_refershTimer->setInterval(1000); +// m_refershTimer->start(); + + connect(m_dataTipsLabel.get(),&SystemMonitorTipsWidget::visibleChanged,this,[=](bool visible){ + if (!visible) { + m_refershTimer->stop(); + } else { + udpateInfo(); + m_dataTipsLabel->setSystemMonitorTipsText(QStringList() << "..." << "..." << "..." << "..."); + m_refershTimer->start(); + } + }); m_itemWidget = new MonitorPluginButtonWidget; diff --git a/deepin-system-monitor-plugin/gui/monitor_plugin.h b/deepin-system-monitor-plugin/gui/monitor_plugin.h index fe952c99..e8ad3241 100644 --- a/deepin-system-monitor-plugin/gui/monitor_plugin.h +++ b/deepin-system-monitor-plugin/gui/monitor_plugin.h @@ -191,6 +191,10 @@ class MonitorPlugin : public QObject, PLUGIN_INTERDACE #endif private slots: + //! + //! \brief udpateInfo 更新CPU MEM NET信息 + //! + void udpateInfo(); //! //! \brief udpateTipsInfo 更新CPU MEM NET信息 diff --git a/deepin-system-monitor-plugin/gui/systemmonitortipswidget.cpp b/deepin-system-monitor-plugin/gui/systemmonitortipswidget.cpp index 7ac9f03a..51228638 100644 --- a/deepin-system-monitor-plugin/gui/systemmonitortipswidget.cpp +++ b/deepin-system-monitor-plugin/gui/systemmonitortipswidget.cpp @@ -42,9 +42,9 @@ void SystemMonitorTipsWidget::setSystemMonitorTipsText(QStringList strList) // 设置左侧字符串宽度 if (cpu.length() == 3) { - m_leftWidth = fontMetrics().width(QString(" ") + DApplication::translate("Plugin.cpu", "CPU") + QString(": 0") + cpu + QString("%") + QString(" ")); + m_leftWidth = fontMetrics().width(QString(" ") + DApplication::translate("Plugin.cpu", "CPU") + QString(": 0") + cpu + QString(" ")); } else { - m_leftWidth = fontMetrics().width(QString(" ") + DApplication::translate("Plugin.cpu", "CPU") + QString(": ") + cpu + QString("%") + QString(" ")); + m_leftWidth = fontMetrics().width(QString(" ") + DApplication::translate("Plugin.cpu", "CPU") + QString(": ") + cpu + QString(" ")); } // 左侧宽度预留20个像素 m_leftWidth += 20; @@ -125,7 +125,7 @@ void SystemMonitorTipsWidget::paintEvent(QPaintEvent *event) painter.setPen(QPen(palette().brightText(), 1)); // 绘制CPU文字信息 - painter.drawText(QRectF(leftMargin, 0.0, m_leftWidth, rectHeight / 2.0), QString(" ") + DApplication::translate("Plugin.cpu", "CPU") + QString(": ") + cpu + QString("%"), optionLeft); + painter.drawText(QRectF(leftMargin, 0.0, m_leftWidth, rectHeight / 2.0), QString(" ") + DApplication::translate("Plugin.cpu", "CPU") + QString(": ") + cpu, optionLeft); // 绘制下箭头 @@ -151,7 +151,7 @@ void SystemMonitorTipsWidget::paintEvent(QPaintEvent *event) painter.restore(); // 绘制内存文字信息 - painter.drawText(QRectF(leftMargin, rectHeight / 2.0, m_leftWidth, rectHeight / 2.0), QString(" ") + DApplication::translate("Plugin.mem", "MEM") + QString(": ") + mem + QString("%"), optionLeft); + painter.drawText(QRectF(leftMargin, rectHeight / 2.0, m_leftWidth, rectHeight / 2.0), QString(" ") + DApplication::translate("Plugin.mem", "MEM") + QString(": ") + mem, optionLeft); painter.save(); painter.setPen(QPen(palette().brightText(), 2)); painter.setRenderHints(QPainter::Antialiasing); @@ -177,6 +177,11 @@ bool SystemMonitorTipsWidget::event(QEvent *event) setSystemMonitorTipsText(m_textList); else setSystemMonitorTipsText(QStringList() << "0.0" << "0.0" << "0KB/s" << "0KB/s"); + } else if (event->type() == QEvent::Hide) { + Q_EMIT visibleChanged(false); + } else if (event->type() == QEvent::Show) { + Q_EMIT visibleChanged(true); } + return QFrame::event(event); } diff --git a/deepin-system-monitor-plugin/gui/systemmonitortipswidget.h b/deepin-system-monitor-plugin/gui/systemmonitortipswidget.h index 0cb65bc8..10438c59 100644 --- a/deepin-system-monitor-plugin/gui/systemmonitortipswidget.h +++ b/deepin-system-monitor-plugin/gui/systemmonitortipswidget.h @@ -9,6 +9,7 @@ #include class SystemMonitorTipsWidget : public QFrame { + Q_OBJECT public: explicit SystemMonitorTipsWidget(QWidget *parent = nullptr); @@ -18,6 +19,9 @@ class SystemMonitorTipsWidget : public QFrame //! void setSystemMonitorTipsText(QStringList strList); +Q_SIGNALS: + void visibleChanged(bool visible); + protected: //! //! \brief paintEvent 重绘系统监视器插件提示框