runningwater 2 سال پیش
کامیت
036a1a7bf6
11فایلهای تغییر یافته به همراه229 افزوده شده و 0 حذف شده
  1. 36 0
      ch02-sysinfo.pro
  2. 11 0
      main.cpp
  3. 15 0
      mainwindow.cpp
  4. 21 0
      mainwindow.h
  5. 22 0
      mainwindow.ui
  6. 11 0
      sysinfo.cpp
  7. 19 0
      sysinfo.h
  8. 18 0
      sysinfomacimpl.cpp
  9. 20 0
      sysinfomacimpl.h
  10. 26 0
      sysinfowindowsimpl.cpp
  11. 30 0
      sysinfowindowsimpl.h

+ 36 - 0
ch02-sysinfo.pro

@@ -0,0 +1,36 @@
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+CONFIG += c++17
+
+# You can make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+    main.cpp \
+    mainwindow.cpp \
+    sysinfo.cpp
+
+HEADERS += \
+    mainwindow.h \
+    sysinfo.h
+
+windows {
+    SOURCES += sysinfowindowsimpl.cpp
+    HEADERS += sysinfowindowsimpl.h
+}
+
+macx {
+    SOURCES += sysinfomacimpl.cpp
+    HEADERS += sysinfomacimpl.h
+}
+
+FORMS += \
+    mainwindow.ui
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target

+ 11 - 0
main.cpp

@@ -0,0 +1,11 @@
+#include "mainwindow.h"
+
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    MainWindow w;
+    w.show();
+    return a.exec();
+}

+ 15 - 0
mainwindow.cpp

@@ -0,0 +1,15 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+
+MainWindow::MainWindow(QWidget *parent)
+    : QMainWindow(parent)
+    , ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}
+

+ 21 - 0
mainwindow.h

@@ -0,0 +1,21 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class MainWindow; }
+QT_END_NAMESPACE
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    MainWindow(QWidget *parent = nullptr);
+    ~MainWindow();
+
+private:
+    Ui::MainWindow *ui;
+};
+#endif // MAINWINDOW_H

+ 22 - 0
mainwindow.ui

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralwidget"/>
+  <widget class="QMenuBar" name="menubar"/>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 11 - 0
sysinfo.cpp

@@ -0,0 +1,11 @@
+#include "sysinfo.h"
+
+SysInfo::SysInfo()
+{
+
+}
+
+SysInfo::~SysInfo()
+{
+
+}

+ 19 - 0
sysinfo.h

@@ -0,0 +1,19 @@
+#ifndef SYSINFO_H
+#define SYSINFO_H
+
+/**
+ * @brief The SysInfo 接口
+ */
+class SysInfo {
+ public:
+  SysInfo();
+  virtual ~SysInfo();
+
+  // Allows the derived class to perform any initialization process depending on
+  // the OS platform
+  virtual void init() = 0;
+  virtual double cpuLoadAverage() = 0;
+  virtual double memoryUsed() = 0;
+};
+
+#endif  // SYSINFO_H

+ 18 - 0
sysinfomacimpl.cpp

@@ -0,0 +1,18 @@
+#include "sysinfomacimpl.h"
+
+SysInfoMacImpl::SysInfoMacImpl() : SysInfo() {}
+
+void SysInfoMacImpl::init()
+{
+
+}
+
+double SysInfoMacImpl::cpuLoadAverage()
+{
+
+}
+
+double SysInfoMacImpl::memoryUsed()
+{
+
+}

+ 20 - 0
sysinfomacimpl.h

@@ -0,0 +1,20 @@
+#ifndef SYSINFOMACIMPL_H
+#define SYSINFOMACIMPL_H
+
+#include <QVector>
+#include <QtGlobal>
+
+#include "sysinfo.h"
+
+class SysInfoMacImpl : public SysInfo {
+ public:
+  SysInfoMacImpl();
+
+  // SysInfo interface
+ public:
+  void init() override;
+  double cpuLoadAverage() override;
+  double memoryUsed() override;
+};
+
+#endif  // SYSINFOMACIMPL_H

+ 26 - 0
sysinfowindowsimpl.cpp

@@ -0,0 +1,26 @@
+#include "sysinfowindowsimpl.h"
+
+SysInfoWindowsImpl::SysInfoWindowsImpl() : SysInfo() {}
+
+void SysInfoWindowsImpl::init() {}
+
+double SysInfoWindowsImpl::cpuLoadAverage() {
+  QVector<qulonglong> firstSample = mCpuLoadLastValues;
+  QVector<qulonglong> secondSample = cpuRawData();
+  mCpuLoadLastValues = secondSample;
+  qulonglong currentIdle = secondSample[0] - firstSample[0];
+  qulonglong currentKernel = secondSample[1] - firstSample[1];
+  qulonglong currentUser = secondSample[2] - firstSample[2];
+  qulonglong currentSystem = currentKernel + currentUser;
+  double percent = (currentSystem - currentIdle) * 100.0 / currentSystem;
+  return qBound(0.0, percent, 100.0);
+}
+
+double SysInfoWindowsImpl::memoryUsed() {
+  MEMORYSTATUSEX memoryStatus;
+  memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
+  GlobalMemoryStatusEx(&memoryStatus);
+  qulonglong memoryPhysicalUsed =
+      memoryStatus.ullTotalPhys - memoryStatus.ullAvailPhys;
+  return (double)memoryPhysicalUsed / (double)memoryStatus.ullTotalPhys * 100.0;
+}

+ 30 - 0
sysinfowindowsimpl.h

@@ -0,0 +1,30 @@
+#ifndef SYSINFOWINDOWSIMPL_H
+#define SYSINFOWINDOWSIMPL_H
+#ifdef __WINDOWS__
+#include <windows.h>
+#endif
+#include <QVector>
+#include <QtGlobal>
+
+#include "sysinfo.h"
+
+typedef struct _FILETIME FILETIME;
+
+class SysInfoWindowsImpl : public SysInfo {
+ public:
+  SysInfoWindowsImpl();
+
+  // SysInfo interface
+  void init() override;
+  double cpuLoadAverage() override;
+  double memoryUsed() override;
+
+ private:
+  QVector<qulonglong> cpuRawData();
+  qulonglong convertFileTime(const FILETIME& filetime) const;
+
+ private:
+  QVector<qulonglong> mCpuLoadLastValues;
+};
+
+#endif  // SYSINFOWINDOWSIMPL_H