1.新增gsetting文件及读取gsetting功能

This commit is contained in:
hewenfei 2022-12-09 15:01:14 +08:00
parent 745f203940
commit 0e9ace9a5c
5 changed files with 110 additions and 5 deletions

View File

@ -75,6 +75,7 @@ set(SOURCE_FILES
set(QRC_FILES qml/qml.qrc res/res.qrc) set(QRC_FILES qml/qml.qrc res/res.qrc)
# desktop file # desktop file
set(DESKTOP_FILE data/ukui-menu.desktop) set(DESKTOP_FILE data/ukui-menu.desktop)
set(GSETTING_FILE data/org.ukui.menu.settings.gschema.xml)
# data files # data files
#set(DATA_FILES data/xxx) #set(DATA_FILES data/xxx)
@ -113,3 +114,4 @@ install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "/usr/bin")
install(FILES ${QM_FILES} DESTINATION "${UKUI_MENU_TRANSLATION_DIR}") install(FILES ${QM_FILES} DESTINATION "${UKUI_MENU_TRANSLATION_DIR}")
# desktop # desktop
install(FILES ${DESKTOP_FILE} DESTINATION "/etc/xdg/autostart") install(FILES ${DESKTOP_FILE} DESTINATION "/etc/xdg/autostart")
install(FILES ${GSETTING_FILE} DESTINATION "/usr/share/glib-2.0/schemas")

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema id="org.ukui.menu.settings" path="/">
<key type="i" name="animation-duration">
<default>300</default>
<summary>animation duration</summary>
<description>Control the duration of animation</description>
</key>
</schema>
</schemalist>

View File

@ -21,6 +21,8 @@
#include <QVariant> #include <QVariant>
#include <QDebug> #include <QDebug>
#define UKUI_MENU_SCHEMA "org.ukui.menu.settings"
#define UKUI_STYLE_SCHEMA "org.ukui.style" #define UKUI_STYLE_SCHEMA "org.ukui.style"
#define UKUI_STYLE_NAME_KEY "styleName" #define UKUI_STYLE_NAME_KEY "styleName"
@ -62,4 +64,59 @@ QVariant GlobalSetting::get(const GlobalSetting::Key& key)
return m_cache.value(key); return m_cache.value(key);
} }
MenuSetting *MenuSetting::instance()
{
static MenuSetting setting(nullptr);
return &setting;
}
MenuSetting::MenuSetting(QObject *parent) : QObject(parent)
{
initSettings();
}
void MenuSetting::initSettings()
{
m_cache.insert(UKUI_MENU_ANIMATION_DURATION, {300});
QByteArray id{UKUI_MENU_SCHEMA};
if (QGSettings::isSchemaInstalled(id)) {
m_gSettings = new QGSettings(id, "/", this);
if (!m_gSettings) {
qWarning() << "Unable to initialize settings.";
return;
}
for (const auto &key: m_gSettings->keys()) {
m_cache.insert(key, m_gSettings->get(key));
}
connect(m_gSettings, &QGSettings::changed, this, &MenuSetting::updateValue);
}
}
QVariant MenuSetting::get(const QString &key)
{
return m_cache.value(key, {});
}
bool MenuSetting::set(const QString &key, const QVariant &value)
{
if (m_gSettings) {
m_gSettings->set(key, value);
// m_cache.insert(key,value);
// Q_EMIT changed(key);
return true;
}
return false;
}
void MenuSetting::updateValue(const QString &key)
{
m_cache.insert(key, m_gSettings->get(key));
Q_EMIT changed(key);
}
} // UkuiMenu } // UkuiMenu

View File

@ -27,6 +27,9 @@
#define UKUI_STYLE_VALUE_LIGHT "ukui-light" #define UKUI_STYLE_VALUE_LIGHT "ukui-light"
#define UKUI_STYLE_VALUE_DEFAULT "ukui-default" #define UKUI_STYLE_VALUE_DEFAULT "ukui-default"
// ukui-menu settings
#define UKUI_MENU_ANIMATION_DURATION "animationDuration"
namespace UkuiMenu { namespace UkuiMenu {
class GlobalSetting : public QObject class GlobalSetting : public QObject
@ -57,6 +60,33 @@ private:
QMap<GlobalSetting::Key, QVariant> m_cache; QMap<GlobalSetting::Key, QVariant> m_cache;
}; };
class MenuSetting : public QObject
{
Q_OBJECT
public:
static MenuSetting *instance();
MenuSetting() = delete;
MenuSetting(const MenuSetting& obj) = delete;
MenuSetting(const MenuSetting&& obj) = delete;
Q_INVOKABLE QVariant get(const QString& key);
Q_INVOKABLE bool set(const QString& key, const QVariant& value);
Q_SIGNALS:
void changed(const QString& key);
private Q_SLOTS:
void updateValue(const QString& key);
private:
explicit MenuSetting(QObject *parent = nullptr);
void initSettings();
private:
QMap<QString, QVariant> m_cache;
QGSettings *m_gSettings{nullptr};
};
} // UkuiMenu } // UkuiMenu
#endif //UKUI_MENU_SETTINGS_H #endif //UKUI_MENU_SETTINGS_H

View File

@ -17,15 +17,16 @@
*/ */
#include "ukui-menu-application.h" #include "ukui-menu-application.h"
#include "settings.h"
#include "commons.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QQmlContext>
#include <QDebug> #include <QDebug>
using namespace UkuiMenu; using namespace UkuiMenu;
static UkuiMenuApplication* menuApplication = nullptr;
UkuiMenuApplication::UkuiMenuApplication(MenuMessageProcessor *processor) : QObject(nullptr) UkuiMenuApplication::UkuiMenuApplication(MenuMessageProcessor *processor) : QObject(nullptr)
{ {
registerQmlTypes(); registerQmlTypes();
@ -41,16 +42,21 @@ void UkuiMenuApplication::startUkuiMenu()
void UkuiMenuApplication::registerQmlTypes() void UkuiMenuApplication::registerQmlTypes()
{ {
const char *uri = "org.ukui.menu.core";
int versionMajor = 1, versionMinor = 0;
CommonsModule::defineCommonsMod(uri, versionMajor, versionMinor);
} }
void UkuiMenuApplication::initQmlEngine() void UkuiMenuApplication::initQmlEngine()
{ {
const QUrl url(QStringLiteral("qrc:/qml/main.qml"));
m_applicationEngine = new QQmlApplicationEngine(this); m_applicationEngine = new QQmlApplicationEngine(this);
m_applicationEngine->addImportPath("qrc:/qml"); m_applicationEngine->addImportPath("qrc:/qml");
const QUrl url(QStringLiteral("qrc:/qml/main.qml")); m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance());
QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated, QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated,
this, [url](QObject *obj, const QUrl &objUrl) { this, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) if (!obj && url == objUrl)
@ -88,7 +94,7 @@ void MenuMessageProcessor::processMessage(quint32 instanceId, const QByteArray &
QStringList options = QString(message).split(' '); QStringList options = QString(message).split(' ');
QCommandLineParser parser; QCommandLineParser parser;
QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu")); QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu."));
QCommandLineOption quitUkuiMenu({"q", "quit"}, QObject::tr("Quit ukui-menu.")); QCommandLineOption quitUkuiMenu({"q", "quit"}, QObject::tr("Quit ukui-menu."));
parser.addOption(showUkuiMenu); parser.addOption(showUkuiMenu);
@ -120,7 +126,7 @@ bool MenuMessageProcessor::preprocessMessage(const QStringList& message)
QCommandLineParser parser; QCommandLineParser parser;
QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu")); QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu."));
QCommandLineOption quitUkuiMenu({"q", "quit"}, QObject::tr("Quit ukui-menu.")); QCommandLineOption quitUkuiMenu({"q", "quit"}, QObject::tr("Quit ukui-menu."));
QCommandLineOption helpOption = parser.addHelpOption(); QCommandLineOption helpOption = parser.addHelpOption();
QCommandLineOption versionOption = parser.addVersionOption(); QCommandLineOption versionOption = parser.addVersionOption();