diff --git a/src/core/clock.cpp b/src/core/clock.cpp index ebb7e92a..90938d21 100644 --- a/src/core/clock.cpp +++ b/src/core/clock.cpp @@ -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); diff --git a/src/core/clock.hpp b/src/core/clock.hpp index 3e669589..67461911 100644 --- a/src/core/clock.hpp +++ b/src/core/clock.hpp @@ -7,9 +7,26 @@ #include <qtmetamacros.h> #include <qtypes.h> -#include "util.hpp" - ///! System clock accessor. +/// SystemClock is a view into the system's clock. +/// It updates at hour, minute, or second intervals depending on @@precision. +/// +/// # Examples +/// ```qml +/// SystemClock { +/// id: clock +/// precision: SystemClock.Seconds +/// } +/// +/// @@QtQuick.Text { +/// text: Qt.formatDateTime(clock.date, "hh:mm:ss - yyyy-MM-dd") +/// } +/// ``` +/// +/// > [!WARNING] Clock updates will trigger within 50ms of the system clock changing, +/// > however this can be either before or after the clock changes (+-50ms). If you +/// > need a date object, use @@date instead of constructing a new one, or the time +/// > of the constructed object could be off by up to a second. class SystemClock: public QObject { Q_OBJECT; /// If the clock should update. Defaults to true. @@ -18,12 +35,17 @@ class SystemClock: public QObject { Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged); /// The precision the clock should measure at. Defaults to `SystemClock.Seconds`. Q_PROPERTY(SystemClock::Enum precision READ precision WRITE setPrecision NOTIFY precisionChanged); + /// The current date and time. + /// + /// > [!TIP] You can use @@QtQml.Qt.formatDateTime() to get the time as a string in + /// > your format of choice. + Q_PROPERTY(QDateTime date READ date NOTIFY dateChanged); /// The current hour. - Q_PROPERTY(quint32 hours READ hours NOTIFY hoursChanged); + Q_PROPERTY(quint32 hours READ hours NOTIFY dateChanged); /// The current minute, or 0 if @@precision is `SystemClock.Hours`. - Q_PROPERTY(quint32 minutes READ minutes NOTIFY minutesChanged); + Q_PROPERTY(quint32 minutes READ minutes NOTIFY dateChanged); /// The current second, or 0 if @@precision is `SystemClock.Hours` or `SystemClock.Minutes`. - Q_PROPERTY(quint32 seconds READ seconds NOTIFY secondsChanged); + Q_PROPERTY(quint32 seconds READ seconds NOTIFY dateChanged); QML_ELEMENT; public: @@ -43,12 +65,15 @@ public: [[nodiscard]] SystemClock::Enum precision() const; void setPrecision(SystemClock::Enum precision); + [[nodiscard]] QDateTime date() const { return this->currentTime; } + [[nodiscard]] quint32 hours() const { return this->currentTime.time().hour(); } + [[nodiscard]] quint32 minutes() const { return this->currentTime.time().minute(); } + [[nodiscard]] quint32 seconds() const { return this->currentTime.time().second(); } + signals: void enabledChanged(); void precisionChanged(); - void hoursChanged(); - void minutesChanged(); - void secondsChanged(); + void dateChanged(); private slots: void onTimeout(); @@ -56,17 +81,11 @@ private slots: private: bool mEnabled = true; SystemClock::Enum mPrecision = SystemClock::Seconds; - quint32 mHours = 0; - quint32 mMinutes = 0; - quint32 mSeconds = 0; QTimer timer; + QDateTime currentTime; QDateTime targetTime; void update(); void setTime(const QDateTime& targetTime); void schedule(const QDateTime& targetTime); - - DECLARE_PRIVATE_MEMBER(SystemClock, hours, setHours, mHours, hoursChanged); - DECLARE_PRIVATE_MEMBER(SystemClock, minutes, setMinutes, mMinutes, minutesChanged); - DECLARE_PRIVATE_MEMBER(SystemClock, seconds, setSeconds, mSeconds, secondsChanged); };