diff --git a/CMakeLists.txt b/CMakeLists.txt index dc8f481..b1d24c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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文件 diff --git a/src/menu/menu-manager.cpp b/src/menu/menu-manager.cpp new file mode 100644 index 0000000..d0e31ce --- /dev/null +++ b/src/menu/menu-manager.cpp @@ -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 . + * + */ + +#include "menu-manager.h" +#include "app-manager.h" + +#include +#include + +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 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 diff --git a/src/menu/menu-manager.h b/src/menu/menu-manager.h new file mode 100644 index 0000000..470ef63 --- /dev/null +++ b/src/menu/menu-manager.h @@ -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 . + * + */ + +#ifndef UKUI_MENU_MENU_MANAGER_H +#define UKUI_MENU_MENU_MANAGER_H + +#include +#include +#include + +#include "commons.h" + +namespace UkuiMenu { + +class MenuProvider +{ +public: + enum RequestType { + DataType = 0, + Custom, + }; + virtual ~MenuProvider() = default; + virtual int index() = 0; + virtual QList 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 m_providers; +}; + +} // UkuiMenu + +#endif //UKUI_MENU_MENU_MANAGER_H