From 2ebef72a1845da199927a5cf84fbc1cbcde2faef Mon Sep 17 00:00:00 2001 From: listenerri Date: Wed, 21 Nov 2018 17:14:07 +0800 Subject: [PATCH] feat: support item sort key interface of TrayPlugin inner plugin Change-Id: I8da85bb4d821c400cac2113696d3c12c7fed0c36 --- .../system-trays/systemtrayscontroller.cpp | 74 +++++++++++++++---- .../tray/system-trays/systemtrayscontroller.h | 11 ++- plugins/tray/trayplugin.cpp | 21 ++++++ plugins/tray/trayplugin.h | 1 + 4 files changed, 91 insertions(+), 16 deletions(-) diff --git a/plugins/tray/system-trays/systemtrayscontroller.cpp b/plugins/tray/system-trays/systemtrayscontroller.cpp index cdd642175..8e6cc4be1 100644 --- a/plugins/tray/system-trays/systemtrayscontroller.cpp +++ b/plugins/tray/system-trays/systemtrayscontroller.cpp @@ -39,15 +39,15 @@ SystemTraysController::SystemTraysController(QObject *parent) void SystemTraysController::itemAdded(PluginsItemInterface * const itemInter, const QString &itemKey) { // check if same item added - if (m_pluginList.contains(itemInter)) - if (m_pluginList[itemInter].contains(itemKey)) + if (m_pluginsMap.contains(itemInter)) + if (m_pluginsMap[itemInter].contains(itemKey)) return; SystemTrayItem *item = new SystemTrayItem(itemInter, itemKey); item->setVisible(false); - m_pluginList[itemInter][itemKey] = item; + m_pluginsMap[itemInter][itemKey] = item; emit systemTrayAdded(itemKey, item); } @@ -74,7 +74,7 @@ void SystemTraysController::itemRemoved(PluginsItemInterface * const itemInter, emit systemTrayRemoved(itemKey); - m_pluginList[itemInter].remove(itemKey); + m_pluginsMap[itemInter].remove(itemKey); // do not delete the itemWidget object(specified in the plugin interface) item->centralWidget()->setParent(nullptr); @@ -104,7 +104,7 @@ void SystemTraysController::startLoader() void SystemTraysController::displayModeChanged() { const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value(); - const auto inters = m_pluginList.keys(); + const auto inters = m_pluginsMap.keys(); for (auto inter : inters) inter->displayModeChanged(displayMode); @@ -113,7 +113,7 @@ void SystemTraysController::displayModeChanged() void SystemTraysController::positionChanged() { const Dock::Position position = qApp->property(PROP_POSITION).value(); - const auto inters = m_pluginList.keys(); + const auto inters = m_pluginsMap.keys(); for (auto inter : inters) inter->positionChanged(position); @@ -138,7 +138,7 @@ void SystemTraysController::loadPlugin(const QString &pluginFile) return; } - m_pluginList.insert(interface, QMap()); + m_pluginsMap.insert(interface, QMap()); QString dbusService = meta.value("depends-daemon-dbus-service").toString(); if (!dbusService.isEmpty() && !m_dbusDaemonInterface->isServiceRegistered(dbusService).value()) { @@ -184,21 +184,69 @@ bool SystemTraysController::eventFilter(QObject *o, QEvent *e) SystemTrayItem *SystemTraysController::pluginItemAt(PluginsItemInterface * const itemInter, const QString &itemKey) const { - if (!m_pluginList.contains(itemInter)) + if (!m_pluginsMap.contains(itemInter)) return nullptr; - return m_pluginList[itemInter][itemKey]; + return m_pluginsMap[itemInter][itemKey]; } -void SystemTraysController::saveValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant &value) { +PluginsItemInterface *SystemTraysController::pluginInterAt(const QString &itemKey) const +{ + for (auto it = m_pluginsMap.constBegin(); it != m_pluginsMap.constEnd(); ++it) { + for (auto key : it.value().keys()) { + if (key == itemKey) { + return it.key(); + } + } + } + + return nullptr; +} + +PluginsItemInterface *SystemTraysController::pluginInterAt(SystemTrayItem *systemTrayItem) const +{ + for (auto it = m_pluginsMap.constBegin(); it != m_pluginsMap.constEnd(); ++it) { + for (auto item : it.value().values()) { + if (item == systemTrayItem) { + return it.key(); + } + } + } + + return nullptr; +} + +void SystemTraysController::saveValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant &value) { m_pluginsSetting.beginGroup(itemInter->pluginName()); - m_pluginsSetting.setValue(itemKey, value); + m_pluginsSetting.setValue(key, value); m_pluginsSetting.endGroup(); } -const QVariant SystemTraysController::getValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant& failback) { +const QVariant SystemTraysController::getValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant& failback) { m_pluginsSetting.beginGroup(itemInter->pluginName()); - QVariant value { std::move(m_pluginsSetting.value(itemKey, failback)) }; + QVariant value { std::move(m_pluginsSetting.value(key, failback)) }; m_pluginsSetting.endGroup(); return std::move(value); } + +int SystemTraysController::systemTrayItemSortKey(const QString &itemKey) +{ + auto inter = pluginInterAt(itemKey); + + if (!inter) { + return -1; + } + + return inter->itemSortKey(itemKey); +} + +void SystemTraysController::setSystemTrayItemSortKey(const QString &itemKey, const int order) +{ + auto inter = pluginInterAt(itemKey); + + if (!inter) { + return; + } + + inter->setSortKey(itemKey, order); +} diff --git a/plugins/tray/system-trays/systemtrayscontroller.h b/plugins/tray/system-trays/systemtrayscontroller.h index 6877117a1..3ccc5ff3f 100644 --- a/plugins/tray/system-trays/systemtrayscontroller.h +++ b/plugins/tray/system-trays/systemtrayscontroller.h @@ -43,8 +43,11 @@ class SystemTraysController : public QObject, PluginProxyInterface void itemUpdate(PluginsItemInterface * const itemInter, const QString &itemKey); void itemRemoved(PluginsItemInterface * const itemInter, const QString &itemKey); void requestContextMenu(PluginsItemInterface * const itemInter, const QString &itemKey); - void saveValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant &value); - const QVariant getValue(PluginsItemInterface *const itemInter, const QString &itemKey, const QVariant& failback = QVariant()); + void saveValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant &value); + const QVariant getValue(PluginsItemInterface *const itemInter, const QString &key, const QVariant& failback = QVariant()); + + int systemTrayItemSortKey(const QString &itemKey); + void setSystemTrayItemSortKey(const QString &itemKey, const int order); public slots: void startLoader(); @@ -63,10 +66,12 @@ private slots: private: bool eventFilter(QObject *o, QEvent *e); SystemTrayItem *pluginItemAt(PluginsItemInterface * const itemInter, const QString &itemKey) const; + PluginsItemInterface *pluginInterAt(const QString &itemKey) const; + PluginsItemInterface *pluginInterAt(SystemTrayItem *systemTrayItem) const; private: QDBusConnectionInterface *m_dbusDaemonInterface; - QMap> m_pluginList; + QMap> m_pluginsMap; QSettings m_pluginsSetting; }; diff --git a/plugins/tray/trayplugin.cpp b/plugins/tray/trayplugin.cpp index 3c87e08ce..1319b8f1d 100644 --- a/plugins/tray/trayplugin.cpp +++ b/plugins/tray/trayplugin.cpp @@ -156,6 +156,11 @@ bool TrayPlugin::itemIsInContainer(const QString &itemKey) int TrayPlugin::itemSortKey(const QString &itemKey) { + // 如果是系统托盘图标则调用内部插件的相应接口 + if (isSystemTrayItem(itemKey)) { + return m_systemTraysController->systemTrayItemSortKey(itemKey); + } + Dock::DisplayMode mode = displayMode(); const QString key = QString("pos_%1_%2").arg(itemKey).arg(mode); @@ -164,6 +169,11 @@ int TrayPlugin::itemSortKey(const QString &itemKey) void TrayPlugin::setSortKey(const QString &itemKey, const int order) { + // 如果是系统托盘图标则调用内部插件的相应接口 + if (isSystemTrayItem(itemKey)) { + return m_systemTraysController->setSystemTrayItemSortKey(itemKey, order); + } + const QString key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); m_proxyInter->saveValue(this, key, order); } @@ -201,6 +211,17 @@ const QString TrayPlugin::getWindowClass(quint32 winId) return ret; } +bool TrayPlugin::isSystemTrayItem(const QString &itemKey) +{ + AbstractTrayWidget * const trayWidget = m_trayMap.value(itemKey, nullptr); + + if (trayWidget && trayWidget->trayTyep() == AbstractTrayWidget::TrayType::SystemTray) { + return true; + } + + return false; +} + void TrayPlugin::sniItemsChanged() { const QStringList &itemServicePaths = m_sniWatcher->RegisteredStatusNotifierItems(); diff --git a/plugins/tray/trayplugin.h b/plugins/tray/trayplugin.h index 0aed5be3b..31ea3283b 100644 --- a/plugins/tray/trayplugin.h +++ b/plugins/tray/trayplugin.h @@ -65,6 +65,7 @@ class TrayPlugin : public QObject, PluginsItemInterface private: void loadIndicator(); const QString getWindowClass(quint32 winId); + bool isSystemTrayItem(const QString &itemKey); private slots: void addTrayWidget(const QString &itemKey, AbstractTrayWidget *trayWidget);