diff --git a/CMakeLists.txt b/CMakeLists.txt index efaf2ba..f83da08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 $<$,$. + * + */ + +#ifndef UKUI_MENU_COMMONS_H +#define UKUI_MENU_COMMONS_H + +#include +#include + +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("SortType::Type"); + qRegisterMetaType("DataType::Type"); + qRegisterMetaType("DataEntity"); + + qmlRegisterUncreatableType(uri, versionMajor, versionMinor, "SortType", "Use enums only"); + qmlRegisterUncreatableType(uri, versionMajor, versionMinor, "DataType", "Use enums only"); +// qmlRegisterUncreatableType(uri, versionMajor, versionMinor, "DataEntity", "unknown"); + } +}; + +} // UkuiMenu + +#endif //UKUI_MENU_COMMONS_H diff --git a/src/main.cpp b/src/main.cpp index d3d8a5d..d222c88 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,7 +21,5 @@ int main(int argc, char *argv[]) }, Qt::QueuedConnection); engine.load(url); - UkuiMenu::Model plugin; - return app.exec(); } diff --git a/src/model/model.cpp b/src/model/model.cpp index 915f1ba..009d524 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -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 . + * + */ #include "model.h" @@ -12,14 +26,5 @@ #include 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 diff --git a/src/model/model.h b/src/model/model.h index 7f2c4ce..57fed7a 100644 --- a/src/model/model.h +++ b/src/model/model.h @@ -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 . + * + */ #ifndef UKUI_MENU_MODEL_H #define UKUI_MENU_MODEL_H +#include + namespace UkuiMenu { class Model { public: - Model(); + Model() = default; }; } // UkuiMenu diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp new file mode 100644 index 0000000..3d3aea6 --- /dev/null +++ b/src/settings/settings.cpp @@ -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 . + * + */ + +#include "settings.h" + +#include +#include + +#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 diff --git a/src/settings/settings.h b/src/settings/settings.h new file mode 100644 index 0000000..28fdaaf --- /dev/null +++ b/src/settings/settings.h @@ -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 . + * + */ + +#ifndef UKUI_MENU_SETTINGS_H +#define UKUI_MENU_SETTINGS_H + +#include +#include +#include + +#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 m_cache; +}; + +} // UkuiMenu + +#endif //UKUI_MENU_SETTINGS_H diff --git a/src/uiconfig/color-helper.cpp b/src/uiconfig/color-helper.cpp new file mode 100644 index 0000000..7ca7da5 --- /dev/null +++ b/src/uiconfig/color-helper.cpp @@ -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 . + * + */ + +#include "color-helper.h" +#include "settings.h" + +#include +#include + +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 diff --git a/src/uiconfig/color-helper.h b/src/uiconfig/color-helper.h new file mode 100644 index 0000000..6834d6f --- /dev/null +++ b/src/uiconfig/color-helper.h @@ -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 . + * + */ + +#ifndef UKUI_MENU_COLOR_HELPER_H +#define UKUI_MENU_COLOR_HELPER_H + +#include +#include +#include +#include + +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 m_colors; + bool m_isDarkStyle{false}; +}; + +} // UkuiMenu + +#endif //UKUI_MENU_COLOR_HELPER_H