添加收藏右键菜单

This commit is contained in:
gjq 2023-04-04 17:20:25 +08:00 committed by hewenfei
parent 2c50db611f
commit b415eac4a4
5 changed files with 80 additions and 6 deletions

View File

@ -118,7 +118,9 @@ UkuiMenuExtension {
}
}
onClicked: {
if (mouse.button === Qt.LeftButton) {
if (mouse.button === Qt.RightButton) {
visualModel.model.openMenu(index)
} else if (mouse.button === Qt.LeftButton) {
var data = {"id": model.id};
send(data);
}

View File

@ -44,6 +44,10 @@ private Q_SLOTS:
void onAppUpdatedAll(const QStringList &infos);
void onAppDeleted(QStringList infos);
public Q_SLOTS:
void fixToFavoriteSlot(const QString &path, const int &num);
void changedFavoriteOrderSlot(const QString &path, const int &num);
private:
void updateFavoriteApps();
void removeApps(QStringList& appIdList, QStringList &removedIdList);
@ -201,6 +205,7 @@ void AppDataWorker::updateApps(const UkuiSearch::ApplicationInfoMap &infos, QLis
if (infos.isEmpty()) {
return;
}
for (const QString &info : infos.keys()) {
if (m_appManager->m_normalApps.contains(info)) {
DataEntity &app = m_appManager->m_normalApps[info];
@ -268,6 +273,20 @@ void AppDataWorker::onAppDeleted(QStringList infos)
updateFavoriteApps();
}
void AppDataWorker::fixToFavoriteSlot(const QString &path, const int &num)
{
if (num == 0) {
m_applicationInfo->setFavoritesOfApp(path, 0);
} else {
m_applicationInfo->setFavoritesOfApp(path, m_appManager->m_favoriteApps.length() + 1);
}
}
void AppDataWorker::changedFavoriteOrderSlot(const QString &path, const int &num)
{
m_applicationInfo->setFavoritesOfApp(path, num);
}
void AppDataWorker::removeApps(QStringList &appIdList, QStringList &removedIdList)
{
QMutexLocker locker(&m_appManager->m_mutex);
@ -296,6 +315,8 @@ AppDataManager::AppDataManager()
connect(appDataWorker, &AppDataWorker::appDeleted, this, &AppDataManager::appDeleted);
connect(appDataWorker, &AppDataWorker::appUpdated, this, &AppDataManager::appUpdated);
connect(appDataWorker, &AppDataWorker::favoriteAppChanged, this, &AppDataManager::favoriteAppChanged);
connect(this, &AppDataManager::fixToFavoriteSignal, appDataWorker, &AppDataWorker::fixToFavoriteSlot);
connect(this, &AppDataManager::changedFavoriteOrderSignal, appDataWorker, &AppDataWorker::changedFavoriteOrderSlot);
m_workerThread.start();
}

View File

@ -54,6 +54,8 @@ Q_SIGNALS:
void appDeleted(QStringList idList);
void appDataChanged();
void favoriteAppChanged();
void fixToFavoriteSignal(const QString &path, const int &num);
void changedFavoriteOrderSignal(const QString &path, const int &num);
private:
AppDataManager();

View File

@ -23,9 +23,17 @@
#include <QDebug>
#include <QAbstractListModel>
#include <application-info.h>
#include <menu-manager.h>
namespace UkuiMenu {
class FavoriteMenuProvider : public MenuProvider
{
public:
int index() override { return 512; }
QList<QAction *> generateActions(QObject *parent, const RequestType &type, const QVariant &data) override;
};
class FavoriteAppsModel : public QAbstractListModel
{
Q_OBJECT
@ -43,6 +51,7 @@ public:
QVariant data(const QModelIndex &index, int role) const override;
void setFavoriteAppsData(QVector<DataEntity> &apps);
Q_INVOKABLE void openMenu(const int &index);
public Q_SLOTS:
void exchangedAppsOrder(int startIndex, int endIndex);
@ -55,6 +64,8 @@ FavoriteExtension::FavoriteExtension(QObject *parent) : MenuExtensionIFace(paren
{
qRegisterMetaType<UkuiMenu::FavoriteAppsModel*>("FavoriteAppsModel*");
MenuManager::instance()->registerMenuProvider(new FavoriteMenuProvider);
m_favoriteAppsModel = new FavoriteAppsModel(this);
m_data.insert("favoriteAppsModel", QVariant::fromValue(m_favoriteAppsModel));
@ -153,11 +164,49 @@ void FavoriteAppsModel::setFavoriteAppsData(QVector<DataEntity> &apps)
endResetModel();
}
void FavoriteAppsModel::openMenu(const int &index)
{
if (index < 0 || index >= m_favoriteAppsData.size()) {
return;
}
MenuManager::instance()->showMenu(m_favoriteAppsData.at(index));
}
void FavoriteAppsModel::exchangedAppsOrder(int startIndex, int endIndex)
{
int endNum = m_favoriteAppsData.at(endIndex).favorite();
QString startId = m_favoriteAppsData.at(startIndex).id();
m_appInfoTable->changeFavoriteAppPos(startId,endNum);
AppDataManager::instance()->changedFavoriteOrderSignal(startId, endNum);
}
QList<QAction *> FavoriteMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data)
{
if (!parent || (type != DataType)) {
return {};
}
DataEntity app = data.value<DataEntity>();
if (app.type() != DataType::Normal) {
return {};
}
QList<QAction *> list;
if (app.favorite() == 0) {
list << new QAction(QObject::tr("Fix to favorite"), parent);
QObject::connect(list.last(), &QAction::triggered, parent, [app] {
Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 1);
});
} else {
list << new QAction(QObject::tr("Remove from favorite"), parent);
QObject::connect(list.last(), &QAction::triggered, parent, [app] {
Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 0);
});
}
return list;
}
} // UkuiMenu

View File

@ -96,9 +96,9 @@ void AppMenuProvider::addToPanelAction(QObject *parent, const QString &appId, QL
return;
}
QString actionName = isFixedOnTaskBar ? QObject::tr("Remove From Taskbar") : QObject::tr("Add To Taskbar");
QString actionName = isFixedOnTaskBar ? QObject::tr("Remove from taskbar") : QObject::tr("Add to taskbar");
QString functionName = isFixedOnTaskBar ? "RemoveFromTaskbar" : "AddToTaskbar";
list << new QAction(actionName);
list << new QAction(actionName, parent);
QObject::connect(list.last(), &QAction::triggered, parent, [appId, functionName] {
QDBusInterface iface("com.ukui.panel.desktop", "/", "com.ukui.panel.desktop", QDBusConnection::sessionBus());
@ -119,7 +119,7 @@ void AppMenuProvider::addToPanelAction(QObject *parent, const QString &appId, QL
void AppMenuProvider::addToDesktopAction(QObject *parent, const QString &appId, QList<QAction *> &list)
{
list << new QAction(QObject::tr("Add to desktop shortcuts"));
list << new QAction(QObject::tr("Add to desktop shortcuts"), parent);
QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString path = QString(desktopPath + "/" + QFileInfo(appId).fileName());
@ -144,7 +144,7 @@ void AppMenuProvider::addUninstall(QObject *parent, const QString &appId, QList<
return appId.contains(appName);
});
if (!isSystemApp) {
list << new QAction(QObject::tr("Uninstall")); //QIcon::fromTheme("edit-delete-symbolic")
list << new QAction(QObject::tr("Uninstall"), parent); //QIcon::fromTheme("edit-delete-symbolic")
QObject::connect(list.last(), &QAction::triggered, parent, [appId] {
AppManager::instance()->launchBinaryApp("kylin-uninstaller", appId);
});