Compare commits
34
Commits
0f70beaab8
...
v2.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0bf771b6b0 | ||
|
|
83f1a2002a | ||
|
|
f50a3fe8e5 | ||
|
|
b27f96b31d | ||
|
|
8a984eac59 | ||
|
|
e75f56a0ba | ||
|
|
4186357380 | ||
|
|
3bec5b156e | ||
|
|
c61542574c | ||
|
|
1c6d31a169 | ||
|
|
f09ef2b424 | ||
|
|
5665e0b522 | ||
|
|
089a11207a | ||
|
|
77e2276e99 | ||
|
|
beb13fc33d | ||
|
|
f4d749daed | ||
|
|
18e433ea06 | ||
|
|
6671bb0bd2 | ||
|
|
e5b2060ff0 | ||
|
|
b0cf2150f4 | ||
|
|
e626b9ce92 | ||
|
|
96c3c055c2 | ||
|
|
1d79dbf261 | ||
|
|
77039d807c | ||
|
|
84e56c547c | ||
|
|
640cc2ab84 | ||
|
|
34e163c6d2 | ||
|
|
da271deea0 | ||
|
|
30a186b6dc | ||
|
|
6ef9a23a24 | ||
|
|
a5cfffc2f1 | ||
|
|
ee885e0dbb | ||
|
|
d18314bdec | ||
|
|
4aca583d2d |
@@ -0,0 +1,64 @@
|
||||
# 更新日志
|
||||
|
||||
## v2.1.0 - 2026-08-09
|
||||
|
||||
v2.0.0 后的修复版本。
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- 修复会话总量显示网卡开机累计值与今日累计不一致:改回本次运行累计(累加差值),重启 dde-shell 清零
|
||||
- 流量统计裁剪上限调整:按日 90 天 -> 30 天,按月 24 个月 -> 12 个月
|
||||
|
||||
## v2.0.0 - 2026-08-09
|
||||
|
||||
本次为大版本更新,涵盖弹窗重新设计、多个新功能窗口、性能优化和架构重构。
|
||||
|
||||
### 新功能
|
||||
|
||||
- **左键弹窗重新设计**:通透分区布局(分隔线+留白),速度数字 24px 视觉锚点,适配 Deepin 原生毛玻璃风格
|
||||
- **IPv4/IPv6 一键复制**:地址旁加复制图标,点击写入剪贴板,复制后显示 ✓ 反馈
|
||||
- **日/月流量统计**(持久化):右键菜单新增"流量统计"窗口,按日/按月展示累计流量,跨重启累加,JSON 持久化到磁盘
|
||||
- **TCP 连接清单**:右键菜单新增"TCP 连接"窗口,展示所有 ESTABLISHED 连接的本地/远程地址端口、状态、进程名,支持搜索筛选
|
||||
- **链路速率显示**:弹窗接口信息区显示协商速率(Gbps/Mbps)
|
||||
- **WiFi 信号强度**:无线接口显示信号 dBm
|
||||
- **包统计/错误/丢包**:弹窗累计流量区展示收发包数、错误数、丢包数
|
||||
- **TCP 连接数**:弹窗显示整机活跃 TCP 连接数
|
||||
- **可配置刷新间隔**:设置窗口新增 1/2/5 秒刷新间隔选项,影响全项目所有定时器
|
||||
- **流量波动图时间窗口**:1/5/30 分钟可切换
|
||||
- **任务栏数值小数点对齐**:双行网速数值小数点垂直对齐,箭头间距固定
|
||||
|
||||
### 优化
|
||||
|
||||
- **弹窗文字颜色修复**:改用 dock 上下文调色板,深色模式文字清晰可见
|
||||
- **弹窗高度固定**:切换网卡时弹窗高度不再跳动
|
||||
- **IPv4/IPv6 地址行对称**:固定宽度标签对齐,不再随地址长度错位
|
||||
- **会话总量语义明确**:改为网卡开机累计值,切换网卡立即显示对应数据
|
||||
- **流量波动图曲线平滑**:5 点滑动平均抑制亚秒混叠尖峰
|
||||
- **C++ 后端性能优化**:正则预编译、信号脏标记、IP 检测降频、历史 QVariantList 缓存
|
||||
- **TCP 进程名反查**:改用 ss 命令(netlink),解决普通用户 /proc 权限限制
|
||||
|
||||
### 重构
|
||||
|
||||
- **提取 WindowTheme + TitleBar**:消除三个独立窗口约 150 行样板重复
|
||||
- **提取 interfaceIcon/interfaceDescription 到 NetCommon**:消除 SettingsWindow 与 NetworkPopup 重复
|
||||
- **清理 fontScale 死代码**:移除为不存在功能预埋的 30+ 处乘法噪声
|
||||
- **CMake Test 改为可选依赖**:解决无 Qt6 Test 开发包环境构建失败
|
||||
|
||||
### Bug 修复
|
||||
|
||||
- 修复关于窗口无法拖动(drag.target 对 Window 无效,改用 startSystemMove)
|
||||
- 修复网卡类型描述国际化翻译 context 错误(迁移到 NetCommon 后未同步)
|
||||
- 修复无 IP 地址时接口信息区塌陷/错位
|
||||
- 修复 IPv6 地址行与 IPv4 行错位
|
||||
- 修复切换网卡时弹窗高度跳动
|
||||
- 修复复制按钮颜色在深色背景下不可见
|
||||
- 修复右键菜单 TCP Connections 国际化缺失
|
||||
- 修复 TextField rightItem 属性不存在导致插件加载失败
|
||||
- 修复 NetworkPopup 大括号多余导致语法错误
|
||||
- 修复 TrafficStatsWindow onVisibleChanged 重复设置导致插件加载失败
|
||||
|
||||
### 其他
|
||||
|
||||
- 右键菜单顺序调整:流量波动图 → 流量统计 → TCP 连接 → 设置 → 关于
|
||||
- 全项目国际化翻译补全(75+ 条翻译)
|
||||
- 任务栏数值右侧留边距
|
||||
+20
-13
@@ -1,12 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(JNetApplet VERSION 1.2.0 LANGUAGES CXX)
|
||||
project(JNetApplet VERSION 2.1.0 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Quick DBus Network LinguistTools Test)
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Quick DBus Network LinguistTools)
|
||||
# Test 仅用于单元测试目标,非主插件依赖;将其设为可选,
|
||||
# 避免未安装 Qt6 Test 开发包的环境构建主插件时失败
|
||||
find_package(Qt6 OPTIONAL_COMPONENTS Test)
|
||||
find_package(Dtk6 REQUIRED COMPONENTS Core)
|
||||
find_package(DDEShell REQUIRED)
|
||||
|
||||
@@ -68,14 +71,17 @@ install(DIRECTORY package/components DESTINATION /usr/share/dde-shell/${PLUGIN_I
|
||||
install(FILES ${QM_FILES} DESTINATION /usr/share/dde-shell/${PLUGIN_ID}/translations)
|
||||
|
||||
# ---- 单元测试 ----
|
||||
# 直接编译源文件到测试可执行文件,避免共享库依赖问题
|
||||
enable_testing()
|
||||
add_executable(tst_networkmonitorapplet
|
||||
# 仅当 Qt6 Test 组件存在时才构建测试目标
|
||||
# 设计原因:Test 为可选组件,未安装 Qt6 Test 开发包的环境仍应能正常构建主插件
|
||||
if(TARGET Qt6::Test)
|
||||
# 直接编译源文件到测试可执行文件,避免共享库依赖问题
|
||||
enable_testing()
|
||||
add_executable(tst_networkmonitorapplet
|
||||
tests/tst_networkmonitorapplet.cpp
|
||||
src/networkmonitorapplet.cpp
|
||||
src/networkmonitorapplet.h
|
||||
)
|
||||
target_link_libraries(tst_networkmonitorapplet PRIVATE
|
||||
)
|
||||
target_link_libraries(tst_networkmonitorapplet PRIVATE
|
||||
Qt6::Test
|
||||
Qt6::Core
|
||||
Qt6::Quick
|
||||
@@ -83,13 +89,14 @@ target_link_libraries(tst_networkmonitorapplet PRIVATE
|
||||
Qt6::Network
|
||||
Dtk6::Core
|
||||
Dde::Shell
|
||||
)
|
||||
target_include_directories(tst_networkmonitorapplet PRIVATE
|
||||
)
|
||||
target_include_directories(tst_networkmonitorapplet PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
/usr/include/dde-shell
|
||||
)
|
||||
target_compile_definitions(tst_networkmonitorapplet PRIVATE
|
||||
)
|
||||
target_compile_definitions(tst_networkmonitorapplet PRIVATE
|
||||
PROJECT_VERSION="${PROJECT_VERSION}"
|
||||
TRANSLATIONS_DIR="${CMAKE_CURRENT_SOURCE_DIR}/translations"
|
||||
)
|
||||
add_test(NAME tst_networkmonitorapplet COMMAND tst_networkmonitorapplet)
|
||||
)
|
||||
add_test(NAME tst_networkmonitorapplet COMMAND tst_networkmonitorapplet)
|
||||
endif()
|
||||
@@ -8,13 +8,21 @@
|
||||
// 深/浅主题由 isDarkMode 切换(networkview.qml 依据 DockPalette 检测任务栏深浅后传入),
|
||||
// 默认浅色,保证组件独立预览时与原版视觉一致
|
||||
// 对外依赖:accentColor、isDarkMode,由 networkview.qml 实例化时传入,触发方式为 show()/raise()/requestActivate()
|
||||
// 公共能力复用:主题色取自 WindowTheme,标题栏/圆角卡片取自 TitleBar,消除三窗口样板重复
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
import QtQuick.Window 2.15
|
||||
import "."
|
||||
|
||||
Window {
|
||||
id: root
|
||||
|
||||
// 公共主题色:集中定义于 WindowTheme.qml,随 isDarkMode 切换深浅
|
||||
WindowTheme {
|
||||
id: theme
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// 对外依赖:关闭按钮 hover 态文字高亮色,由父组件传入
|
||||
// 默认值与 networkview.qml 中 root.accentRed 一致,确保独立可用
|
||||
property color accentColor: Qt.rgba(220 / 255, 38 / 255, 38 / 255, 1)
|
||||
@@ -27,14 +35,6 @@ Window {
|
||||
// 默认 false(浅色)保证组件独立预览时与原版视觉一致
|
||||
property bool isDarkMode: false
|
||||
|
||||
// ---- 主题感知颜色:isDarkMode 为 false 时取值与原浅色硬编码完全一致 ----
|
||||
readonly property color winBg: isDarkMode ? "#202020" : "#FFFFFF"
|
||||
readonly property color cardBg: isDarkMode ? "#2A2A2A" : "#FFFFFF"
|
||||
readonly property color lineColor: isDarkMode ? "#383838" : "#E8E8E8"
|
||||
readonly property color textPrimary: isDarkMode ? "#E0E0E0" : "#333333"
|
||||
readonly property color textSecondary: isDarkMode ? "#AAAAAA" : "#666666"
|
||||
readonly property color textTertiary: isDarkMode ? "#888888" : "#999999"
|
||||
|
||||
// 复制成功状态标记:true 时复制图标变绿并显示"已复制"提示,
|
||||
// 2 秒后由 copyResetTimer 复位
|
||||
property bool copied: false
|
||||
@@ -58,63 +58,22 @@ Window {
|
||||
// 窗口主体:圆角卡片(背景与边框随 isDarkMode 切换深浅),1px 边框模拟 DTK 窗口描边
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: root.winBg
|
||||
color: theme.winBg
|
||||
radius: 12
|
||||
border.width: 1
|
||||
border.color: root.lineColor
|
||||
border.color: theme.lineColor
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: 0
|
||||
|
||||
// 自定义标题栏:左侧"关于"标题 + 右侧圆形关闭按钮,整栏可拖动窗口
|
||||
Rectangle {
|
||||
// 公共标题栏:标题文字 + 圆形关闭按钮,整栏可拖动
|
||||
TitleBar {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 44
|
||||
color: "transparent"
|
||||
|
||||
// 拖动层:声明在关闭按钮之前(位于其下方),避免遮挡按钮点击与 hover
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
drag.target: root
|
||||
}
|
||||
|
||||
Text {
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 16
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
text: qsTr("About")
|
||||
font.pixelSize: 15
|
||||
font.weight: Font.Bold
|
||||
color: root.textPrimary
|
||||
}
|
||||
|
||||
// 关闭按钮:28x28 圆形,hover 时淡红底 + 红色 ×(颜色由 accentColor 决定)
|
||||
Rectangle {
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 12
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 28
|
||||
height: 28
|
||||
radius: 14
|
||||
color: aboutCloseMouse.containsMouse ? Qt.rgba(220 / 255, 38 / 255, 38 / 255, 0.15) : "transparent"
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: "×"
|
||||
font.pixelSize: 20
|
||||
font.weight: Font.Bold
|
||||
color: aboutCloseMouse.containsMouse ? accentColor : root.textSecondary
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: aboutCloseMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: root.hide()
|
||||
}
|
||||
}
|
||||
titleText: qsTr("About")
|
||||
textColor: theme.textPrimary
|
||||
secondaryTextColor: theme.textSecondary
|
||||
closeHoverColor: root.accentColor
|
||||
}
|
||||
|
||||
// 标题区上方间距
|
||||
@@ -133,7 +92,7 @@ Window {
|
||||
text: qsTr("Network Speed Monitor")
|
||||
font.pixelSize: 18
|
||||
font.weight: Font.Bold
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
Layout.fillWidth: true
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
}
|
||||
@@ -141,7 +100,7 @@ Window {
|
||||
Text {
|
||||
text: "JNetApplet"
|
||||
font.pixelSize: 12
|
||||
color: root.textTertiary
|
||||
color: theme.textTertiary
|
||||
Layout.fillWidth: true
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
}
|
||||
@@ -158,7 +117,7 @@ Window {
|
||||
Layout.leftMargin: 24
|
||||
Layout.rightMargin: 24
|
||||
Layout.preferredHeight: 1
|
||||
color: root.lineColor
|
||||
color: theme.lineColor
|
||||
}
|
||||
|
||||
// 信息区上方间距
|
||||
@@ -189,7 +148,7 @@ Window {
|
||||
Text {
|
||||
text: version
|
||||
font.pixelSize: 12
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
}
|
||||
@@ -210,7 +169,7 @@ Window {
|
||||
Text {
|
||||
text: "Jokul"
|
||||
font.pixelSize: 12
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
Layout.fillWidth: true
|
||||
}
|
||||
}
|
||||
@@ -231,7 +190,7 @@ Window {
|
||||
Text {
|
||||
text: qsTr("Monitor network speed and traffic")
|
||||
font.pixelSize: 12
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
Layout.fillWidth: true
|
||||
wrapMode: Text.WordWrap
|
||||
}
|
||||
@@ -253,7 +212,7 @@ Window {
|
||||
Text {
|
||||
text: "git.jokul.space/Jokul/JNetApplet"
|
||||
font.pixelSize: 11
|
||||
color: root.textTertiary
|
||||
color: theme.textTertiary
|
||||
Layout.fillWidth: true
|
||||
wrapMode: Text.WrapAnywhere
|
||||
}
|
||||
@@ -274,7 +233,7 @@ Window {
|
||||
width: copiedHintText.implicitWidth + 10
|
||||
height: 18
|
||||
radius: 4
|
||||
color: root.cardBg
|
||||
color: theme.cardBg
|
||||
border.width: 1
|
||||
border.color: "#22A34A"
|
||||
|
||||
@@ -310,9 +269,9 @@ Window {
|
||||
width: 10
|
||||
height: 10
|
||||
radius: 2
|
||||
color: root.cardBg
|
||||
color: theme.cardBg
|
||||
border.width: 1
|
||||
border.color: copyRepoMouse.containsMouse ? root.textPrimary : root.textTertiary
|
||||
border.color: copyRepoMouse.containsMouse ? theme.textPrimary : theme.textTertiary
|
||||
}
|
||||
|
||||
// 前层矩形(左上),复制成功时边框短暂变绿
|
||||
@@ -322,9 +281,9 @@ Window {
|
||||
width: 10
|
||||
height: 10
|
||||
radius: 2
|
||||
color: root.cardBg
|
||||
color: theme.cardBg
|
||||
border.width: 1
|
||||
border.color: copied ? "#22A34A" : (copyRepoMouse.containsMouse ? root.textPrimary : root.textTertiary)
|
||||
border.color: copied ? "#22A34A" : (copyRepoMouse.containsMouse ? theme.textPrimary : theme.textTertiary)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,4 +124,13 @@ QtObject {
|
||||
var idx = str.indexOf(' ')
|
||||
return idx < 0 ? "" : str.substring(idx + 1)
|
||||
}
|
||||
|
||||
// 格式化链路协商速率(Mbps 输入):>=1000 显示 Gbps(保留 1 位小数),否则显示 Mbps
|
||||
// 设计原因:有线常见 1000/2500 Mbps,无线常见 866 Mbps,按阈值切换单位更易读
|
||||
function formatLinkSpeed(mbps) {
|
||||
if (mbps >= 1000) {
|
||||
return (mbps / 1000).toFixed(1) + " Gbps"
|
||||
}
|
||||
return mbps.toFixed(0) + " Mbps"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
// 由 networkview.qml 的 PanelPopup 实例化,传入 applet 属性
|
||||
// 毛玻璃背景、圆角、阴影由 PanelPopup 自动提供,本组件不自绘背景
|
||||
// 颜色与格式化函数统一取自同目录 NetCommon.qml(common.xxx),数据由 applet 提供
|
||||
// 可配置适配:primaryText 跟随用户 textColor、fontScale 预留字号缩放扩展点
|
||||
// 可配置适配:primaryText 跟随用户 textColor
|
||||
// 设计原因:PanelPopup 是 dock 上下文类型,不能作为独立组件根元素,
|
||||
// 因此本组件用 Control 作为根,由 networkview.qml 的 PanelPopup 包裹
|
||||
|
||||
@@ -43,6 +43,21 @@ Control {
|
||||
readonly property string ipv6Address: applet ? (applet.ipv6Address || "") : ""
|
||||
readonly property var networkInterfaces: applet ? (applet.networkInterfaces || []) : []
|
||||
|
||||
// 活动接口的收发包/错误/丢包累计统计(自开机起),由 C++ 后端持续更新
|
||||
readonly property real rxPackets: applet ? (applet.rxPackets || 0) : 0
|
||||
readonly property real txPackets: applet ? (applet.txPackets || 0) : 0
|
||||
readonly property real rxErrors: applet ? (applet.rxErrors || 0) : 0
|
||||
readonly property real txErrors: applet ? (applet.txErrors || 0) : 0
|
||||
readonly property real rxDropped: applet ? (applet.rxDropped || 0) : 0
|
||||
readonly property real txDropped: applet ? (applet.txDropped || 0) : 0
|
||||
|
||||
// 活动接口的链路协商速率(Mbps),由 C++ 后端低频检测;0 表示不可用
|
||||
readonly property int linkSpeed: applet ? (applet.linkSpeed || 0) : 0
|
||||
// 当前活动接口的 TCP ESTABLISHED 连接数,由 C++ 后端每秒统计
|
||||
readonly property int tcpConnections: applet ? (applet.tcpConnections || 0) : 0
|
||||
// 活动接口的 WiFi 信号强度(dBm,负值),由 C++ 后端低频检测;0 表示不可用
|
||||
readonly property int wifiSignal: applet ? (applet.wifiSignal || 0) : 0
|
||||
|
||||
// 用户自定义字体色:非空时覆盖 primaryText,空串回退 DTK 调色板派生
|
||||
// 仅作用于主要文字(接口名、速度数值、累计数值、chip 未选中文字)
|
||||
// secondaryText/tertiaryText/强调色不跟随,保持系统主题层次不崩
|
||||
@@ -63,12 +78,6 @@ Control {
|
||||
// 上传值颜色:固定绿色,不做高速警示
|
||||
readonly property color uploadValueColor: common.uploadValueColor
|
||||
|
||||
// 字号缩放因子:默认 1.0,后续 C++ 后端新增 applet.fontScale 后自动绑定
|
||||
// 所有字号 = 基准字号 × fontScale,实现全局字号调节
|
||||
// 当前 applet.fontScale 尚不存在,恒为 1.0,视觉无变化
|
||||
readonly property real fontScale: (applet && applet.fontScale !== undefined)
|
||||
? applet.fontScale : 1.0
|
||||
|
||||
// 复制反馈状态:记录刚复制的字段("ipv4"/"ipv6"/""),用于图标切换为 ✓
|
||||
property string copiedField: ""
|
||||
|
||||
@@ -118,14 +127,14 @@ Control {
|
||||
|
||||
Text {
|
||||
text: "⚠"
|
||||
font.pixelSize: Math.round(40 * popup.fontScale)
|
||||
font.pixelSize: 40
|
||||
color: popup.tertiaryText
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
}
|
||||
|
||||
Text {
|
||||
text: qsTr("No network interface detected")
|
||||
font.pixelSize: Math.round(14 * popup.fontScale)
|
||||
font.pixelSize: 14
|
||||
color: popup.secondaryText
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
}
|
||||
@@ -139,7 +148,7 @@ Control {
|
||||
// 内容用 anchors.top 顶部对齐,接口名行始终在顶部同一垂直位置
|
||||
Item {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 60
|
||||
Layout.preferredHeight: 76
|
||||
visible: popup.ready
|
||||
|
||||
ColumnLayout {
|
||||
@@ -156,16 +165,24 @@ Control {
|
||||
// 网卡类型图标:与设置窗口列表图标一致,次要色不抢速度区焦点
|
||||
Text {
|
||||
text: common.interfaceIcon(popup.activeInterface)
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
}
|
||||
|
||||
// 接口名:跟随用户 textColor(primaryText),Spec 5.1 节要求
|
||||
Text {
|
||||
text: popup.activeInterface
|
||||
font.pixelSize: Math.round(13 * popup.fontScale)
|
||||
font.pixelSize: 13
|
||||
color: popup.primaryText
|
||||
}
|
||||
|
||||
// 链路协商速率:speed>=1000 显示 Gbps,否则 Mbps;无速率时隐藏
|
||||
Text {
|
||||
text: popup.linkSpeed > 0 ? "· " + common.formatLinkSpeed(popup.linkSpeed) : ""
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
visible: popup.linkSpeed > 0
|
||||
}
|
||||
}
|
||||
|
||||
// 行2:IPv4 标签 + 地址 + 复制图标
|
||||
@@ -179,7 +196,7 @@ Control {
|
||||
// IPv4 标签:固定宽度,与 IPv6 标签列对齐
|
||||
Text {
|
||||
text: qsTr("IPv4")
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
Layout.preferredWidth: 32
|
||||
horizontalAlignment: Text.AlignLeft
|
||||
@@ -188,7 +205,7 @@ Control {
|
||||
// IPv4 地址:超长截断 + hover tooltip
|
||||
Text {
|
||||
text: popup.ipAddress
|
||||
font.pixelSize: Math.round(13 * popup.fontScale)
|
||||
font.pixelSize: 13
|
||||
color: popup.secondaryText
|
||||
elide: Text.ElideRight
|
||||
Layout.fillWidth: true
|
||||
@@ -210,7 +227,7 @@ Control {
|
||||
// 默认态用 primaryText 保证深色背景上清晰可见,hover 变蓝
|
||||
Text {
|
||||
text: popup.copiedField === "ipv4" ? "✓" : "⧉"
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.copiedField === "ipv4"
|
||||
? popup.accentGreen
|
||||
: (ipv4CopyMouse.containsMouse ? popup.accentBlue : popup.primaryText)
|
||||
@@ -240,7 +257,7 @@ Control {
|
||||
// IPv6 标签:固定宽度,与 IPv4 标签列对齐
|
||||
Text {
|
||||
text: qsTr("IPv6")
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
Layout.preferredWidth: 32
|
||||
horizontalAlignment: Text.AlignLeft
|
||||
@@ -249,7 +266,7 @@ Control {
|
||||
// IPv6 地址:超长截断 + hover tooltip 显示完整地址
|
||||
Text {
|
||||
text: popup.ipv6Address
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
elide: Text.ElideRight
|
||||
Layout.fillWidth: true
|
||||
@@ -271,7 +288,7 @@ Control {
|
||||
// 默认态用 primaryText 保证深色背景上清晰可见,hover 变蓝
|
||||
Text {
|
||||
text: popup.copiedField === "ipv6" ? "✓" : "⧉"
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.copiedField === "ipv6"
|
||||
? popup.accentGreen
|
||||
: (ipv6CopyMouse.containsMouse ? popup.accentBlue : popup.primaryText)
|
||||
@@ -289,6 +306,19 @@ Control {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 行4:WiFi 信号强度(dBm),仅无线接口有信号时显示
|
||||
RowLayout {
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
spacing: 6
|
||||
visible: popup.wifiSignal < 0
|
||||
|
||||
Text {
|
||||
text: qsTr("Signal") + " " + popup.wifiSignal.toFixed(0) + " dBm"
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -322,7 +352,7 @@ Control {
|
||||
// 下载标签
|
||||
Text {
|
||||
text: qsTr("Download")
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.secondaryText
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
}
|
||||
@@ -334,21 +364,21 @@ Control {
|
||||
|
||||
Text {
|
||||
text: "↓"
|
||||
font.pixelSize: Math.round(24 * popup.fontScale)
|
||||
font.pixelSize: 24
|
||||
font.weight: Font.Bold
|
||||
color: common.downloadValueColor(popup.downloadSpeed)
|
||||
}
|
||||
|
||||
Text {
|
||||
text: common.formatSpeedValue(popup.downloadSpeed)
|
||||
font.pixelSize: Math.round(24 * popup.fontScale)
|
||||
font.pixelSize: 24
|
||||
font.weight: Font.Bold
|
||||
color: common.downloadValueColor(popup.downloadSpeed)
|
||||
}
|
||||
|
||||
Text {
|
||||
text: common.formatSpeedUnit(popup.downloadSpeed)
|
||||
font.pixelSize: Math.round(13 * popup.fontScale)
|
||||
font.pixelSize: 13
|
||||
color: common.downloadValueColor(popup.downloadSpeed)
|
||||
Layout.alignment: Qt.AlignBaseline
|
||||
}
|
||||
@@ -371,7 +401,7 @@ Control {
|
||||
// 上传标签
|
||||
Text {
|
||||
text: qsTr("Upload")
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
color: popup.secondaryText
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
}
|
||||
@@ -383,21 +413,21 @@ Control {
|
||||
|
||||
Text {
|
||||
text: "↑"
|
||||
font.pixelSize: Math.round(24 * popup.fontScale)
|
||||
font.pixelSize: 24
|
||||
font.weight: Font.Bold
|
||||
color: popup.uploadValueColor
|
||||
}
|
||||
|
||||
Text {
|
||||
text: common.formatSpeedValue(popup.uploadSpeed)
|
||||
font.pixelSize: Math.round(24 * popup.fontScale)
|
||||
font.pixelSize: 24
|
||||
font.weight: Font.Bold
|
||||
color: popup.uploadValueColor
|
||||
}
|
||||
|
||||
Text {
|
||||
text: common.formatSpeedUnit(popup.uploadSpeed)
|
||||
font.pixelSize: Math.round(13 * popup.fontScale)
|
||||
font.pixelSize: 13
|
||||
color: popup.uploadValueColor
|
||||
Layout.alignment: Qt.AlignBaseline
|
||||
}
|
||||
@@ -424,40 +454,123 @@ Control {
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
Text {
|
||||
text: qsTr("Total")
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
text: qsTr("Session Total")
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
}
|
||||
|
||||
Text {
|
||||
text: "↓"
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: popup.accentBlue
|
||||
}
|
||||
|
||||
Text {
|
||||
text: common.formatTotal(popup.totalDownload)
|
||||
font.pixelSize: Math.round(13 * popup.fontScale)
|
||||
font.pixelSize: 13
|
||||
color: popup.primaryText
|
||||
}
|
||||
|
||||
Text {
|
||||
text: "↑"
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: popup.accentGreen
|
||||
}
|
||||
|
||||
Text {
|
||||
text: common.formatTotal(popup.totalUpload)
|
||||
font.pixelSize: Math.round(13 * popup.fontScale)
|
||||
font.pixelSize: 13
|
||||
color: popup.primaryText
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
}
|
||||
|
||||
// 包统计:收发包 + 错误/丢包(累计值,自开机起),紧接累计流量下方
|
||||
// 布局与流量总计行一致:标签 + ↓蓝粗 + 数值 + ↑绿粗 + 数值,拆分为独立 Text
|
||||
// 设计原因:包统计与流量统计同属"累计数据",放一起信息归属清晰且视觉对称;
|
||||
// 包总数始终显示;错误/丢包仅在存在时显示,避免正常网卡信息过载
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 8
|
||||
visible: popup.ready
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
// 包统计标签(与"流量总计"标签对齐)
|
||||
Text {
|
||||
text: qsTr("Packets")
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
}
|
||||
|
||||
// 接收包数:蓝色下箭头(与流量总计行下载箭头一致)
|
||||
Text {
|
||||
text: "↓"
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: popup.accentBlue
|
||||
}
|
||||
|
||||
Text {
|
||||
text: popup.rxPackets.toFixed(0)
|
||||
font.pixelSize: 13
|
||||
color: popup.primaryText
|
||||
}
|
||||
|
||||
// 发送包数:绿色上箭头(与流量总计行上传箭头一致)
|
||||
Text {
|
||||
text: "↑"
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: popup.accentGreen
|
||||
}
|
||||
|
||||
Text {
|
||||
text: popup.txPackets.toFixed(0)
|
||||
font.pixelSize: 13
|
||||
color: popup.primaryText
|
||||
}
|
||||
|
||||
// 错误数:仅在存在错误或丢包时显示,格式 "错误 {rxE}/{txE}"
|
||||
Text {
|
||||
text: qsTr("Errors") + " " + popup.rxErrors.toFixed(0) + "/" + popup.txErrors.toFixed(0)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
visible: popup.rxErrors > 0 || popup.txErrors > 0 || popup.rxDropped > 0 || popup.txDropped > 0
|
||||
}
|
||||
|
||||
// 丢包数:仅在存在错误或丢包时显示,格式 "丢包 {rxD}/{txD}"
|
||||
Text {
|
||||
text: qsTr("Dropped") + " " + popup.rxDropped.toFixed(0) + "/" + popup.txDropped.toFixed(0)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
visible: popup.rxErrors > 0 || popup.txErrors > 0 || popup.rxDropped > 0 || popup.txDropped > 0
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
}
|
||||
|
||||
// TCP 连接数:仅在连接数>0 时显示,紧跟包统计下方
|
||||
// 设计原因:连接数反映当前网络会话活跃度,是用户关心的即时信息
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 8
|
||||
visible: popup.ready && popup.tcpConnections > 0
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
Text {
|
||||
text: qsTr("TCP Connections") + " " + popup.tcpConnections.toFixed(0)
|
||||
font.pixelSize: 12
|
||||
color: popup.tertiaryText
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
}
|
||||
|
||||
// 分隔线3
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
@@ -566,7 +679,7 @@ Control {
|
||||
id: chipText
|
||||
anchors.centerIn: parent
|
||||
text: modelData
|
||||
font.pixelSize: Math.round(12 * popup.fontScale)
|
||||
font.pixelSize: 12
|
||||
font.weight: modelData === popup.activeInterface ? Font.Bold : Font.Normal
|
||||
color: modelData === popup.activeInterface
|
||||
? "white"
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
// 对外依赖:applet(C++ 后端对象)、networkInterfaces(接口列表)、activeInterface(当前接口)、
|
||||
// accentColor(强调色)、isDarkMode(深色模式标记),由 networkview.qml 实例化时传入,
|
||||
// 触发方式为 show()/raise()/requestActivate()
|
||||
// 公共能力复用:主题色取自 WindowTheme(本窗口 winBg 浅色 #F5F5F5、lineColor 浅色
|
||||
// #E0E0E0 与其他窗口不同,通过覆盖 WindowTheme 的浅色对实现),标题栏/圆角卡片取自 TitleBar
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Controls 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
@@ -22,6 +24,15 @@ Window {
|
||||
// 与 NetworkPopup 共享同一份实现,消除跨组件重复定义
|
||||
NetCommon { id: common }
|
||||
|
||||
// 公共主题色:集中定义于 WindowTheme.qml,随 isDarkMode 切换深浅。
|
||||
// 本窗口 winBg 浅色 #F5F5F5、lineColor 浅色 #E0E0E0 与默认值不同,在此覆盖
|
||||
WindowTheme {
|
||||
id: theme
|
||||
isDarkMode: root.isDarkMode
|
||||
lightWinBg: "#F5F5F5"
|
||||
lightLineColor: "#E0E0E0"
|
||||
}
|
||||
|
||||
// 对外依赖:C++ 后端对象,用于获取/设置接口、字体颜色等
|
||||
property var applet: null
|
||||
|
||||
@@ -39,15 +50,8 @@ Window {
|
||||
// 默认 false(浅色)保证组件独立预览时与原版视觉一致
|
||||
property bool isDarkMode: false
|
||||
|
||||
// ---- 主题感知颜色:isDarkMode 为 false 时取值与原浅色硬编码完全一致 ----
|
||||
readonly property color winBg: isDarkMode ? "#202020" : "#F5F5F5"
|
||||
readonly property color cardBg: isDarkMode ? "#2A2A2A" : "#FFFFFF"
|
||||
readonly property color lineColor: isDarkMode ? "#383838" : "#E0E0E0"
|
||||
readonly property color textPrimary: isDarkMode ? "#E0E0E0" : "#333333"
|
||||
readonly property color textSecondary: isDarkMode ? "#AAAAAA" : "#666666"
|
||||
readonly property color textTertiary: isDarkMode ? "#888888" : "#999999"
|
||||
readonly property color iconGray: isDarkMode ? "#AAAAAA" : "#777777"
|
||||
readonly property color hoverBg: isDarkMode ? "#353535" : "#F0F0F0"
|
||||
// ---- 设置窗口特有颜色(不属于三窗口公共主题)----
|
||||
// 命令显示区底色
|
||||
readonly property color codeBg: isDarkMode ? "#2A2A2A" : "#E8E8E8"
|
||||
// 选中态蓝色系:深色模式下背景压暗、文字改浅蓝,保证深色卡片上的对比度
|
||||
readonly property color selBg: isDarkMode ? "#1A3A5C" : "#E3F2FD"
|
||||
@@ -59,11 +63,11 @@ Window {
|
||||
property string uninstallButtonText: qsTr("Uninstall Plugin")
|
||||
|
||||
width: 350
|
||||
height: 390
|
||||
height: 450
|
||||
minimumWidth: 350
|
||||
maximumWidth: 350
|
||||
minimumHeight: 390
|
||||
maximumHeight: 390
|
||||
minimumHeight: 450
|
||||
maximumHeight: 450
|
||||
visible: false
|
||||
flags: Qt.FramelessWindowHint | Qt.Window
|
||||
// NonModal:不阻塞桌面其他区域,用户可同时操作任务栏
|
||||
@@ -83,76 +87,30 @@ Window {
|
||||
// 主容器:提供不透明背景(随 isDarkMode 切换深浅)+ 圆角 + 边框
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: root.winBg
|
||||
color: theme.winBg
|
||||
radius: 12
|
||||
border.width: 1
|
||||
border.color: root.lineColor
|
||||
border.color: theme.lineColor
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
anchors.margins: 0
|
||||
spacing: 0
|
||||
|
||||
// 自定义标题栏(可拖动)
|
||||
Rectangle {
|
||||
// 公共标题栏:标题文字 + 圆形关闭按钮,整栏可拖动
|
||||
TitleBar {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 44
|
||||
color: "transparent"
|
||||
|
||||
// 拖动层:声明在关闭按钮之前(位于其下方),避免遮挡按钮点击与 hover
|
||||
// 用 startSystemMove() 系统级窗口拖动,由窗口管理器接管移动,
|
||||
// 这是 frameless Window 的正确做法,pressed 即触发,丝滑无抖动
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
cursorShape: Qt.OpenHandCursor
|
||||
onPressed: root.startSystemMove()
|
||||
}
|
||||
|
||||
// 标题文字
|
||||
Text {
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 16
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
text: qsTr("Settings")
|
||||
font.pixelSize: 15
|
||||
font.weight: Font.Bold
|
||||
color: root.textPrimary
|
||||
}
|
||||
|
||||
// 关闭按钮
|
||||
Rectangle {
|
||||
id: closeButton
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 12
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 28
|
||||
height: 28
|
||||
radius: 14
|
||||
color: closeMouse.containsMouse ? Qt.rgba(220/255, 38/255, 38/255, 0.15) : "transparent"
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: "×"
|
||||
font.pixelSize: 20
|
||||
font.weight: Font.Bold
|
||||
color: closeMouse.containsMouse ? accentColor : root.textSecondary
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: closeMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: root.hide()
|
||||
}
|
||||
}
|
||||
titleText: qsTr("Settings")
|
||||
textColor: theme.textPrimary
|
||||
secondaryTextColor: theme.textSecondary
|
||||
closeHoverColor: root.accentColor
|
||||
}
|
||||
|
||||
// 分隔线
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 1
|
||||
color: root.lineColor
|
||||
color: theme.lineColor
|
||||
}
|
||||
|
||||
// 内容区域
|
||||
@@ -170,7 +128,7 @@ Window {
|
||||
text: qsTr("Network Interface")
|
||||
font.pixelSize: 13
|
||||
font.weight: Font.Bold
|
||||
color: root.textSecondary
|
||||
color: theme.textSecondary
|
||||
}
|
||||
|
||||
// 接口列表容器:最多显示约 5 行(160px),超出部分垂直滚动
|
||||
@@ -181,10 +139,10 @@ Window {
|
||||
Layout.preferredHeight: networkInterfaces.length > 0
|
||||
? Math.min(ifaceColumn.implicitHeight, 160) + 20
|
||||
: 80
|
||||
color: root.cardBg
|
||||
color: theme.cardBg
|
||||
radius: 10
|
||||
border.width: 1
|
||||
border.color: root.lineColor
|
||||
border.color: theme.lineColor
|
||||
|
||||
// 用 ScrollView 而非裸 Flickable:自带滚轮滚动支持,
|
||||
// 且滚动条显示时自动让出内容宽度,不会遮挡行内文字
|
||||
@@ -209,8 +167,8 @@ Window {
|
||||
implicitWidth: 4
|
||||
radius: 2
|
||||
// 按压/悬停加深、默认稍淡,保证滚动条在卡片背景上可见
|
||||
color: ifaceScrollBar.pressed ? root.textSecondary
|
||||
: (ifaceScrollBar.hovered ? root.textSecondary : root.textTertiary)
|
||||
color: ifaceScrollBar.pressed ? theme.textSecondary
|
||||
: (ifaceScrollBar.hovered ? theme.textSecondary : theme.textTertiary)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,7 +197,7 @@ Window {
|
||||
// 背景优先级:选中 > hover;选中行 hover 时稍加深以保留交互反馈
|
||||
color: isActive
|
||||
? (hovered ? root.selBgHover : root.selBg)
|
||||
: (hovered ? root.hoverBg : "transparent")
|
||||
: (hovered ? theme.hoverBg : "transparent")
|
||||
// 选中行加细边框增强视觉;未选中保持透明边框占位,避免切换时出现 1px 抖动
|
||||
border.width: 1
|
||||
border.color: isActive ? root.selBorder : "transparent"
|
||||
@@ -267,7 +225,7 @@ Window {
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
text: common.interfaceIcon(modelData)
|
||||
font.pixelSize: 14
|
||||
color: ifaceRow.isActive ? ifaceRow.activeColor : root.iconGray
|
||||
color: ifaceRow.isActive ? ifaceRow.activeColor : theme.iconGray
|
||||
}
|
||||
|
||||
// 自绘单选圆点:替代 RadioButton 作纯视觉指示器,
|
||||
@@ -281,7 +239,7 @@ Window {
|
||||
color: "transparent"
|
||||
border.width: 2
|
||||
border.color: ifaceRow.isActive ? ifaceRow.activeColor
|
||||
: (ifaceRow.hovered ? root.textSecondary : root.textTertiary)
|
||||
: (ifaceRow.hovered ? theme.textSecondary : theme.textTertiary)
|
||||
|
||||
Rectangle {
|
||||
anchors.centerIn: parent
|
||||
@@ -300,7 +258,7 @@ Window {
|
||||
text: modelData
|
||||
font.pixelSize: 13
|
||||
font.weight: ifaceRow.isActive ? Font.Medium : Font.Normal
|
||||
color: ifaceRow.isActive ? ifaceRow.activeColor : root.textPrimary
|
||||
color: ifaceRow.isActive ? ifaceRow.activeColor : theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
|
||||
@@ -309,7 +267,7 @@ Window {
|
||||
Layout.alignment: Qt.AlignVCenter
|
||||
text: common.interfaceDescription(modelData)
|
||||
font.pixelSize: 12
|
||||
color: root.textTertiary
|
||||
color: theme.textTertiary
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -322,7 +280,7 @@ Window {
|
||||
anchors.centerIn: parent
|
||||
text: qsTr("No network interface detected")
|
||||
font.pixelSize: 12
|
||||
color: root.textTertiary
|
||||
color: theme.textTertiary
|
||||
visible: networkInterfaces.length === 0
|
||||
}
|
||||
}
|
||||
@@ -334,7 +292,7 @@ Window {
|
||||
text: qsTr("Font Color")
|
||||
font.pixelSize: 13
|
||||
font.weight: Font.Bold
|
||||
color: root.textSecondary
|
||||
color: theme.textSecondary
|
||||
}
|
||||
|
||||
TextColorPicker {
|
||||
@@ -345,6 +303,56 @@ Window {
|
||||
onColorSelected: if (applet) applet.textColor = color
|
||||
}
|
||||
|
||||
// 刷新间隔选择区:1 秒 / 2 秒 / 5 秒
|
||||
// 设计原因:用户可能觉得每秒刷新开销大或过于频繁,提供更长的间隔选项;
|
||||
// 选择结果通过 applet.refreshInterval 持久化并即时生效,
|
||||
// 速度计算基于真实流逝时间(elapsedSec),改变间隔不影响计算正确性
|
||||
Text {
|
||||
text: qsTr("Refresh Interval")
|
||||
font.pixelSize: 13
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
|
||||
// 三个间隔选项按钮:1 秒 / 2 秒 / 5 秒
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
spacing: 8
|
||||
|
||||
Repeater {
|
||||
model: [1000, 2000, 5000]
|
||||
|
||||
Rectangle {
|
||||
id: intervalOption
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 34
|
||||
radius: 8
|
||||
// 选中态蓝色填充边框,未选中态卡片背景,hover 态加深
|
||||
color: applet && applet.refreshInterval === modelData
|
||||
? root.selBg : (intervalMouse.containsMouse ? theme.hoverBg : theme.cardBg)
|
||||
border.width: 1
|
||||
border.color: applet && applet.refreshInterval === modelData
|
||||
? root.selBorder : theme.lineColor
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: (modelData / 1000) + "s"
|
||||
font.pixelSize: 13
|
||||
color: applet && applet.refreshInterval === modelData
|
||||
? root.selText : theme.textPrimary
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: intervalMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: if (applet) applet.refreshInterval = modelData
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Item { Layout.preferredHeight: 4 }
|
||||
|
||||
// 卸载插件按钮:文字可动态切换(复制命令后显示提示)
|
||||
@@ -405,10 +413,10 @@ Window {
|
||||
y: (root.height - height) / 2
|
||||
|
||||
background: Rectangle {
|
||||
color: root.winBg
|
||||
color: theme.winBg
|
||||
radius: 12
|
||||
border.width: 1
|
||||
border.color: root.lineColor
|
||||
border.color: theme.lineColor
|
||||
}
|
||||
|
||||
contentItem: ColumnLayout {
|
||||
@@ -427,7 +435,7 @@ Window {
|
||||
Text {
|
||||
text: qsTr("The following command uninstalls the plugin and restarts dde-shell. Please copy and run it in terminal:")
|
||||
font.pixelSize: 12
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
Layout.fillWidth: true
|
||||
horizontalAlignment: Text.AlignHCenter
|
||||
@@ -447,7 +455,7 @@ Window {
|
||||
text: "sudo rm -rf /usr/share/dde-shell/space.jokul.JNetApplet/ && systemctl --user restart dde-shell@DDE"
|
||||
font.pixelSize: 10
|
||||
font.family: "monospace"
|
||||
color: root.textSecondary
|
||||
color: theme.textSecondary
|
||||
wrapMode: Text.WordWrap
|
||||
}
|
||||
}
|
||||
@@ -463,16 +471,16 @@ Window {
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 36
|
||||
color: cancelUninstallMouse.containsMouse ? root.hoverBg : root.cardBg
|
||||
color: cancelUninstallMouse.containsMouse ? theme.hoverBg : theme.cardBg
|
||||
radius: 8
|
||||
border.width: 1
|
||||
border.color: root.lineColor
|
||||
border.color: theme.lineColor
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: qsTr("Cancel")
|
||||
font.pixelSize: 13
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
// SPDX-FileCopyrightText: 2026 Jokul
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
// TCP 连接清单窗口:屏幕居中的独立顶层窗口,展示当前所有 ESTABLISHED 状态的
|
||||
// TCP 连接详情(本地地址:端口、远程地址:端口、状态、进程名),数据来自 C++ 后端
|
||||
// 解析 /proc/net/tcp 与 tcp6(含进程名反查)。
|
||||
// 设计要点:
|
||||
// - 视觉风格复用 TrafficStatsWindow.qml(圆角卡片 12px、1px 边框、44px 标题栏、
|
||||
// 28x28 圆形关闭按钮 hover 淡红底);深/浅主题由 isDarkMode 切换
|
||||
// (networkview.qml 依据 DockPalette 检测任务栏深浅后传入),默认浅色保证独立预览可用
|
||||
// - 数据通过属性注入:applet 即 networkview.qml 的 root.applet(C++ 后端对象),
|
||||
// applet.tcpConnectionList 为 QVariantList of QVariantMap,每项含
|
||||
// {localAddress, localPort, remoteAddress, remotePort, state, processName},
|
||||
// 为 null 时组件可独立预览(显示空状态)
|
||||
// - 定时刷新:窗口可见时每 5 秒重建清单(与 C++ 后端降频周期一致),关闭时停止
|
||||
// 触发方式:右键菜单"TCP 连接清单" -> show()/raise()/requestActivate()
|
||||
// 公共能力复用:主题色取自 WindowTheme,标题栏(含关闭按钮)取自 TitleBar
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Controls 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
import QtQuick.Window 2.15
|
||||
import "."
|
||||
|
||||
Window {
|
||||
id: root
|
||||
|
||||
// 公共主题色:集中定义于 WindowTheme.qml,随 isDarkMode 切换深浅
|
||||
WindowTheme {
|
||||
id: theme
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// 对外依赖:关闭按钮 hover 态文字高亮色,由父组件传入
|
||||
// 默认值与 networkview.qml 中 root.accentRed 一致,确保独立可用
|
||||
property color accentColor: Qt.rgba(220 / 255, 38 / 255, 38 / 255, 1)
|
||||
|
||||
// 对外依赖:C++ 后端对象(NetworkMonitorApplet),由 networkview.qml 传入
|
||||
// 提供 tcpConnectionList(QVariantList of QVariantMap);为 null 时可独立预览(显示空状态)
|
||||
property var applet: null
|
||||
|
||||
// 深色模式标记:由 networkview.qml 根据 DockPalette 检测后传入;
|
||||
// 默认 false(浅色)保证组件独立预览时与原版视觉一致
|
||||
property bool isDarkMode: false
|
||||
|
||||
// 表格模型:由 refreshModel() 生成的连接记录数组,每项
|
||||
// {localAddress, localPort, remoteAddress, remotePort, state, processName}
|
||||
property var connModel: []
|
||||
|
||||
// 搜索关键字:非空时筛选匹配本地/远程地址、端口、进程名的连接
|
||||
property string searchText: ""
|
||||
|
||||
// 筛选后的模型:searchText 为空时返回全部,否则按关键字过滤
|
||||
// 匹配字段:localAddress/localPort/remoteAddress/remotePort/processName
|
||||
readonly property var filteredModel: {
|
||||
var list = root.connModel
|
||||
if (root.searchText.length === 0) return list
|
||||
var key = root.searchText.toLowerCase()
|
||||
var result = []
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var c = list[i]
|
||||
if (String(c.localAddress).toLowerCase().indexOf(key) >= 0
|
||||
|| String(c.localPort).indexOf(key) >= 0
|
||||
|| String(c.remoteAddress).toLowerCase().indexOf(key) >= 0
|
||||
|| String(c.remotePort).indexOf(key) >= 0
|
||||
|| String(c.processName).toLowerCase().indexOf(key) >= 0) {
|
||||
result.push(c)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 当前连接总数,底部汇总与顶部状态条共用
|
||||
property int connCount: 0
|
||||
|
||||
// 表格列宽:与表头/行/底部汇总共用同一组固定宽度,保证各列垂直对齐
|
||||
readonly property int colLocal: 180
|
||||
readonly property int colRemote: 180
|
||||
readonly property int colState: 90
|
||||
readonly property int colSpacing: 6
|
||||
|
||||
// 从 applet.tcpConnectionList 重建表格模型
|
||||
// 设计原因:连接清单数量随时变化,每次读取重建数组供 ListView 展示;
|
||||
// applet 为 null 时置空模型,保证独立预览可用
|
||||
function refreshModel() {
|
||||
var list = root.applet ? (root.applet.tcpConnectionList || []) : []
|
||||
root.connModel = list
|
||||
root.connCount = list.length
|
||||
}
|
||||
|
||||
width: 680
|
||||
height: 420
|
||||
visible: false
|
||||
flags: Qt.FramelessWindowHint | Qt.Window
|
||||
modality: Qt.NonModal
|
||||
color: "transparent"
|
||||
|
||||
onVisibleChanged: {
|
||||
if (visible) {
|
||||
// 居中到当前屏幕(任务栏所在屏幕),加 virtualX/virtualY 偏移
|
||||
// 避免多显示器下窗口出现在非预期屏幕
|
||||
x = Screen.virtualX + (Screen.width - width) / 2
|
||||
y = Screen.virtualY + (Screen.height - height) / 2
|
||||
// 窗口显示时立即刷新一次,确保数据最新
|
||||
refreshModel()
|
||||
}
|
||||
}
|
||||
|
||||
// 定时刷新:窗口可见时按刷新间隔的 5 倍重建清单(与 C++ 后端降频周期一致),
|
||||
// 不依赖 C++ 信号;关闭时停止,兼顾实时性与性能
|
||||
Timer {
|
||||
interval: root.applet ? root.applet.refreshInterval : 1000
|
||||
repeat: true
|
||||
running: root.visible
|
||||
onTriggered: refreshModel()
|
||||
}
|
||||
|
||||
// 窗口主体:圆角卡片(背景与边框随 isDarkMode 切换深浅),1px 边框模拟 DTK 窗口描边
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: theme.winBg
|
||||
radius: 12
|
||||
border.width: 1
|
||||
border.color: theme.lineColor
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: 0
|
||||
|
||||
// 公共标题栏:标题文字 + 关闭按钮,整栏可拖动
|
||||
TitleBar {
|
||||
Layout.fillWidth: true
|
||||
titleText: qsTr("TCP Connections")
|
||||
textColor: theme.textPrimary
|
||||
secondaryTextColor: theme.textSecondary
|
||||
closeHoverColor: root.accentColor
|
||||
}
|
||||
|
||||
// 顶部状态条:当前连接数 + 搜索框 + 刷新间隔说明
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 34
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
spacing: 8
|
||||
|
||||
Text {
|
||||
// 动态刷新连接总数,显示筛选后数量/总数
|
||||
text: qsTr("Active connections: ") + root.filteredModel.length + "/" + root.connCount
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Medium
|
||||
color: theme.textPrimary
|
||||
}
|
||||
|
||||
// 搜索框:输入关键字筛选匹配地址/端口/进程名的连接
|
||||
TextField {
|
||||
Layout.preferredWidth: 200
|
||||
placeholderText: qsTr("Search address, port, process...")
|
||||
font.pixelSize: 12
|
||||
color: theme.textPrimary
|
||||
selectByMouse: true
|
||||
text: root.searchText
|
||||
onTextChanged: root.searchText = text
|
||||
}
|
||||
|
||||
// 清除按钮:有输入时显示 ×,点击清空搜索
|
||||
Rectangle {
|
||||
visible: root.searchText.length > 0
|
||||
width: 20; height: 20
|
||||
radius: 10
|
||||
color: clearSearchMouse.containsMouse ? theme.hoverBg : "transparent"
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: "×"
|
||||
font.pixelSize: 14
|
||||
color: clearSearchMouse.containsMouse ? root.accentColor : theme.textTertiary
|
||||
}
|
||||
MouseArea {
|
||||
id: clearSearchMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: root.searchText = ""
|
||||
}
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
Text {
|
||||
text: qsTr("Refreshes every ") + ((root.applet ? root.applet.refreshInterval : 1000) / 1000) + qsTr(" seconds")
|
||||
font.pixelSize: 11
|
||||
color: theme.textTertiary
|
||||
}
|
||||
}
|
||||
|
||||
// 表头行:本地地址:端口 | 远程地址:端口 | 状态 | 进程名
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
Layout.topMargin: 2
|
||||
spacing: root.colSpacing
|
||||
|
||||
Text {
|
||||
Layout.preferredWidth: root.colLocal
|
||||
text: qsTr("Local Address:Port")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colRemote
|
||||
text: qsTr("Remote Address:Port")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colState
|
||||
text: qsTr("State")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.fillWidth: true
|
||||
text: qsTr("Process")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
}
|
||||
|
||||
// 表头与内容间的分隔线
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 1
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
Layout.topMargin: 4
|
||||
color: theme.lineColor
|
||||
}
|
||||
|
||||
// 表格区:ListView 展示每条连接记录,行 hover 高亮
|
||||
ListView {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
Layout.topMargin: 2
|
||||
Layout.bottomMargin: 2
|
||||
clip: true
|
||||
model: root.filteredModel
|
||||
onCountChanged: emptyHint.visible = root.filteredModel.length === 0
|
||||
|
||||
// 空状态提示(当筛选结果为空时显示)
|
||||
Item {
|
||||
id: emptyHint
|
||||
anchors.fill: parent
|
||||
visible: root.filteredModel.length === 0
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: root.searchText.length > 0 ? qsTr("No matching connections") : qsTr("No active TCP connections")
|
||||
font.pixelSize: 13
|
||||
color: theme.textTertiary
|
||||
}
|
||||
}
|
||||
|
||||
delegate: Rectangle {
|
||||
required property int index
|
||||
required property var modelData
|
||||
width: ListView.view.width
|
||||
height: 28
|
||||
radius: 4
|
||||
// 行 hover 高亮:浅色用淡黑、深色用淡白,保证两种卡片背景上均可见
|
||||
color: rowMouse.containsMouse ? theme.hoverBg : "transparent"
|
||||
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
anchors.leftMargin: 8
|
||||
anchors.rightMargin: 8
|
||||
spacing: root.colSpacing
|
||||
|
||||
Text {
|
||||
Layout.preferredWidth: root.colLocal
|
||||
text: modelData.localAddress + ":" + modelData.localPort
|
||||
font.pixelSize: 12
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colRemote
|
||||
text: modelData.remoteAddress + ":" + modelData.remotePort
|
||||
font.pixelSize: 12
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colState
|
||||
text: modelData.state
|
||||
font.pixelSize: 12
|
||||
color: theme.textSecondary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.fillWidth: true
|
||||
// 进程名可能为空(反查失败),显示占位符避免空白行
|
||||
text: modelData.processName.length > 0 ? modelData.processName : "-"
|
||||
font.pixelSize: 12
|
||||
color: modelData.processName.length > 0 ? theme.textPrimary : theme.textTertiary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: rowMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 表头与底部汇总间的分隔线
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 1
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
color: theme.lineColor
|
||||
}
|
||||
|
||||
// 底部汇总行:连接总数
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 34
|
||||
Layout.leftMargin: 24
|
||||
Layout.rightMargin: 16
|
||||
spacing: root.colSpacing
|
||||
|
||||
Text {
|
||||
Layout.fillWidth: true
|
||||
text: qsTr("Total: ") + root.filteredModel.length + "/" + root.connCount + qsTr(" connections")
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: theme.textPrimary
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 数据刷新:C++ 每 5 秒更新并发射 tcpConnectionListChanged,据此重建清单;
|
||||
// 本窗口自身的 Timer 兜底,避免依赖信号时序
|
||||
Connections {
|
||||
target: root.applet
|
||||
function onTcpConnectionListChanged() {
|
||||
refreshModel()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
// SPDX-FileCopyrightText: 2026 Jokul
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
// 独立窗口公共自定义标题栏
|
||||
// 消除 AboutWindow / SettingsWindow / TrafficChartWindow 三个独立顶层窗口之间的
|
||||
// 标题栏样板重复(此前各窗口各持一份"拖动层 + 标题文字 + 圆形关闭按钮"的拷贝)。
|
||||
// 现提取为本组件,各窗口在圆角卡片顶部的 ColumnLayout 中实例化 TitleBar 即可。
|
||||
// 对外能力:
|
||||
// - titleText:标题栏文字
|
||||
// - textColor / secondaryTextColor:标题文字色与关闭按钮默认文字色(取主题色)
|
||||
// - closeHoverColor:关闭按钮 hover 态文字高亮色(accentColor,默认红色)
|
||||
// - 拖动:整栏按住调用 Window.window.startSystemMove() 系统级拖动(frameless
|
||||
// Window 的正确做法,drag.target 对 Window 无效);拖动层声明在按钮下方,
|
||||
// 不遮挡关闭/额外按钮的点击与 hover
|
||||
// - 关闭按钮:28x28 圆形,hover 时淡红底 + 红色 ×,点击 Window.window.hide()
|
||||
// - extraActions:default property,供子窗口在关闭按钮左侧填充额外按钮
|
||||
// (如 TrafficChartWindow 的"置顶"按钮),其逻辑仍保留在子窗口内
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Window 2.15
|
||||
|
||||
Item {
|
||||
id: titleBar
|
||||
|
||||
// 标题栏文字,由子窗口传入(qsTr 文案在子窗口侧)
|
||||
property string titleText: ""
|
||||
|
||||
// 标题文字颜色:取主题色 textPrimary
|
||||
property color textColor: "#333333"
|
||||
|
||||
// 关闭按钮默认文字色:取主题色 textSecondary
|
||||
property color secondaryTextColor: "#666666"
|
||||
|
||||
// 关闭按钮 hover 态文字高亮色:取子窗口 accentColor(默认红色)
|
||||
property color closeHoverColor: Qt.rgba(220 / 255, 38 / 255, 38 / 255, 1)
|
||||
|
||||
// 额外右侧按钮容器:default property 让子窗口直接把按钮写进标题栏,
|
||||
// 按钮将布局在关闭按钮左侧(用于置顶按钮等)
|
||||
default property alias extraActions: actionHost.data
|
||||
|
||||
implicitHeight: 44
|
||||
|
||||
// 拖动层:声明在关闭/额外按钮之前(位于其下方),避免遮挡按钮点击与 hover
|
||||
// 用 startSystemMove() 系统级窗口拖动,由窗口管理器接管移动,
|
||||
// 这是 frameless Window 的正确做法,pressed 即触发,丝滑无抖动
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
cursorShape: Qt.OpenHandCursor
|
||||
onPressed: Window.window.startSystemMove()
|
||||
}
|
||||
|
||||
// 标题文字
|
||||
Text {
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 16
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
text: titleBar.titleText
|
||||
font.pixelSize: 15
|
||||
font.weight: Font.Bold
|
||||
color: titleBar.textColor
|
||||
}
|
||||
|
||||
// 额外右侧按钮容器:右缘贴关闭按钮左侧,高度与标题栏一致
|
||||
Item {
|
||||
id: actionHost
|
||||
anchors.right: closeButton.left
|
||||
anchors.rightMargin: 4
|
||||
anchors.top: parent.top
|
||||
anchors.bottom: parent.bottom
|
||||
}
|
||||
|
||||
// 关闭按钮:28x28 圆形,hover 时淡红底 + 红色 ×(颜色由 closeHoverColor 决定)
|
||||
Rectangle {
|
||||
id: closeButton
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 12
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 28
|
||||
height: 28
|
||||
radius: 14
|
||||
color: closeMouse.containsMouse ? Qt.rgba(220 / 255, 38 / 255, 38 / 255, 0.15) : "transparent"
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: "×"
|
||||
font.pixelSize: 20
|
||||
font.weight: Font.Bold
|
||||
color: closeMouse.containsMouse ? titleBar.closeHoverColor : titleBar.secondaryTextColor
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: closeMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: Window.window.hide()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
// 流量波动图窗口:屏幕居中的独立顶层窗口,展示当前活动接口最近 5 分钟的
|
||||
// 网速趋势(下载/上传双折线图),随 C++ 后端 speedHistoryChanged 信号每秒动态刷新
|
||||
// 流量波动图窗口:屏幕居中的独立顶层窗口,展示当前活动接口最近 1/5/30 分钟(可切换)
|
||||
// 的网速趋势(下载/上传双折线图),随 C++ 后端 speedHistoryChanged 信号每秒动态刷新
|
||||
// 设计要点:
|
||||
// - 视觉风格复用 AboutWindow.qml(圆角卡片 12px、1px 边框、44px 标题栏、
|
||||
// 28x28 圆形关闭按钮 hover 淡红底);深/浅主题由 isDarkMode 切换
|
||||
@@ -13,6 +13,8 @@
|
||||
// - 数据通过属性注入:applet 即 networkview.qml 的 root.applet(C++ 后端对象),
|
||||
// 为 null 时组件可独立预览(Canvas 显示"暂无历史数据"空状态)
|
||||
// 触发方式:右键菜单"流量波动图" -> show()/raise()/requestActivate()
|
||||
// 公共能力复用:主题色取自 WindowTheme,标题栏(含置顶/关闭按钮)取自 TitleBar,
|
||||
// 消除三窗口样板重复
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
import QtQuick.Window 2.15
|
||||
@@ -25,6 +27,12 @@ Window {
|
||||
// 与 networkview.qml、NetworkPopup 共享同一份实现,消除跨组件重复定义
|
||||
NetCommon { id: common }
|
||||
|
||||
// 公共主题色:集中定义于 WindowTheme.qml,随 isDarkMode 切换深浅
|
||||
WindowTheme {
|
||||
id: theme
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// 对外依赖:关闭按钮 hover 态文字高亮色,由父组件传入
|
||||
// 默认值与 networkview.qml 中 root.accentRed 一致,确保独立可用
|
||||
property color accentColor: Qt.rgba(220 / 255, 38 / 255, 38 / 255, 1)
|
||||
@@ -39,16 +47,10 @@ Window {
|
||||
// 默认 false(浅色)保证组件独立预览时与原版视觉一致
|
||||
property bool isDarkMode: false
|
||||
|
||||
// ---- 主题感知颜色:isDarkMode 为 false 时取值与原浅色硬编码完全一致 ----
|
||||
readonly property color winBg: isDarkMode ? "#202020" : "#FFFFFF"
|
||||
readonly property color lineColor: isDarkMode ? "#383838" : "#E8E8E8"
|
||||
readonly property color textPrimary: isDarkMode ? "#E0E0E0" : "#333333"
|
||||
readonly property color textSecondary: isDarkMode ? "#AAAAAA" : "#666666"
|
||||
readonly property color textTertiary: isDarkMode ? "#888888" : "#999999"
|
||||
// 置顶按钮 hover 底色:浅色用淡黑、深色用淡白,保证两种卡片背景上均可见
|
||||
// 置顶按钮 hover 底色(本窗口特有):浅色用淡黑、深色用淡白,保证两种卡片背景上均可见
|
||||
readonly property color pinHoverBg: isDarkMode ? Qt.rgba(1, 1, 1, 0.10) : Qt.rgba(0, 0, 0, 0.06)
|
||||
|
||||
// 图表绘制色:声明为 string 供 Canvas 2D 上下文直接使用
|
||||
// 图表绘制色(本窗口特有):声明为 string 供 Canvas 2D 上下文直接使用
|
||||
//(QML color 类型含 alpha 时序列化为 #AARRGGBB,Canvas 无法正确解析,故不用 color 类型)
|
||||
readonly property string gridBaselineColor: isDarkMode ? "#383838" : "#DDDDDD"
|
||||
readonly property string gridLineColor: isDarkMode ? "#2A2A2A" : "#E5E5E5"
|
||||
@@ -65,6 +67,31 @@ Window {
|
||||
// 格式:"14:23:05 ↓856.00 KB/s ↑120.00 KB/s"
|
||||
property string hoverHint: ""
|
||||
|
||||
// 时间窗口选择(秒):60=1分钟, 300=5分钟, 1800=30分钟
|
||||
// 纯 QML 展示层选择——后端已存够 30 分钟历史(MAX_HISTORY_SAMPLES=1800),
|
||||
// 这里仅决定 X 轴绘制范围与标签,不改变后端采集行为
|
||||
property int timeWindowSec: 300
|
||||
|
||||
// 时间窗口选项列表:由 (label, seconds) 键值对组成,供顶部状态条选择器遍历
|
||||
// 及绘制逻辑按 timeWindowSec 反查标签/刻度使用
|
||||
property var timeWindows: [
|
||||
{ label: "1m", seconds: 60 },
|
||||
{ label: "5m", seconds: 300 },
|
||||
{ label: "30m", seconds: 1800 }
|
||||
]
|
||||
|
||||
// 依据当前 timeWindowSec 反查 X 轴刻度标签数组:
|
||||
// 设计原因:不同时间窗口下刻度粒度不同,1 分钟用 15 秒间隔、5 分钟用 1 分钟间隔、
|
||||
// 30 分钟用 5 分钟间隔,保证标签数量适中(5~7 个)且刻度易读
|
||||
readonly property var xAxisLabels: {
|
||||
if (timeWindowSec <= 60) return ["-1m", "-45s", "-30s", "-15s", "now"]
|
||||
if (timeWindowSec <= 300) return ["-5m", "-4m", "-3m", "-2m", "-1m", "now"]
|
||||
return ["-30m", "-25m", "-20m", "-15m", "-10m", "-5m", "now"]
|
||||
}
|
||||
|
||||
// 顶部状态条右侧的时间窗口标签文本(如 "· 5min"),随 timeWindowSec 动态显示
|
||||
readonly property string timeWindowLabel: "· " + (timeWindowSec / 60) + "min"
|
||||
|
||||
// 窗口置顶状态:true 时 flags 附加 Qt.WindowStaysOnTopHint,窗口保持在
|
||||
// 所有非置顶窗口之上(便于边下载大文件边观察曲线);由标题栏图钉按钮切换
|
||||
property bool pinned: false
|
||||
@@ -148,47 +175,29 @@ Window {
|
||||
// 窗口主体:圆角卡片(背景与边框随 isDarkMode 切换深浅),1px 边框模拟 DTK 窗口描边(与 AboutWindow 一致)
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: root.winBg
|
||||
color: theme.winBg
|
||||
radius: 12
|
||||
border.width: 1
|
||||
border.color: root.lineColor
|
||||
border.color: theme.lineColor
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: 0
|
||||
|
||||
// 自定义标题栏:左侧"流量波动图"标题 + 右侧置顶/关闭按钮,整栏可拖动窗口
|
||||
Rectangle {
|
||||
// 公共标题栏:标题文字 + 置顶/关闭按钮,整栏可拖动。
|
||||
// 置顶按钮通过 TitleBar 的额外按钮槽(extraActions)注入,逻辑仍在本窗口
|
||||
TitleBar {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 44
|
||||
color: "transparent"
|
||||
|
||||
// 拖动层:声明在关闭/置顶按钮之前(位于其下方),避免遮挡按钮点击与 hover
|
||||
// 注意:drag.target 只对 Item 生效,对 Window 无效(原实现拖动不工作);
|
||||
// 改用 startSystemMove() 系统级窗口拖动,这是 frameless Window 的正确做法,
|
||||
// pressed 即触发,由窗口管理器接管移动,不干扰按钮的 hover/click
|
||||
MouseArea {
|
||||
anchors.fill: parent
|
||||
cursorShape: Qt.OpenHandCursor
|
||||
onPressed: root.startSystemMove()
|
||||
}
|
||||
|
||||
Text {
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 16
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
text: qsTr("Traffic Chart")
|
||||
font.pixelSize: 15
|
||||
font.weight: Font.Bold
|
||||
color: root.textPrimary
|
||||
}
|
||||
titleText: qsTr("Traffic Chart")
|
||||
textColor: theme.textPrimary
|
||||
secondaryTextColor: theme.textSecondary
|
||||
closeHoverColor: root.accentColor
|
||||
|
||||
// 置顶按钮:28x28 圆形(与关闭按钮一致),位于关闭按钮左侧
|
||||
// 三态视觉:未置顶=灰色图钉轮廓;置顶=deepin 蓝填充 + 淡蓝底;
|
||||
// hover=淡灰底(置顶时淡蓝底优先)
|
||||
Rectangle {
|
||||
anchors.right: chartCloseButton.left
|
||||
anchors.rightMargin: 4
|
||||
anchors.right: parent.right
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 28
|
||||
height: 28
|
||||
@@ -256,34 +265,6 @@ Window {
|
||||
onClicked: root.pinned = !root.pinned
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭按钮:28x28 圆形,hover 时淡红底 + 红色 ×(颜色由 accentColor 决定)
|
||||
Rectangle {
|
||||
id: chartCloseButton
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 12
|
||||
anchors.verticalCenter: parent.verticalCenter
|
||||
width: 28
|
||||
height: 28
|
||||
radius: 14
|
||||
color: chartCloseMouse.containsMouse ? Qt.rgba(220 / 255, 38 / 255, 38 / 255, 0.15) : "transparent"
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: "×"
|
||||
font.pixelSize: 20
|
||||
font.weight: Font.Bold
|
||||
color: chartCloseMouse.containsMouse ? root.accentColor : root.textSecondary
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: chartCloseMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: root.hide()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 顶部状态条:实时下行/上行速度(箭头颜色与折线一致,兼作图例)
|
||||
@@ -306,7 +287,7 @@ Window {
|
||||
// applet 为空时兜底为 0,保证独立预览不报错
|
||||
text: common.formatSpeed(root.applet ? root.applet.downloadSpeed : 0)
|
||||
font.pixelSize: 12
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
}
|
||||
|
||||
Item { Layout.preferredWidth: 10 }
|
||||
@@ -321,16 +302,62 @@ Window {
|
||||
Text {
|
||||
text: common.formatSpeed(root.applet ? root.applet.uploadSpeed : 0)
|
||||
font.pixelSize: 12
|
||||
color: root.textPrimary
|
||||
color: theme.textPrimary
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
// 时间窗口选择器:三个小按钮 1m / 5m / 30m,当前选中项用 accentBlue 高亮
|
||||
// 设计原因:时间窗口切换是图表最常用的操作,直接置于顶部状态条便于快速切换;
|
||||
// 用比接口 chip 更小的尺寸(22x18)避免挤占状态条空间
|
||||
Repeater {
|
||||
model: root.timeWindows
|
||||
|
||||
Rectangle {
|
||||
required property var modelData
|
||||
property bool isSelected: root.timeWindowSec === modelData.seconds
|
||||
|
||||
width: 24
|
||||
height: 18
|
||||
radius: 4
|
||||
// 选中态:蓝色填充+白字;未选中态:透明背景+浅边框,hover 边框变蓝
|
||||
color: isSelected ? common.accentBlue : "transparent"
|
||||
border.width: 1
|
||||
border.color: isSelected
|
||||
? common.accentBlue
|
||||
: (mouse.containsMouse ? common.accentBlue : theme.lineColor)
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: modelData.label
|
||||
font.pixelSize: 10
|
||||
font.weight: isSelected ? Font.Bold : Font.Normal
|
||||
color: isSelected ? "white" : (mouse.containsMouse ? common.accentBlue : theme.textSecondary)
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: mouse
|
||||
anchors.fill: parent
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
hoverEnabled: true
|
||||
onClicked: {
|
||||
if (root.timeWindowSec !== modelData.seconds) {
|
||||
root.timeWindowSec = modelData.seconds
|
||||
// 时间窗口变化后:清空 Hover(坐标系已变),并重绘图表
|
||||
root.hoverIndex = -1
|
||||
root.hoverHint = ""
|
||||
canvas.requestPaint()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Text {
|
||||
text: (root.applet && root.applet.activeInterface
|
||||
? root.applet.activeInterface : "—") + " · 5min"
|
||||
? root.applet.activeInterface : "—") + " " + root.timeWindowLabel
|
||||
font.pixelSize: 12
|
||||
color: root.textTertiary
|
||||
color: theme.textTertiary
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,11 +393,12 @@ Window {
|
||||
var n = dl.length
|
||||
|
||||
// ---- Y 轴上限计算(动态适应最近活动)----
|
||||
// 使用最近 60 秒的数据计算 Y 轴上限,而非全量 5 分钟缓冲。
|
||||
// 使用最近 60 秒的数据计算 Y 轴上限,而非全量历史缓冲。
|
||||
// 设计原因:若使用全量缓冲,当历史中存在大流量峰值(如 12MB/s 下载)时,
|
||||
// 即使当前网速已降至 KB/s 级别,Y 轴仍保持高位,导致当前曲线被压到底部
|
||||
// 无法观察波动。改用 60 秒窗口后,峰值滑出窗口时 Y 轴自动缩小,
|
||||
// 始终为当前活动提供合适的显示比例。
|
||||
// 始终为当前活动提供合适的显示比例。30 分钟视图下 60 秒窗口仍合理
|
||||
//(关注近期活动),故不随 timeWindowSec 变化。
|
||||
var vMax = 0
|
||||
var i
|
||||
var recentWindow = 60 // 秒,Y 轴基于最近 1 分钟的活动计算
|
||||
@@ -404,10 +432,11 @@ Window {
|
||||
ctx.fillText(root.formatAxisValue(yMax * g / 4, unit), pl - 8, gy)
|
||||
}
|
||||
|
||||
// ---- X 轴刻度:每分钟一个标签,均布于绘图区 ----
|
||||
// 5 分钟窗口用 -5m/-4m/-3m/-2m/-1m/now 共 6 个标签,比 4 个三分点
|
||||
// 的 -3m20s/-1m40s 更易读;循环按数组长度参数化,两端左/右对齐
|
||||
var xLabels = ["-5m", "-4m", "-3m", "-2m", "-1m", "now"]
|
||||
// ---- X 轴刻度:按时间窗口参数化,均布于绘图区 ----
|
||||
// 标签数组由 root.xAxisLabels 依据 timeWindowSec 动态生成
|
||||
// (1 分钟用 15 秒间隔 5 个标签,5 分钟用 1 分钟间隔 6 个,
|
||||
// 30 分钟用 5 分钟间隔 7 个);循环按数组长度参数化,两端左/右对齐
|
||||
var xLabels = root.xAxisLabels
|
||||
ctx.fillStyle = root.axisTextColor
|
||||
ctx.textBaseline = "alphabetic"
|
||||
for (i = 0; i < xLabels.length; i++) {
|
||||
@@ -428,15 +457,16 @@ Window {
|
||||
}
|
||||
|
||||
// ---- 折线绘制 ----
|
||||
// X 轴固定为 5 分钟窗口 [tMax-300, tMax]:右缘始终对齐"now",
|
||||
// 数据不足 5 分钟时曲线从右侧向左生长,左侧留空网格。
|
||||
// X 轴为可配置窗口 [tMax-timeWindowSec, tMax]:右缘始终对齐"now",
|
||||
// 数据不足时间窗口时曲线从右侧向左生长,左侧留空网格。
|
||||
// 设计原因:若按实际数据范围 [dl[0].x, tMax] 映射,刚启动时仅有
|
||||
// 几秒数据会被拉伸到整个绘图宽度,X 轴标签 -5m/-4m/... 与实际范围
|
||||
// 严重不符,用户看到的是"几秒趋势图"而非"5 分钟趋势图"。
|
||||
// 300 = 5 分钟 × 60 秒,与 C++ MAX_HISTORY_SAMPLES 一致
|
||||
// 几秒数据会被拉伸到整个绘图宽度,X 轴标签与实际范围严重不符,
|
||||
// 用户看到的是"几秒趋势图"而非"时间窗口趋势图"。
|
||||
// tRange 取 timeWindowSec(60/300/1800),对应 1/5/30 分钟窗口,
|
||||
// 与 C++ MAX_HISTORY_SAMPLES(1800)保证 30 分钟窗口下数据充足
|
||||
var tMax = dl[n - 1].x
|
||||
var tMin = tMax - 300
|
||||
var tRange = 300
|
||||
var tRange = root.timeWindowSec
|
||||
var tMin = tMax - tRange
|
||||
|
||||
// 单点边界:只有 1 个采样点时无法连线,画一个圆点。
|
||||
// 固定窗口下单点对齐 now(右缘),而非居中
|
||||
@@ -531,7 +561,7 @@ Window {
|
||||
|
||||
// Hover 交互层:横向移动时按 mouseX 反查最近采样点
|
||||
// 设计原因:采样点按时间均匀分布(1Hz),先由 X 坐标线性反推时间戳,
|
||||
// 再线性扫描找最近点;300 点上限下扫描开销可忽略,无需二分
|
||||
// 再线性扫描找最近点;1800 点上限下扫描开销仍可忽略,无需二分
|
||||
MouseArea {
|
||||
id: chartHover
|
||||
anchors.fill: parent
|
||||
@@ -545,11 +575,11 @@ Window {
|
||||
var pl = root.chartMarginLeft
|
||||
var pw = width - pl - root.chartMarginRight
|
||||
if (pw <= 0) return
|
||||
// X 轴用与绘制一致的固定 5 分钟窗口 [tMax-300, tMax] 反推时间戳,
|
||||
// X 轴用与绘制一致的可配置窗口 [tMax-timeWindowSec, tMax] 反推时间戳,
|
||||
// 保证 Hover 辅助线与折线上的采样点严格对齐
|
||||
var tMax = dl[n - 1].x
|
||||
var tMin = tMax - 300
|
||||
var tRange = 300
|
||||
var tRange = root.timeWindowSec
|
||||
var tMin = tMax - tRange
|
||||
// 将 mouseX 限制在绘图区内,再线性反推时间戳
|
||||
var clampedX = Math.max(pl, Math.min(mouse.x, pl + pw))
|
||||
var target = tMin + (clampedX - pl) / pw * tRange
|
||||
@@ -592,7 +622,7 @@ Window {
|
||||
text: root.hoverHint.length > 0
|
||||
? root.hoverHint : qsTr("Hover over the curve for details")
|
||||
font.pixelSize: 11
|
||||
color: root.hoverHint.length > 0 ? root.textPrimary : root.textTertiary
|
||||
color: root.hoverHint.length > 0 ? theme.textPrimary : theme.textTertiary
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,424 @@
|
||||
// SPDX-FileCopyrightText: 2026 Jokul
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
// 流量统计窗口:屏幕居中的独立顶层窗口,展示按日/按月持久化的累计流量
|
||||
// (下载/上传/总计),数据来自 C++ 后端持久化的 traffic_log.json,
|
||||
// 跨重启累加,让用户了解长期流量趋势。
|
||||
// 设计要点:
|
||||
// - 视觉风格复用 TrafficChartWindow.qml(圆角卡片 12px、1px 边框、44px 标题栏、
|
||||
// 28x28 圆形关闭按钮 hover 淡红底);深/浅主题由 isDarkMode 切换
|
||||
// (networkview.qml 依据 DockPalette 检测任务栏深浅后传入),默认浅色保证独立预览可用
|
||||
// - 数据通过属性注入:applet 即 networkview.qml 的 root.applet(C++ 后端对象),
|
||||
// applet.trafficLog 为 JSON 对象 {"byDay": {日期: {接口: {rx, tx}}},
|
||||
// "byMonth": {月份: {接口: {rx, tx}}}},为 null 时组件可独立预览(显示空状态)
|
||||
// - 顶部 tab 切换按日/按月;表格按日期/月份降序(最新在上)展示,每行一个
|
||||
// (日期, 接口)记录;底部汇总所有显示记录的总下载/总上传/总流量
|
||||
// - 数据刷新:C++ 每 30 秒保存并发射 trafficLogChanged,本窗口据此重建表格
|
||||
// 触发方式:右键菜单"流量统计" -> show()/raise()/requestActivate()
|
||||
// 公共能力复用:主题色取自 WindowTheme,标题栏(含关闭按钮)取自 TitleBar,
|
||||
// 格式化函数取自 NetCommon.formatTotal
|
||||
import QtQuick 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
import QtQuick.Window 2.15
|
||||
import "."
|
||||
|
||||
Window {
|
||||
id: root
|
||||
|
||||
// 公共格式化函数:集中定义于同目录 NetCommon.qml,
|
||||
// 与 networkview.qml、TrafficChartWindow 共享同一份实现
|
||||
NetCommon { id: common }
|
||||
|
||||
// 公共主题色:集中定义于 WindowTheme.qml,随 isDarkMode 切换深浅
|
||||
WindowTheme {
|
||||
id: theme
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// 对外依赖:关闭按钮 hover 态文字高亮色,由父组件传入
|
||||
// 默认值与 networkview.qml 中 root.accentRed 一致,确保独立可用
|
||||
property color accentColor: Qt.rgba(220 / 255, 38 / 255, 38 / 255, 1)
|
||||
|
||||
// 对外依赖:C++ 后端对象(NetworkMonitorApplet),由 networkview.qml 传入
|
||||
// 提供 trafficLog(JSON 对象,见文件头注释);为 null 时组件可独立预览(显示空状态)
|
||||
property var applet: null
|
||||
|
||||
// 深色模式标记:由 networkview.qml 根据 DockPalette 检测后传入;
|
||||
// 默认 false(浅色)保证组件独立预览时与原版视觉一致
|
||||
property bool isDarkMode: false
|
||||
|
||||
// 当前 tab:0=按日, 1=按月,切换后重建表格模型
|
||||
property int currentTab: 0
|
||||
|
||||
// 表格模型:由 buildStatsModel() 生成的扁平记录数组,每项
|
||||
// {date, iface, rx, tx, total},按日期/月份降序(最新在上)排列
|
||||
property var statsModel: []
|
||||
|
||||
// 底部汇总:所有显示记录的 rx/tx/total 累加值
|
||||
property var totals: ({ rx: 0, tx: 0, total: 0 })
|
||||
|
||||
// 表格列宽:与表头/行/底部汇总共用同一组固定宽度,保证各列垂直对齐
|
||||
readonly property int colDate: 118
|
||||
readonly property int colIface: 108
|
||||
readonly property int colNum: 102
|
||||
readonly property int colSpacing: 6
|
||||
|
||||
// 依据 currentTab 与 applet.trafficLog 重建表格模型
|
||||
// 设计原因:trafficLog 为 {日期/月份: {接口: {rx, tx}}} 的嵌套对象,
|
||||
// 需展平为每行一个(键, 接口)记录并按日期降序排序,才能直接供 ListView 展示;
|
||||
// 同时累加所有记录得到底部汇总
|
||||
function buildStatsModel() {
|
||||
var log = root.applet ? root.applet.trafficLog : {}
|
||||
var key = root.currentTab === 0 ? "byDay" : "byMonth"
|
||||
var entries = (log && log[key]) || {}
|
||||
var rows = []
|
||||
var tRx = 0, tTx = 0
|
||||
for (var d in entries) {
|
||||
var ifaces = entries[d]
|
||||
for (var i in ifaces) {
|
||||
var rec = ifaces[i]
|
||||
var rx = rec ? (rec.rx || 0) : 0
|
||||
var tx = rec ? (rec.tx || 0) : 0
|
||||
rows.push({ date: d, iface: i, rx: rx, tx: tx, total: rx + tx })
|
||||
tRx += rx
|
||||
tTx += tx
|
||||
}
|
||||
}
|
||||
// 按日期/月份降序(最新在上),同键内按接口名升序
|
||||
rows.sort(function (a, b) {
|
||||
if (a.date !== b.date) return a.date < b.date ? 1 : -1
|
||||
return a.iface < b.iface ? -1 : 1
|
||||
})
|
||||
root.statsModel = rows
|
||||
root.totals = { rx: tRx, tx: tTx, total: tRx + tTx }
|
||||
}
|
||||
|
||||
width: 620
|
||||
height: 440
|
||||
visible: false
|
||||
flags: Qt.FramelessWindowHint | Qt.Window
|
||||
modality: Qt.NonModal
|
||||
color: "transparent"
|
||||
|
||||
onVisibleChanged: {
|
||||
if (visible) {
|
||||
// 居中到当前屏幕(任务栏所在屏幕),加 virtualX/virtualY 偏移
|
||||
// 避免多显示器下窗口出现在非预期屏幕
|
||||
x = Screen.virtualX + (Screen.width - width) / 2
|
||||
y = Screen.virtualY + (Screen.height - height) / 2
|
||||
// 窗口显示时立即刷新一次,确保数据最新
|
||||
buildStatsModel()
|
||||
}
|
||||
}
|
||||
|
||||
// 切换 tab 或首次创建时重建表格模型
|
||||
onCurrentTabChanged: buildStatsModel()
|
||||
Component.onCompleted: buildStatsModel()
|
||||
|
||||
// 定时刷新:窗口可见时按刷新间隔的 5 倍重建表格,不依赖 C++ 30 秒信号
|
||||
// 设计原因:C++ 降频 30 秒通知,用户打开窗口期间看不到实时更新;
|
||||
// 窗口可见时主动刷新,间隔跟随设置中的刷新间隔(5 倍降频),关闭时停止
|
||||
Timer {
|
||||
interval: root.applet ? root.applet.refreshInterval : 1000
|
||||
repeat: true
|
||||
running: root.visible
|
||||
onTriggered: buildStatsModel()
|
||||
}
|
||||
|
||||
// 窗口主体:圆角卡片(背景与边框随 isDarkMode 切换深浅),1px 边框模拟 DTK 窗口描边
|
||||
Rectangle {
|
||||
anchors.fill: parent
|
||||
color: theme.winBg
|
||||
radius: 12
|
||||
border.width: 1
|
||||
border.color: theme.lineColor
|
||||
|
||||
ColumnLayout {
|
||||
anchors.fill: parent
|
||||
spacing: 0
|
||||
|
||||
// 公共标题栏:标题文字 + 关闭按钮,整栏可拖动
|
||||
TitleBar {
|
||||
Layout.fillWidth: true
|
||||
titleText: qsTr("Traffic Statistics")
|
||||
textColor: theme.textPrimary
|
||||
secondaryTextColor: theme.textSecondary
|
||||
closeHoverColor: root.accentColor
|
||||
}
|
||||
|
||||
// 顶部 tab 切换:按日 / 按月
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 34
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
spacing: 8
|
||||
|
||||
// 按日 tab
|
||||
Rectangle {
|
||||
property bool sel: root.currentTab === 0
|
||||
width: 80
|
||||
height: 24
|
||||
radius: 5
|
||||
color: sel ? common.accentBlue : "transparent"
|
||||
border.width: 1
|
||||
border.color: sel ? common.accentBlue
|
||||
: (dayMouse.containsMouse ? common.accentBlue : theme.lineColor)
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: qsTr("By Day")
|
||||
font.pixelSize: 12
|
||||
font.weight: sel ? Font.Bold : Font.Normal
|
||||
color: sel ? "white" : (dayMouse.containsMouse ? common.accentBlue : theme.textSecondary)
|
||||
}
|
||||
MouseArea {
|
||||
id: dayMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: root.currentTab = 0
|
||||
}
|
||||
}
|
||||
|
||||
// 按月 tab
|
||||
Rectangle {
|
||||
property bool sel: root.currentTab === 1
|
||||
width: 90
|
||||
height: 24
|
||||
radius: 5
|
||||
color: sel ? common.accentBlue : "transparent"
|
||||
border.width: 1
|
||||
border.color: sel ? common.accentBlue
|
||||
: (monthMouse.containsMouse ? common.accentBlue : theme.lineColor)
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: qsTr("By Month")
|
||||
font.pixelSize: 12
|
||||
font.weight: sel ? Font.Bold : Font.Normal
|
||||
color: sel ? "white" : (monthMouse.containsMouse ? common.accentBlue : theme.textSecondary)
|
||||
}
|
||||
MouseArea {
|
||||
id: monthMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
cursorShape: Qt.PointingHandCursor
|
||||
onClicked: root.currentTab = 1
|
||||
}
|
||||
}
|
||||
|
||||
Item { Layout.fillWidth: true }
|
||||
|
||||
// 右侧数据说明:按接口累计
|
||||
Text {
|
||||
text: root.currentTab === 0 ? qsTr("Today accumulated") : qsTr("This month accumulated")
|
||||
font.pixelSize: 11
|
||||
color: theme.textTertiary
|
||||
}
|
||||
}
|
||||
|
||||
// 表头行:日期/月份 | 接口 | 下载流量 | 上传流量 | 总计
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
Layout.topMargin: 2
|
||||
spacing: root.colSpacing
|
||||
|
||||
Text {
|
||||
Layout.preferredWidth: root.colDate
|
||||
text: root.currentTab === 0 ? qsTr("Date") : qsTr("Month")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colIface
|
||||
text: qsTr("Interface")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: qsTr("Download")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: qsTr("Upload")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: qsTr("Total")
|
||||
font.pixelSize: 11
|
||||
font.weight: Font.Bold
|
||||
color: theme.textSecondary
|
||||
}
|
||||
Item { Layout.fillWidth: true }
|
||||
}
|
||||
|
||||
// 表头与内容间的分隔线
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 1
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
Layout.topMargin: 4
|
||||
color: theme.lineColor
|
||||
}
|
||||
|
||||
// 表格区:ListView 展示每条(日期, 接口)记录,行 hover 高亮
|
||||
ListView {
|
||||
Layout.fillWidth: true
|
||||
Layout.fillHeight: true
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
Layout.topMargin: 2
|
||||
Layout.bottomMargin: 2
|
||||
clip: true
|
||||
model: root.statsModel
|
||||
onCountChanged: emptyHint.visible = root.statsModel.length === 0
|
||||
|
||||
// 空状态提示(当 statsModel 为空时显示)
|
||||
Item {
|
||||
id: emptyHint
|
||||
anchors.fill: parent
|
||||
visible: root.statsModel.length === 0
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: qsTr("No traffic records yet")
|
||||
font.pixelSize: 13
|
||||
color: theme.textTertiary
|
||||
}
|
||||
}
|
||||
|
||||
delegate: Rectangle {
|
||||
required property int index
|
||||
required property var modelData
|
||||
width: ListView.view.width
|
||||
height: 28
|
||||
radius: 4
|
||||
// 行 hover 高亮:浅色用淡黑、深色用淡白,保证两种卡片背景上均可见
|
||||
color: rowMouse.containsMouse ? theme.hoverBg : "transparent"
|
||||
|
||||
RowLayout {
|
||||
anchors.fill: parent
|
||||
anchors.leftMargin: 8
|
||||
anchors.rightMargin: 8
|
||||
spacing: root.colSpacing
|
||||
|
||||
Text {
|
||||
Layout.preferredWidth: root.colDate
|
||||
text: modelData.date
|
||||
font.pixelSize: 12
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colIface
|
||||
text: modelData.iface
|
||||
font.pixelSize: 12
|
||||
color: theme.textSecondary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: common.formatTotal(modelData.rx)
|
||||
font.pixelSize: 12
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: common.formatTotal(modelData.tx)
|
||||
font.pixelSize: 12
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: common.formatTotal(modelData.total)
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Medium
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Item { Layout.fillWidth: true }
|
||||
}
|
||||
|
||||
MouseArea {
|
||||
id: rowMouse
|
||||
anchors.fill: parent
|
||||
hoverEnabled: true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 表头与底部汇总间的分隔线
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 1
|
||||
Layout.leftMargin: 16
|
||||
Layout.rightMargin: 16
|
||||
color: theme.lineColor
|
||||
}
|
||||
|
||||
// 底部汇总行:所有显示记录的总下载/总上传/总流量
|
||||
RowLayout {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 34
|
||||
Layout.leftMargin: 24
|
||||
Layout.rightMargin: 16
|
||||
spacing: root.colSpacing
|
||||
|
||||
Text {
|
||||
Layout.preferredWidth: root.colDate + root.colIface
|
||||
text: qsTr("Total")
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: theme.textPrimary
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: common.formatTotal(root.totals.rx)
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: common.accentBlue
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: common.formatTotal(root.totals.tx)
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: common.accentGreen
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Text {
|
||||
Layout.preferredWidth: root.colNum
|
||||
text: common.formatTotal(root.totals.total)
|
||||
font.pixelSize: 12
|
||||
font.weight: Font.Bold
|
||||
color: theme.textPrimary
|
||||
elide: Text.ElideRight
|
||||
}
|
||||
Item { Layout.fillWidth: true }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 数据刷新:C++ 每 30 秒保存并发射 trafficLogChanged,据此重建表格;
|
||||
// 接口切换不影响日志(日志按活动接口累计,切接口后数据仍完整)
|
||||
Connections {
|
||||
target: root.applet
|
||||
function onTrafficLogChanged() {
|
||||
buildStatsModel()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// SPDX-FileCopyrightText: 2026 Jokul
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
// 独立窗口公共主题色提供者
|
||||
// 消除 AboutWindow / SettingsWindow / TrafficChartWindow 三个独立顶层窗口之间的
|
||||
// 主题颜色样板重复(此前各窗口各持一份 isDarkMode ? 深 : 浅 的颜色定义,
|
||||
// 改一处需同步多处)。现集中到本文件,各窗口实例化 WindowTheme { id: theme } 后
|
||||
// 统一经 theme.xxx 访问,仅需向 theme.isDarkMode 传入深色模式标记。
|
||||
// 设计原因:
|
||||
// - 深/浅主题由 networkview.qml 依据 DockPalette 检测任务栏深浅后通过 isDarkMode 传入,
|
||||
// 浅色取值与原各窗口硬编码完全一致,保证独立预览时视觉不变。
|
||||
// - 个别窗口存在差异值(如 SettingsWindow 的 winBg 浅色为 #F5F5F5、lineColor 为
|
||||
// #E0E0E0),因此每个颜色拆成 light/dark 一对可覆盖属性,派生色由它们计算,
|
||||
// 差异窗口仅需覆盖对应浅/深值即可,无需整体重写。
|
||||
import QtQuick 2.15
|
||||
|
||||
QtObject {
|
||||
id: theme
|
||||
|
||||
// 输入:深色模式标记,由父窗口从 networkview.qml 传入
|
||||
property bool isDarkMode: false
|
||||
|
||||
// ---- 浅色/深色取色对(默认值与 AboutWindow/TrafficChartWindow 一致,个别窗口可覆盖差异值)----
|
||||
property color lightWinBg: "#FFFFFF"
|
||||
property color darkWinBg: "#202020"
|
||||
property color lightCardBg: "#FFFFFF"
|
||||
property color darkCardBg: "#2A2A2A"
|
||||
property color lightLineColor: "#E8E8E8"
|
||||
property color darkLineColor: "#383838"
|
||||
property color lightTextPrimary: "#333333"
|
||||
property color darkTextPrimary: "#E0E0E0"
|
||||
property color lightTextSecondary: "#666666"
|
||||
property color darkTextSecondary: "#AAAAAA"
|
||||
property color lightTextTertiary: "#999999"
|
||||
property color darkTextTertiary: "#888888"
|
||||
property color lightIconGray: "#777777"
|
||||
property color darkIconGray: "#AAAAAA"
|
||||
property color lightHoverBg: "#F0F0F0"
|
||||
property color darkHoverBg: "#353535"
|
||||
|
||||
// ---- 派生主题色:isDarkMode 为 false 时取值与原浅色硬编码完全一致 ----
|
||||
readonly property color winBg: isDarkMode ? darkWinBg : lightWinBg
|
||||
readonly property color cardBg: isDarkMode ? darkCardBg : lightCardBg
|
||||
readonly property color lineColor: isDarkMode ? darkLineColor : lightLineColor
|
||||
readonly property color textPrimary: isDarkMode ? darkTextPrimary : lightTextPrimary
|
||||
readonly property color textSecondary: isDarkMode ? darkTextSecondary : lightTextSecondary
|
||||
readonly property color textTertiary: isDarkMode ? darkTextTertiary : lightTextTertiary
|
||||
readonly property color iconGray: isDarkMode ? darkIconGray : lightIconGray
|
||||
readonly property color hoverBg: isDarkMode ? darkHoverBg : lightHoverBg
|
||||
}
|
||||
+50
-10
@@ -320,7 +320,7 @@ AppletItem {
|
||||
PanelPopup {
|
||||
id: networkPopup
|
||||
width: 330
|
||||
height: 250
|
||||
height: 320
|
||||
popupX: DockPanelPositioner.x
|
||||
popupY: DockPanelPositioner.y
|
||||
|
||||
@@ -355,15 +355,6 @@ AppletItem {
|
||||
Platform.Menu {
|
||||
id: contextMenu
|
||||
|
||||
Platform.MenuItem {
|
||||
text: qsTr("Settings")
|
||||
onTriggered: {
|
||||
settingsWindow.show()
|
||||
settingsWindow.raise()
|
||||
settingsWindow.requestActivate()
|
||||
}
|
||||
}
|
||||
|
||||
// 流量波动图:屏幕居中独立窗口,展示活动接口最近 30 分钟网速趋势
|
||||
Platform.MenuItem {
|
||||
text: qsTr("Traffic Chart")
|
||||
@@ -374,6 +365,35 @@ AppletItem {
|
||||
}
|
||||
}
|
||||
|
||||
// 流量统计:屏幕居中独立窗口,展示按日/按月持久化的累计流量
|
||||
Platform.MenuItem {
|
||||
text: qsTr("Traffic Statistics")
|
||||
onTriggered: {
|
||||
trafficStatsWindow.show()
|
||||
trafficStatsWindow.raise()
|
||||
trafficStatsWindow.requestActivate()
|
||||
}
|
||||
}
|
||||
|
||||
// TCP 连接清单:屏幕居中独立窗口,展示当前所有 ESTABLISHED 状态的 TCP 连接
|
||||
Platform.MenuItem {
|
||||
text: qsTr("TCP Connections")
|
||||
onTriggered: {
|
||||
tcpConnectionsWindow.show()
|
||||
tcpConnectionsWindow.raise()
|
||||
tcpConnectionsWindow.requestActivate()
|
||||
}
|
||||
}
|
||||
|
||||
Platform.MenuItem {
|
||||
text: qsTr("Settings")
|
||||
onTriggered: {
|
||||
settingsWindow.show()
|
||||
settingsWindow.raise()
|
||||
settingsWindow.requestActivate()
|
||||
}
|
||||
}
|
||||
|
||||
Platform.MenuSeparator {}
|
||||
|
||||
Platform.MenuItem {
|
||||
@@ -406,6 +426,26 @@ AppletItem {
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// 流量统计窗口:屏幕居中独立窗口,展示按日/按月持久化的累计流量
|
||||
// 依赖通过属性传入:accentColor = root.accentRed,applet = root.applet,
|
||||
// isDarkMode 取自上方 DockPalette 检测结果
|
||||
TrafficStatsWindow {
|
||||
id: trafficStatsWindow
|
||||
accentColor: root.accentRed
|
||||
applet: root.applet
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// TCP 连接清单窗口:屏幕居中独立窗口,展示当前所有 ESTABLISHED 状态的 TCP 连接
|
||||
// 依赖通过属性传入:accentColor = root.accentRed,applet = root.applet,
|
||||
// isDarkMode 取自上方 DockPalette 检测结果
|
||||
TcpConnectionsWindow {
|
||||
id: tcpConnectionsWindow
|
||||
accentColor: root.accentRed
|
||||
applet: root.applet
|
||||
isDarkMode: root.isDarkMode
|
||||
}
|
||||
|
||||
// 设置窗口:独立顶层窗口,在桌面中间弹出
|
||||
// 设计原因:Dialog 使用父窗口(dock layer-surface)的 overlay,仅覆盖任务栏区域,
|
||||
// 无法在桌面中间显示。改用 Window 创建独立顶层窗口,可在桌面任意位置弹出。
|
||||
|
||||
+552
-20
@@ -17,6 +17,11 @@
|
||||
#include <QTranslator>
|
||||
#include <QLocale>
|
||||
#include <QCoreApplication>
|
||||
#include <QProcess>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QFileInfo>
|
||||
#include <QDate>
|
||||
|
||||
DS_BEGIN_NAMESPACE
|
||||
|
||||
@@ -30,9 +35,23 @@ NetworkMonitorApplet::NetworkMonitorApplet(QObject *parent)
|
||||
, m_uploadSpeed(0)
|
||||
, m_totalDownload(0)
|
||||
, m_totalUpload(0)
|
||||
, m_rxPackets(0)
|
||||
, m_txPackets(0)
|
||||
, m_rxErrors(0)
|
||||
, m_txErrors(0)
|
||||
, m_rxDropped(0)
|
||||
, m_txDropped(0)
|
||||
, m_linkSpeed(0)
|
||||
, m_refreshInterval(1000)
|
||||
, m_tcpConnections(0)
|
||||
, m_wifiSignal(0)
|
||||
, m_lowFreqCounter(4)
|
||||
, m_ipDetectCounter(4)
|
||||
, m_ready(false)
|
||||
, m_firstUpdate(true)
|
||||
, m_textColor()
|
||||
, m_historyDirty(true)
|
||||
, m_trafficSaveCounter(0)
|
||||
{
|
||||
// 从独立配置文件读取持久化的字体颜色
|
||||
// 设计原因:使用独立配置文件避免污染 dde-shell 的共享配置,
|
||||
@@ -55,10 +74,22 @@ NetworkMonitorApplet::NetworkMonitorApplet(QObject *parent)
|
||||
if (settings.contains(QStringLiteral("activeInterface"))) {
|
||||
m_activeInterface = settings.value(QStringLiteral("activeInterface")).toString();
|
||||
}
|
||||
|
||||
// 从配置文件读取持久化的刷新间隔(毫秒),仅接受 1/2/5 秒合法值
|
||||
if (settings.contains(QStringLiteral("refreshInterval"))) {
|
||||
const int iv = settings.value(QStringLiteral("refreshInterval")).toInt();
|
||||
if (iv == 1000 || iv == 2000 || iv == 5000) {
|
||||
m_refreshInterval = iv;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NetworkMonitorApplet::~NetworkMonitorApplet()
|
||||
{
|
||||
// 析构时兜底保存流量日志,确保进程退出前最后一段累加数据不丢失
|
||||
// 设计原因:降频保存每 30 秒才写一次盘,若用户在两次保存之间退出,
|
||||
// 这段增量会丢失;析构保存保证数据完整性
|
||||
saveTrafficLog();
|
||||
}
|
||||
|
||||
bool NetworkMonitorApplet::load()
|
||||
@@ -81,10 +112,20 @@ bool NetworkMonitorApplet::init()
|
||||
|
||||
detectInterfaces();
|
||||
|
||||
// 初始化流量日志持久化
|
||||
// 设计原因:日志与设置共用 ~/.config/jnetapplet 目录;
|
||||
// 启动时记录当前日期/月份用于跨日跨月检测,并从 JSON 文件加载历史累计数据
|
||||
m_trafficLogPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
|
||||
+ QStringLiteral("/jnetapplet/traffic_log.json");
|
||||
m_currentDate = QDate::currentDate().toString(QStringLiteral("yyyy-MM-dd"));
|
||||
m_currentMonth = QDate::currentDate().toString(QStringLiteral("yyyy-MM"));
|
||||
loadTrafficLog();
|
||||
|
||||
// 启动定时刷新
|
||||
m_refreshTimer = new QTimer(this);
|
||||
connect(m_refreshTimer, &QTimer::timeout, this, &NetworkMonitorApplet::refresh);
|
||||
m_refreshTimer->start(1000); // 每秒刷新一次
|
||||
// 用持久化的刷新间隔启动(默认 1000ms = 1 秒)
|
||||
m_refreshTimer->start(m_refreshInterval);
|
||||
|
||||
// 初始读取
|
||||
readNetworkStats();
|
||||
@@ -139,6 +180,53 @@ QString NetworkMonitorApplet::ipv6Address() const
|
||||
return m_ipv6Address;
|
||||
}
|
||||
|
||||
// ---- 活动接口包统计 getter(累计值,自开机起)----
|
||||
// 供弹窗接口信息区展示收发包/错误/丢包数
|
||||
quint64 NetworkMonitorApplet::rxPackets() const { return m_rxPackets; }
|
||||
quint64 NetworkMonitorApplet::txPackets() const { return m_txPackets; }
|
||||
quint64 NetworkMonitorApplet::rxErrors() const { return m_rxErrors; }
|
||||
quint64 NetworkMonitorApplet::txErrors() const { return m_txErrors; }
|
||||
quint64 NetworkMonitorApplet::rxDropped() const { return m_rxDropped; }
|
||||
quint64 NetworkMonitorApplet::txDropped() const { return m_txDropped; }
|
||||
|
||||
// 返回活动接口的链路协商速率(Mbps),0 表示不可用
|
||||
int NetworkMonitorApplet::linkSpeed() const { return m_linkSpeed; }
|
||||
|
||||
// 返回刷新间隔(毫秒)
|
||||
int NetworkMonitorApplet::refreshInterval() const { return m_refreshInterval; }
|
||||
|
||||
// 设置刷新间隔(毫秒),校验后持久化并即时生效
|
||||
// 设计原因:仅接受 1/2/5 秒,避免非法值破坏刷新频率;
|
||||
// 速度计算基于真实流逝时间(elapsedSec),改变间隔不影响计算正确性
|
||||
void NetworkMonitorApplet::setRefreshInterval(int ms)
|
||||
{
|
||||
if (ms != 1000 && ms != 2000 && ms != 5000) {
|
||||
return;
|
||||
}
|
||||
if (m_refreshInterval == ms) {
|
||||
return;
|
||||
}
|
||||
m_refreshInterval = ms;
|
||||
if (m_refreshTimer) {
|
||||
m_refreshTimer->start(m_refreshInterval);
|
||||
}
|
||||
|
||||
// 持久化到独立配置文件,重启后仍生效
|
||||
const QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
|
||||
+ QStringLiteral("/jnetapplet/settings.ini");
|
||||
QSettings settings(configPath, QSettings::IniFormat);
|
||||
settings.setValue(QStringLiteral("refreshInterval"), m_refreshInterval);
|
||||
settings.sync();
|
||||
|
||||
emit refreshIntervalChanged();
|
||||
}
|
||||
|
||||
// 返回当前活动接口的 TCP ESTABLISHED 连接数
|
||||
int NetworkMonitorApplet::tcpConnections() const { return m_tcpConnections; }
|
||||
|
||||
// 返回活动接口的 WiFi 信号强度(dBm),0 表示不可用
|
||||
int NetworkMonitorApplet::wifiSignal() const { return m_wifiSignal; }
|
||||
|
||||
// 返回插件版本号,从 dde-shell 插件元数据(metadata.json)读取
|
||||
// 版本唯一源为 CMakeLists.txt 的 project(VERSION),经 configure_file 写入 metadata.json,
|
||||
// dde-shell 加载时存入 DPluginMetaData,此处通过继承自 DApplet 的 pluginMetaData() 读取
|
||||
@@ -190,40 +278,73 @@ void NetworkMonitorApplet::setTextColor(const QString &color)
|
||||
emit textColorChanged();
|
||||
}
|
||||
|
||||
// 返回流量统计日志(JSON 对象),结构见 trafficLog 属性注释
|
||||
// QJsonObject 可直接暴露给 QML 作为 JS 对象读取,无需额外转换
|
||||
QJsonObject NetworkMonitorApplet::trafficLog() const
|
||||
{
|
||||
return m_trafficLog;
|
||||
}
|
||||
|
||||
// 返回 TCP 连接详情列表(QVariantList of QVariantMap),供 QML 连接清单窗口展示
|
||||
QVariantList NetworkMonitorApplet::tcpConnectionList() const
|
||||
{
|
||||
return m_tcpConnectionList;
|
||||
}
|
||||
|
||||
// 返回当前活动接口的下行速度历史,转换为 QVariantList of QPointF 供 QML 使用
|
||||
// 设计原因:QPointF 仅携带 x/y 两个值,上传与下载分别对应两个列表
|
||||
// 设计原因:QPointF 仅携带 x/y 两个值,上传与下载分别对应两个列表;
|
||||
// 结果缓存于成员变量,仅当 m_historyDirty 时重建,避免 QML 每次读取都重新构造 300 个点
|
||||
QVariantList NetworkMonitorApplet::speedHistoryDownload() const
|
||||
{
|
||||
QVariantList list;
|
||||
if (m_activeInterface.isEmpty() || !m_speedHistory.contains(m_activeInterface)) {
|
||||
return list;
|
||||
if (m_historyDirty) {
|
||||
rebuildHistoryCache();
|
||||
}
|
||||
const QVector<SpeedSample> &samples = m_speedHistory[m_activeInterface];
|
||||
list.reserve(samples.size());
|
||||
for (const SpeedSample &s : samples) {
|
||||
list.append(QPointF(s.timestamp, s.downloadSpeed));
|
||||
}
|
||||
return list;
|
||||
return m_cachedHistoryDl;
|
||||
}
|
||||
|
||||
// 返回当前活动接口的上行速度历史,结构同 speedHistoryDownload
|
||||
QVariantList NetworkMonitorApplet::speedHistoryUpload() const
|
||||
{
|
||||
QVariantList list;
|
||||
if (m_activeInterface.isEmpty() || !m_speedHistory.contains(m_activeInterface)) {
|
||||
return list;
|
||||
if (m_historyDirty) {
|
||||
rebuildHistoryCache();
|
||||
}
|
||||
return m_cachedHistoryUpload;
|
||||
}
|
||||
|
||||
// 重建上下行历史缓存(一次遍历同时填充两个列表)
|
||||
// 仅当 dirty 时由两个 getter 调用,避免重复构建;
|
||||
// 单 dirty 标记 + 一次重建双缓存,保证先后读取两个列表时数据一致
|
||||
void NetworkMonitorApplet::rebuildHistoryCache() const
|
||||
{
|
||||
m_cachedHistoryDl.clear();
|
||||
m_cachedHistoryUpload.clear();
|
||||
if (!m_activeInterface.isEmpty() && m_speedHistory.contains(m_activeInterface)) {
|
||||
const QVector<SpeedSample> &samples = m_speedHistory[m_activeInterface];
|
||||
list.reserve(samples.size());
|
||||
m_cachedHistoryDl.reserve(samples.size());
|
||||
m_cachedHistoryUpload.reserve(samples.size());
|
||||
for (const SpeedSample &s : samples) {
|
||||
list.append(QPointF(s.timestamp, s.uploadSpeed));
|
||||
m_cachedHistoryDl.append(QPointF(s.timestamp, s.downloadSpeed));
|
||||
m_cachedHistoryUpload.append(QPointF(s.timestamp, s.uploadSpeed));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
m_historyDirty = false;
|
||||
}
|
||||
|
||||
void NetworkMonitorApplet::refresh()
|
||||
{
|
||||
readNetworkStats();
|
||||
|
||||
// 流量日志降频保存:每 30 秒写盘一次并通知 QML
|
||||
// 设计原因:累加在 calculateSpeed 中每秒执行(纯内存操作开销低),
|
||||
// 而写盘与 QML 重绘若每秒执行会造成不必要的磁盘 IO 与界面刷新;
|
||||
// 计数器按刷新间隔折算(1s=30 次,2s=15 次,5s=6 次)保证实际每 30 秒保存一次
|
||||
const int saveEveryMs = 30000;
|
||||
const int saveThreshold = qMax(1, saveEveryMs / m_refreshInterval);
|
||||
if (++m_trafficSaveCounter >= saveThreshold) {
|
||||
m_trafficSaveCounter = 0;
|
||||
saveTrafficLog();
|
||||
emit trafficLogChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkMonitorApplet::setActiveInterface(const QString &interface)
|
||||
@@ -240,6 +361,19 @@ void NetworkMonitorApplet::setActiveInterface(const QString &interface)
|
||||
m_activeInterface = interface;
|
||||
m_firstUpdate = true;
|
||||
|
||||
// 接口切换后历史数据变化,标记脏缓存让趋势图立即重建
|
||||
m_historyDirty = true;
|
||||
|
||||
// 接口切换后包统计指向新接口,旧值不再有效,重置并通知 QML
|
||||
// 设计原因:包统计为各接口独立的累计值,切换后应显示新接口的数据
|
||||
m_rxPackets = 0;
|
||||
m_txPackets = 0;
|
||||
m_rxErrors = 0;
|
||||
m_txErrors = 0;
|
||||
m_rxDropped = 0;
|
||||
m_txDropped = 0;
|
||||
emit packetStatsChanged();
|
||||
|
||||
// 持久化到配置文件,下次启动自动加载用户选择的网卡
|
||||
// 设计原因:弹窗 chip 和设置窗口都调用此方法,统一持久化保证两处选择一致
|
||||
const QString configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)
|
||||
@@ -253,6 +387,8 @@ void NetworkMonitorApplet::setActiveInterface(const QString &interface)
|
||||
emit speedHistoryChanged();
|
||||
// 接口切换后立即检测新接口的 IP 地址
|
||||
detectIpAddress();
|
||||
// 接口切换后立即读取新接口的链路速率与 WiFi 信号
|
||||
detectLowFreqStats();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,7 +412,8 @@ void NetworkMonitorApplet::readNetworkStats()
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
// 格式: "interface: rx_bytes rx_packets rx_errors rx_dropped ... tx_bytes tx_packets tx_errors tx_dropped ..."
|
||||
QRegularExpression re("^([^:]+):\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
|
||||
// static const 仅首次调用时编译正则一次,后续复用,避免每秒对每行重复编译
|
||||
static const QRegularExpression re("^([^:]+):\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
|
||||
QRegularExpressionMatch match = re.match(line);
|
||||
|
||||
if (match.hasMatch()) {
|
||||
@@ -357,8 +494,23 @@ void NetworkMonitorApplet::readNetworkStats()
|
||||
}
|
||||
|
||||
calculateSpeed();
|
||||
// 每次刷新都检测 IP,以应对 DHCP 续约等 IP 变更场景
|
||||
// IP 地址变化频率极低(DHCP 续约/网络切换等),降频至每 5 秒检测一次,
|
||||
// 减少每秒 QNetworkInterface::interfaceFromName 的系统查询开销
|
||||
// 计数器每次 refresh 递增,达到 5 时归零并执行检测
|
||||
if (++m_ipDetectCounter >= 5) {
|
||||
m_ipDetectCounter = 0;
|
||||
detectIpAddress();
|
||||
}
|
||||
// 链路速率与 WiFi 信号同样变化频率低,每 5 秒检测一次
|
||||
if (++m_lowFreqCounter >= 5) {
|
||||
m_lowFreqCounter = 0;
|
||||
detectLowFreqStats();
|
||||
}
|
||||
// TCP 连接清单同样每 5 秒更新一次(进程名反查需遍历 /proc 开销大,不能每秒执行)
|
||||
if (++m_tcpListCounter >= 5) {
|
||||
m_tcpListCounter = 0;
|
||||
buildTcpConnectionList();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkMonitorApplet::calculateSpeed()
|
||||
@@ -369,6 +521,13 @@ void NetworkMonitorApplet::calculateSpeed()
|
||||
const double elapsedSec = (nowMs - m_lastTimestampMs) / 1000.0;
|
||||
m_lastTimestampMs = nowMs;
|
||||
|
||||
// 记录旧值用于 dirty 比较:网络空闲时速度/总量可能不变,
|
||||
// 仅在真正变化时才发射信号,避免 QML 每秒无效重绘
|
||||
const double oldDownload = m_downloadSpeed;
|
||||
const double oldUpload = m_uploadSpeed;
|
||||
const qint64 oldTotalDownload = m_totalDownload;
|
||||
const qint64 oldTotalUpload = m_totalUpload;
|
||||
|
||||
// ---- 活动接口速度计算(用于任务栏显示和总量累加)----
|
||||
if (m_firstUpdate) {
|
||||
// 首次更新或接口切换后:仅记录基线,不计算速度
|
||||
@@ -386,13 +545,45 @@ void NetworkMonitorApplet::calculateSpeed()
|
||||
const qint64 txDeltaClamped = txDelta > 0 ? txDelta : 0;
|
||||
m_downloadSpeed = rxDeltaClamped / elapsedSec;
|
||||
m_uploadSpeed = txDeltaClamped / elapsedSec;
|
||||
// 累加会话总量:总量 = 本次会话期间所有活动接口的流量总和
|
||||
// 会话总量:累加差值(dde-shell 启动至今的流量),重启清零
|
||||
// 设计原因:用户期望"本次会话"是本次 dde-shell 运行期间的流量,
|
||||
// 与流量统计窗口的"今日累计"(从0点起)区分,数值不会超过今日累计
|
||||
m_totalDownload += rxDeltaClamped;
|
||||
m_totalUpload += txDeltaClamped;
|
||||
// 累加日/月流量日志(仅活动接口增量,与速度计算口径一致)
|
||||
appendToTrafficLog(rxDeltaClamped, txDeltaClamped);
|
||||
m_lastRxBytes = currentRxBytes;
|
||||
m_lastTxBytes = currentTxBytes;
|
||||
}
|
||||
|
||||
// ---- 活动接口包统计(累计值,自开机起)----
|
||||
// 直接从当前活动接口读取收发包/错误/丢包计数,任一值变化时发射 packetStatsChanged
|
||||
if (!m_activeInterface.isEmpty() && m_interfaces.contains(m_activeInterface)) {
|
||||
const NetworkInterface &iface = m_interfaces[m_activeInterface];
|
||||
if (m_rxPackets != static_cast<quint64>(iface.rxPackets)
|
||||
|| m_txPackets != static_cast<quint64>(iface.txPackets)
|
||||
|| m_rxErrors != static_cast<quint64>(iface.rxErrors)
|
||||
|| m_txErrors != static_cast<quint64>(iface.txErrors)
|
||||
|| m_rxDropped != static_cast<quint64>(iface.rxDropped)
|
||||
|| m_txDropped != static_cast<quint64>(iface.txDropped)) {
|
||||
m_rxPackets = static_cast<quint64>(iface.rxPackets);
|
||||
m_txPackets = static_cast<quint64>(iface.txPackets);
|
||||
m_rxErrors = static_cast<quint64>(iface.rxErrors);
|
||||
m_txErrors = static_cast<quint64>(iface.txErrors);
|
||||
m_rxDropped = static_cast<quint64>(iface.rxDropped);
|
||||
m_txDropped = static_cast<quint64>(iface.txDropped);
|
||||
emit packetStatsChanged();
|
||||
}
|
||||
}
|
||||
|
||||
// ---- TCP 连接数(每秒统计)----
|
||||
// /proc/net/tcp 与 tcp6 文件很小,遍历开销低;仅在连接数变化时发射信号
|
||||
const int conns = countTcpConnections();
|
||||
if (m_tcpConnections != conns) {
|
||||
m_tcpConnections = conns;
|
||||
emit tcpConnectionsChanged();
|
||||
}
|
||||
|
||||
// ---- 为所有接口采集速度历史(非仅活动接口)----
|
||||
// 设计原因:原仅采集活动接口,切换到非活动接口时趋势图为空需等待数分钟。
|
||||
// 改为遍历所有接口计算各自速度并追加历史,切换接口时趋势图立即有数据。
|
||||
@@ -426,11 +617,18 @@ void NetworkMonitorApplet::calculateSpeed()
|
||||
m_lastRxBytesByIface[name] = iface.rxBytes;
|
||||
m_lastTxBytesByIface[name] = iface.txBytes;
|
||||
}
|
||||
// 历史采样点已追加,标记脏缓存,QML 下次读取时重建
|
||||
m_historyDirty = true;
|
||||
emit speedHistoryChanged();
|
||||
}
|
||||
|
||||
// 仅在速度/总量实际变化时发射信号,避免空闲时每秒触发 QML 无效重绘
|
||||
if (m_downloadSpeed != oldDownload || m_uploadSpeed != oldUpload) {
|
||||
emit speedChanged();
|
||||
}
|
||||
if (m_totalDownload != oldTotalDownload || m_totalUpload != oldTotalUpload) {
|
||||
emit totalChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkMonitorApplet::detectInterfaces()
|
||||
@@ -509,6 +707,222 @@ void NetworkMonitorApplet::detectIpAddress()
|
||||
}
|
||||
}
|
||||
|
||||
// 低频统计检测:链路速率与 WiFi 信号(每 5 秒或接口切换时),变化时发射对应信号
|
||||
// 设计原因:这两项数据变化频率极低(协商速率/信号强度基本稳定),
|
||||
// 无需每秒读取,降频降低系统调用与文件读取开销
|
||||
void NetworkMonitorApplet::detectLowFreqStats()
|
||||
{
|
||||
if (!m_activeInterface.isEmpty()) {
|
||||
const int speed = detectLinkSpeed(m_activeInterface);
|
||||
if (m_linkSpeed != speed) {
|
||||
m_linkSpeed = speed;
|
||||
emit linkSpeedChanged();
|
||||
}
|
||||
const int sig = detectWifiSignal(m_activeInterface);
|
||||
if (m_wifiSignal != sig) {
|
||||
m_wifiSignal = sig;
|
||||
emit wifiSignalChanged();
|
||||
}
|
||||
} else {
|
||||
// 无活动接口时归零,避免显示上一接口的残留数据
|
||||
if (m_linkSpeed != 0) {
|
||||
m_linkSpeed = 0;
|
||||
emit linkSpeedChanged();
|
||||
}
|
||||
if (m_wifiSignal != 0) {
|
||||
m_wifiSignal = 0;
|
||||
emit wifiSignalChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 读取指定接口的链路协商速率(Mbps)
|
||||
// 有线:直接读 /sys/class/net/<iface>/speed(Mbps)
|
||||
// 无线:speed 文件常返回错误(EINVAL),降级执行 `iw dev <iface> link` 解析 bitrate
|
||||
// 任何一步失败都返回 0(表示不可用),不抛出不记录
|
||||
int NetworkMonitorApplet::detectLinkSpeed(const QString &iface)
|
||||
{
|
||||
// 尝试 /sys/class/net/<iface>/speed
|
||||
QFile f(QStringLiteral("/sys/class/net/%1/speed").arg(iface));
|
||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
const QString val = QString::fromLatin1(f.readAll()).trimmed();
|
||||
f.close();
|
||||
bool ok = false;
|
||||
const int speed = val.toInt(&ok);
|
||||
if (ok && speed >= 0) {
|
||||
return speed;
|
||||
}
|
||||
}
|
||||
|
||||
// 无线网卡降级:解析 iw dev link 的 bitrate
|
||||
if (iface.startsWith("wlp") || iface.startsWith("wlan")) {
|
||||
QProcess proc;
|
||||
proc.start(QStringLiteral("iw"), {QStringLiteral("dev"), iface, QStringLiteral("link")});
|
||||
if (proc.waitForFinished(1000)) {
|
||||
const int br = parseIwBitrate(QString::fromUtf8(proc.readAllStandardOutput()));
|
||||
if (br > 0) {
|
||||
return br;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 解析 `iw dev <iface> link` 输出中的 bitrate(Mbps),失败返回 0
|
||||
// 输出形如 " bitrate: 866.7 MBit/s",也兼容 "Gbit/s"(换算为 Mbps)
|
||||
int NetworkMonitorApplet::parseIwBitrate(const QString &output) const
|
||||
{
|
||||
static const QRegularExpression re("bitrate:\\s*([\\d.]+)\\s*(M|G)bit/s");
|
||||
QRegularExpressionMatch m = re.match(output);
|
||||
if (!m.hasMatch()) {
|
||||
return 0;
|
||||
}
|
||||
const double val = m.captured(1).toDouble();
|
||||
const bool isGigabit = m.captured(2) == QLatin1String("G");
|
||||
return isGigabit ? qRound(val * 1000.0) : qRound(val);
|
||||
}
|
||||
|
||||
// 统计 /proc/net/tcp 与 /proc/net/tcp6 中 ESTABLISHED(状态 01)的连接数
|
||||
// 文件第 4 列(st)为连接状态码,01 表示 ESTABLISHED;
|
||||
// 任一文件读取失败时跳过,不因单个文件缺失而失败
|
||||
int NetworkMonitorApplet::countTcpConnections() const
|
||||
{
|
||||
int count = 0;
|
||||
const QStringList paths = {QStringLiteral("/proc/net/tcp"), QStringLiteral("/proc/net/tcp6")};
|
||||
for (const QString &path : paths) {
|
||||
QFile f(path);
|
||||
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
continue;
|
||||
}
|
||||
QTextStream stream(&f);
|
||||
// 跳过标题行
|
||||
stream.readLine();
|
||||
while (!stream.atEnd()) {
|
||||
const QString line = stream.readLine().trimmed();
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
const QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
|
||||
if (parts.size() > 3 && parts[3] == QLatin1String("01")) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// 构建 TCP 连接详情列表(调用 ss 命令获取 ESTABLISHED 连接,含进程名反查)
|
||||
// 结果存入 m_tcpConnectionList(QVariantList of QVariantMap)并发射 tcpConnectionListChanged;
|
||||
// 仅保留 ESTABLISHED 状态连接。优雅降级:ss 命令不存在/执行失败时返回空列表,不崩溃
|
||||
// 设计原因:原先通过遍历 /proc/<pid>/fd 反查进程名,但 dde-shell 以普通用户运行,
|
||||
// 无权 readlink 其他进程的 fd(lr-x------ 权限),导致进程名全空;改用 ss 命令一次
|
||||
// 获取全部 ESTABLISHED 连接的地址/端口/进程名,ss 通过 netlink 获取信息不受 /proc 权限限制,
|
||||
// 且仍由 readNetworkStats 降频至每 5 秒调用一次(ss 启动子进程仍有开销)
|
||||
void NetworkMonitorApplet::buildTcpConnectionList()
|
||||
{
|
||||
QVariantList list;
|
||||
|
||||
// 用 ss 命令获取 ESTABLISHED 连接(含进程名),通过 netlink 不受 /proc 权限限制
|
||||
// -t TCP, -n 不解析服务名(显示端口), -p 显示进程信息, state established 仅已建立连接
|
||||
QProcess process;
|
||||
process.start(QStringLiteral("ss"), {QStringLiteral("-tnp"), QStringLiteral("state"), QStringLiteral("established")});
|
||||
// ss 命令不存在或启动失败(如精简系统未安装 iproute2)时优雅降级:返回空列表,不崩溃
|
||||
if (!process.waitForStarted(1000)) {
|
||||
m_tcpConnectionList = list;
|
||||
emit tcpConnectionListChanged();
|
||||
return;
|
||||
}
|
||||
process.waitForFinished(3000); // 3 秒超时,ss 卡住时不阻塞主线程过久
|
||||
const QString output = QString::fromUtf8(process.readAllStandardOutput());
|
||||
|
||||
const QStringList lines = output.split(QLatin1Char('\n'), Qt::SkipEmptyParts);
|
||||
for (int i = 0; i < lines.size(); ++i) {
|
||||
if (i == 0) continue; // 跳过表头(Recv-Q Send-Q ...)
|
||||
|
||||
const QString line = lines[i].trimmed();
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
// ss 输出按多空格分割:Recv-Q Send-Q Local:Port Peer:Port [Process]
|
||||
// 注意 Process 列可能为空,且 IPv6 地址形如 [::1]:port(方括号内无空格),
|
||||
// 用正则提取更可靠,可容忍列之间任意空白
|
||||
QRegularExpression re(QStringLiteral("(\\S+)\\s+(\\S+)\\s+(\\S+):(\\d+)\\s+(\\S+):(\\d+)\\s*(.*)"));
|
||||
QRegularExpressionMatch match = re.match(line);
|
||||
if (!match.hasMatch()) continue;
|
||||
|
||||
QString localAddress = match.captured(3);
|
||||
const QString localPort = match.captured(4);
|
||||
QString remoteAddress = match.captured(5);
|
||||
const QString remotePort = match.captured(6);
|
||||
const QString processField = match.captured(7).trimmed();
|
||||
|
||||
// 去掉地址中的接口后缀(如 192.168.3.83%wlp3s0 -> 192.168.3.83)
|
||||
localAddress = localAddress.split(QLatin1Char('%')).first();
|
||||
remoteAddress = remoteAddress.split(QLatin1Char('%')).first();
|
||||
// 去掉 IPv6 地址的方括号([::1] -> ::1)
|
||||
if (localAddress.startsWith(QLatin1Char('['))) localAddress = localAddress.mid(1, localAddress.size() - 2);
|
||||
if (remoteAddress.startsWith(QLatin1Char('['))) remoteAddress = remoteAddress.mid(1, remoteAddress.size() - 2);
|
||||
|
||||
// 提取进程名:users:(("进程名",pid=xxx,fd=xxx)),取第一个双引号内的内容
|
||||
QString processName;
|
||||
if (processField.startsWith(QStringLiteral("users:"))) {
|
||||
QRegularExpression nameRe(QStringLiteral("\\(\"([^\"]+)\""));
|
||||
QRegularExpressionMatch nameMatch = nameRe.match(processField);
|
||||
if (nameMatch.hasMatch()) {
|
||||
processName = nameMatch.captured(1);
|
||||
}
|
||||
}
|
||||
|
||||
QVariantMap item;
|
||||
item[QStringLiteral("localAddress")] = localAddress;
|
||||
item[QStringLiteral("localPort")] = localPort.toInt();
|
||||
item[QStringLiteral("remoteAddress")] = remoteAddress;
|
||||
item[QStringLiteral("remotePort")] = remotePort.toInt();
|
||||
item[QStringLiteral("state")] = QStringLiteral("ESTABLISHED");
|
||||
item[QStringLiteral("processName")] = processName;
|
||||
list.append(item);
|
||||
}
|
||||
|
||||
m_tcpConnectionList = list;
|
||||
emit tcpConnectionListChanged();
|
||||
}
|
||||
|
||||
// 读取指定接口的 WiFi 信号强度(dBm)
|
||||
// 仅无线接口(wlp/wlan)有效;/proc/net/wireless 桌面环境可能不存在(返回 0)
|
||||
// 该文件每行形如 "wlp3s0: 60. -65. -256 0 ...",level(dBm)为第 3 个数值字段
|
||||
int NetworkMonitorApplet::detectWifiSignal(const QString &iface)
|
||||
{
|
||||
if (!iface.startsWith("wlp") && !iface.startsWith("wlan")) {
|
||||
return 0;
|
||||
}
|
||||
QFile f(QStringLiteral("/proc/net/wireless"));
|
||||
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
return 0;
|
||||
}
|
||||
QTextStream stream(&f);
|
||||
// 跳过前两行标题
|
||||
stream.readLine();
|
||||
stream.readLine();
|
||||
int sig = 0;
|
||||
while (!stream.atEnd()) {
|
||||
const QString line = stream.readLine().trimmed();
|
||||
if (line.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
const QStringList parts = line.split(QRegularExpression("\\s+"), Qt::SkipEmptyParts);
|
||||
if (parts.size() > 3 && parts[0].startsWith(iface)) {
|
||||
bool ok = false;
|
||||
const double level = parts[3].toDouble(&ok);
|
||||
if (ok) {
|
||||
sig = qRound(level);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
return sig;
|
||||
}
|
||||
|
||||
// 判断是否为物理网卡
|
||||
// 物理网卡前缀:无线 wlp/wlan/wifi,有线 enp/eth
|
||||
// 虚拟接口(Meta/tun/tap/docker/veth/br-)返回 false,
|
||||
@@ -519,6 +933,124 @@ bool NetworkMonitorApplet::isPhysicalInterface(const QString &name) const
|
||||
|| name.startsWith("enp") || name.startsWith("eth");
|
||||
}
|
||||
|
||||
// ---- 流量日志(日/月持久化)----
|
||||
|
||||
// 启动时从 traffic_log.json 加载流量日志
|
||||
// 优雅降级:文件不存在、读取失败或 JSON 解析失败时均使用空结构,不崩溃不抛异常;
|
||||
// 首次运行(文件不存在)时先创建空的 byDay/byMonth 结构,由后续保存写盘创建文件
|
||||
void NetworkMonitorApplet::loadTrafficLog()
|
||||
{
|
||||
QFile file(m_trafficLogPath);
|
||||
if (!file.exists()) {
|
||||
m_trafficLog = QJsonObject();
|
||||
m_trafficLog[QStringLiteral("byDay")] = QJsonObject();
|
||||
m_trafficLog[QStringLiteral("byMonth")] = QJsonObject();
|
||||
return;
|
||||
}
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
QJsonParseError err;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &err);
|
||||
file.close();
|
||||
if (err.error == QJsonParseError::NoError && doc.isObject()) {
|
||||
m_trafficLog = doc.object();
|
||||
} else {
|
||||
// 解析失败(如文件被外部写坏):降级为空结构,避免后续累加异常
|
||||
m_trafficLog = QJsonObject();
|
||||
m_trafficLog[QStringLiteral("byDay")] = QJsonObject();
|
||||
m_trafficLog[QStringLiteral("byMonth")] = QJsonObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 将流量日志写入 traffic_log.json
|
||||
// 先裁剪超期记录再写盘,避免日志无限膨胀;
|
||||
// 写前 mkpath 确保目录存在(首次运行时目录尚未创建)
|
||||
void NetworkMonitorApplet::saveTrafficLog()
|
||||
{
|
||||
// 测试等场景未初始化日志路径时直接跳过,避免对空路径 mkpath/写文件产生告警
|
||||
if (m_trafficLogPath.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
pruneTrafficLog();
|
||||
QDir dir;
|
||||
dir.mkpath(QFileInfo(m_trafficLogPath).absolutePath());
|
||||
QFile file(m_trafficLogPath);
|
||||
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
// 用 Indented 缩进格式便于用户手动查看/调试日志文件
|
||||
file.write(QJsonDocument(m_trafficLog).toJson(QJsonDocument::Indented));
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
// 将本次流量增量累加到当日/当月/当前活动接口的日志记录中(每秒调用,纯内存操作)
|
||||
// 跨日/跨月检测:日期或月份与上次记录不同时切换当前记录,不累加到昨天的记录;
|
||||
// 只累加活动接口(与 m_totalDownload 口径一致),避免多接口重复计数
|
||||
void NetworkMonitorApplet::appendToTrafficLog(qint64 rxDelta, qint64 txDelta)
|
||||
{
|
||||
if (rxDelta <= 0 && txDelta <= 0) {
|
||||
return;
|
||||
}
|
||||
if (m_activeInterface.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 跨日/跨月检测:更新当前日期/月份,切换记录(旧记录保留不再累加)
|
||||
const QString date = QDate::currentDate().toString(QStringLiteral("yyyy-MM-dd"));
|
||||
const QString month = QDate::currentDate().toString(QStringLiteral("yyyy-MM"));
|
||||
m_currentDate = date;
|
||||
m_currentMonth = month;
|
||||
|
||||
// 累加到按日记录:byDay[date][iface] = {rx, tx}
|
||||
// 用 toVariant().toLongLong() 读取并写回 qint64,保留整数精度
|
||||
//(直接赋值 double 会导致超过 2^53 的字节数精度丢失)
|
||||
QJsonObject byDay = m_trafficLog.value(QStringLiteral("byDay")).toObject();
|
||||
QJsonObject dayEntry = byDay.value(date).toObject();
|
||||
QJsonObject ifaceDay = dayEntry.value(m_activeInterface).toObject();
|
||||
ifaceDay[QStringLiteral("rx")] =
|
||||
ifaceDay.value(QStringLiteral("rx")).toVariant().toLongLong() + rxDelta;
|
||||
ifaceDay[QStringLiteral("tx")] =
|
||||
ifaceDay.value(QStringLiteral("tx")).toVariant().toLongLong() + txDelta;
|
||||
dayEntry[m_activeInterface] = ifaceDay;
|
||||
byDay[date] = dayEntry;
|
||||
m_trafficLog[QStringLiteral("byDay")] = byDay;
|
||||
|
||||
// 累加到按月记录:byMonth[month][iface] = {rx, tx}
|
||||
QJsonObject byMonth = m_trafficLog.value(QStringLiteral("byMonth")).toObject();
|
||||
QJsonObject monthEntry = byMonth.value(month).toObject();
|
||||
QJsonObject ifaceMonth = monthEntry.value(m_activeInterface).toObject();
|
||||
ifaceMonth[QStringLiteral("rx")] =
|
||||
ifaceMonth.value(QStringLiteral("rx")).toVariant().toLongLong() + rxDelta;
|
||||
ifaceMonth[QStringLiteral("tx")] =
|
||||
ifaceMonth.value(QStringLiteral("tx")).toVariant().toLongLong() + txDelta;
|
||||
monthEntry[m_activeInterface] = ifaceMonth;
|
||||
byMonth[month] = monthEntry;
|
||||
m_trafficLog[QStringLiteral("byMonth")] = byMonth;
|
||||
}
|
||||
|
||||
// 裁剪超期记录:按日最多 30 天、按月最多 12 个月,超出删除最旧记录
|
||||
// 日期/月份均为 ISO 格式("yyyy-MM-dd"/"yyyy-MM"),字典序即时间序,
|
||||
// 排序后取最小的超量个删除,保证始终保留最近的记录
|
||||
void NetworkMonitorApplet::pruneTrafficLog()
|
||||
{
|
||||
QJsonObject byDay = m_trafficLog.value(QStringLiteral("byDay")).toObject();
|
||||
QStringList dayKeys = byDay.keys();
|
||||
dayKeys.sort();
|
||||
while (dayKeys.size() > MAX_DAY_ENTRIES) {
|
||||
byDay.remove(dayKeys.first());
|
||||
dayKeys.removeFirst();
|
||||
}
|
||||
m_trafficLog[QStringLiteral("byDay")] = byDay;
|
||||
|
||||
QJsonObject byMonth = m_trafficLog.value(QStringLiteral("byMonth")).toObject();
|
||||
QStringList monthKeys = byMonth.keys();
|
||||
monthKeys.sort();
|
||||
while (monthKeys.size() > MAX_MONTH_ENTRIES) {
|
||||
byMonth.remove(monthKeys.first());
|
||||
monthKeys.removeFirst();
|
||||
}
|
||||
m_trafficLog[QStringLiteral("byMonth")] = byMonth;
|
||||
}
|
||||
|
||||
D_APPLET_CLASS(NetworkMonitorApplet)
|
||||
|
||||
DS_END_NAMESPACE
|
||||
|
||||
+157
-2
@@ -16,6 +16,9 @@
|
||||
#include <QDateTime>
|
||||
#include <QColor>
|
||||
#include <QSettings>
|
||||
#include <QJsonObject>
|
||||
#include <QMetaType>
|
||||
#include <QVariantMap>
|
||||
|
||||
DS_BEGIN_NAMESPACE
|
||||
|
||||
@@ -57,6 +60,23 @@ class NetworkMonitorApplet : public DApplet
|
||||
Q_PROPERTY(QString ipAddress READ ipAddress NOTIFY ipAddressChanged)
|
||||
// 活动接口的 IPv6 全球地址(已过滤 link-local 和 loopback),供 QML 显示
|
||||
Q_PROPERTY(QString ipv6Address READ ipv6Address NOTIFY ipv6AddressChanged)
|
||||
// 活动接口的收发包/错误/丢包累计统计(自开机起累计值),供弹窗接口信息区展示
|
||||
// 全部用 quint64 类型,QML 侧可直接读取;任一值变化时统一发射 packetStatsChanged
|
||||
Q_PROPERTY(quint64 rxPackets READ rxPackets NOTIFY packetStatsChanged)
|
||||
Q_PROPERTY(quint64 txPackets READ txPackets NOTIFY packetStatsChanged)
|
||||
Q_PROPERTY(quint64 rxErrors READ rxErrors NOTIFY packetStatsChanged)
|
||||
Q_PROPERTY(quint64 txErrors READ txErrors NOTIFY packetStatsChanged)
|
||||
Q_PROPERTY(quint64 rxDropped READ rxDropped NOTIFY packetStatsChanged)
|
||||
Q_PROPERTY(quint64 txDropped READ txDropped NOTIFY packetStatsChanged)
|
||||
// 活动接口的链路协商速率(Mbps):有线读 /sys/class/net/<iface>/speed,
|
||||
// 无线读失败时降级解析 `iw dev link` 的 bitrate;0 表示不可用
|
||||
Q_PROPERTY(int linkSpeed READ linkSpeed NOTIFY linkSpeedChanged)
|
||||
// 刷新间隔(毫秒),默认 1000,用户在设置窗口选择后持久化并即时生效
|
||||
Q_PROPERTY(int refreshInterval READ refreshInterval WRITE setRefreshInterval NOTIFY refreshIntervalChanged)
|
||||
// 当前活动接口的 TCP ESTABLISHED 连接数,供弹窗展示
|
||||
Q_PROPERTY(int tcpConnections READ tcpConnections NOTIFY tcpConnectionsChanged)
|
||||
// 活动接口的 WiFi 信号强度(dBm,负值如 -65),非无线接口或不可用时为 0
|
||||
Q_PROPERTY(int wifiSignal READ wifiSignal NOTIFY wifiSignalChanged)
|
||||
// 插件版本号,从 dde-shell 元数据读取,版本唯一源为 CMakeLists.txt project(VERSION)
|
||||
Q_PROPERTY(QString version READ version CONSTANT)
|
||||
// 任务栏网速数值字体颜色,空串表示跟随系统主题(由 QML 回退到 primaryText),
|
||||
@@ -67,6 +87,19 @@ class NetworkMonitorApplet : public DApplet
|
||||
Q_PROPERTY(QVariantList speedHistoryDownload READ speedHistoryDownload NOTIFY speedHistoryChanged)
|
||||
// 当前活动接口的上行速度历史采样点,结构同 speedHistoryDownload
|
||||
Q_PROPERTY(QVariantList speedHistoryUpload READ speedHistoryUpload NOTIFY speedHistoryChanged)
|
||||
// 流量统计日志(JSON 对象),供 QML 流量统计窗口读取展示
|
||||
// 结构:{"byDay": {日期: {接口: {rx, tx}}}, "byMonth": {月份: {接口: {rx, tx}}}}
|
||||
// 设计原因:会话总量重启清零,用户无法了解长期趋势;按日/月聚合让用户看
|
||||
// 每日使用量和月度汇总,按接口分让多网卡各自独立统计。
|
||||
// 保存频率:累加每秒执行(内存操作),写盘与通知 QML 降频(每 30 秒一次),
|
||||
// 避免每秒磁盘 IO 与 QML 重绘
|
||||
Q_PROPERTY(QJsonObject trafficLog READ trafficLog NOTIFY trafficLogChanged)
|
||||
// TCP 连接详情列表,供 QML 连接清单窗口展示
|
||||
// 每项为 QVariantMap(键:localAddress/localPort/remoteAddress/remotePort/state/processName)
|
||||
// 仅包含 ESTABLISHED 状态连接,含进程名反查
|
||||
// 设计原因:用 ss 命令反查进程名,通过 netlink 获取不受 /proc 权限限制,
|
||||
// 但仍需启动子进程开销较大,故降频至每 5 秒更新一次
|
||||
Q_PROPERTY(QVariantList tcpConnectionList READ tcpConnectionList NOTIFY tcpConnectionListChanged)
|
||||
|
||||
public:
|
||||
explicit NetworkMonitorApplet(QObject *parent = nullptr);
|
||||
@@ -84,6 +117,23 @@ public:
|
||||
QString activeInterface() const;
|
||||
QString ipAddress() const;
|
||||
QString ipv6Address() const;
|
||||
// 返回活动接口的收发包/错误/丢包累计值(自开机起),供弹窗接口信息区展示
|
||||
quint64 rxPackets() const;
|
||||
quint64 txPackets() const;
|
||||
quint64 rxErrors() const;
|
||||
quint64 txErrors() const;
|
||||
quint64 rxDropped() const;
|
||||
quint64 txDropped() const;
|
||||
// 返回活动接口的链路协商速率(Mbps),0 表示不可用
|
||||
int linkSpeed() const;
|
||||
// 返回刷新间隔(毫秒)
|
||||
int refreshInterval() const;
|
||||
// 设置刷新间隔(毫秒),校验后持久化并即时生效
|
||||
void setRefreshInterval(int ms);
|
||||
// 返回当前活动接口的 TCP ESTABLISHED 连接数
|
||||
int tcpConnections() const;
|
||||
// 返回活动接口的 WiFi 信号强度(dBm),0 表示不可用
|
||||
int wifiSignal() const;
|
||||
// 返回当前活动接口的下行速度历史(QPointF 列表),供 QML 趋势图绘制
|
||||
QVariantList speedHistoryDownload() const;
|
||||
// 返回当前活动接口的上行速度历史(QPointF 列表),供 QML 趋势图绘制
|
||||
@@ -94,6 +144,10 @@ public:
|
||||
QString textColor() const;
|
||||
// 设置任务栏网速字体颜色,空串表示跟随系统主题
|
||||
void setTextColor(const QString &color);
|
||||
// 返回流量统计日志(JSON 对象),结构见 trafficLog 属性注释,供 QML 流量统计窗口读取
|
||||
QJsonObject trafficLog() const;
|
||||
// 返回 TCP 连接详情列表(QVariantList of QVariantMap),供 QML 连接清单窗口展示
|
||||
QVariantList tcpConnectionList() const;
|
||||
|
||||
// 定时器回调,由 m_refreshTimer 每秒调用。非 Q_INVOKABLE(QML 不应直接调用,
|
||||
// 否则会打破 1 秒定时间隔导致速度计算失真)
|
||||
@@ -108,9 +162,23 @@ signals:
|
||||
void activeInterfaceChanged();
|
||||
void ipAddressChanged();
|
||||
void ipv6AddressChanged();
|
||||
// 活动接口包统计(收发包/错误/丢包)变化时通知 QML 更新
|
||||
void packetStatsChanged();
|
||||
// 活动接口链路速率变化时通知 QML 更新
|
||||
void linkSpeedChanged();
|
||||
// 刷新间隔变化时通知 QML 更新
|
||||
void refreshIntervalChanged();
|
||||
// TCP ESTABLISHED 连接数变化时通知 QML 更新
|
||||
void tcpConnectionsChanged();
|
||||
// WiFi 信号强度变化时通知 QML 更新
|
||||
void wifiSignalChanged();
|
||||
void speedHistoryChanged();
|
||||
// 任务栏网速字体颜色变化时通知 QML 更新
|
||||
void textColorChanged();
|
||||
// 流量统计日志变化时通知 QML 更新(降频,每 30 秒发射一次)
|
||||
void trafficLogChanged();
|
||||
// TCP 连接详情列表变化时通知 QML 更新(降频,每 5 秒发射一次)
|
||||
void tcpConnectionListChanged();
|
||||
|
||||
private:
|
||||
void readNetworkStats();
|
||||
@@ -118,12 +186,35 @@ private:
|
||||
void detectInterfaces();
|
||||
// 检测活动接口的 IPv4 与 IPv6 地址,变化时分别发射对应信号
|
||||
void detectIpAddress();
|
||||
// 低频统计检测:链路速率与 WiFi 信号(每 5 秒),变化时发射对应信号
|
||||
void detectLowFreqStats();
|
||||
// 读取指定接口的链路协商速率(Mbps),无线读 speed 失败时降级解析 iw bitrate
|
||||
int detectLinkSpeed(const QString &iface);
|
||||
// 解析 `iw dev <iface> link` 输出中的 bitrate(Mbps),失败返回 0
|
||||
int parseIwBitrate(const QString &output) const;
|
||||
// 统计 /proc/net/tcp 与 tcp6 中 ESTABLISHED(状态 01)的连接数
|
||||
int countTcpConnections() const;
|
||||
// 构建 TCP 连接详情列表(调用 ss 命令获取 ESTABLISHED 连接,含进程名反查)
|
||||
// 结果存入 m_tcpConnectionList 并发射 tcpConnectionListChanged;仅 ESTABLISHED
|
||||
void buildTcpConnectionList();
|
||||
// 读取指定接口的 WiFi 信号强度(dBm),非无线接口或不可用时返回 0
|
||||
int detectWifiSignal(const QString &iface);
|
||||
// 判断是否为物理网卡(无线 wlp/wlan,有线 enp/eth),
|
||||
// 用于自动选择时优先真实网卡而非虚拟代理接口(如 Meta/tun0)
|
||||
bool isPhysicalInterface(const QString &name) const;
|
||||
qint64 getActiveRxBytes() const;
|
||||
qint64 getActiveTxBytes() const;
|
||||
|
||||
// ---- 流量日志(日/月持久化)----
|
||||
// 启动时从 traffic_log.json 加载日志;文件不存在或解析失败时降级为空结构,不崩溃
|
||||
void loadTrafficLog();
|
||||
// 将日志写入 traffic_log.json(先裁剪超期记录再写盘),供析构与降频保存调用
|
||||
void saveTrafficLog();
|
||||
// 将本次流量增量累加到当日/当月/当前活动接口的日志记录中(每秒调用,内存操作)
|
||||
void appendToTrafficLog(qint64 rxDelta, qint64 txDelta);
|
||||
// 裁剪超期记录:按日最多 30 天、按月最多 12 个月,超出删除最旧记录
|
||||
void pruneTrafficLog();
|
||||
|
||||
QTimer *m_refreshTimer;
|
||||
QMap<QString, NetworkInterface> m_interfaces;
|
||||
QStringList m_interfaceList;
|
||||
@@ -148,6 +239,31 @@ private:
|
||||
qint64 m_totalDownload;
|
||||
qint64 m_totalUpload;
|
||||
|
||||
// 活动接口包统计缓存(自开机起累计值),供弹窗接口信息区展示
|
||||
// 设计原因:/proc/net/dev 已解析包计数但此前直接丢弃,现缓存并暴露给 QML
|
||||
quint64 m_rxPackets;
|
||||
quint64 m_txPackets;
|
||||
quint64 m_rxErrors;
|
||||
quint64 m_txErrors;
|
||||
quint64 m_rxDropped;
|
||||
quint64 m_txDropped;
|
||||
|
||||
// IP 地址检测降频计数器:IP 变化频率极低,每 5 次 refresh 才检测一次
|
||||
// 初始化 4 使首次 refresh 立即检测,避免启动后 IP 显示延迟
|
||||
int m_ipDetectCounter;
|
||||
|
||||
// 活动接口链路协商速率(Mbps),0 表示不可用
|
||||
int m_linkSpeed;
|
||||
// 刷新间隔(毫秒),默认 1000,可配置并持久化
|
||||
int m_refreshInterval;
|
||||
// 当前活动接口的 TCP ESTABLISHED 连接数
|
||||
int m_tcpConnections;
|
||||
// 活动接口的 WiFi 信号强度(dBm),0 表示不可用
|
||||
int m_wifiSignal;
|
||||
// 低频统计检测计数器:链路速率/WiFi 信号每 5 次 refresh 检测一次
|
||||
// 初始化 4 使首次 refresh 立即检测
|
||||
int m_lowFreqCounter;
|
||||
|
||||
bool m_ready;
|
||||
bool m_firstUpdate;
|
||||
// 任务栏网速字体颜色,空串表示跟随系统主题
|
||||
@@ -158,8 +274,47 @@ private:
|
||||
// 速度历史环形缓冲:每个接口独立维护一份,key 为接口名
|
||||
// 设计原因:接口切换时趋势图不出现跳变,切回时仍能看到该接口历史
|
||||
QHash<QString, QVector<SpeedSample>> m_speedHistory;
|
||||
// 每个接口最多保留的采样点数:5 分钟 × 60 秒 = 300
|
||||
static constexpr int MAX_HISTORY_SAMPLES = 300;
|
||||
// 每个接口最多保留的采样点数:30 分钟 × 60 秒 = 1800
|
||||
// 设计原因:流量波动图支持 1/5/30 分钟时间窗口切换,需在展示层选择显示范围,
|
||||
// 故后端需存够 30 分钟的历史数据;时间窗口选择是纯 QML 展示层行为,后端只需
|
||||
// 提供足够长的历史缓冲,无需新增 Q_PROPERTY
|
||||
static constexpr int MAX_HISTORY_SAMPLES = 1800;
|
||||
|
||||
// 历史 QVariantList 缓存:避免 QML 每次读取都重新构造 300 个 QPointF
|
||||
// 设计原因:Canvas 每秒读取一次 + hover 时也读取,逐个 append 开销可观;
|
||||
// 用 dirty 标记懒重建,仅当追加采样点或切换接口后重建一次
|
||||
// 注意:getter 为 const,故缓存与 dirty 标记声明为 mutable
|
||||
mutable QVariantList m_cachedHistoryDl;
|
||||
mutable QVariantList m_cachedHistoryUpload;
|
||||
mutable bool m_historyDirty;
|
||||
// 重建历史缓存:仅当 m_historyDirty 时由两个 getter 调用
|
||||
void rebuildHistoryCache() const;
|
||||
|
||||
// ---- 流量日志成员 ----
|
||||
// 完整流量日志 JSON:{"byDay": {日期: {接口: {rx, tx}}}, "byMonth": {月份: {接口: {rx, tx}}}}
|
||||
// 设计原因:会话总量重启清零,用户无法了解长期趋势;按日/月聚合让用户看
|
||||
// 每日使用量和月度汇总,按接口分让多网卡各自独立统计
|
||||
QJsonObject m_trafficLog;
|
||||
// 日志文件路径(~/.config/jnetapplet/traffic_log.json)
|
||||
QString m_trafficLogPath;
|
||||
// 当前日期 "yyyy-MM-dd" 与月份 "yyyy-MM",用于跨日/跨月检测
|
||||
QString m_currentDate;
|
||||
QString m_currentMonth;
|
||||
// 降频保存计数器:每 30 秒(按刷新间隔折算)写盘一次并通知 QML,
|
||||
// 避免每秒磁盘 IO 与 QML 重绘;析构函数再兜底保存一次
|
||||
int m_trafficSaveCounter;
|
||||
// 按日记录最多保留 30 天、按月最多保留 12 个月,超出自动裁剪最旧记录
|
||||
static constexpr int MAX_DAY_ENTRIES = 30;
|
||||
static constexpr int MAX_MONTH_ENTRIES = 12;
|
||||
|
||||
// ---- TCP 连接清单成员 ----
|
||||
// TCP 连接详情列表(QVariantList of QVariantMap),供 QML 连接清单窗口读取
|
||||
// 设计原因:QVariantMap 便于 QML 以 JS 对象直接访问各字段,
|
||||
// 不暴露中间结构体(需注册元类型,QML 访问繁琐)
|
||||
QVariantList m_tcpConnectionList;
|
||||
// TCP 连接清单降频计数器:ss 命令需启动子进程开销大,每 5 次 refresh 更新一次
|
||||
// 初始化 4 使首次 refresh 立即更新
|
||||
int m_tcpListCounter = 4;
|
||||
};
|
||||
|
||||
DS_END_NAMESPACE
|
||||
@@ -78,8 +78,33 @@
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/NetworkPopup.qml" line="427"/>
|
||||
<source>Total</source>
|
||||
<translation>总计</translation>
|
||||
<source>Session Total</source>
|
||||
<translation>本次会话</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/NetworkPopup.qml" line="474"/>
|
||||
<source>Packets</source>
|
||||
<translation>包统计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/NetworkPopup.qml" line="483"/>
|
||||
<source>Errors</source>
|
||||
<translation>错误</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/NetworkPopup.qml" line="494"/>
|
||||
<source>Dropped</source>
|
||||
<translation>丢包</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/NetworkPopup.qml" line="317"/>
|
||||
<source>Signal</source>
|
||||
<translation>信号</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/NetworkPopup.qml" line="566"/>
|
||||
<source>TCP Connections</source>
|
||||
<translation>TCP 连接</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
@@ -159,6 +184,11 @@
|
||||
<source>Font Color</source>
|
||||
<translation>字体颜色</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/SettingsWindow.qml" line="311"/>
|
||||
<source>Refresh Interval</source>
|
||||
<translation>刷新间隔</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/SettingsWindow.qml" line="446"/>
|
||||
<source>Warning</source>
|
||||
@@ -240,5 +270,146 @@
|
||||
<source>About</source>
|
||||
<translation>关于</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/networkview.qml" line="314"/>
|
||||
<source>Traffic Statistics</source>
|
||||
<translation>流量统计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/networkview.qml" line="389"/>
|
||||
<source>TCP Connections</source>
|
||||
<translation>TCP 连接</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>TrafficStatsWindow</name>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="132"/>
|
||||
<source>Traffic Statistics</source>
|
||||
<translation>流量统计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="158"/>
|
||||
<source>By Day</source>
|
||||
<translation>按日</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="184"/>
|
||||
<source>By Month</source>
|
||||
<translation>按月</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="202"/>
|
||||
<source>Today accumulated</source>
|
||||
<translation>今日累计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="214"/>
|
||||
<source>This month accumulated</source>
|
||||
<translation>本月累计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="218"/>
|
||||
<source>Date</source>
|
||||
<translation>日期</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="218"/>
|
||||
<source>Month</source>
|
||||
<translation>月份</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="225"/>
|
||||
<source>Interface</source>
|
||||
<translation>接口</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="232"/>
|
||||
<source>Download</source>
|
||||
<translation>下载</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="239"/>
|
||||
<source>Upload</source>
|
||||
<translation>上传</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="246"/>
|
||||
<source>Total</source>
|
||||
<translation>总计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TrafficStatsWindow.qml" line="283"/>
|
||||
<source>No traffic records yet</source>
|
||||
<translation>暂无流量记录</translation>
|
||||
</message>
|
||||
</context>
|
||||
<context>
|
||||
<name>TcpConnectionsWindow</name>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="132"/>
|
||||
<source>TCP Connections</source>
|
||||
<translation>TCP 连接</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="148"/>
|
||||
<source>Active connections: </source>
|
||||
<translation>活跃连接:</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="159"/>
|
||||
<source>Search address, port, process...</source>
|
||||
<translation>搜索地址、端口、进程...</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="191"/>
|
||||
<source>Refreshes every </source>
|
||||
<translation>每 </translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="190"/>
|
||||
<source> seconds</source>
|
||||
<translation> 秒刷新</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="204"/>
|
||||
<source>Local Address:Port</source>
|
||||
<translation>本地地址:端口</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="211"/>
|
||||
<source>Remote Address:Port</source>
|
||||
<translation>远程地址:端口</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="218"/>
|
||||
<source>State</source>
|
||||
<translation>状态</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="225"/>
|
||||
<source>Process</source>
|
||||
<translation>进程</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="265"/>
|
||||
<source>No active TCP connections</source>
|
||||
<translation>无活跃 TCP 连接</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="265"/>
|
||||
<source>No matching connections</source>
|
||||
<translation>无匹配连接</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="340"/>
|
||||
<source>Total: </source>
|
||||
<translation>共计:</translation>
|
||||
</message>
|
||||
<message>
|
||||
<location filename="../package/components/TcpConnectionsWindow.qml" line="340"/>
|
||||
<source> connections</source>
|
||||
<translation> 个连接</translation>
|
||||
</message>
|
||||
</context>
|
||||
</TS>
|
||||
|
||||
Reference in New Issue
Block a user