From c9ab90589d008a5ac1a979aad63dad637d240afc Mon Sep 17 00:00:00 2001 From: hewenfei Date: Tue, 28 Mar 2023 17:25:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E5=90=8E=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 3 + src/appdata/app-folder-helper.cpp | 62 ++++++++++++----- src/appdata/app-folder-helper.h | 10 ++- src/model/folder-model.cpp | 108 ++++++++++++++++++++++++++++++ src/model/folder-model.h | 48 +++++++++++++ src/model/model-manager.cpp | 9 +++ src/model/model-manager.h | 3 + 7 files changed, 227 insertions(+), 16 deletions(-) create mode 100644 src/model/folder-model.cpp create mode 100644 src/model/folder-model.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 443d28b..b4a4fb6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,6 +85,7 @@ set(SOURCE_FILES src/model/label-model.cpp src/model/label-model.h src/model/app-group-model.cpp src/model/app-group-model.h src/model/model-manager.cpp src/model/model-manager.h + src/model/folder-model.cpp src/model/folder-model.h src/settings/settings.cpp src/settings/settings.h src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h src/uiconfig/theme-palette.cpp src/uiconfig/theme-palette.h @@ -115,6 +116,7 @@ set(QRC_FILES qml/qml.qrc res/res.qrc) # desktop file set(DESKTOP_FILE data/ukui-menu.desktop) set(GSETTING_FILE data/org.ukui.menu.settings.gschema.xml) +set(GLOBAL_CONFIG_FILE data/ukui-menu-global-config.conf) # data files #set(DATA_FILES data/xxx) # extension @@ -157,4 +159,5 @@ install(FILES ${QM_FILES} DESTINATION "${UKUI_MENU_TRANSLATION_DIR}") # 安装desktop文件 install(FILES ${DESKTOP_FILE} DESTINATION "/etc/xdg/autostart") install(FILES ${GSETTING_FILE} DESTINATION "/usr/share/glib-2.0/schemas") +install(FILES ${GLOBAL_CONFIG_FILE} DESTINATION "${UKUI_MENU_DATA_DIR}") #install(FILES ${EXTENSION_IFACE_QML_FILES} DESTINATION "${UKUI_MENU_EXTENSION_IFACE_QML_DIR}") diff --git a/src/appdata/app-folder-helper.cpp b/src/appdata/app-folder-helper.cpp index 37fb058..bfdf71e 100644 --- a/src/appdata/app-folder-helper.cpp +++ b/src/appdata/app-folder-helper.cpp @@ -18,6 +18,7 @@ #include "app-folder-helper.h" #include "menu-manager.h" +#include "app-data-manager.h" #include #include @@ -96,7 +97,7 @@ void FolderMenuProvider::folderMenuForNormal(QObject *parent, const QString &app QList folders = AppFolderHelper::instance()->folderData(); for (const Folder &folder : folders) { - QString name = QObject::tr("Add to %1").arg(folder.getName()); + QString name = QObject::tr("Add to \"%1\"").arg(folder.getName()); QAction* act = new QAction(name, subMenu); QObject::connect(act, &QAction::triggered, parent, [appId, folder] { AppFolderHelper::instance()->addAppToFolder(appId, folder.getId()); @@ -183,8 +184,7 @@ void AppFolderHelper::addAppToFolder(const QString &appId, const int &folderId) } forceSync(); - - Q_EMIT folderDataChanged(); + Q_EMIT folderDataChanged(folderId); } void AppFolderHelper::addAppToNewFolder(const QString &appId, const QString &folderName) @@ -206,7 +206,7 @@ void AppFolderHelper::addAppToNewFolder(const QString &appId, const QString &fol folder.apps.append(appId); insertFolder(folder); - Q_EMIT folderDataChanged(); + Q_EMIT folderAdded(folder.id); } void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folderId) @@ -231,22 +231,24 @@ void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folde deleteFolder(folderId); } - Q_EMIT folderDataChanged(); + Q_EMIT folderDataChanged(folderId); } bool AppFolderHelper::deleteFolder(const int& folderId) { - QMutexLocker locker(&m_mutex); - if (!m_folders.contains(folderId)) { - return false; + { + QMutexLocker locker(&m_mutex); + if (!m_folders.contains(folderId)) { + return false; + } + + if (!m_folders.remove(folderId)) { + return false; + } } - if (m_folders.remove(folderId)) { - Q_EMIT folderDataChanged(); - return true; - } - - return false; + Q_EMIT folderDeleted(folderId); + return true; } void AppFolderHelper::renameFolder(const int &folderId, const QString &folderName) @@ -259,7 +261,7 @@ void AppFolderHelper::renameFolder(const int &folderId, const QString &folderNam m_folders[folderId].name = folderName; } - Q_EMIT folderDataChanged(); + Q_EMIT folderDataChanged(folderId); } QList AppFolderHelper::folderData() @@ -298,6 +300,20 @@ bool AppFolderHelper::searchFolderByAppName(const QString &appId, Folder &folder return false; } +bool AppFolderHelper::containFolder(int folderId) +{ + QMutexLocker locker(&m_mutex); + return m_folders.contains(folderId); +} + +bool AppFolderHelper::containApp(const QString &appId) +{ + QMutexLocker locker(&m_mutex); + return std::any_of(m_folders.constBegin(), m_folders.constEnd(), [appId] (const Folder &folder) { + return folder.apps.contains(appId); + }); +} + void AppFolderHelper::forceSync() { saveData(); @@ -384,4 +400,20 @@ void AppFolderHelper::saveData() file.close(); } +QStringList AppFolderHelper::folderIcon(const Folder &folder) +{ + // TODO: 使用绘图API生成图片 + QStringList icons; + DataEntity app; + + int count = qMin(folder.apps.count(), FOLDER_MAX_ICON_NUM); + for (int i = 0; i < count; ++i) { + if (AppDataManager::instance()->getApp(folder.apps.at(i), app)) { + icons.append(app.icon()); + } + } + + return icons; +} + } // UkuiMenu diff --git a/src/appdata/app-folder-helper.h b/src/appdata/app-folder-helper.h index d147b84..8bddb5d 100644 --- a/src/appdata/app-folder-helper.h +++ b/src/appdata/app-folder-helper.h @@ -28,6 +28,8 @@ namespace UkuiMenu { +#define FOLDER_MAX_ICON_NUM 16 + class AppFolderHelper; class Folder @@ -53,6 +55,7 @@ class AppFolderHelper : public QObject Q_OBJECT public: static AppFolderHelper *instance(); + static QStringList folderIcon(const Folder &folder); ~AppFolderHelper() override; AppFolderHelper(const AppFolderHelper& obj) = delete; @@ -64,6 +67,9 @@ public: // xxxx bool searchFolder(const int& folderId, Folder &folder); bool searchFolderByAppName(const QString& appId, Folder &folder); + bool containFolder(int folderId); + bool containApp(const QString& appId); + QList folderData(); void addAppToFolder(const QString& appId, const int& folderId); @@ -75,7 +81,9 @@ public: void forceSync(); Q_SIGNALS: - void folderDataChanged(); + void folderAdded(int folderId); + void folderDeleted(int folderId); + void folderDataChanged(int folderId); private: AppFolderHelper(); diff --git a/src/model/folder-model.cpp b/src/model/folder-model.cpp new file mode 100644 index 0000000..4c2f9bb --- /dev/null +++ b/src/model/folder-model.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2023, 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 "folder-model.h" +#include "app-folder-helper.h" +#include "app-data-manager.h" + +namespace UkuiMenu { + +FolderModel::FolderModel(QObject *parent) : QAbstractListModel(parent) +{ + connect(AppFolderHelper::instance(), &AppFolderHelper::folderDataChanged, this, &FolderModel::loadFolderData); + connect(AppFolderHelper::instance(), &AppFolderHelper::folderDeleted, this, [this] (int id) { + if (id == m_folderId) { + beginResetModel(); + m_folderId = -1; + m_apps.clear(); + endResetModel(); + } + }); +} + +void FolderModel::setFolderId(const QString &folderId) +{ + bool ok; + int id = folderId.toInt(&ok); + if (!ok) { + return; + } + + loadFolderData(id); +} + +void FolderModel::loadFolderData(int id) +{ + Folder folder; + if (!AppFolderHelper::instance()->searchFolder(id, folder)) { + return; + } + + beginResetModel(); + m_folderId = id; + m_apps = folder.getApps(); + endResetModel(); +} + +int FolderModel::rowCount(const QModelIndex &parent) const +{ + return m_apps.count(); +} + +QVariant FolderModel::data(const QModelIndex &index, int role) const +{ + int i = index.row(); + if (i < 0 || i >= m_apps.size()) { + return {}; + } + + DataEntity app; + if (!AppDataManager::instance()->getApp(m_apps.at(i), app)) { + return {}; + } + + switch (role) { + case DataEntity::Type: + return app.type(); + case DataEntity::Icon: + return app.icon(); + case DataEntity::Name: + return app.name(); + case DataEntity::Comment: + return app.comment(); + case DataEntity::ExtraData: + return app.extraData(); + default: + break; + } + + return {}; +} + +QHash FolderModel::roleNames() const +{ + QHash names; + names.insert(DataEntity::Type, "type"); + names.insert(DataEntity::Icon, "icon"); + names.insert(DataEntity::Name, "name"); + names.insert(DataEntity::Comment, "comment"); + names.insert(DataEntity::ExtraData, "extraData"); + return names; +} + +} // UkuiMenu diff --git a/src/model/folder-model.h b/src/model/folder-model.h new file mode 100644 index 0000000..9a0f52b --- /dev/null +++ b/src/model/folder-model.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2023, 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_FOLDER_MODEL_H +#define UKUI_MENU_FOLDER_MODEL_H + +#include +#include "commons.h" + +namespace UkuiMenu { + +class FolderModel : public QAbstractListModel +{ + Q_OBJECT +public: + explicit FolderModel(QObject *parent = nullptr); + Q_INVOKABLE void setFolderId(const QString &folderId); + + int rowCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role) const override; + QHash roleNames() const override; + +private Q_SLOTS: + void loadFolderData(int id); + +private: + int m_folderId; + QStringList m_apps; +}; + +} // UkuiMenu + +#endif //UKUI_MENU_FOLDER_MODEL_H diff --git a/src/model/model-manager.cpp b/src/model/model-manager.cpp index 94ce953..03b37a9 100644 --- a/src/model/model-manager.cpp +++ b/src/model/model-manager.cpp @@ -20,6 +20,7 @@ #include "label-model.h" #include "app-model.h" #include "app-group-model.h" +#include "folder-model.h" #include namespace UkuiMenu { @@ -34,6 +35,7 @@ void ModelManager::registerMetaTypes() { qRegisterMetaType("AppModel*"); qRegisterMetaType("LabelModel*"); + qRegisterMetaType("FolderModel*"); qRegisterMetaType("AppGroupModel*"); } @@ -43,10 +45,12 @@ ModelManager::ModelManager(QObject *parent) : QObject(parent) appModel = new AppModel(this); labelModel = new LabelModel(this); + folderModel = new FolderModel(this); labelGroupModel = new AppGroupModel(appModel, this); QQmlEngine::setObjectOwnership(appModel, QQmlEngine::CppOwnership); QQmlEngine::setObjectOwnership(labelModel, QQmlEngine::CppOwnership); + QQmlEngine::setObjectOwnership(folderModel, QQmlEngine::CppOwnership); QQmlEngine::setObjectOwnership(labelGroupModel, QQmlEngine::CppOwnership); } @@ -65,4 +69,9 @@ AppGroupModel *ModelManager::getLabelGroupModel() return labelGroupModel; } +FolderModel *ModelManager::getFolderModel() +{ + return folderModel; +} + } // UkuiMenu diff --git a/src/model/model-manager.h b/src/model/model-manager.h index f196833..591d942 100644 --- a/src/model/model-manager.h +++ b/src/model/model-manager.h @@ -26,6 +26,7 @@ namespace UkuiMenu { class AppModel; class LabelModel; class AppGroupModel; +class FolderModel; class ModelManager : public QObject { @@ -37,6 +38,7 @@ public: Q_INVOKABLE AppModel *getAppModel(); Q_INVOKABLE LabelModel *getLabelModel(); + Q_INVOKABLE FolderModel *getFolderModel(); Q_INVOKABLE AppGroupModel *getLabelGroupModel(); private: @@ -45,6 +47,7 @@ private: private: AppModel *appModel{nullptr}; LabelModel *labelModel{nullptr}; + FolderModel *folderModel{nullptr}; AppGroupModel *labelGroupModel{nullptr}; };