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;
1718using namespace Dtk ::Widget;
1819DGUI_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
3962DssTestWidget::~DssTestWidget ()
@@ -42,24 +65,90 @@ DssTestWidget::~DssTestWidget()
4265
4366bool 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+ }
0 commit comments