forked from openkylin/ukui-menu
添加字母分类
This commit is contained in:
parent
75c3d20f6c
commit
22a7871f20
|
@ -92,6 +92,7 @@ set(SOURCE_FILES
|
|||
src/appdata/plugin/all-app-data-provider.cpp src/appdata/plugin/all-app-data-provider.h
|
||||
src/appdata/plugin/app-search-plugin.cpp src/appdata/plugin/app-search-plugin.h
|
||||
src/appdata/plugin/app-category-plugin.cpp src/appdata/plugin/app-category-plugin.h
|
||||
src/appdata/plugin/app-letter-sort-plugin.cpp src/appdata/plugin/app-letter-sort-plugin.h
|
||||
src/extension/menu-extension-iface.h
|
||||
src/extension/menu-extension.cpp src/extension/menu-extension.h
|
||||
src/extension/extensions/folder-extension.cpp src/extension/extensions/folder-extension.h
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "plugin/all-app-data-provider.h"
|
||||
#include "plugin/app-search-plugin.h"
|
||||
#include "plugin/app-category-plugin.h"
|
||||
#include "plugin/app-letter-sort-plugin.h"
|
||||
|
||||
namespace UkuiMenu {
|
||||
|
||||
|
@ -51,10 +52,14 @@ void DataProviderManager::initProviders()
|
|||
auto *category = new AppCategoryPlugin;
|
||||
registerProvider(category);
|
||||
|
||||
auto *sort = new AppLetterSortPlugin;
|
||||
registerProvider(sort);
|
||||
|
||||
activateProvider(allProvider->id());
|
||||
allProvider->moveToThread(&m_worker);
|
||||
search->moveToThread(&m_worker);
|
||||
category->moveToThread(&m_worker);
|
||||
sort->moveToThread(&m_worker);
|
||||
}
|
||||
|
||||
void DataProviderManager::registerProvider(DataProviderPluginIFace *provider)
|
||||
|
|
|
@ -0,0 +1,181 @@
|
|||
/*
|
||||
* 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-letter-sort-plugin.h"
|
||||
#include "app-data-manager.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace UkuiMenu {
|
||||
|
||||
AppLetterSortPlugin::AppLetterSortPlugin()
|
||||
{
|
||||
loadAppData();
|
||||
connect(AppDataManager::instance(), &AppDataManager::appAdded, this, &AppLetterSortPlugin::onAppAdded);
|
||||
connect(AppDataManager::instance(), &AppDataManager::appDeleted, this, &AppLetterSortPlugin::onAppDeleted);
|
||||
connect(AppDataManager::instance(), &AppDataManager::appUpdated, this, &AppLetterSortPlugin::onAppUpdated);
|
||||
}
|
||||
|
||||
int AppLetterSortPlugin::index()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
QString AppLetterSortPlugin::id()
|
||||
{
|
||||
return "letterSort";
|
||||
}
|
||||
|
||||
QString AppLetterSortPlugin::name()
|
||||
{
|
||||
return tr("Letter Sort");
|
||||
}
|
||||
|
||||
QString AppLetterSortPlugin::icon()
|
||||
{
|
||||
return "image://appicon/text-plain";
|
||||
}
|
||||
|
||||
QString AppLetterSortPlugin::title()
|
||||
{
|
||||
return tr("Letter Sort");
|
||||
}
|
||||
|
||||
PluginGroup::Group AppLetterSortPlugin::group()
|
||||
{
|
||||
return PluginGroup::SortMenuItem;
|
||||
}
|
||||
|
||||
QVector<DataEntity> AppLetterSortPlugin::data()
|
||||
{
|
||||
return m_appData;
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::forceUpdate()
|
||||
{
|
||||
updateAppData();
|
||||
}
|
||||
|
||||
QVector<LabelItem> AppLetterSortPlugin::labels()
|
||||
{
|
||||
return m_labels;
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::onAppAdded(const QList<DataEntity> &apps)
|
||||
{
|
||||
Q_UNUSED(apps);
|
||||
updateAppData();
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::onAppDeleted(QStringList idList)
|
||||
{
|
||||
Q_UNUSED(idList);
|
||||
updateAppData();
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::onAppUpdated(const QList<DataEntity> &apps)
|
||||
{
|
||||
Q_UNUSED(apps);
|
||||
updateAppData();
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::updateAppData()
|
||||
{
|
||||
loadAppData();
|
||||
sendChangeSignal();
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::sendChangeSignal()
|
||||
{
|
||||
Q_EMIT labelChanged();
|
||||
Q_EMIT dataChanged(m_appData);
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::loadAppData()
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
m_appData.clear();
|
||||
m_labels.clear();
|
||||
QList<DataEntity> appData;
|
||||
appData = AppDataManager::instance()->normalApps();
|
||||
|
||||
if (appData.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
sortAppData(appData);
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::sortAppData(QList<DataEntity> &apps)
|
||||
{
|
||||
QVector<DataEntity> appVector[28];
|
||||
|
||||
for (int i = 0; i < apps.length(); i++) {
|
||||
QString letter = apps.at(i).firstLetter();
|
||||
char firstLetter;
|
||||
|
||||
if (!letter.isEmpty())
|
||||
{
|
||||
firstLetter = letter.at(0).toUpper().toLatin1();
|
||||
} else {
|
||||
firstLetter = '&';
|
||||
}
|
||||
|
||||
if (firstLetter >= 'A' && firstLetter <= 'Z') {
|
||||
appVector[(static_cast<int>(firstLetter) - 65)].append(apps.at(i));
|
||||
} else if (firstLetter >= '1' && firstLetter <= '9') {
|
||||
appVector[26].append(apps.at(i));
|
||||
} else {
|
||||
appVector[27].append(apps.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 26; i++) {
|
||||
addAppToLabel(QString(QChar(static_cast<char>(i + 65))), appVector[i]);
|
||||
}
|
||||
addAppToLabel(QString("#"), appVector[26]);
|
||||
addAppToLabel(QString("&"), appVector[27]);
|
||||
}
|
||||
|
||||
void AppLetterSortPlugin::addAppToLabel(const QString &labelName, QVector<DataEntity> &apps)
|
||||
{
|
||||
LabelItem labelItem;
|
||||
labelItem.setDisable(!apps.isEmpty());
|
||||
labelItem.setIndex(m_labels.size());
|
||||
labelItem.setId(labelName);
|
||||
labelItem.setDisplayName(labelName);
|
||||
m_labels.append(labelItem);
|
||||
|
||||
if (!apps.isEmpty()) {
|
||||
DataEntity dataEntity;
|
||||
dataEntity.setComment(tr("Letter sort button"));
|
||||
dataEntity.setName(labelName);
|
||||
dataEntity.setId(labelName);
|
||||
dataEntity.setType(DataType::Label);
|
||||
m_appData.append(dataEntity);
|
||||
|
||||
std::sort(apps.begin(), apps.end(), [](const DataEntity &a, const DataEntity &b) {
|
||||
return !(a.firstLetter().compare(b.firstLetter(), Qt::CaseInsensitive) > 0);
|
||||
});
|
||||
|
||||
m_appData.append(apps);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // UkuiMenu
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* 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_LETTER_SORT_PLUGIN_H
|
||||
#define UKUI_MENU_APP_LETTER_SORT_PLUGIN_H
|
||||
|
||||
#include "data-provider-plugin-iface.h"
|
||||
|
||||
namespace UkuiMenu {
|
||||
|
||||
class AppLetterSortPlugin : public DataProviderPluginIFace
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit AppLetterSortPlugin();
|
||||
|
||||
int index() override;
|
||||
QString id() override;
|
||||
QString name() override;
|
||||
QString icon() override;
|
||||
QString title() override;
|
||||
PluginGroup::Group group() override;
|
||||
QVector<DataEntity> data() override;
|
||||
void forceUpdate() override;
|
||||
QVector<LabelItem> labels() override;
|
||||
|
||||
private Q_SLOTS:
|
||||
void onAppAdded(const QList<DataEntity>& apps);
|
||||
void onAppDeleted(QStringList idList);
|
||||
void onAppUpdated(const QList<DataEntity>& apps);
|
||||
|
||||
private:
|
||||
void updateAppData();
|
||||
void sendChangeSignal();
|
||||
void loadAppData();
|
||||
void sortAppData(QList<DataEntity> &apps);
|
||||
void addAppToLabel(const QString &labelName, QVector<DataEntity> &apps);
|
||||
|
||||
private:
|
||||
QMutex m_mutex;
|
||||
QVector<DataEntity> m_appData;
|
||||
QVector<LabelItem> m_labels;
|
||||
};
|
||||
|
||||
} // UkuiMenu
|
||||
|
||||
#endif // UKUI_MENU_APP_LETTER_SORT_PLUGIN_H
|
Loading…
Reference in New Issue