插件接口新增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;
registerProvider(allProvider);
activateProvider(allProvider->name());
activateProvider(allProvider->id());
allProvider->moveToThread(&m_worker);
}
@ -53,14 +53,14 @@ void DataProviderManager::registerProvider(DataProviderPluginIFace *provider)
return;
}
if (m_providers.contains(provider->name())) {
if (m_providers.contains(provider->id())) {
return;
}
m_providers.insert(provider->name(), provider);
m_providers.insert(provider->id(), provider);
connect(provider, &AllAppDataProvider::dataChanged, this, [this, provider](const QVector<DataEntity> &data) {
//qDebug() << "==DataProviderManager dataChanged:" << provider->name() << QThread::currentThreadId();
if (m_activatedPlugin != provider->name()) {
if (m_activatedPlugin != provider->id()) {
return;
}
@ -83,14 +83,33 @@ QStringList DataProviderManager::providers() const
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;
}
m_activatedPlugin = name;
Q_EMIT pluginChanged();
m_activatedPlugin = id;
Q_EMIT pluginChanged(m_activatedPlugin, m_providers.value(m_activatedPlugin)->group());
}
DataProviderManager::~DataProviderManager()

View File

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

View File

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

View File

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

View File

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