feat: add group interface for DSettingsGroup
authorIceyer <me@iceyer.net>
Wed, 28 Feb 2018 06:48:48 +0000 (14:48 +0800)
committerIceyer <me@iceyer.net>
Wed, 28 Feb 2018 06:51:08 +0000 (14:51 +0800)
Change-Id: Iba5963b4d5f4b3dc28ac00b4d57a2f5c97c5defd

12 files changed:
src/settings/dsettings.cpp
src/settings/dsettings.h
src/settings/dsettingsgroup.cpp
src/settings/dsettingsgroup.h
src/settings/dsettingsoption.cpp
src/settings/dsettingsoption.h
tests/data.qrc [new file with mode: 0644]
tests/data/dt-settings.json [new file with mode: 0644]
tests/dutiltester.cpp
tests/dutiltester.h
tests/singletontester.h
tests/tests.pro

index 7edd233a84bb80942d118bb61ccef557e6767177..d5a66367f8302739b20e76a2f9a719c79a794a1f 100644 (file)
@@ -49,7 +49,7 @@ public:
 
 
 DSettings::DSettings(QObject *parent) :
-    QObject(parent), d_ptr(new DSettingsPrivate(this))
+    QObject(parent), dd_ptr(new DSettingsPrivate(this))
 {
 }
 
@@ -143,11 +143,25 @@ QList<QPointer<DSettingsGroup> > DSettings::groups() const
     Q_D(const DSettings);
     return d->childGroups.values();
 }
-
+/*!
+ * \brief DSettings::group will recurrence find childGroup
+ * \param key
+ * \return
+ */
 QPointer<DSettingsGroup> DSettings::group(const QString &key) const
 {
     Q_D(const DSettings);
-    return d->childGroups.value(key);
+    auto childKeylist = key.split(".");
+    if (0 >= childKeylist.length()) {
+        return nullptr;
+    }
+
+    auto mainGroupKey = childKeylist.value(0);
+    if (1 >= childKeylist.length()) {
+        return d->childGroups.value(mainGroupKey);
+    }
+
+    return d->childGroups.value(mainGroupKey)->childGroup(key);
 }
 
 QList<QPointer<DSettingsOption> > DSettings::options() const
index 60bf05f1094c6017dc7b4580831695b8e32ad78a..c8a8dfae4e88d21632197648ae02ef7d7a4d8204 100644 (file)
@@ -33,7 +33,7 @@ class DSettings : public QObject
 {
     Q_OBJECT
 public:
-    explicit DSettings(QObject *parent = 0);
+    explicit DSettings(QObject *parent = Q_NULLPTR);
     ~DSettings();
 
     void setBackend(DSettingsBackend *backend = nullptr);
@@ -69,8 +69,8 @@ private:
     void parseJson(const QByteArray &json);
     void loadValue();
 
-    QScopedPointer<DSettingsPrivate> d_ptr;
-    Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSettings)
+    QScopedPointer<DSettingsPrivate> dd_ptr;
+    Q_DECLARE_PRIVATE_D(qGetPtrHelper(dd_ptr), DSettings)
 };
 
 DCORE_END_NAMESPACE
index da68b43b6ab3e6c0c4cdf325b1fa8950ee1f825a..ea27766fdcdcae11a24aa9f725711f15bfc9dcc8 100644 (file)
@@ -47,7 +47,7 @@ public:
 };
 
 DSettingsGroup::DSettingsGroup(QObject *parent) :
-    QObject(parent), d_ptr(new DSettingsGroupPrivate(this))
+    QObject(parent), dd_ptr(new DSettingsGroupPrivate(this))
 {
 
 }
@@ -81,6 +81,12 @@ QString DSettingsGroup::name() const
     return d->name;
 }
 
+QPointer<DSettingsGroup> DSettingsGroup::childGroup(const QString &key) const
+{
+    Q_D(const DSettingsGroup);
+    return d->childGroups.value(key);
+}
+
 QList<QPointer<DSettingsGroup> > DSettingsGroup::childGroups() const
 {
     Q_D(const DSettingsGroup);
index b39229b5f7bea82038d3f1b2e40b169966eec602..97a5adb1447868b5a6b8996b7464060e4b3ff402 100644 (file)
@@ -31,7 +31,7 @@ class DSettingsGroup : public QObject
 {
     Q_OBJECT
 public:
-    explicit DSettingsGroup(QObject *parent = 0);
+    explicit DSettingsGroup(QObject *parent = Q_NULLPTR);
     ~DSettingsGroup();
 
     QPointer<DSettingsGroup> parentGroup() const;
@@ -40,6 +40,7 @@ public:
     QString key() const;
     QString name() const;
 
+    QPointer<DSettingsGroup> childGroup(const QString &key) const;
     QList<QPointer<DSettingsGroup> > childGroups() const;
     QList<QPointer<DSettingsOption> > childOptions() const;
     QList<QPointer<DSettingsOption> > options() const;
@@ -52,8 +53,8 @@ public Q_SLOTS:
 private:
     void parseJson(const QString &prefixKey, const QJsonObject &group);
 
-    QScopedPointer<DSettingsGroupPrivate> d_ptr;
-    Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSettingsGroup)
+    QScopedPointer<DSettingsGroupPrivate> dd_ptr;
+    Q_DECLARE_PRIVATE_D(qGetPtrHelper(dd_ptr), DSettingsGroup)
 };
 
 typedef QPointer<DSettingsGroup> GroupPtr;
index 0a69f02f388aa80d9ea03c7173c7a3ecb0d5bf4c..f807fd19176b00ba456b7fd12c836fcdc3a4bedc 100644 (file)
@@ -46,7 +46,7 @@ public:
 };
 
 DSettingsOption::DSettingsOption(QObject *parent) :
-    QObject(parent), d_ptr(new DSettingsOptionPrivate(this))
+    QObject(parent), dd_ptr(new DSettingsOptionPrivate(this))
 {
 }
 
index 346923d3f956659445a6a6bfc86aaf7a6c75b4e0..c1ccbfe475f58840fe824b8cb6f3a30efb96a437 100644 (file)
@@ -33,7 +33,7 @@ class DSettingsOption : public QObject
     Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
 
 public:
-    explicit DSettingsOption(QObject *parent = 0);
+    explicit DSettingsOption(QObject *parent = Q_NULLPTR);
     ~DSettingsOption();
 
     QPointer<DSettingsGroup> parentGroup() const;
@@ -61,8 +61,8 @@ public Q_SLOTS:
 private:
     void parseJson(const QString &prefixKey, const QJsonObject &option);
 
-    QScopedPointer<DSettingsOptionPrivate> d_ptr;
-    Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSettingsOption)
+    QScopedPointer<DSettingsOptionPrivate> dd_ptr;
+    Q_DECLARE_PRIVATE_D(qGetPtrHelper(dd_ptr), DSettingsOption)
 };
 
 typedef QPointer<DSettingsOption> OptionPtr;
diff --git a/tests/data.qrc b/tests/data.qrc
new file mode 100644 (file)
index 0000000..038939b
--- /dev/null
@@ -0,0 +1,5 @@
+<RCC>
+    <qresource prefix="/">
+        <file>data/dt-settings.json</file>
+    </qresource>
+</RCC>
diff --git a/tests/data/dt-settings.json b/tests/data/dt-settings.json
new file mode 100644 (file)
index 0000000..a94c4fc
--- /dev/null
@@ -0,0 +1,218 @@
+{
+    "groups": [
+        {
+            "key": "base",
+            "name": "Basic settings",
+            "groups": [
+                {
+                    "key": "theme",
+                    "name": "Theme",
+                    "options": [
+                        {
+                            "key": "theme",
+                            "type": "checkpicture",
+                            "default": 0
+                        },
+                        {
+                            "key": "opticy",
+                            "name": "Opticy",
+                            "type": "slider",
+                            "max": 100,
+                            "min": 0,
+                            "default": 90
+                        }
+                    ]
+                },
+                {
+                    "key": "font",
+                    "name": "Font Style",
+                    "options": [
+                        {
+                            "key": "family",
+                            "name": "Font",
+                            "type": "combobox",
+                            "default": ""
+                        },
+                        {
+                            "key": "size",
+                            "name": "Font Size",
+                            "type": "spinbutton",
+                            "default": 12
+                        },
+                        {
+                            "key": "style",
+                            "name": "Font Style",
+                            "type": "buttongroup",
+                            "items": ["B","/"],
+                            "default": 0
+                        }
+                    ]
+                }
+            ]
+        },
+        {
+            "key": "shortcuts",
+            "name": "Shortcuts",
+            "groups": [
+                {
+                    "key": "ternimal",
+                    "name": "Ternimal",
+                    "options": [
+                        {
+                            "key": "copy",
+                            "name": "Copy",
+                            "type": "shortcut",
+                            "default": "Ctrl+Alt+C"
+                        },
+                        {
+                            "key": "paste",
+                            "name": "Paste",
+                            "type": "shortcut",
+                            "default": "Ctrl+Alt+V"
+                        },
+                        {
+                            "key": "scroll_up",
+                            "name": "Scroll Up",
+                            "type": "shortcut",
+                            "default": "Alt+."
+                        },
+                        {
+                            "key": "scroll_down",
+                            "name": "Scroll down",
+                            "type": "shortcut",
+                            "default": "Alt+,"
+                        }
+                    ]
+                },
+                {
+                    "key": "workspace",
+                    "name": "Workspace",
+                    "options": [
+                        {
+                            "key": "new_window",
+                            "name": "New Window",
+                            "type": "shortcut",
+                            "default": "Ctrl+Shitf+<"
+                        },
+                        {
+                            "key": "next_tab",
+                            "name": "Next Tab",
+                            "type": "shortcut",
+                            "default": "Ctrl+N"
+                        },
+                        {
+                            "key": "prev_up",
+                            "name": "Previous Tab",
+                            "type": "shortcut",
+                            "default": "Ctrl+Shitf+>"
+                        },
+                        {
+                            "key": "close_tab",
+                            "name": "Close Tab",
+                            "type": "shortcut",
+                            "default": "Ctrl+W"
+                        }
+                    ]
+                }
+            ]
+        },
+        {
+            "key": "advance",
+            "name": "Advance",
+            "groups": [
+                {
+                    "key": "cursor",
+                    "name": "Cursor",
+                    "options": [
+                        {
+                            "key": "shrap",
+                            "name": "Cursor Shrap",
+                            "type": "buttongroup",
+                            "items": ["█","_","|"],
+                            "default": 0
+                        },
+                        {
+                            "key": "blink",
+                            "type": "checkbox",
+                            "text": "Cursor blink",
+                            "default": true
+                        },
+                        {
+                            "key": "radiogroup",
+                            "name": "  ",
+                            "type": "radiogroup",
+                            "items": ["Minimize to tray","Exit Deepin Music"],
+                            "default": 0
+                        }
+                    ]
+                },
+                {
+                    "key": "encoding",
+                    "name": "Default encoding",
+                    "options": [
+                        {
+                            "key": "encoding",
+                            "name": "Encoding",
+                            "type": "combobox",
+                            "default": "utf-8"
+                        }
+                    ]
+                },
+                {
+                    "key": "coustom",
+                    "name": "Coustom",
+                    "options": [
+                        {
+                            "key": "coustom_command",
+                            "name": "Coustom Command",
+                            "type": "lineedit",
+                            "default": ""
+                        },
+                        {
+                            "key": "coustom_directory",
+                            "name": "Coustom Directory",
+                            "type": "lineedit",
+                            "default": ""
+                        }
+                    ]
+                },
+                {
+                    "key": "scroll",
+                    "name": "Scroll",
+                    "options": [
+                        {
+                            "key": "scroll_bottom",
+                            "text": "Scroll Bottom",
+                            "type": "checkbox",
+                            "default": ""
+                        },
+                        {
+                            "key": "scroll_line_count",
+                            "name": "Scroll line count",
+                            "type": "spinbutton",
+                            "default": 10
+                        }
+                    ]
+                },
+                {
+                    "key": "compatibility",
+                    "name": "Compatibility",
+                    "options": [
+                        {
+                            "key": "breakspce_action",
+                            "name": "Breakspce Action",
+                            "type": "combobox",
+                            "default": ""
+                        },
+                        {
+                            "key": "delete_action",
+                            "name": "Delete Action",
+                            "type": "combobox",
+                            "default": ""
+                        }
+                    ]
+                }
+            ]
+        }
+    ]
+}
index 0cff403e0e39ab5a8126847632199d5a0f595575..2c63b96d6abb207de8562ba28794c2e6f54e2ca5 100644 (file)
 #include "singletontester.h"
 #include "util/dtimeunitformatter.h"
 #include "util/ddisksizeformatter.h"
+#include "settings/dsettings.h"
+#include "settings/dsettingsgroup.h"
+#include "settings/dsettingsoption.h"
 
 DCORE_USE_NAMESPACE
 
-TestDUtil::TestDUtil()
-{
-
-}
-
 void TestDUtil::testLogPath()
 {
     qApp->setOrganizationName("deepin");
@@ -154,3 +152,17 @@ void TestDUtil::testDiskFormatter1024()
     const auto d2 = diskFormatter.formatAs(100000000000, DDiskSizeFormatter::B, DDiskSizeFormatter::T);
     Q_ASSERT(qFuzzyCompare(0.09094947017729282, d2));
 }
+
+void TestDUtil::testGroups()
+{
+    auto path = ":/data/dt-settings.json";
+    auto settings = DSettings::fromJsonFile(path);
+
+    qDebug() << settings->groupKeys();
+    qDebug() << settings->group("shortcuts");
+    for (auto cg : settings->group("shortcuts")->childGroups()) {
+        qDebug() << cg->key();
+    }
+    qDebug() << settings->group("shortcuts.ternimal");
+    qDebug() << settings->group("shortcuts.ternimal")->options();
+}
index cab11cc583514fe1977748034899c186ebabd737..a17e4136f012786aab17445110c9678f4ae413d2 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef DUTILTESTER_H
-#define DUTILTESTER_H
+#pragma once
 
 #include <QObject>
 
 class TestDUtil: public QObject
 {
     Q_OBJECT
-public:
-    TestDUtil();
 
 private Q_SLOTS:
     void testLogPath();
@@ -35,6 +32,8 @@ private Q_SLOTS:
     void testDiskFormatter();
     void testDiskFormatterList();
     void testDiskFormatter1024();
+
+    void testGroups();
 };
 
-#endif // DUTILTESTER_H
+
index 8f13e870fc19086a31efe66bdc630cd4752fd347..3463bfe9f151c48fa0faed82e6f57de2244b3bcf 100644 (file)
@@ -26,7 +26,7 @@ class Singleton : public QObject, public Dtk::Core::DSingleton<Singleton>
     Q_OBJECT
     friend class Dtk::Core::DSingleton<Singleton>;
 public:
-    explicit Singleton(QObject *parent = 0);
+    explicit Singleton(QObject *parent = nullptr);
 
     void test();
 };
@@ -35,7 +35,7 @@ class MultiSingletonTester : public QObject
 {
     Q_OBJECT
 public:
-    explicit MultiSingletonTester(QObject *parent = 0);
+    explicit MultiSingletonTester(QObject *parent = nullptr);
 
     void run();
 };
index 12a8940316c007ba7132fb2a59bd141b30aee414..ecd8d4181586cb7933bf9a8d2178f2791a083b52 100644 (file)
@@ -24,3 +24,6 @@ else:unix: LIBS += -L$$OUT_PWD/../src/ -ldtkcore
 INCLUDEPATH += $$PWD/../src
 DEPENDPATH += $$PWD/../src
 QMAKE_RPATHDIR += $$PWD/../src
+
+RESOURCES += \
+    data.qrc