forked from openkylin/ukui-menu
新建菜单插件接口
This commit is contained in:
parent
2c77395f40
commit
19eefc13fd
|
@ -48,6 +48,7 @@ set(SingleApplication "SingleApplication")
|
|||
|
||||
# include文件夹
|
||||
include_directories(src)
|
||||
include_directories(src/menu)
|
||||
include_directories(src/model)
|
||||
include_directories(src/appdata)
|
||||
include_directories(src/settings)
|
||||
|
@ -102,6 +103,7 @@ set(SOURCE_FILES
|
|||
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
|
||||
)
|
||||
|
||||
# qrc文件
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* 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 "menu-manager.h"
|
||||
#include "app-manager.h"
|
||||
|
||||
#include <QMenu>
|
||||
#include <QCursor>
|
||||
|
||||
namespace UkuiMenu {
|
||||
|
||||
MenuManager *MenuManager::instance()
|
||||
{
|
||||
static MenuManager manager;
|
||||
return &manager;
|
||||
}
|
||||
|
||||
MenuManager::MenuManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
MenuManager::~MenuManager()
|
||||
{
|
||||
for (auto &provider : m_providers) {
|
||||
delete provider;
|
||||
provider = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void MenuManager::registerMenuProvider(MenuProvider *provider)
|
||||
{
|
||||
if (!provider) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_providers.append(provider);
|
||||
}
|
||||
|
||||
void MenuManager::showMenu(const DataEntity &entity, const QPoint &point)
|
||||
{
|
||||
showMenu(MenuProvider::DataType, QVariant::fromValue(entity), point);
|
||||
}
|
||||
|
||||
void MenuManager::showMenu(const MenuProvider::RequestType &type, const QVariant &data, const QPoint &point)
|
||||
{
|
||||
QMenu menu;
|
||||
|
||||
QList<QAction*> actions;
|
||||
for (const auto &provider : m_providers) {
|
||||
actions.append(provider->generateActions(&menu, type, data));
|
||||
}
|
||||
|
||||
if (actions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
menu.addActions(actions);
|
||||
menu.exec(checkPoint(point));
|
||||
}
|
||||
|
||||
inline QPoint MenuManager::checkPoint(const QPoint &rawPoint)
|
||||
{
|
||||
if (rawPoint.isNull()) {
|
||||
return QCursor::pos();
|
||||
}
|
||||
return rawPoint;
|
||||
}
|
||||
|
||||
} // 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_MENU_MANAGER_H
|
||||
#define UKUI_MENU_MENU_MANAGER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QAction>
|
||||
#include <QPoint>
|
||||
|
||||
#include "commons.h"
|
||||
|
||||
namespace UkuiMenu {
|
||||
|
||||
class MenuProvider
|
||||
{
|
||||
public:
|
||||
enum RequestType {
|
||||
DataType = 0,
|
||||
Custom,
|
||||
};
|
||||
virtual ~MenuProvider() = default;
|
||||
virtual int index() = 0;
|
||||
virtual QList<QAction*> generateActions(QObject *parent, const RequestType &type, const QVariant &data) = 0;
|
||||
};
|
||||
|
||||
class MenuManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static MenuManager *instance();
|
||||
~MenuManager() override;
|
||||
void registerMenuProvider(MenuProvider *provider);
|
||||
void showMenu(const DataEntity &entity, const QPoint &point = QPoint());
|
||||
void showMenu(const MenuProvider::RequestType &type, const QVariant &data, const QPoint &point = QPoint());
|
||||
|
||||
private:
|
||||
MenuManager();
|
||||
inline QPoint checkPoint(const QPoint &rawPoint);
|
||||
|
||||
private:
|
||||
QList<MenuProvider*> m_providers;
|
||||
};
|
||||
|
||||
} // UkuiMenu
|
||||
|
||||
#endif //UKUI_MENU_MENU_MANAGER_H
|
Loading…
Reference in New Issue