1.注册颜色模块到qml上下文中,2.更新listmodel测试功能

This commit is contained in:
hewenfei 2022-12-13 15:00:05 +08:00
parent f25ad51865
commit c6b038a168
9 changed files with 106 additions and 37 deletions

View File

@ -68,7 +68,7 @@ add_compile_definitions(UKUI_MENU_TRANSLATION_DIR="${UKUI_MENU_TRANSLATION_DIR}"
# ukui-menu # ukui-menu
set(SOURCE_FILES set(SOURCE_FILES
src/main.cpp src/main.cpp
src/commons.h src/commons.h src/commons.cpp
src/menu-dbus-service.cpp src/menu-dbus-service.h src/menu-dbus-service.cpp src/menu-dbus-service.h
src/model/model.cpp src/model/model.h src/model/model.cpp src/model/model.h
src/settings/settings.cpp src/settings/settings.h src/settings/settings.cpp src/settings/settings.h

36
src/commons.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
* 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 "commons.h"
#include <utility>
UkuiMenu::DataEntity::DataEntity(const UkuiMenu::DataEntity &obj)
{
m_type = obj.m_type;
m_name = obj.m_name;
m_icon = obj.m_icon;
m_comment = obj.m_comment;
m_extraData = obj.m_extraData;
}
UkuiMenu::DataEntity::DataEntity(UkuiMenu::DataType::Type type, QString name, QString icon, QString comment, QString extraData)
: m_type(type), m_name(std::move(name)), m_icon(std::move(icon)), m_comment(std::move(comment)), m_extraData(std::move(extraData))
{
}

View File

@ -59,6 +59,10 @@ class DataEntity
Q_PROPERTY(QString comment READ comment WRITE setComment) Q_PROPERTY(QString comment READ comment WRITE setComment)
Q_PROPERTY(QString extraData READ extraData WRITE setExtraData) Q_PROPERTY(QString extraData READ extraData WRITE setExtraData)
public: public:
DataEntity() = default;
DataEntity(const DataEntity& obj);
DataEntity(DataType::Type type, QString name, QString icon, QString comment, QString extraData);
void setType(DataType::Type type) {m_type = type;} void setType(DataType::Type type) {m_type = type;}
DataType::Type type() {return m_type;} DataType::Type type() {return m_type;}

View File

@ -46,9 +46,20 @@ QHash<int, QByteArray> AppModel::roleNames() const
return names; return names;
} }
QList<DataEntity> AppModel::folderApps(const QString &folderName) QVariantList AppModel::folderApps(const QString &folderName)
{ {
return {}; DataEntity item1{DataType::Normal, "name 1", "icon", "comment", "extra"};
DataEntity item2{DataType::Normal, "name 2", "icon", "comment", "extra"};
DataEntity item3{DataType::Label, "name 3", "icon", "comment", "extra"};
DataEntity item4{DataType::Normal, "name 4", "icon", "comment", "extra"};
QVariantList list;
list.append(QVariant::fromValue(item1));
list.append(QVariant::fromValue(item2));
list.append(QVariant::fromValue(item3));
list.append(QVariant::fromValue(item4));
return list;
} }
} // UkuiMenu } // UkuiMenu

View File

@ -19,7 +19,7 @@
#ifndef UKUI_MENU_MODEL_H #ifndef UKUI_MENU_MODEL_H
#define UKUI_MENU_MODEL_H #define UKUI_MENU_MODEL_H
#include <QList> #include <QVariantList>
#include <QAbstractListModel> #include <QAbstractListModel>
#include "commons.h" #include "commons.h"
@ -36,7 +36,7 @@ public:
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override; QHash<int, QByteArray> roleNames() const override;
Q_INVOKABLE QList<DataEntity> folderApps(const QString &folderName); Q_INVOKABLE QVariantList folderApps(const QString &folderName);
}; };
} // UkuiMenu } // UkuiMenu

View File

@ -24,7 +24,7 @@
#define UKUI_MENU_SCHEMA "org.ukui.menu.settings" #define UKUI_MENU_SCHEMA "org.ukui.menu.settings"
#define CONTROL_CENTER_SETTING "org.ukui.control-center.personalise" #define CONTROL_CENTER_SETTING "org.ukui.control-center.personalise"
#define CONTROL_CENTER_TRANSPARENCY_KEY "Transparency" #define CONTROL_CENTER_TRANSPARENCY_KEY "transparency"
#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"

View File

@ -32,33 +32,29 @@ ColorHelper *ColorHelper::instance()
ColorHelper::ColorHelper(QObject *parent) : QObject(parent) ColorHelper::ColorHelper(QObject *parent) : QObject(parent)
{ {
m_colors.insert(ColorRole::Light, {"#000000"}); m_colors.insert(ColorHelper::Light, {"#000000"});
m_colors.insert(ColorRole::Gray, {"#10171D"}); m_colors.insert(ColorHelper::Gray, {"#10171D"});
m_colors.insert(ColorRole::Dark, {"#FFFFFF"}); m_colors.insert(ColorHelper::Dark, {"#FFFFFF"});
initStyleMonitor(); initStyleMonitor();
} }
QColor ColorHelper::getColor(ColorRole::Role role) QColor ColorHelper::getColor(ColorHelper::Role role)
{ {
// TODO 从新设计颜色获取的方式,根据主题切换 QMutexLocker locker(&m_mutex);
if (m_colors.contains(role)) { return m_colors.value(role, {"#000000"});
return m_colors.value(role);
}
return {"#000000"};
} }
QColor ColorHelper::colorWithTheme(ColorRole::Role role) QColor ColorHelper::colorWithTheme(ColorHelper::Role role)
{ {
QColor color = getColor(role); QColor color = getColor(role);
color.setAlphaF(0.5); color.setAlphaF(m_transparency);
return color; return color;
} }
QColor ColorHelper::colorWithAlpha(ColorRole::Role role, qreal alpha) QColor ColorHelper::colorWithAlpha(ColorHelper::Role role, qreal alpha)
{ {
QColor color = getColor(role); QColor color = getColor(role);
color.setAlphaF(0.5); color.setAlphaF(alpha);
return color; return color;
} }
@ -66,18 +62,31 @@ void ColorHelper::initStyleMonitor()
{ {
updateStyle(); updateStyle();
connect(GlobalSetting::instance(), &GlobalSetting::styleChanged, this , [this] (const GlobalSetting::Key& key) { connect(GlobalSetting::instance(), &GlobalSetting::styleChanged, this , [this] (const GlobalSetting::Key& key) {
if (key & GlobalSetting::StyleName) { if (key & GlobalSetting::StyleName || key & GlobalSetting::Transparency) {
updateStyle(); updateStyle();
} }
}); });
connect(qApp, &QApplication::paletteChanged, this, &ColorHelper::colorChanged);
} }
void ColorHelper::updateStyle() void ColorHelper::updateStyle()
{ {
QString styleName = GlobalSetting::instance()->get(GlobalSetting::StyleName).toString(); GlobalSetting *setting = GlobalSetting::instance();
m_isDarkStyle = (styleName != UKUI_STYLE_VALUE_LIGHT); m_transparency = setting->get(GlobalSetting::Transparency).toReal();
QString styleName = setting->get(GlobalSetting::StyleName).toString();
bool isDarkStyle = (styleName != UKUI_STYLE_VALUE_LIGHT);
if (isDarkStyle != m_isDarkStyle) {
m_isDarkStyle = isDarkStyle;
m_mutex.lock();
m_colors.insert(ColorHelper::Light, {m_isDarkStyle ? "#FFFFFF" : "#000000"});
m_colors.insert(ColorHelper::Gray, {m_isDarkStyle ? "#000000" : "#10171D"});
m_colors.insert(ColorHelper::Dark, {m_isDarkStyle ? "#000000" : "#FFFFFF"});
m_mutex.unlock();
}
Q_EMIT colorChanged(); Q_EMIT colorChanged();
} }

View File

@ -22,13 +22,14 @@
#include <QObject> #include <QObject>
#include <QColor> #include <QColor>
#include <QMap> #include <QMap>
#include <QGSettings> #include <QMutex>
#include <QMutexLocker>
namespace UkuiMenu { namespace UkuiMenu {
class ColorRole class ColorHelper : public QObject
{ {
Q_GADGET Q_OBJECT
public: public:
enum Role { enum Role {
Base = 0, Base = 0,
@ -39,20 +40,15 @@ public:
Dark Dark
}; };
Q_ENUM(Role) Q_ENUM(Role)
};
class ColorHelper : public QObject
{
Q_OBJECT
public:
static ColorHelper *instance(); static ColorHelper *instance();
ColorHelper() = delete; ColorHelper() = delete;
ColorHelper(const ColorHelper& obj) = delete; ColorHelper(const ColorHelper& obj) = delete;
ColorHelper(ColorHelper&& obj) = delete; ColorHelper(ColorHelper&& obj) = delete;
Q_INVOKABLE QColor getColor(ColorRole::Role role); Q_INVOKABLE QColor getColor(ColorHelper::Role role);
Q_INVOKABLE QColor colorWithTheme(ColorRole::Role role); Q_INVOKABLE QColor colorWithTheme(ColorHelper::Role role);
Q_INVOKABLE QColor colorWithAlpha(ColorRole::Role role, qreal alpha); Q_INVOKABLE QColor colorWithAlpha(ColorHelper::Role role, qreal alpha);
Q_SIGNALS: Q_SIGNALS:
void colorChanged(); void colorChanged();
@ -65,8 +61,10 @@ private:
void initStyleMonitor(); void initStyleMonitor();
private: private:
QMap<ColorRole::Role, QColor> m_colors; QMutex m_mutex;
QMap<ColorHelper::Role, QColor> m_colors;
bool m_isDarkStyle{false}; bool m_isDarkStyle{false};
qreal m_transparency{1.0};
}; };
} // UkuiMenu } // UkuiMenu

View File

@ -20,6 +20,8 @@
#include "settings.h" #include "settings.h"
#include "commons.h" #include "commons.h"
#include "model-manager.h" #include "model-manager.h"
#include "color-helper.h"
#include "theme-palette.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
@ -49,6 +51,13 @@ void UkuiMenuApplication::registerQmlTypes()
SettingModule::defineModule(uri, versionMajor, versionMinor); SettingModule::defineModule(uri, versionMajor, versionMinor);
ModelManager::registerMetaTypes(); ModelManager::registerMetaTypes();
// vis colors
qRegisterMetaType<Palette::ColorRole>("Palette::ColorRole");
qRegisterMetaType<Palette::ColorGroup>("Palette::ColorGroup");
qRegisterMetaType<ColorHelper::Role>("ColorHelper::Role");
qmlRegisterUncreatableType<Palette>(uri, versionMajor, versionMinor, "Palette", "Use enums only.");
qmlRegisterUncreatableType<ColorHelper>(uri, versionMajor, versionMinor, "ColorHelper", "Use enums only.");
} }
void UkuiMenuApplication::initQmlEngine() void UkuiMenuApplication::initQmlEngine()
@ -58,6 +67,8 @@ void UkuiMenuApplication::initQmlEngine()
m_applicationEngine->addImportPath("qrc:/qml"); m_applicationEngine->addImportPath("qrc:/qml");
m_applicationEngine->rootContext()->setContextProperty("colorHelper", ColorHelper::instance());
m_applicationEngine->rootContext()->setContextProperty("themePalette", ThemePalette::getInstance());
m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance()); m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance());
m_applicationEngine->rootContext()->setContextProperty("modelManager", new ModelManager(this)); m_applicationEngine->rootContext()->setContextProperty("modelManager", new ModelManager(this));