Track and display screen lock info

Record lock time, duration and count in CustomWidget and show a
lock-info overlay. setPaintingEnabled now logs lock/unlock times and
increments lock count. Emit screenLocked/screenUnlocked signals in
ScreenLockDetector. Minor UI and whitespace cleanup.
This commit is contained in:
ubuntu1804 2025-11-08 11:37:10 +08:00
parent 7e919ddef9
commit 7e468e0232
4 changed files with 167 additions and 95 deletions

View File

@ -12,6 +12,8 @@ CustomWidget::CustomWidget(QWidget *parent)
, m_rotationAngle(0.0)
, m_wavePhase(0.0)
, m_startTime(QDateTime::currentDateTime())
, m_lastLockDuration(0)
, m_lockCount(0)
{
// 设置窗口属性
setMinimumSize(600, 400);
@ -40,11 +42,25 @@ void CustomWidget::setPaintingEnabled(bool enabled)
if (enabled) {
qDebug() << "Painting ENABLED - Resuming animations";
m_animationTimer->start(16);
// 解锁:计算锁屏持续时间
if (m_lastLockTime.isValid()) {
QDateTime unlockTime = QDateTime::currentDateTime();
m_lastLockDuration = m_lastLockTime.secsTo(unlockTime);
qDebug() << "Screen was locked for" << m_lastLockDuration << "seconds";
}
m_startTime = QDateTime::currentDateTime();
} else {
qDebug() << "Painting DISABLED - Stopping animations";
m_animationTimer->stop();
// 锁屏:记录锁屏时间
m_pauseTime = QDateTime::currentDateTime();
m_lastLockTime = m_pauseTime;
m_lockCount++;
qDebug() << "Screen locked at" << m_lastLockTime.toString("yyyy-MM-dd hh:mm:ss")
<< "- Lock count:" << m_lockCount;
}
// 触发重绘以更新状态显示
@ -268,6 +284,10 @@ void CustomWidget::drawStatusInfo(QPainter &painter)
painter.setPen(QColor(200, 255, 200));
painter.drawText(statsRect.adjusted(10, 5, -10, -5), Qt::AlignLeft | Qt::AlignTop, stats);
if (m_lastLockTime.isValid()) {
drawLockInfo(painter);
}
// 绘制提示信息
painter.setPen(QColor(255, 255, 150));
infoFont.setPointSize(11);
@ -279,3 +299,47 @@ void CustomWidget::drawStatusInfo(QPainter &painter)
painter.restore();
}
void CustomWidget::drawLockInfo(QPainter &painter)
{
QFont infoFont = painter.font();
QRect lockInfoRect(10, 180, 350, 110);
painter.fillRect(lockInfoRect, QColor(60, 0, 60, 180));
painter.setPen(QColor(255, 200, 255));
infoFont.setBold(true);
infoFont.setPointSize(11);
painter.setFont(infoFont);
painter.drawText(lockInfoRect.adjusted(10, 5, -10, -5), Qt::AlignLeft | Qt::AlignTop,
"Last Lock Info:");
infoFont.setBold(false);
infoFont.setPointSize(11);
painter.setFont(infoFont);
painter.setPen(QColor(255, 220, 255));
// 格式化锁屏时长
QString durationStr;
if (m_lastLockDuration < 60) {
durationStr = QString("%1 seconds").arg(m_lastLockDuration);
} else if (m_lastLockDuration < 3600) {
int minutes = m_lastLockDuration / 60;
int seconds = m_lastLockDuration % 60;
durationStr = QString("%1m %2s").arg(minutes).arg(seconds);
} else {
int hours = m_lastLockDuration / 3600;
int minutes = (m_lastLockDuration % 3600) / 60;
int seconds = m_lastLockDuration % 60;
durationStr = QString("%1h %2m %3s").arg(hours).arg(minutes).arg(seconds);
}
QString lockInfo = QString(
"\nLock Time: %1\n"
"Duration: %2\n"
"Total Locks: %3"
).arg(m_lastLockTime.toString("yyyy-MM-dd hh:mm:ss"))
.arg(durationStr)
.arg(m_lockCount);
painter.drawText(lockInfoRect.adjusted(10, 25, -10, -5), Qt::AlignLeft | Qt::AlignTop, lockInfo);
}

View File

@ -83,6 +83,12 @@ private:
*/
void drawStatusInfo(QPainter &painter);
/**
* @brief
* @param painter
*/
void drawLockInfo(QPainter &painter);
private:
QTimer *m_animationTimer; // 动画定时器
bool m_paintingEnabled; // 绘制是否启用
@ -91,6 +97,9 @@ private:
double m_wavePhase; // 波浪相位
QDateTime m_startTime; // 开始时间
QDateTime m_pauseTime; // 暂停时间
QDateTime m_lastLockTime; // 上次锁屏时间
qint64 m_lastLockDuration; // 上次锁屏持续时长(秒)
int m_lockCount; // 锁屏次数
};
#endif // CUSTOMWIDGET_H

View File

@ -148,7 +148,6 @@ void MainWindow::onScreenLocked()
// 停止绘制
if (m_customWidget) {
m_customWidget->setPaintingEnabled(false);
m_customWidget->resetFrameCount();
}
updateStatusDisplay();

View File

@ -78,12 +78,12 @@ void ScreenLockDetector::setLockState(bool locked)
qDebug() << "## Screen lock state changed:" << (locked ? "LOCKED" : "UNLOCKED");
qDebug() << "##################################################";
// emit lockStateChanged(locked);
emit lockStateChanged(locked);
if (locked) {
emit screenLocked();
} else {
// emit screenUnlocked();
emit screenUnlocked();
}
}
}