first commit
This commit is contained in:
156
widgets/statusbar.cpp
Normal file
156
widgets/statusbar.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
#include "statusbar.h"
|
||||
#include <QHBoxLayout>
|
||||
#include <QDateTime>
|
||||
#include <QPalette>
|
||||
#include <QProcess>
|
||||
#include <QRegularExpression>
|
||||
|
||||
StatusBar::StatusBar(QWidget *parent)
|
||||
: QWidget(parent), m_batteryLevel(100), m_networkConnected(true), m_networkType("WiFi")
|
||||
{
|
||||
setFixedHeight(40);
|
||||
setupUI();
|
||||
|
||||
// 定时更新时间
|
||||
m_timer = new QTimer(this);
|
||||
connect(m_timer, &QTimer::timeout, this, &StatusBar::updateTime);
|
||||
m_timer->start(1000);
|
||||
updateTime();
|
||||
|
||||
// 定时更新电池和WiFi状态(每10秒)
|
||||
QTimer *statusTimer = new QTimer(this);
|
||||
connect(statusTimer, &QTimer::timeout, this, &StatusBar::updateBatteryLevel);
|
||||
connect(statusTimer, &QTimer::timeout, this, &StatusBar::updateNetworkStatus);
|
||||
statusTimer->start(10000);
|
||||
updateBatteryLevel();
|
||||
updateNetworkStatus();
|
||||
}
|
||||
|
||||
StatusBar::~StatusBar()
|
||||
{
|
||||
}
|
||||
|
||||
void StatusBar::setupUI()
|
||||
{
|
||||
// 透明背景 - Android风格
|
||||
setAttribute(Qt::WA_TranslucentBackground);
|
||||
setStyleSheet("background: transparent;");
|
||||
|
||||
QHBoxLayout *layout = new QHBoxLayout(this);
|
||||
layout->setContentsMargins(24, 8, 24, 8);
|
||||
layout->setSpacing(20);
|
||||
|
||||
// 标题 - 使用白色带阴影效果
|
||||
m_titleLabel = new QLabel("智能校验仪", this);
|
||||
m_titleLabel->setStyleSheet(R"(
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
color: white;
|
||||
background: transparent;
|
||||
)");
|
||||
layout->addWidget(m_titleLabel);
|
||||
|
||||
layout->addStretch();
|
||||
|
||||
// 网络状态
|
||||
m_networkLabel = new QLabel("\u25CF", this); // 实心圆点
|
||||
m_networkLabel->setStyleSheet("font-size: 16px; color: rgba(255,255,255,0.9); background: transparent;");
|
||||
layout->addWidget(m_networkLabel);
|
||||
|
||||
// 电池状态
|
||||
m_batteryLabel = new QLabel("100%", this);
|
||||
m_batteryLabel->setStyleSheet("font-size: 13px; color: rgba(255,255,255,0.9); background: transparent;");
|
||||
layout->addWidget(m_batteryLabel);
|
||||
|
||||
// 时间
|
||||
m_timeLabel = new QLabel(this);
|
||||
m_timeLabel->setStyleSheet("font-size: 13px; min-width: 50px; color: rgba(255,255,255,0.9); background: transparent;");
|
||||
layout->addWidget(m_timeLabel);
|
||||
}
|
||||
|
||||
void StatusBar::setTitle(const QString &title)
|
||||
{
|
||||
m_titleLabel->setText(title);
|
||||
}
|
||||
|
||||
void StatusBar::setBatteryLevel(int level)
|
||||
{
|
||||
m_batteryLevel = qBound(0, level, 100);
|
||||
QString icon = m_batteryLevel > 20 ? "🔋" : "🪫";
|
||||
m_batteryLabel->setText(QString("%1 %2%").arg(icon).arg(m_batteryLevel));
|
||||
}
|
||||
|
||||
void StatusBar::setNetworkStatus(bool connected, const QString &type)
|
||||
{
|
||||
m_networkConnected = connected;
|
||||
m_networkType = type;
|
||||
|
||||
QString icon;
|
||||
if (!connected)
|
||||
{
|
||||
icon = "📵";
|
||||
}
|
||||
else if (type == "4G")
|
||||
{
|
||||
icon = "📶";
|
||||
}
|
||||
else
|
||||
{
|
||||
icon = "📶";
|
||||
}
|
||||
m_networkLabel->setText(icon);
|
||||
}
|
||||
|
||||
void StatusBar::updateTime()
|
||||
{
|
||||
QString timeStr = QDateTime::currentDateTime().toString("HH:mm");
|
||||
m_timeLabel->setText(timeStr);
|
||||
}
|
||||
|
||||
int StatusBar::getBatteryPercentage()
|
||||
{
|
||||
QProcess process;
|
||||
process.start("pmset", QStringList() << "-g" << "batt");
|
||||
process.waitForFinished(1000);
|
||||
QString output = process.readAllStandardOutput();
|
||||
|
||||
// 解析输出,格式如:Now drawing from 'Battery Power' -InternalBattery-0 (id=123456) 85%; discharging; 3:45 remaining
|
||||
QRegularExpression re("(\\d+)%");
|
||||
QRegularExpressionMatch match = re.match(output);
|
||||
if (match.hasMatch())
|
||||
{
|
||||
return match.captured(1).toInt();
|
||||
}
|
||||
return 100; // 默认值
|
||||
}
|
||||
|
||||
bool StatusBar::isWiFiConnected()
|
||||
{
|
||||
QProcess process;
|
||||
process.start("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport", QStringList() << "-I");
|
||||
process.waitForFinished(1000);
|
||||
QString output = process.readAllStandardOutput();
|
||||
|
||||
// 如果输出包含SSID,说明WiFi已连接
|
||||
return output.contains("SSID:");
|
||||
}
|
||||
|
||||
void StatusBar::updateBatteryLevel()
|
||||
{
|
||||
int level = getBatteryPercentage();
|
||||
setBatteryLevel(level);
|
||||
}
|
||||
|
||||
void StatusBar::updateNetworkStatus()
|
||||
{
|
||||
bool connected = isWiFiConnected();
|
||||
|
||||
if (connected)
|
||||
{
|
||||
m_networkLabel->setText("\u25CF"); // 实心圆点表示已连接
|
||||
}
|
||||
else
|
||||
{
|
||||
m_networkLabel->setText("\u25CB"); // 空心圆点表示未连接
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user