插件接口新增id属性作为唯一标识

This commit is contained in:
hewenfei 2023-02-13 20:17:29 +08:00
parent 86a8b6387d
commit c61c67b387
5 changed files with 50 additions and 12 deletions

View File

@ -42,7 +42,7 @@ void DataProviderManager::initProviders()
auto *allProvider = new AllAppDataProvider; auto *allProvider = new AllAppDataProvider;
registerProvider(allProvider); registerProvider(allProvider);
activateProvider(allProvider->name()); activateProvider(allProvider->id());
allProvider->moveToThread(&m_worker); allProvider->moveToThread(&m_worker);
} }
@ -53,14 +53,14 @@ void DataProviderManager::registerProvider(DataProviderPluginIFace *provider)
return; return;
} }
if (m_providers.contains(provider->name())) { if (m_providers.contains(provider->id())) {
return; return;
} }
m_providers.insert(provider->name(), provider); m_providers.insert(provider->id(), provider);
connect(provider, &AllAppDataProvider::dataChanged, this, [this, provider](const QVector<DataEntity> &data) { connect(provider, &AllAppDataProvider::dataChanged, this, [this, provider](const QVector<DataEntity> &data) {
//qDebug() << "==DataProviderManager dataChanged:" << provider->name() << QThread::currentThreadId(); //qDebug() << "==DataProviderManager dataChanged:" << provider->name() << QThread::currentThreadId();
if (m_activatedPlugin != provider->name()) { if (m_activatedPlugin != provider->id()) {
return; return;
} }
@ -83,14 +83,33 @@ QStringList DataProviderManager::providers() const
return m_providers.keys(); return m_providers.keys();
} }
void DataProviderManager::activateProvider(const QString &name) QVector<ProviderInfo> DataProviderManager::providers(PluginGroup::Group group) const
{ {
if (!m_providers.contains(name)) { QVector<ProviderInfo> infos;
for (const auto &provider : m_providers) {
if (group == provider->group()) {
infos.append({
provider->index(),
provider->id(),
provider->name(),
provider->icon(),
provider->title(),
provider->group()
});
}
}
return infos;
}
void DataProviderManager::activateProvider(const QString &id)
{
if (!m_providers.contains(id)) {
return; return;
} }
m_activatedPlugin = name; m_activatedPlugin = id;
Q_EMIT pluginChanged(); Q_EMIT pluginChanged(m_activatedPlugin, m_providers.value(m_activatedPlugin)->group());
} }
DataProviderManager::~DataProviderManager() DataProviderManager::~DataProviderManager()

View File

@ -27,6 +27,17 @@
namespace UkuiMenu { namespace UkuiMenu {
class ProviderInfo
{
public:
int index;
QString id;
QString name;
QString icon;
QString title;
PluginGroup::Group group;
};
class DataProviderManager : public QObject class DataProviderManager : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -35,12 +46,13 @@ public:
~DataProviderManager() override; ~DataProviderManager() override;
QStringList providers() const; QStringList providers() const;
QVector<ProviderInfo> providers(PluginGroup::Group group) const;
QString activatedProvider() const; QString activatedProvider() const;
void activateProvider(const QString &name); void activateProvider(const QString &id);
QVector<DataEntity> data() const; QVector<DataEntity> data() const;
Q_SIGNALS: Q_SIGNALS:
void pluginChanged(); void pluginChanged(const QString &id, PluginGroup::Group group);
void dataChanged(QVector<DataEntity> data); void dataChanged(QVector<DataEntity> data);
private: private:

View File

@ -34,7 +34,7 @@ class PluginGroup
Q_GADGET Q_GADGET
public: public:
enum Group { enum Group {
Button, Button = 0,
SortMenuItem SortMenuItem
}; };
Q_ENUM(Group) Q_ENUM(Group)
@ -46,6 +46,7 @@ class DataProviderPluginIFace : public QObject
public: public:
explicit DataProviderPluginIFace() : QObject(nullptr) {} explicit DataProviderPluginIFace() : QObject(nullptr) {}
virtual int index() = 0; virtual int index() = 0;
virtual QString id() = 0;
virtual QString name() = 0; virtual QString name() = 0;
virtual QString icon() = 0; virtual QString icon() = 0;
virtual QString title() = 0; virtual QString title() = 0;

View File

@ -40,6 +40,11 @@ int AllAppDataProvider::index()
return 0; return 0;
} }
QString AllAppDataProvider::id()
{
return "all";
}
QString AllAppDataProvider::name() QString AllAppDataProvider::name()
{ {
return tr("All"); return tr("All");
@ -47,7 +52,7 @@ QString AllAppDataProvider::name()
QString AllAppDataProvider::icon() QString AllAppDataProvider::icon()
{ {
return "text-plain"; return "image://appicon/text-plain";
} }
QString AllAppDataProvider::title() QString AllAppDataProvider::title()

View File

@ -31,6 +31,7 @@ class AllAppDataProvider : public DataProviderPluginIFace
public: public:
explicit AllAppDataProvider(); explicit AllAppDataProvider();
int index() override; int index() override;
QString id() override;
QString name() override; QString name() override;
QString icon() override; QString icon() override;
QString title() override; QString title() override;