Skip to content

Commit da30b7a

Browse files
ut003640deepin-bot[bot]
authored andcommitted
tests: update dss-network-plugin test example
update dss-network-plugin test example Log: update dss-network-plugin test example
1 parent 0c73dd1 commit da30b7a

File tree

8 files changed

+465
-51
lines changed

8 files changed

+465
-51
lines changed

dss_example/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ add_executable(${PROJECT_NAME}
2121
target_include_directories(${PROJECT_NAME} PUBLIC
2222
.
2323
../src
24+
../dss-network-plugin
2425
Dtk6::Widget
2526
Qt6::DBus
2627
Qt6::Network
@@ -35,5 +36,6 @@ target_link_libraries(${PROJECT_NAME} PRIVATE
3536
Qt6::Network
3637
Qt6::Widgets
3738
KF6::NetworkManagerQt
39+
dss-network-plugin
3840
dde-network-core6
3941
)

dss_example/dssscreenmanager.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// SPDX-FileCopyrightText: 2011 - 2026 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
#include "dssscreenmanager.h"
6+
7+
#include "dsstestwidget.h"
8+
#include "networkmodule.h"
9+
10+
#include <QGuiApplication>
11+
#include <QScreen>
12+
13+
DssScreenManager::DssScreenManager(QObject *parent)
14+
: QObject(parent)
15+
, m_netModule(new dde::network::NetworkPlugin(this))
16+
{
17+
m_netModule->init();
18+
initConnection();
19+
initScreen();
20+
}
21+
22+
DssScreenManager::~DssScreenManager()
23+
{
24+
qDeleteAll(m_screenWidget);
25+
m_screenWidget.clear();
26+
}
27+
28+
void DssScreenManager::showWindow()
29+
{
30+
for (auto it = m_screenWidget.constBegin(); it != m_screenWidget.constEnd(); it++) {
31+
showWindow(it.key(), it.value());
32+
}
33+
}
34+
35+
void DssScreenManager::initConnection()
36+
{
37+
connect(qApp, &QGuiApplication::screenAdded, this, &DssScreenManager::onScreenAdded);
38+
connect(qApp, &QGuiApplication::screenRemoved, this, &DssScreenManager::onScreenRemoved);
39+
}
40+
41+
void DssScreenManager::initScreen()
42+
{
43+
QList<QScreen *> screens = QGuiApplication::screens();
44+
for (QScreen *screen : screens) {
45+
DssTestWidget *testWidget = new DssTestWidget(m_netModule);
46+
m_screenWidget[screen] = testWidget;
47+
}
48+
}
49+
50+
void DssScreenManager::showWindow(QScreen *screen, DssTestWidget *testWidget)
51+
{
52+
const QRect screenGeometry = screen->geometry();
53+
testWidget->resize(330, 800);
54+
testWidget->move(screenGeometry.x() + (screenGeometry.width() - testWidget->width()) / 2, (screenGeometry.height() - testWidget->height()) / 2);
55+
testWidget->show();
56+
}
57+
58+
void DssScreenManager::onScreenAdded(QScreen *screen)
59+
{
60+
DssTestWidget *testWidget = new DssTestWidget(m_netModule);
61+
m_screenWidget[screen] = testWidget;
62+
showWindow(screen, testWidget);
63+
}
64+
65+
void DssScreenManager::onScreenRemoved(QScreen *screen)
66+
{
67+
if (m_screenWidget.contains(screen)) {
68+
DssTestWidget *testWidget = m_screenWidget[screen];
69+
m_screenWidget.remove(screen);
70+
testWidget->deleteLater();
71+
}
72+
}

dss_example/dssscreenmanager.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-FileCopyrightText: 2011 - 2026 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
#ifndef DSSSCREENMANAGER_H
6+
#define DSSSCREENMANAGER_H
7+
8+
#include <QMap>
9+
#include <QObject>
10+
11+
class DssTestWidget;
12+
class QScreen;
13+
14+
namespace dde {
15+
namespace network {
16+
class NetworkPlugin;
17+
} // namespace network
18+
} // namespace dde
19+
20+
class DssScreenManager : public QObject
21+
{
22+
Q_OBJECT
23+
24+
public:
25+
explicit DssScreenManager(QObject *parent = nullptr);
26+
~DssScreenManager();
27+
void showWindow();
28+
29+
private:
30+
void initConnection();
31+
void initScreen();
32+
void showWindow(QScreen *screen, DssTestWidget *testWidget);
33+
34+
protected:
35+
void onScreenAdded(QScreen *screen);
36+
void onScreenRemoved(QScreen *screen);
37+
38+
private:
39+
QMap<QScreen *, DssTestWidget *> m_screenWidget;
40+
dde::network::NetworkPlugin *m_netModule;
41+
};
42+
43+
#endif // DSSSCREENMANAGER_H

dss_example/dsstestwidget.cpp

Lines changed: 118 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,62 @@
11
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
22
//
3-
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
// SPDX-License-Identifier: GPL-3.0-or-later
44

55
#include "dsstestwidget.h"
6-
#include <QPushButton>
7-
#include <QLabel>
8-
#include <QMessageBox>
6+
7+
#include "networkmodule.h"
8+
#include "popupwindow.h"
9+
10+
#include <networkcontroller.h>
911

1012
#include <DPalette>
11-
#include <DFloatingButton>
12-
#include <QMouseEvent>
1313

1414
#include <QHBoxLayout>
15-
#include <networkcontroller.h>
15+
#include <QMouseEvent>
1616

17+
using namespace dde::network;
1718
using namespace Dtk::Widget;
1819
DGUI_USE_NAMESPACE
1920

20-
DssTestWidget::DssTestWidget(QWidget *parent)
21+
DssTestWidget::DssTestWidget(dde::network::NetworkPlugin *networkPlugin, QWidget *parent)
2122
: QWidget(parent)
22-
, m_button(nullptr)
23+
, m_pModule(networkPlugin)
24+
, m_container(nullptr)
25+
, m_tipContainer(nullptr)
2326
{
24-
m_button = new Dtk::Widget::DFloatingButton(this);
25-
m_button->setIconSize(QSize(26, 26));
26-
m_button->setFixedSize(QSize(52, 52));
27-
m_button->setAutoExclusive(true);
28-
m_button->setBackgroundRole(DPalette::Button);
29-
m_button->setIcon(QIcon("network-online-symbolic"));
30-
m_button->installEventFilter(this);
31-
32-
// 创建一个简单的布局
33-
QHBoxLayout *layout = new QHBoxLayout(this);
34-
layout->setSpacing(0);
27+
QWidget *iconWidget = new QWidget(this);
28+
m_iconButton = new DFloatingButton(iconWidget);
29+
m_iconButton->setIconSize(QSize(26, 26));
30+
m_iconButton->setFixedSize(QSize(52, 52));
31+
m_iconButton->setAutoExclusive(true);
32+
QPalette palette = m_iconButton->palette();
33+
palette.setColor(QPalette::ColorRole::Window, Qt::transparent);
34+
m_iconButton->setPalette(palette);
35+
m_iconButton->installEventFilter(this);
36+
connect(m_iconButton, &Dtk::Widget::DFloatingButton::clicked, this, &DssTestWidget::onClickButton);
37+
38+
QHBoxLayout *iconLayout = new QHBoxLayout(iconWidget);
39+
iconLayout->setAlignment(Qt::AlignHCenter);
40+
iconLayout->addWidget(m_iconButton);
41+
42+
QPalette paletteSelf = this->palette();
43+
paletteSelf.setColor(QPalette::ColorRole::Window, Qt::darkBlue);
44+
setAutoFillBackground(true);
45+
setPalette(paletteSelf);
46+
47+
QVBoxLayout *layout = new QVBoxLayout(this);
3548
layout->setContentsMargins(0, 0, 0, 0);
36-
layout->addWidget(m_button);
49+
layout->addStretch();
50+
layout->addWidget(iconWidget);
51+
52+
QHBoxLayout *iconButtonLayout = new QHBoxLayout(m_iconButton);
53+
QWidget *itemWidget = m_pModule->itemWidget();
54+
itemWidget->setParent(m_iconButton);
55+
iconButtonLayout->addWidget(itemWidget);
56+
57+
installEventFilter(this);
58+
itemWidget->installEventFilter(this);
59+
m_iconButton->installEventFilter(this);
3760
}
3861

3962
DssTestWidget::~DssTestWidget()
@@ -42,24 +65,85 @@ DssTestWidget::~DssTestWidget()
4265

4366
bool DssTestWidget::eventFilter(QObject *watched, QEvent *event)
4467
{
45-
if (watched == m_button) {
68+
if (watched == m_iconButton) {
69+
// 当鼠标移入的时候显示提示信息
4670
switch (event->type()) {
47-
case QEvent::MouseButtonPress: {
48-
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
49-
if (mouseEvent->button() == Qt::RightButton) {
50-
QMessageBox::information(this, "DSS Example", "右键点击 - 网络测试程序");
51-
} else if (mouseEvent->button() == Qt::LeftButton) {
52-
QMessageBox::information(this, "DSS Example", "左键点击 - 网络测试程序");
53-
}
71+
case QEvent::Enter: {
72+
if (!m_tipContainer) {
73+
QWidget *tipWidget = m_pModule->itemTipsWidget();
74+
m_tipContainer = new PopupWindow(this);
75+
m_tipContainer->setContent(tipWidget);
76+
m_tipContainer->resizeWithContent();
77+
m_tipContainer->setArrowX(tipWidget->width() / 2);
5478
}
79+
m_tipContainer->raise();
80+
m_tipContainer->show(QPoint(rect().center().x(), m_iconButton->parentWidget()->y()));
81+
} break;
82+
case QEvent::Leave: {
83+
m_tipContainer->hide();
5584
break;
56-
case QEvent::Enter:
57-
break;
58-
case QEvent::Leave:
85+
}
86+
default:
5987
break;
60-
default: break;
6188
}
6289
}
63-
6490
return QWidget::eventFilter(watched, event);
6591
}
92+
93+
void DssTestWidget::resizeEvent(QResizeEvent *event)
94+
{
95+
if (m_container && m_container->isVisible()) {
96+
m_container->resizeWithContent();
97+
m_container->show(QPoint(rect().width() / 2, m_iconButton->parentWidget()->y()));
98+
}
99+
QWidget::resizeEvent(event);
100+
}
101+
102+
void DssTestWidget::mousePressEvent(QMouseEvent *event)
103+
{
104+
if (m_tipContainer && m_tipContainer->isVisible())
105+
m_tipContainer->hide();
106+
if (m_container && m_container->isVisible())
107+
m_container->hide();
108+
109+
QWidget::mousePressEvent(event);
110+
}
111+
112+
void DssTestWidget::onClickButton()
113+
{
114+
if (!m_pModule->content())
115+
return;
116+
117+
// 左键弹出菜单
118+
if (!m_container) {
119+
m_container = new PopupWindow(this);
120+
connect(m_container, &PopupWindow::contentDetach, this, [ this ] {
121+
m_container->setContent(nullptr);
122+
m_container->hide();
123+
});
124+
}
125+
static QWidget *netlistWidget = nullptr;
126+
if (!netlistWidget) {
127+
netlistWidget = m_pModule->content();
128+
}
129+
netlistWidget->setParent(m_container);
130+
netlistWidget->adjustSize();
131+
m_container->setContent(netlistWidget);
132+
m_container->resizeWithContent();
133+
m_container->setArrowX(m_container->width() / 2);
134+
135+
if (m_container->isVisible()) {
136+
m_container->hide();
137+
if (m_tipContainer) {
138+
m_tipContainer->toggle();
139+
}
140+
} else {
141+
if (m_tipContainer) {
142+
m_tipContainer->hide();
143+
}
144+
QWidget *content = m_container->getContent();
145+
content->adjustSize();
146+
m_container->resizeWithContent();
147+
m_container->show(QPoint(rect().width() / 2, m_iconButton->parentWidget()->y()));
148+
}
149+
}

dss_example/dsstestwidget.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,46 @@
11
// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd.
22
//
3-
// SPDX-License-Identifier: LGPL-3.0-or-later
3+
// SPDX-License-Identifier: GPL-3.0-or-later
44

55
#ifndef DSSTESTWIDGET_H
66
#define DSSTESTWIDGET_H
77

8+
#include <DFloatingButton>
9+
810
#include <QWidget>
911

10-
namespace Dtk {
11-
namespace Widget {
12-
class DFloatingButton;
13-
}
14-
}
12+
class PopupWindow;
13+
14+
namespace dde {
15+
namespace network {
16+
class NetworkPlugin;
17+
} // namespace network
18+
} // namespace dde
1519

1620
class QPushButton;
21+
using namespace Dtk::Widget;
1722

1823
class DssTestWidget : public QWidget
1924
{
2025
Q_OBJECT
2126

2227
public:
23-
DssTestWidget(QWidget *parent = Q_NULLPTR);
24-
~DssTestWidget();
28+
explicit DssTestWidget(dde::network::NetworkPlugin *networkPlugin, QWidget *parent = Q_NULLPTR);
29+
~DssTestWidget() override;
2530

26-
private:
27-
bool eventFilter(QObject *watched, QEvent *event);
31+
protected:
32+
bool eventFilter(QObject *watched, QEvent *event) override;
33+
void resizeEvent(QResizeEvent *event) override;
34+
void mousePressEvent(QMouseEvent *event) override;
35+
36+
protected slots:
37+
void onClickButton();
2838

2939
private:
30-
Dtk::Widget::DFloatingButton *m_button;
40+
dde::network::NetworkPlugin *m_pModule;
41+
DFloatingButton *m_iconButton;
42+
PopupWindow *m_container;
43+
PopupWindow *m_tipContainer;
3144
};
3245

3346
#endif // DSSTESTWIDGET_H

0 commit comments

Comments
 (0)