Skip to content

Commit 3b7c0ed

Browse files
committed
tests: update dss-network-plugin test example
update dss-network-plugin test example Log: update dss-network-plugin test example
1 parent 0c73dd1 commit 3b7c0ed

File tree

8 files changed

+472
-51
lines changed

8 files changed

+472
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
void DssScreenManager::showWindow()
23+
{
24+
for (auto it = m_screenWidget.begin(); it != m_screenWidget.end(); it++) {
25+
showWindow(it.key(), it.value());
26+
}
27+
}
28+
29+
void DssScreenManager::initConnection()
30+
{
31+
connect(qApp, &QGuiApplication::screenAdded, this, &DssScreenManager::onScreenAdded);
32+
connect(qApp, &QGuiApplication::screenRemoved, this, &DssScreenManager::onScreenRemoved);
33+
}
34+
35+
void DssScreenManager::initScreen()
36+
{
37+
QList<QScreen *> screens = QGuiApplication::screens();
38+
for (QScreen *screen : screens) {
39+
DssTestWidget *testWidget = new DssTestWidget(m_netModule);
40+
m_screenWidget[screen] = testWidget;
41+
}
42+
}
43+
44+
void DssScreenManager::showWindow(QScreen *screen, DssTestWidget *testWidget)
45+
{
46+
QRect screenGeometry = screen->geometry();
47+
testWidget->resize(330, 800);
48+
testWidget->move(screenGeometry.x() + (screenGeometry.width() - testWidget->width()) / 2, (screenGeometry.height() - testWidget->height()) / 2);
49+
testWidget->show();
50+
}
51+
52+
void DssScreenManager::onScreenAdded(QScreen *screen)
53+
{
54+
DssTestWidget *testWidget = new DssTestWidget(m_netModule);
55+
m_screenWidget[screen] = testWidget;
56+
showWindow(screen, testWidget);
57+
}
58+
59+
void DssScreenManager::onScreenRemoved(QScreen *screen)
60+
{
61+
if (m_screenWidget.contains(screen)) {
62+
DssTestWidget *testWidget = m_screenWidget[screen];
63+
m_screenWidget.remove(screen);
64+
testWidget->deleteLater();
65+
}
66+
}

dss_example/dssscreenmanager.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
void showWindow();
27+
28+
private:
29+
void initConnection();
30+
void initScreen();
31+
void showWindow(QScreen *screen, DssTestWidget *testWidget);
32+
33+
protected:
34+
void onScreenAdded(QScreen *screen);
35+
void onScreenRemoved(QScreen *screen);
36+
37+
private:
38+
QMap<QScreen *, DssTestWidget *> m_screenWidget;
39+
dde::network::NetworkPlugin *m_netModule;
40+
};
41+
42+
#endif // DSSSCREENMANAGER_H

dss_example/dsstestwidget.cpp

Lines changed: 123 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 FloatingButton(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,90 @@ 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+
}
150+
151+
FloatingButton::FloatingButton(QWidget *parent)
152+
: DFloatingButton(parent)
153+
{
154+
}

dss_example/dsstestwidget.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
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+
class FloatingButton;
22+
using namespace Dtk::Widget;
1723

1824
class DssTestWidget : public QWidget
1925
{
2026
Q_OBJECT
2127

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

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

2940
private:
30-
Dtk::Widget::DFloatingButton *m_button;
41+
dde::network::NetworkPlugin *m_pModule;
42+
FloatingButton *m_iconButton;
43+
PopupWindow *m_container;
44+
PopupWindow *m_tipContainer;
45+
};
46+
47+
class FloatingButton : public DFloatingButton
48+
{
49+
Q_OBJECT
50+
51+
public:
52+
explicit FloatingButton(QWidget *parent = nullptr);
3153
};
3254

3355
#endif // DSSTESTWIDGET_H

0 commit comments

Comments
 (0)