Bläddra i källkod

add global settings

xuqiang 3 månader sedan
förälder
incheckning
a45262f262
5 ändrade filer med 120 tillägg och 0 borttagningar
  1. 3 0
      include/appcontext.h
  2. 3 0
      include/defs.h
  3. 24 0
      include/globalsettings.h
  4. 5 0
      src/appcontext.cpp
  5. 85 0
      src/globalsettings.cpp

+ 3 - 0
include/appcontext.h

@@ -13,6 +13,7 @@
 #include "datadict.h"
 #include "sysstate.h"
 #include "mcumodule.h"
+#include "globalsettings.h"
 
 class AppContext : public QObject
 {
@@ -34,6 +35,7 @@ public:
     DataDict &dataDict();
     SysState &sysState();
     MCUModule &mcuModule();
+    GlobalSettings &globalSettings();
 
 private:
     PluginManager m_pluginMgr;
@@ -47,6 +49,7 @@ private:
     DataDict m_dataDict;
     SysState m_sysState;
     MCUModule m_mcuModule;
+    GlobalSettings m_globalSettings;
 };
 
 #endif  // APPCONTEXT_H

+ 3 - 0
include/defs.h

@@ -20,4 +20,7 @@
 #define MOUNT_ROOTFS    "/"
 #define MOUNT_USERDATA  "/userdata"
 
+// global settings file
+#define GLOBAL_SETTINGS_FILE "storage/conf/settings.ini"
+
 #endif

+ 24 - 0
include/globalsettings.h

@@ -0,0 +1,24 @@
+#ifndef __GLOBALSETTINGS_H__
+#define __GLOBALSETTINGS_H__
+
+#include <QObject>
+#include <QJsonObject>
+#include <QVariant>
+
+class GlobalSettings : public QObject
+{
+    Q_OBJECT
+
+public:
+    explicit GlobalSettings(QObject *parent = nullptr);
+    ~GlobalSettings();
+
+    QVariant value(const QString &group, const QString &key);
+    void setValue(const QString &group, const QString &key, const QVariant &value);
+
+private:
+    QJsonObject m_globalSettings;
+
+};
+
+#endif  // GLOBALSETTINGS_H

+ 5 - 0
src/appcontext.cpp

@@ -81,3 +81,8 @@ MCUModule &AppContext::mcuModule()
 {
     return m_mcuModule;
 }
+
+GlobalSettings &AppContext::globalSettings()
+{
+    return m_globalSettings;
+}

+ 85 - 0
src/globalsettings.cpp

@@ -0,0 +1,85 @@
+#include "globalsettings.h"
+#include "defs.h"
+
+#include <QSettings>
+
+QJsonValue variantToJsonValue(const QVariant &value)
+{
+    // 自动转换常见类型
+    switch (value.type()) {
+    case QVariant::Bool:
+        return QJsonValue(value.toBool());
+    case QVariant::Int:
+    case QVariant::LongLong:
+        return QJsonValue(value.toLongLong());
+    case QVariant::Double:
+        return QJsonValue(value.toDouble());
+    default:
+        return QJsonValue(value.toString());
+    }
+}
+
+GlobalSettings::GlobalSettings(QObject *parent)
+    : QObject{parent}
+{
+    QSettings settings(GLOBAL_SETTINGS_FILE, QSettings::IniFormat);
+    settings.setIniCodec("UTF-8");
+
+
+    for (const QString &group : settings.childGroups()) {
+        settings.beginGroup(group);
+
+        QJsonObject groupObj;
+        for (const QString &key : settings.childKeys()) {
+            QVariant value = settings.value(key);
+            groupObj.insert(key, variantToJsonValue(value));
+        }
+
+        settings.endGroup();
+        m_globalSettings.insert(group, groupObj);
+    }
+}
+
+GlobalSettings::~GlobalSettings()
+{
+
+}
+
+QVariant GlobalSettings::value(const QString &group, const QString &key)
+{
+    QVariant value;
+
+    if (!m_globalSettings.contains(group) || !m_globalSettings.value(group).isObject())
+        return value;
+
+    QJsonObject groupObj = m_globalSettings.value(group).toObject();
+
+    if (!groupObj.contains(key))
+        return value;
+
+    return groupObj.value(key).toVariant();
+}
+
+void GlobalSettings::setValue(const QString &group, const QString &key, const QVariant &value)
+{
+    QJsonObject groupObj;
+
+    // 如果 group 已存在且是 object,取出来
+    if (m_globalSettings.contains(group) &&
+        m_globalSettings.value(group).isObject())
+    {
+        groupObj = m_globalSettings.value(group).toObject();
+    }
+
+    // 设置 / 覆盖 key
+    groupObj.insert(key, QJsonValue::fromVariant(value));
+
+    // 写回 group
+    m_globalSettings.insert(group, groupObj);
+
+    QSettings settings(GLOBAL_SETTINGS_FILE, QSettings::IniFormat);
+    settings.setIniCodec("UTF-8");
+    settings.beginGroup(group);
+    settings.setValue(key, value);
+    settings.endGroup();
+}