Skip to content

Commit 03f8893

Browse files
committed
feat: Add NetPlugin right-click menu function (DDE)
1 parent d3214ad commit 03f8893

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

dde-dock-plugin/NetPlugin.cpp

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "NetPlugin.h"
66
#include <QLabel>
7+
78
#include <QDebug>
89

910
DWIDGET_USE_NAMESPACE
@@ -22,7 +23,7 @@ NetPlugin::NetPlugin(QObject *parent)
2223
*/
2324
const QString NetPlugin::pluginName() const
2425
{
25-
return "datetime"; // MonitorNet
26+
return "datetime";
2627
}
2728

2829
/*!
@@ -49,24 +50,92 @@ QWidget *NetPlugin::itemWidget(const QString &itemKey)
4950
return m_winDockNet;
5051
}
5152

53+
/*!
54+
* \brief NetPlugin::pluginIsAllowDisable 插件是否允许被禁用
55+
* \return 告诉 dde-dock 本插件允许禁用
56+
*/
57+
bool NetPlugin::pluginIsAllowDisable()
58+
{
59+
return true;
60+
}
61+
62+
/*!
63+
* \brief NetPlugin::pluginIsDisable 插件是否被禁用
64+
* \return 插件是否被禁用的状态
65+
* \note getValue() 的第二个参数 “disabled” 表示存储这个值的键(所有配置都是以键值对的方式存储的)
66+
* 第三个参数表示默认值,即默认不禁用
67+
*/
68+
bool NetPlugin::pluginIsDisable()
69+
{
70+
return m_proxyInter->getValue(this, "disabled", false).toBool();
71+
}
72+
73+
/*!
74+
* \brief NetPlugin::pluginStateSwitched 插件状态切换
75+
*/
76+
void NetPlugin::pluginStateSwitched()
77+
{
78+
// 将旧的 "禁用状态" 数值取反后,保存到 key-val 中
79+
const bool disableState = !pluginIsDisable();
80+
m_proxyInter->saveValue(this, "disabled", disableState);
81+
82+
// 根据新的禁用状态值,处理主控的加载与卸载
83+
if (disableState)
84+
m_proxyInter->itemRemoved(this, pluginName());
85+
else
86+
m_proxyInter->itemAdded(this, pluginName());
87+
}
88+
89+
/*!
90+
* \brief NetPlugin::pluginDisplayName 插件在 dock 的右键时候,显示名称
91+
* \return 插件在 dock 右键显示的预称
92+
*/
93+
const QString NetPlugin::pluginDisplayName() const
94+
{
95+
return QString("MonitorNet");
96+
}
97+
98+
/*!
99+
* \brief NetPlugin::itemContextMenu 插件右键后现实的菜单列表
100+
* \param itemKey
101+
* \return 返回 JSON 格式的菜单数据
102+
* \note 增加右键菜单功能需要实现此两个接口:itemContextMenu + invokedMenuItem
103+
*/
52104
const QString NetPlugin::itemContextMenu(const QString &itemKey)
53105
{
54106
Q_UNUSED(itemKey);
55107

56-
QList<QVariant> items;
57-
items.reserve(1);
58-
59108
QMap<QString, QVariant> update;
60109
update["itemId"] = "update";
61110
update["itemText"] = "刷新";
62111
update["isActive"] = true;
112+
113+
QList<QVariant> items;
114+
items.reserve(1);
63115
items.push_back(update);
64116

65117
QMap<QString, QVariant> menu;
66118
menu["items"] = items;
67119
menu["checkableMenu"] = false;
68120
menu["singleCheck"] = false;
69121

70-
// 返回 JSON 格式的菜单数据
71122
return QJsonDocument::fromVariant(menu).toJson();
72123
}
124+
125+
/*!
126+
* \brief NetPlugin::invokedMenuItem 菜单项被点击后的回调函数
127+
* \param itemKey
128+
* \param menuId menu item ID
129+
* \param checked
130+
* \note * \note 增加右键菜单功能需要实现此两个接口:itemContextMenu + invokedMenuItem
131+
*/
132+
void NetPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)
133+
{
134+
Q_UNUSED(itemKey)
135+
Q_UNUSED(checked)
136+
137+
if (menuId == "update") {
138+
m_proxyInter->itemRemoved(this, pluginName());
139+
m_proxyInter->itemAdded(this, pluginName());
140+
}
141+
}

dde-dock-plugin/NetPlugin.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <pluginsiteminterface.h>
1010
#include <dtkwidget_global.h>
1111
#include "WinDockNet.h"
12-
//#include "../widgets/WinDockNet.h"
1312

1413
DWIDGET_USE_NAMESPACE
1514

@@ -28,12 +27,24 @@ class NetPlugin : public QObject, public PluginsItemInterface
2827
virtual void init(PluginProxyInterface *proxyInter) override;
2928
virtual QWidget *itemWidget(const QString &itemKey) override;
3029

30+
// 插件禁用和启用相关的接口
31+
virtual bool pluginIsAllowDisable() override;
32+
virtual bool pluginIsDisable() override;
33+
virtual void pluginStateSwitched() override;
34+
35+
// 其它额外接口
36+
virtual const QString pluginDisplayName() const override;
3137
virtual const QString itemContextMenu(const QString &itemKey) override;
38+
virtual void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) override;
3239

3340
private:
3441
WinDockNet *m_winDockNet;
3542
PluginProxyInterface *m_proxyInter;
3643

44+
45+
46+
// PluginsItemInterface interface
47+
public:
3748
};
3849

3950
#endif //LFXNET_NETPLUGIN_H

dde-dock-plugin/WinDockNet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class WinDockNet : public QWidget
2323

2424
void init();
2525

26-
2726
public slots:
2827
void onNet();
2928
void onCpu();

0 commit comments

Comments
 (0)