重命名app-model,增加应用分组model

This commit is contained in:
hewenfei 2023-03-17 17:16:29 +08:00
parent 92a4badea3
commit 52b10c02d1
9 changed files with 300 additions and 21 deletions

View File

@ -81,7 +81,9 @@ set(SOURCE_FILES
src/commons.h src/commons.cpp
src/menu-dbus-service.cpp src/menu-dbus-service.h
src/ukui-menu-application.cpp src/ukui-menu-application.h
src/model/model.cpp src/model/model.h
src/model/app-model.cpp src/model/app-model.h
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/settings/settings.cpp src/settings/settings.h
src/uiconfig/color-helper.cpp src/uiconfig/color-helper.h
@ -104,7 +106,6 @@ set(SOURCE_FILES
src/utils/app-page-header-utils.cpp src/utils/app-page-header-utils.h
src/utils/power-button.cpp src/utils/power-button.h
src/utils/app-manager.cpp src/utils/app-manager.h
src/model/label-model.cpp src/model/label-model.h
src/menu/menu-manager.cpp src/menu/menu-manager.h
)

View File

@ -27,7 +27,7 @@ Item {
property string title: ""
function labelSelection(labelId) {
appListView.positionViewAtIndex(appListView.model.currentAppIndex(labelId), ListView.Beginning)
appListView.positionViewAtIndex(appListView.model.getLabelIndex(labelId), ListView.Beginning)
}
MouseArea {
@ -107,4 +107,3 @@ Item {
AppControls2.FolderItem {}
}
}

View File

@ -0,0 +1,180 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*
*/
#include "app-group-model.h"
#include "data-provider-manager.h"
#include "app-model.h"
namespace UkuiMenu {
class LabelGroupModelPrivate
{
public:
explicit LabelGroupModelPrivate(AppModel *appModel) : appModel(appModel) {}
AppModel *appModel{nullptr};
bool containLabel{false};
QVector<LabelItem> labels;
QVector<int> labelIndex;
};
AppGroupModel::AppGroupModel(AppModel *appModel, QObject *parent) : QAbstractListModel(parent), d(new LabelGroupModelPrivate(appModel))
{
reloadLabels();
connect(DataProviderManager::instance(), &DataProviderManager::pluginChanged, this, &AppGroupModel::reloadLabels);
connect(DataProviderManager::instance(), &DataProviderManager::labelChanged, this, &AppGroupModel::reloadLabels);
connectSignals();
}
void AppGroupModel::reloadLabels()
{
Q_EMIT beginResetModel();
d->labels.clear();
for (const auto &label : DataProviderManager::instance()->labels()) {
if (label.isDisable()) {
continue;
}
d->labels.append(label);
}
d->containLabel = !d->labels.isEmpty();
d->labelIndex = QVector<int>(d->labels.size(), -1);
Q_EMIT endResetModel();
Q_EMIT containLabelChanged(d->containLabel);
}
int AppGroupModel::rowCount(const QModelIndex &parent) const
{
return d->labels.count();
}
QVariant AppGroupModel::data(const QModelIndex &index, int role) const
{
int i = index.row();
if (i < 0 || i >= d->labels.size()) {
return {};
}
switch (role) {
case DataEntity::Type:
return DataType::Label;
case DataEntity::Icon:
return "";
case DataEntity::Name:
return d->labels.at(i).displayName();
case DataEntity::Comment:
return {};
case DataEntity::ExtraData: {
int start = getLabelIndex(i) + 1;
int end;
if (i >= (d->labels.size() - 1)) {
end = d->appModel->rowCount(QModelIndex());
} else {
end = getLabelIndex(i + 1);
}
return d->appModel->getApps(start, end);
}
default:
break;
}
return {};
}
QHash<int, QByteArray> AppGroupModel::roleNames() const
{
return d->appModel->roleNames();
}
void AppGroupModel::connectSignals()
{
connect(d->appModel, &QAbstractItemModel::modelReset, this, [this] {
if (!d->containLabel) {
return;
}
beginResetModel();
for (auto &item : d->labelIndex) {
item = -1;
}
endResetModel();
});
connect(d->appModel, &QAbstractItemModel::rowsInserted, this, [this] (const QModelIndex &parent, int first, int last) {
if (d->containLabel) {
beginResetModel();
endResetModel();
}
});
// connect(d->appModel, &QAbstractItemModel::rowsRemoved,
// this, &QAbstractItemModel::rowsRemoved);
//
// connect(d->appModel, &QAbstractItemModel::rowsAboutToBeRemoved,
// this, &QAbstractItemModel::rowsAboutToBeRemoved);
//
// connect(d->appModel, &QAbstractItemModel::dataChanged,
// this, &QAbstractItemModel::dataChanged);
//
// connect(d->appModel, &QAbstractItemModel::rowsMoved,
// this, &QAbstractItemModel::rowsMoved);
//
// connect(d->appModel, &QAbstractItemModel::layoutChanged,
// this, &QAbstractItemModel::layoutChanged);
}
bool AppGroupModel::containLabel()
{
return d->containLabel;
}
inline int AppGroupModel::getLabelIndex(int i) const
{
int index = d->labelIndex.at(i);
if (index < 0) {
index = d->appModel->getLabelIndex(d->labels.at(i).id());
d->labelIndex[i] = index;
}
return index;
}
void AppGroupModel::openApp(int labelIndex, int appIndex)
{
int index = d->labelIndex.at(labelIndex);
d->appModel->appClicked(++index + appIndex);
}
int AppGroupModel::getLabelIndex(const QString &labelId)
{
for (int i = 0; i < d->labels.size(); ++i) {
if (d->labels.at(i).id() == labelId) {
return i;
}
}
return -1;
}
inline void AppGroupModel::resetModel(QVector<LabelItem> &labels)
{
d->labels.swap(labels);
d->containLabel = !d->labels.isEmpty();
d->labelIndex = QVector<int>(d->labels.size(), -1);
}
} // UkuiMenu

View File

@ -0,0 +1,63 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*
*/
#ifndef UKUI_MENU_APP_GROUP_MODEL_H
#define UKUI_MENU_APP_GROUP_MODEL_H
#include <QAbstractListModel>
#include "commons.h"
namespace UkuiMenu {
class AppModel;
class LabelGroupModelPrivate;
class AppGroupModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(bool containLabel READ containLabel NOTIFY containLabelChanged)
public:
explicit AppGroupModel(AppModel *appModel, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
bool containLabel();
Q_INVOKABLE void openApp(int labelIndex, int appIndex);
Q_INVOKABLE int getLabelIndex(const QString &labelId);
Q_SIGNALS:
void containLabelChanged(bool containLabel);
private Q_SLOTS:
void reloadLabels();
private:
void connectSignals();
inline int getLabelIndex(int index) const;
inline void resetModel(QVector<LabelItem> &labels);
private:
LabelGroupModelPrivate *d{nullptr};
};
} // UkuiMenu
#endif //UKUI_MENU_APP_GROUP_MODEL_H

View File

@ -16,7 +16,7 @@
*
*/
#include "model.h"
#include "app-model.h"
#include "app-manager.h"
#include "menu-manager.h"
@ -73,7 +73,7 @@ QHash<int, QByteArray> AppModel::roleNames() const
return names;
}
int AppModel::currentAppIndex(const QString &id)
int AppModel::getLabelIndex(const QString &id)
{
for (int index = 0; index < m_apps.count(); ++index) {
if (m_apps.at(index).type() == DataType::Label && m_apps.at(index).id() == id) {
@ -183,3 +183,17 @@ void AppModel::openMenu(const int &index)
MenuManager::instance()->showMenu(m_apps.at(index));
}
QVariantList AppModel::getApps(int start, int end)
{
if (start < 0 || start >= m_apps.size() || end < 0 || end > m_apps.size()) {
return {};
}
QVariantList list;
for (int i = start; i < end; ++i) {
list.append(QVariant::fromValue(m_apps.at(i)));
}
return list;
}

View File

@ -16,8 +16,8 @@
*
*/
#ifndef UKUI_MENU_MODEL_H
#define UKUI_MENU_MODEL_H
#ifndef UKUI_MENU_APP_MODEL_H
#define UKUI_MENU_APP_MODEL_H
#include <QVariantList>
#include <QAbstractListModel>
@ -36,8 +36,9 @@ public:
int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
QVariantList getApps(int start = 0, int end = 0);
Q_INVOKABLE int currentAppIndex(const QString &id);
Q_INVOKABLE int getLabelIndex(const QString &id);
Q_INVOKABLE QVariantList folderApps(const QString &folderName);
Q_INVOKABLE void appClicked(const int &index);
Q_INVOKABLE void openMenu(const int &index);
@ -58,4 +59,4 @@ private:
} // UkuiMenu
#endif //UKUI_MENU_MODEL_H
#endif //UKUI_MENU_APP_MODEL_H

View File

@ -18,36 +18,51 @@
#include "model-manager.h"
#include "label-model.h"
#include "model.h"
#include "app-model.h"
#include "app-group-model.h"
#include <QQmlEngine>
namespace UkuiMenu {
ModelManager *ModelManager::instance()
{
static ModelManager modelManager;
return &modelManager;
}
void ModelManager::registerMetaTypes()
{
qRegisterMetaType<AppModel*>("AppModel*");
qRegisterMetaType<LabelModel*>("LabelModel*");
qRegisterMetaType<AppGroupModel*>("AppGroupModel*");
}
ModelManager::ModelManager(QObject *parent) : QObject(parent)
{
ModelManager::registerMetaTypes();
appModel = new AppModel(this);
labelModel = new LabelModel(this);
labelGroupModel = new AppGroupModel(appModel, this);
QQmlEngine::setObjectOwnership(appModel, QQmlEngine::CppOwnership);
QQmlEngine::setObjectOwnership(labelModel, QQmlEngine::CppOwnership);
QQmlEngine::setObjectOwnership(labelGroupModel, QQmlEngine::CppOwnership);
}
AppModel *ModelManager::getAppModel()
{
if (appModel) {
return appModel;
}
return nullptr;
return appModel;
}
LabelModel *ModelManager::getLabelModel()
{
if (labelModel) {
return labelModel;
}
return nullptr;
return labelModel;
}
AppGroupModel *ModelManager::getLabelGroupModel()
{
return labelGroupModel;
}
} // UkuiMenu

View File

@ -25,21 +25,27 @@ namespace UkuiMenu {
class AppModel;
class LabelModel;
class AppGroupModel;
class ModelManager : public QObject
{
Q_OBJECT
public:
static ModelManager *instance();
static void registerMetaTypes();
explicit ModelManager(QObject *parent = nullptr);
~ModelManager() override = default;
Q_INVOKABLE AppModel *getAppModel();
Q_INVOKABLE LabelModel *getLabelModel();
Q_INVOKABLE AppGroupModel *getLabelGroupModel();
private:
explicit ModelManager(QObject *parent = nullptr);
private:
AppModel *appModel{nullptr};
LabelModel *labelModel{nullptr};
AppGroupModel *labelGroupModel{nullptr};
};
} // UkuiMenu

View File

@ -78,7 +78,7 @@ void UkuiMenuApplication::initQmlEngine()
context->setContextProperty("colorHelper", ColorHelper::instance());
context->setContextProperty("themePalette", ThemePalette::getInstance());
context->setContextProperty("menuSetting", MenuSetting::instance());
context->setContextProperty("modelManager", new ModelManager(this));
context->setContextProperty("modelManager", ModelManager::instance());
context->setContextProperty("extensionManager", MenuExtension::instance());
context->setContextProperty("appPageHeaderUtils", new AppPageHeaderUtils(this));