增加监听主题gsetting功能,新增颜色管理后端,新增基础数据类型

This commit is contained in:
hewenfei 2022-12-07 13:49:52 +08:00
parent ef39b03c44
commit 2d8e7e85c2
9 changed files with 439 additions and 19 deletions

View File

@ -13,7 +13,7 @@ set(CMAKE_AUTORCC ON)
# qt
find_package(Qt5 COMPONENTS
Core Quick LinguistTools DBus X11Extras
Core Gui Quick LinguistTools DBus X11Extras
REQUIRED)
# find kde framework components
@ -58,7 +58,10 @@ add_compile_definitions(UKUI_MENU_TRANSLATION_DIR="${UKUI_MENU_TRANSLATION_DIR}"
# ukui-menu
set(SOURCE_FILES
src/main.cpp
src/commons.h
src/model/model.cpp src/model/model.h
src/settings/settings.cpp src/settings/settings.h
src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h
)
# qrc
@ -87,6 +90,7 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONF
target_link_libraries(${PROJECT_NAME}
PRIVATE
Qt5::Core
Qt5::Gui
Qt5::Quick
Qt5::DBus
Qt5::X11Extras

101
src/commons.h Normal file
View File

@ -0,0 +1,101 @@
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_COMMONS_H
#define UKUI_MENU_COMMONS_H
#include <QObject>
#include <QtQml>
namespace UkuiMenu {
class SortType
{
Q_GADGET
public:
enum Type {
All,
Alphabetically,
Function
};
Q_ENUM(Type);
};
class DataType
{
Q_GADGET
public:
enum Type {
Normal, // 普通Item
Label, // 标签数据
Folder // 文件夹
};
Q_ENUM(Type);
};
// 应用数据
class DataEntity
{
// TODO 改用 Q_OBJECT
Q_GADGET
Q_PROPERTY(DataType::Type type READ type WRITE setType)
Q_PROPERTY(QString icon READ icon WRITE setIcon)
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString comment READ comment WRITE setComment)
Q_PROPERTY(QString extraData READ extraData WRITE setExtraData)
public:
void setType(DataType::Type type) {m_type = type;}
DataType::Type type() {return m_type;}
void setIcon(const QString& icon) {m_icon = icon;}
QString icon() {return m_icon;}
void setName(const QString& name) {m_name = name;}
QString name() {return m_name;}
void setComment(const QString& comment) {m_comment = comment;}
QString comment() {return m_comment;}
void setExtraData(const QString& extraData) {m_extraData = extraData;}
QString extraData() {return m_extraData;}
private:
QString m_icon;
QString m_name;
QString m_comment; // 应用描述
QString m_extraData; // 额外的数据
DataType::Type m_type {DataType::Normal};
};
class CommonsModule
{
public:
static void defineCommonsMod(const char *uri, int versionMajor, int versionMinor) {
qRegisterMetaType<UkuiMenu::SortType::Type>("SortType::Type");
qRegisterMetaType<UkuiMenu::DataType::Type>("DataType::Type");
qRegisterMetaType<UkuiMenu::DataEntity>("DataEntity");
qmlRegisterUncreatableType<UkuiMenu::SortType>(uri, versionMajor, versionMinor, "SortType", "Use enums only");
qmlRegisterUncreatableType<UkuiMenu::DataType>(uri, versionMajor, versionMinor, "DataType", "Use enums only");
// qmlRegisterUncreatableType<UkuiMenu::DataEntity>(uri, versionMajor, versionMinor, "DataEntity", "unknown");
}
};
} // UkuiMenu
#endif //UKUI_MENU_COMMONS_H

View File

@ -21,7 +21,5 @@ int main(int argc, char *argv[])
}, Qt::QueuedConnection);
engine.load(url);
UkuiMenu::Model plugin;
return app.exec();
}

View File

@ -1,6 +1,20 @@
//
// Created by hxf on 22-11-30.
//
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "model.h"
@ -12,14 +26,5 @@
#include <QGSettings>
namespace UkuiMenu {
Model::Model() {
QString name = QObject::tr("i am a text.");
qDebug() << "==" << name;
// GType type = g_app_info_get_type();
QGSettings qgSettings("");
for (const auto &item: qgSettings.keys()) {
qDebug() << "==" << item;
}
}
} // UkuiMenu

View File

@ -1,16 +1,32 @@
//
// Created by hxf on 22-11-30.
//
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_MODEL_H
#define UKUI_MENU_MODEL_H
#include <QAbstractListModel>
namespace UkuiMenu {
class Model
{
public:
Model();
Model() = default;
};
} // UkuiMenu

70
src/settings/settings.cpp Normal file
View File

@ -0,0 +1,70 @@
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "settings.h"
#include <QVariant>
#include <QDebug>
#define UKUI_STYLE_SCHEMA "org.ukui.style"
#define UKUI_STYLE_NAME_KEY "styleName"
namespace UkuiMenu {
StyleGSetting::StyleGSetting(QObject *parent) : QObject(parent)
{
init();
}
QVariant StyleGSetting::get(StyleGSetting::Key key)
{
return m_cache.value(key);
}
void StyleGSetting::init()
{
m_cache.insert(StyleName, UKUI_STYLE_VALUE_LIGHT);
if (QGSettings::isSchemaInstalled(UKUI_STYLE_SCHEMA)) {
m_GSettings = new QGSettings(UKUI_STYLE_SCHEMA, {}, this);
QStringList keys = m_GSettings->keys();
if (keys.contains(UKUI_STYLE_NAME_KEY)) {
valueChangedSlot(UKUI_STYLE_NAME_KEY);
}
connect(m_GSettings, &QGSettings::changed,
this, &StyleGSetting::valueChangedSlot);
}
}
void StyleGSetting::valueChangedSlot(const QString &key)
{
if (key == UKUI_STYLE_NAME_KEY) {
m_cache.insert(StyleName, m_GSettings->get(key).toString());
Q_EMIT valueChanged(StyleName);
}
}
StyleGSetting *StyleGSetting::instance()
{
static StyleGSetting setting(nullptr);
return &setting;
}
} // UkuiMenu

67
src/settings/settings.h Normal file
View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_SETTINGS_H
#define UKUI_MENU_SETTINGS_H
#include <QObject>
#include <QGSettings>
#include <QVariant>
#define UKUI_STYLE_VALUE_DARK "ukui-dark"
#define UKUI_STYLE_VALUE_LIGHT "ukui-light"
#define UKUI_STYLE_VALUE_DEFAULT "ukui-default"
namespace UkuiMenu {
class StyleGSetting : public QObject
{
Q_OBJECT
public:
enum Key {
UnKnowKey = 0,
StyleName
};
Q_ENUM(Key)
static StyleGSetting *instance();
StyleGSetting() = delete;
StyleGSetting(const StyleGSetting& obj) = delete;
StyleGSetting(const StyleGSetting&& obj) = delete;
public Q_SLOTS:
QVariant get(StyleGSetting::Key key);
Q_SIGNALS:
void valueChanged(const StyleGSetting::Key& key);
private Q_SLOTS:
void valueChangedSlot(const QString& key);
private:
explicit StyleGSetting(QObject *parent = nullptr);
void init();
private:
QGSettings *m_GSettings{nullptr};
QMap<StyleGSetting::Key, QVariant> m_cache;
};
} // UkuiMenu
#endif //UKUI_MENU_SETTINGS_H

View File

@ -0,0 +1,85 @@
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "color-helper.h"
#include "settings.h"
#include <QDebug>
#include <QApplication>
namespace UkuiMenu {
ColorHelper *ColorHelper::instance()
{
static ColorHelper colorHelper(nullptr);
return &colorHelper;
}
ColorHelper::ColorHelper(QObject *parent) : QObject(parent)
{
m_colors.insert(ColorRole::Light, {"#000000"});
m_colors.insert(ColorRole::Gray, {"#10171D"});
m_colors.insert(ColorRole::Dark, {"#FFFFFF"});
initStyleMonitor();
}
QColor ColorHelper::getColor(ColorRole::Role role)
{
// TODO 从新设计颜色获取的方式,根据主题切换
if (m_colors.contains(role)) {
return m_colors.value(role);
}
return {"#000000"};
}
QColor ColorHelper::colorWithTheme(ColorRole::Role role)
{
QColor color = getColor(role);
color.setAlphaF(0.5);
return color;
}
QColor ColorHelper::colorWithAlpha(ColorRole::Role role, qreal alpha)
{
QColor color = getColor(role);
color.setAlphaF(0.5);
return color;
}
void ColorHelper::initStyleMonitor()
{
updateStyle();
connect(StyleGSetting::instance(), &StyleGSetting::valueChanged, this , [this] (const StyleGSetting::Key& key) {
if (key & StyleGSetting::StyleName) {
updateStyle();
}
});
connect(qApp, &QApplication::paletteChanged, this, &ColorHelper::colorChanged);
}
void ColorHelper::updateStyle()
{
QString styleName = StyleGSetting::instance()->get(StyleGSetting::StyleName).toString();
m_isDarkStyle = (styleName != UKUI_STYLE_VALUE_LIGHT);
Q_EMIT colorChanged();
}
} // UkuiMenu

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2022, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_COLOR_HELPER_H
#define UKUI_MENU_COLOR_HELPER_H
#include <QObject>
#include <QColor>
#include <QMap>
#include <QGSettings>
namespace UkuiMenu {
class ColorRole
{
Q_GADGET
public:
enum Role {
Base = 0,
HighLight,
Window,
Light,
Gray,
Dark
};
Q_ENUM(Role)
};
class ColorHelper : public QObject
{
Q_OBJECT
public:
static ColorHelper *instance();
ColorHelper() = delete;
ColorHelper(const ColorHelper& obj) = delete;
ColorHelper(ColorHelper&& obj) = delete;
Q_INVOKABLE QColor getColor(ColorRole::Role role);
Q_INVOKABLE QColor colorWithTheme(ColorRole::Role role);
Q_INVOKABLE QColor colorWithAlpha(ColorRole::Role role, qreal alpha);
Q_SIGNALS:
void colorChanged();
private Q_SLOTS:
void updateStyle();
private:
explicit ColorHelper(QObject *parent = nullptr);
void initStyleMonitor();
private:
QMap<ColorRole::Role, QColor> m_colors;
bool m_isDarkStyle{false};
};
} // UkuiMenu
#endif //UKUI_MENU_COLOR_HELPER_H