From: Iceyer Date: Wed, 28 Feb 2018 06:48:48 +0000 (+0800) Subject: feat: add group interface for DSettingsGroup X-Git-Tag: archive/raspbian/5.7.12-2+rpi1^2~130^2~5 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=c876180f535e3027dce63628f31379ef874367ed;p=dtkcore.git feat: add group interface for DSettingsGroup Change-Id: Iba5963b4d5f4b3dc28ac00b4d57a2f5c97c5defd --- diff --git a/src/settings/dsettings.cpp b/src/settings/dsettings.cpp index 7edd233..d5a6636 100644 --- a/src/settings/dsettings.cpp +++ b/src/settings/dsettings.cpp @@ -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 > DSettings::groups() const Q_D(const DSettings); return d->childGroups.values(); } - +/*! + * \brief DSettings::group will recurrence find childGroup + * \param key + * \return + */ QPointer 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 > DSettings::options() const diff --git a/src/settings/dsettings.h b/src/settings/dsettings.h index 60bf05f..c8a8dfa 100644 --- a/src/settings/dsettings.h +++ b/src/settings/dsettings.h @@ -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 d_ptr; - Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSettings) + QScopedPointer dd_ptr; + Q_DECLARE_PRIVATE_D(qGetPtrHelper(dd_ptr), DSettings) }; DCORE_END_NAMESPACE diff --git a/src/settings/dsettingsgroup.cpp b/src/settings/dsettingsgroup.cpp index da68b43..ea27766 100644 --- a/src/settings/dsettingsgroup.cpp +++ b/src/settings/dsettingsgroup.cpp @@ -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::childGroup(const QString &key) const +{ + Q_D(const DSettingsGroup); + return d->childGroups.value(key); +} + QList > DSettingsGroup::childGroups() const { Q_D(const DSettingsGroup); diff --git a/src/settings/dsettingsgroup.h b/src/settings/dsettingsgroup.h index b39229b..97a5adb 100644 --- a/src/settings/dsettingsgroup.h +++ b/src/settings/dsettingsgroup.h @@ -31,7 +31,7 @@ class DSettingsGroup : public QObject { Q_OBJECT public: - explicit DSettingsGroup(QObject *parent = 0); + explicit DSettingsGroup(QObject *parent = Q_NULLPTR); ~DSettingsGroup(); QPointer parentGroup() const; @@ -40,6 +40,7 @@ public: QString key() const; QString name() const; + QPointer childGroup(const QString &key) const; QList > childGroups() const; QList > childOptions() const; QList > options() const; @@ -52,8 +53,8 @@ public Q_SLOTS: private: void parseJson(const QString &prefixKey, const QJsonObject &group); - QScopedPointer d_ptr; - Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSettingsGroup) + QScopedPointer dd_ptr; + Q_DECLARE_PRIVATE_D(qGetPtrHelper(dd_ptr), DSettingsGroup) }; typedef QPointer GroupPtr; diff --git a/src/settings/dsettingsoption.cpp b/src/settings/dsettingsoption.cpp index 0a69f02..f807fd1 100644 --- a/src/settings/dsettingsoption.cpp +++ b/src/settings/dsettingsoption.cpp @@ -46,7 +46,7 @@ public: }; DSettingsOption::DSettingsOption(QObject *parent) : - QObject(parent), d_ptr(new DSettingsOptionPrivate(this)) + QObject(parent), dd_ptr(new DSettingsOptionPrivate(this)) { } diff --git a/src/settings/dsettingsoption.h b/src/settings/dsettingsoption.h index 346923d..c1ccbfe 100644 --- a/src/settings/dsettingsoption.h +++ b/src/settings/dsettingsoption.h @@ -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 parentGroup() const; @@ -61,8 +61,8 @@ public Q_SLOTS: private: void parseJson(const QString &prefixKey, const QJsonObject &option); - QScopedPointer d_ptr; - Q_DECLARE_PRIVATE_D(qGetPtrHelper(d_ptr), DSettingsOption) + QScopedPointer dd_ptr; + Q_DECLARE_PRIVATE_D(qGetPtrHelper(dd_ptr), DSettingsOption) }; typedef QPointer OptionPtr; diff --git a/tests/data.qrc b/tests/data.qrc new file mode 100644 index 0000000..038939b --- /dev/null +++ b/tests/data.qrc @@ -0,0 +1,5 @@ + + + data/dt-settings.json + + diff --git a/tests/data/dt-settings.json b/tests/data/dt-settings.json new file mode 100644 index 0000000..a94c4fc --- /dev/null +++ b/tests/data/dt-settings.json @@ -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": "" + } + ] + } + ] + } + ] +} diff --git a/tests/dutiltester.cpp b/tests/dutiltester.cpp index 0cff403..2c63b96 100644 --- a/tests/dutiltester.cpp +++ b/tests/dutiltester.cpp @@ -27,14 +27,12 @@ #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(); +} diff --git a/tests/dutiltester.h b/tests/dutiltester.h index cab11cc..a17e413 100644 --- a/tests/dutiltester.h +++ b/tests/dutiltester.h @@ -15,16 +15,13 @@ * along with this program. If not, see . */ -#ifndef DUTILTESTER_H -#define DUTILTESTER_H +#pragma once #include 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 + diff --git a/tests/singletontester.h b/tests/singletontester.h index 8f13e87..3463bfe 100644 --- a/tests/singletontester.h +++ b/tests/singletontester.h @@ -26,7 +26,7 @@ class Singleton : public QObject, public Dtk::Core::DSingleton Q_OBJECT friend class Dtk::Core::DSingleton; 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(); }; diff --git a/tests/tests.pro b/tests/tests.pro index 12a8940..ecd8d41 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -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