2025-11-11 15:21:39 +08:00
|
|
|
#include "powermonitor.h"
|
|
|
|
|
|
2025-11-11 20:12:39 +08:00
|
|
|
#ifdef Q_OS_LINUX
|
2025-11-11 20:39:04 +08:00
|
|
|
#include "platform/powermonitor_linux.h"
|
2025-11-11 20:12:39 +08:00
|
|
|
#elif defined(Q_OS_MAC)
|
2025-11-11 20:39:04 +08:00
|
|
|
#include "platform/powermonitor_macos.h"
|
2025-11-11 20:12:39 +08:00
|
|
|
#endif
|
2025-11-11 15:21:39 +08:00
|
|
|
|
2025-11-11 20:12:39 +08:00
|
|
|
#include <QDebug>
|
2025-11-11 15:21:39 +08:00
|
|
|
|
2025-11-11 20:12:39 +08:00
|
|
|
PowerMonitor::PowerMonitor(QObject *parent)
|
|
|
|
|
: PowerMonitorBase(parent)
|
|
|
|
|
, m_impl(nullptr)
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
m_impl = new PowerMonitorLinux(this);
|
|
|
|
|
qDebug() << "创建 Linux 电源监视器实例";
|
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
|
m_impl = new PowerMonitorMacOS(this);
|
|
|
|
|
qDebug() << "创建 macOS 电源监视器实例";
|
|
|
|
|
#else
|
|
|
|
|
qWarning() << "警告:当前平台不支持电源监视功能";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (m_impl) {
|
|
|
|
|
// 转发平台实现的信号到工厂类
|
|
|
|
|
connect(m_impl, &PowerMonitorBase::aboutToSleep,
|
|
|
|
|
this, &PowerMonitor::aboutToSleep);
|
|
|
|
|
connect(m_impl, &PowerMonitorBase::aboutToWakeUp,
|
|
|
|
|
this, &PowerMonitor::aboutToWakeUp);
|
|
|
|
|
connect(m_impl, &PowerMonitorBase::powerStateChanged,
|
|
|
|
|
this, &PowerMonitor::powerStateChanged);
|
|
|
|
|
|
|
|
|
|
// 自动初始化
|
|
|
|
|
m_impl->initialize();
|
2025-11-11 15:21:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 20:12:39 +08:00
|
|
|
PowerMonitor::~PowerMonitor()
|
|
|
|
|
{
|
|
|
|
|
// m_impl 会被 Qt 的父对象机制自动删除
|
2025-11-11 15:21:39 +08:00
|
|
|
}
|
2025-11-11 20:12:39 +08:00
|
|
|
|
|
|
|
|
bool PowerMonitor::initialize()
|
|
|
|
|
{
|
|
|
|
|
if (m_impl) {
|
|
|
|
|
return m_impl->initialize();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|