diff --git a/ScreenPlaySysInfo/CMakeLists.txt b/ScreenPlaySysInfo/CMakeLists.txt index caa46928..4cfd681f 100644 --- a/ScreenPlaySysInfo/CMakeLists.txt +++ b/ScreenPlaySysInfo/CMakeLists.txt @@ -9,8 +9,22 @@ find_package( COMPONENTS Quick Core REQUIRED) -set(src screenplaysysinfo_plugin.cpp sysinfo.cpp cpu.cpp ram.cpp storage.cpp) -set(headers screenplaysysinfo_plugin.h sysinfo.h cpu.h ram.h mathhelper.h storage.h) +set(src + screenplaysysinfo_plugin.cpp + sysinfo.cpp + cpu.cpp + ram.cpp + storage.cpp + uptime.cpp) + +set(headers + screenplaysysinfo_plugin.h + sysinfo.h + cpu.h + ram.h + mathhelper.h + storage.h + uptime.h) add_library(${PROJECT_NAME} SHARED ${src} ${headers}) diff --git a/ScreenPlaySysInfo/ram.h b/ScreenPlaySysInfo/ram.h index 99e120d0..3a723f74 100644 --- a/ScreenPlaySysInfo/ram.h +++ b/ScreenPlaySysInfo/ram.h @@ -37,10 +37,10 @@ #include #include #include + #ifdef Q_OS_WIN #include #endif -//#include #define DWORDLONG unsigned long long diff --git a/ScreenPlaySysInfo/sysinfo.cpp b/ScreenPlaySysInfo/sysinfo.cpp index 895186c0..5c4348e9 100644 --- a/ScreenPlaySysInfo/sysinfo.cpp +++ b/ScreenPlaySysInfo/sysinfo.cpp @@ -5,5 +5,6 @@ SysInfo::SysInfo(QQuickItem* parent) , m_ram(std::make_unique()) , m_cpu(std::make_unique()) , m_storage(std::make_unique()) + , m_uptime(std::make_unique()) { } diff --git a/ScreenPlaySysInfo/sysinfo.h b/ScreenPlaySysInfo/sysinfo.h index 35be06c8..9ca2b337 100644 --- a/ScreenPlaySysInfo/sysinfo.h +++ b/ScreenPlaySysInfo/sysinfo.h @@ -40,6 +40,7 @@ #include "cpu.h" #include "ram.h" #include "storage.h" +#include "uptime.h" class SysInfo : public QQuickItem { Q_OBJECT @@ -47,33 +48,25 @@ class SysInfo : public QQuickItem { Q_PROPERTY(RAM* ram READ ram NOTIFY ramChanged) Q_PROPERTY(CPU* cpu READ cpu NOTIFY cpuChanged) Q_PROPERTY(Storage* storage READ storage NOTIFY storageChanged) + Q_PROPERTY(Uptime* uptime READ uptime NOTIFY uptimeChanged) public: SysInfo(QQuickItem* parent = nullptr); - ~SysInfo() { } - RAM* ram() const - { - return m_ram.get(); - } - - CPU* cpu() const - { - return m_cpu.get(); - } - - Storage* storage() const - { - return m_storage.get(); - } + RAM* ram() const { return m_ram.get(); } + CPU* cpu() const { return m_cpu.get(); } + Storage* storage() const { return m_storage.get(); } + Uptime* uptime() const { return m_uptime.get(); } signals: void ramChanged(RAM* ram); void cpuChanged(CPU* cpu); void storageChanged(Storage* storage); + void uptimeChanged(Uptime* uptime); private: std::unique_ptr m_ram; std::unique_ptr m_cpu; std::unique_ptr m_storage; + std::unique_ptr m_uptime; }; diff --git a/ScreenPlaySysInfo/uptime.cpp b/ScreenPlaySysInfo/uptime.cpp new file mode 100644 index 00000000..f850e731 --- /dev/null +++ b/ScreenPlaySysInfo/uptime.cpp @@ -0,0 +1,37 @@ +#include "uptime.h" + +#ifdef Q_OS_WINDOWS +#include +#include +#endif + +Uptime::Uptime(QObject* parent) + : QObject(parent) +{ + QObject::connect(&m_updateTimer, &QTimer::timeout, this, &Uptime::update); + m_updateTimer.start(m_tickRate); +} + +void Uptime::update() +{ +#ifdef Q_OS_WINDOWS + auto ticks = GetTickCount64(); + auto milliseconds = ticks % 1000; + ticks /= 1000; + auto seconds = ticks % 60; + ticks /= 60; + auto minutes = ticks % 60; + ticks /= 60; + auto hours = ticks; + ticks /= 24; + auto days = ticks; + ticks /= 365; + auto years = ticks; + + setSeconds(seconds); + setMinutes(minutes); + setHours(hours); + setDays(days); + setYears(years); +#endif +} diff --git a/ScreenPlaySysInfo/uptime.h b/ScreenPlaySysInfo/uptime.h new file mode 100644 index 00000000..444864ab --- /dev/null +++ b/ScreenPlaySysInfo/uptime.h @@ -0,0 +1,86 @@ +#pragma once +#include +#include + +class Uptime : public QObject { + Q_OBJECT + + Q_PROPERTY(int days READ days WRITE setDays NOTIFY daysChanged) + Q_PROPERTY(int years READ years WRITE setYears NOTIFY yearsChanged) + Q_PROPERTY(int hours READ hours WRITE setHours NOTIFY hoursChanged) + Q_PROPERTY(int minutes READ minutes WRITE setMinutes NOTIFY minutesChanged) + Q_PROPERTY(int seconds READ seconds WRITE setSeconds NOTIFY secondsChanged) + +public: + Uptime(QObject* parent = nullptr); + + int days() const { return m_days; } + int years() const { return m_years; } + int hours() const { return m_hours; } + int minutes() const { return m_minutes; } + int seconds() const { return m_seconds; } + +public slots: + void update(); + + void setDays(int days) + { + if (m_days == days) + return; + + m_days = days; + emit daysChanged(m_days); + } + void setYears(int years) + { + if (m_years == years) + return; + + m_years = years; + emit yearsChanged(m_years); + } + + void setHours(int hours) + { + if (m_hours == hours) + return; + + m_hours = hours; + emit hoursChanged(m_hours); + } + + void setMinutes(int minutes) + { + if (m_minutes == minutes) + return; + + m_minutes = minutes; + emit minutesChanged(m_minutes); + } + + void setSeconds(int seconds) + { + if (m_seconds == seconds) + return; + + m_seconds = seconds; + emit secondsChanged(m_seconds); + } + +signals: + void daysChanged(int days); + void yearsChanged(int years); + void hoursChanged(int hours); + void minutesChanged(int minutes); + void secondsChanged(int seconds); + +private: + int m_days = 0; + int m_years = 0; + int m_hours = 0; + int m_minutes = 0; + int m_seconds = 0; + + int m_tickRate = 1000; + QTimer m_updateTimer; +};