forked from quickshell/quickshell
core/clock: expose date as a QDateTime
This commit is contained in:
parent
325be8857c
commit
420529362f
2 changed files with 47 additions and 37 deletions
|
@ -6,8 +6,6 @@
|
|||
#include <qtmetamacros.h>
|
||||
#include <qtypes.h>
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
SystemClock::SystemClock(QObject* parent): QObject(parent) {
|
||||
QObject::connect(&this->timer, &QTimer::timeout, this, &SystemClock::onTimeout);
|
||||
this->update();
|
||||
|
@ -48,19 +46,16 @@ void SystemClock::update() {
|
|||
void SystemClock::setTime(const QDateTime& targetTime) {
|
||||
auto currentTime = QDateTime::currentDateTime();
|
||||
auto offset = currentTime.msecsTo(targetTime);
|
||||
auto dtime = offset > -500 && offset < 500 ? targetTime : currentTime;
|
||||
auto time = dtime.time();
|
||||
this->currentTime = offset > -500 && offset < 500 ? targetTime : currentTime;
|
||||
|
||||
auto secondPrecision = this->mPrecision >= SystemClock::Seconds;
|
||||
auto secondChanged = this->setSeconds(secondPrecision ? time.second() : 0);
|
||||
auto time = this->currentTime.time();
|
||||
this->currentTime.setTime(QTime(
|
||||
this->mPrecision >= SystemClock::Hours ? time.hour() : 0,
|
||||
this->mPrecision >= SystemClock::Minutes ? time.minute() : 0,
|
||||
this->mPrecision >= SystemClock::Seconds ? time.second() : 0
|
||||
));
|
||||
|
||||
auto minutePrecision = this->mPrecision >= SystemClock::Minutes;
|
||||
auto minuteChanged = this->setMinutes(minutePrecision ? time.minute() : 0);
|
||||
|
||||
auto hourPrecision = this->mPrecision >= SystemClock::Hours;
|
||||
auto hourChanged = this->setHours(hourPrecision ? time.hour() : 0);
|
||||
|
||||
DropEmitter::call(secondChanged, minuteChanged, hourChanged);
|
||||
emit this->dateChanged();
|
||||
}
|
||||
|
||||
void SystemClock::schedule(const QDateTime& targetTime) {
|
||||
|
@ -76,11 +71,11 @@ void SystemClock::schedule(const QDateTime& targetTime) {
|
|||
auto nextTime = offset > 0 && offset < 500 ? targetTime : currentTime;
|
||||
|
||||
auto baseTimeT = nextTime.time();
|
||||
nextTime.setTime(
|
||||
{hourPrecision ? baseTimeT.hour() : 0,
|
||||
minutePrecision ? baseTimeT.minute() : 0,
|
||||
secondPrecision ? baseTimeT.second() : 0}
|
||||
);
|
||||
nextTime.setTime(QTime(
|
||||
hourPrecision ? baseTimeT.hour() : 0,
|
||||
minutePrecision ? baseTimeT.minute() : 0,
|
||||
secondPrecision ? baseTimeT.second() : 0
|
||||
));
|
||||
|
||||
if (secondPrecision) nextTime = nextTime.addSecs(1);
|
||||
else if (minutePrecision) nextTime = nextTime.addSecs(60);
|
||||
|
@ -91,7 +86,3 @@ void SystemClock::schedule(const QDateTime& targetTime) {
|
|||
this->timer.start(static_cast<qint32>(delay));
|
||||
this->targetTime = nextTime;
|
||||
}
|
||||
|
||||
DEFINE_MEMBER_GETSET(SystemClock, hours, setHours);
|
||||
DEFINE_MEMBER_GETSET(SystemClock, minutes, setMinutes);
|
||||
DEFINE_MEMBER_GETSET(SystemClock, seconds, setSeconds);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue