2022-06-02 16:31:24 +08:00
|
|
|
|
/*
|
2024-01-19 14:12:25 +08:00
|
|
|
|
* Copyright (C) 2023, KylinSoft Co., Ltd.
|
2022-06-02 16:31:24 +08:00
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
|
#include "sleeptime.h"
|
|
|
|
|
#include <QLabel>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QListWidget>
|
2024-01-15 16:03:34 +08:00
|
|
|
|
#include <QApplication>
|
2022-06-02 16:31:24 +08:00
|
|
|
|
|
|
|
|
|
SleepTime::SleepTime(QWidget *parent) : QWidget(parent),
|
2024-01-15 16:03:34 +08:00
|
|
|
|
sleepTime(0),
|
|
|
|
|
m_nLastSleepLeave(0),
|
|
|
|
|
m_nLastSleepTimeSecs(0),
|
|
|
|
|
configuration(SCConfiguration::instance())
|
2022-06-02 16:31:24 +08:00
|
|
|
|
{
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SleepTime::~SleepTime()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SleepTime::init()
|
|
|
|
|
{
|
|
|
|
|
layout = new QHBoxLayout(this);
|
|
|
|
|
layout->setDirection(QBoxLayout::RightToLeft);
|
2024-01-15 16:03:34 +08:00
|
|
|
|
layout->setSpacing(8);
|
|
|
|
|
curFontSize = configuration->getFontSize();
|
|
|
|
|
sysFont = qApp->font();
|
|
|
|
|
sysFont.setPointSize((20 + curFontSize) *m_ptToPx);
|
|
|
|
|
for(int i=0;i<3;i++)
|
2022-06-02 16:31:24 +08:00
|
|
|
|
{
|
|
|
|
|
QLabel *label = new QLabel(this);
|
|
|
|
|
label->setText("0");
|
|
|
|
|
label->setFixedSize(40,40);
|
|
|
|
|
label->setObjectName("clockTime");
|
2024-01-15 16:03:34 +08:00
|
|
|
|
label->setFont(sysFont);
|
2022-06-02 16:31:24 +08:00
|
|
|
|
list.append(label);
|
2024-01-15 16:03:34 +08:00
|
|
|
|
if (i < 2) {
|
|
|
|
|
QLabel *colon = new QLabel(this);
|
|
|
|
|
colon->setText(":");
|
|
|
|
|
colon->setObjectName("colon");
|
|
|
|
|
colon->setFont(sysFont);
|
|
|
|
|
list.append(colon);
|
|
|
|
|
}
|
2022-06-02 16:31:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i=0;i<list.count();i++)
|
|
|
|
|
{
|
|
|
|
|
layout->addWidget(list.at(i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
restTime = new QLabel(this);
|
2024-01-15 16:03:34 +08:00
|
|
|
|
sysFont.setPointSize((20 + curFontSize) *m_ptToPx);
|
|
|
|
|
restTime->setFont(sysFont);
|
|
|
|
|
restTime->setText(tr("You have rested"));
|
2022-06-02 16:31:24 +08:00
|
|
|
|
restTime->setObjectName("restTime");
|
|
|
|
|
restTime->setAlignment(Qt::AlignBottom);
|
|
|
|
|
restTime->adjustSize();
|
|
|
|
|
|
2024-01-15 16:03:34 +08:00
|
|
|
|
layout->addSpacing(8);
|
2022-06-02 16:31:24 +08:00
|
|
|
|
layout->addWidget(restTime);
|
|
|
|
|
|
|
|
|
|
initTime = QDateTime::currentDateTime();
|
2024-01-15 16:03:34 +08:00
|
|
|
|
m_lastTime = initTime;
|
2022-06-02 16:31:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SleepTime::setTime(QDateTime time)
|
|
|
|
|
{
|
2024-01-15 16:03:34 +08:00
|
|
|
|
// 与上一次取时间的时间差
|
|
|
|
|
long nNewSleepTime = qAbs(m_lastTime.msecsTo(time));
|
|
|
|
|
sleepTime = qAbs(initTime.msecsTo(time));
|
|
|
|
|
// 时间差大于1s,则认为时间异常变化,保存已过去的时间
|
|
|
|
|
if (nNewSleepTime > 1000) {
|
|
|
|
|
m_nLastSleepLeave += qAbs(m_lastTime.msecsTo(initTime));
|
|
|
|
|
sleepTime = 0;
|
|
|
|
|
initTime = time;
|
|
|
|
|
}
|
|
|
|
|
m_lastTime = time;
|
|
|
|
|
//当前时间差+异常情况过去的时间
|
|
|
|
|
sleepTime += m_nLastSleepLeave;
|
|
|
|
|
sleepTime = sleepTime/1000;
|
|
|
|
|
|
|
|
|
|
if (m_nLastSleepTimeSecs == 0 || qAbs(sleepTime - m_nLastSleepTimeSecs) >= 1) {
|
|
|
|
|
int hour = sleepTime / 3600;
|
|
|
|
|
int sec = sleepTime % 3600 % 60;
|
|
|
|
|
int min = sleepTime % 3600 / 60;
|
|
|
|
|
setHour(hour);
|
|
|
|
|
setSeconds(sec);
|
|
|
|
|
setMinute(min);
|
|
|
|
|
m_nLastSleepTimeSecs = sleepTime;
|
2022-06-02 16:31:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 16:03:34 +08:00
|
|
|
|
void SleepTime::setHour(int hour)
|
2022-06-02 16:31:24 +08:00
|
|
|
|
{
|
2024-01-15 16:03:34 +08:00
|
|
|
|
QString time;
|
|
|
|
|
if (hour >= 100) {
|
|
|
|
|
QLabelSetText(list.at(4), QString::number(hour));
|
|
|
|
|
} else if (hour < 10) {
|
|
|
|
|
time = "0" + QString::number(hour);
|
|
|
|
|
list.at(4)->setText(time);
|
|
|
|
|
} else {
|
|
|
|
|
time = QString::number(hour);
|
|
|
|
|
list.at(4)->setText(time);
|
|
|
|
|
}
|
2022-06-02 16:31:24 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 16:03:34 +08:00
|
|
|
|
void SleepTime::setSeconds(int seconds)
|
2022-06-02 16:31:24 +08:00
|
|
|
|
{
|
2024-01-15 16:03:34 +08:00
|
|
|
|
QString time;
|
|
|
|
|
if (seconds < 10) {
|
|
|
|
|
time = "0" + QString::number(seconds);
|
|
|
|
|
} else {
|
|
|
|
|
time = QString::number(seconds);
|
|
|
|
|
}
|
|
|
|
|
list.at(0)->setText(time);
|
|
|
|
|
}
|
2022-06-02 16:31:24 +08:00
|
|
|
|
|
2024-01-15 16:03:34 +08:00
|
|
|
|
void SleepTime::setMinute(int minutes)
|
|
|
|
|
{
|
|
|
|
|
QString time;
|
|
|
|
|
if (minutes < 10) {
|
|
|
|
|
time = "0" + QString::number(minutes);
|
|
|
|
|
} else {
|
|
|
|
|
time = QString::number(minutes);
|
|
|
|
|
}
|
|
|
|
|
list.at(2)->setText(time);
|
2022-06-02 16:31:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SleepTime::setSmallMode()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0;i<5;i++)
|
|
|
|
|
list.at(i)->setFixedSize(8,8);
|
|
|
|
|
adjustSize();
|
|
|
|
|
}
|
2024-01-15 16:03:34 +08:00
|
|
|
|
|
|
|
|
|
bool SleepTime::QLabelSetText(QLabel *label, QString string)
|
|
|
|
|
{
|
|
|
|
|
bool is_over_length = false;
|
|
|
|
|
QFontMetrics fontMetrics(label->font());
|
|
|
|
|
int fontSize = fontMetrics.width(string);
|
|
|
|
|
QString str = string;
|
|
|
|
|
if (fontSize > (label->width()-5)) {
|
|
|
|
|
str = fontMetrics.elidedText(string, Qt::ElideRight, label->width());
|
|
|
|
|
is_over_length = true;
|
|
|
|
|
}
|
|
|
|
|
label->setText(str);
|
|
|
|
|
return is_over_length;
|
|
|
|
|
}
|
|
|
|
|
|