From c0eb659b8250877a5a9fb77e0bb1497ead6317b9 Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Thu, 8 Sep 2022 17:12:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E5=88=B6=E9=9D=A2=E6=9D=BFui=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/netconnect/lanitem.cpp | 70 +++++++++++++++++++++++++++++++ plugins/netconnect/lanitem.h | 21 +++++++++- plugins/netconnect/netconnect.cpp | 26 ++++++++++-- plugins/netconnect/netconnect.h | 3 ++ 4 files changed, 115 insertions(+), 5 deletions(-) diff --git a/plugins/netconnect/lanitem.cpp b/plugins/netconnect/lanitem.cpp index a8903cf3..49047838 100644 --- a/plugins/netconnect/lanitem.cpp +++ b/plugins/netconnect/lanitem.cpp @@ -47,11 +47,28 @@ LanItem::LanItem(bool isAcitve, QWidget *parent) statusLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); // statusLabel->setMinimumSize(36,36); infoLabel = new GrayInfoButton(this); + + //【更多】菜单 + m_moreButton = new QToolButton(this); + m_moreButton->setProperty("useButtonPalette", true); + m_moreButton->setPopupMode(QToolButton::InstantPopup); + m_moreButton->setAutoRaise(true); + m_moreButton->setIcon(QIcon::fromTheme("view-more-horizontal-symbolic")); + m_moreMenu = new QMenu(m_moreButton); + m_connectAction = new QAction(m_moreMenu); + m_deleteAction = new QAction("Delete", m_moreMenu); + setConnectActionText(isAcitve); + + m_moreMenu->addAction(m_connectAction); + m_moreMenu->addAction(m_deleteAction); + m_moreButton->setMenu(m_moreMenu); + mLanLyt->addWidget(iconLabel); mLanLyt->addWidget(titileLabel,Qt::AlignLeft); mLanLyt->addStretch(); mLanLyt->addWidget(statusLabel); mLanLyt->addWidget(infoLabel); + mLanLyt->addWidget(m_moreButton); loadIcons.append(QIcon::fromTheme("ukui-loading-1-symbolic")); loadIcons.append(QIcon::fromTheme("ukui-loading-2-symbolic")); @@ -62,6 +79,10 @@ LanItem::LanItem(bool isAcitve, QWidget *parent) loadIcons.append(QIcon::fromTheme("ukui-loading-7-symbolic")); waitTimer = new QTimer(this); connect(waitTimer, &QTimer::timeout, this, &LanItem::updateIcon); + + connect(m_connectAction, &QAction::triggered, this, &LanItem::onConnectTriggered); + connect(m_deleteAction, &QAction::triggered, this, &LanItem::onDeletetTriggered); + m_moreMenu->installEventFilter(this); } LanItem::~LanItem() @@ -89,6 +110,40 @@ void LanItem::stopLoading(){ loading = false; } +/** + * @brief LanItem::setConnectActionText + * 【更多】菜单状态切换 连接/断开 + * @param isAcitve + */ +void LanItem::setConnectActionText(bool isAcitve) +{ + if (isAcitve) { + m_connectAction->setText(tr("Disconnect")); + } else { + m_connectAction->setText(tr("Connect")); + } +} + +void LanItem::onConnectTriggered() +{ + if (!m_connectAction) { + return; + } + if (m_connectAction->text() == tr("Connect")) { + Q_EMIT connectActionTriggered(); + } else if (m_connectAction->text() == tr("Disconnect")) { + Q_EMIT disconnectActionTriggered(); + } +} + +void LanItem::onDeletetTriggered() +{ + if (!m_deleteAction) { + return; + } + Q_EMIT deleteActionTriggered(); +} + void LanItem::paintEvent(QPaintEvent *event) { QPalette pal = this->palette(); @@ -104,3 +159,18 @@ void LanItem::paintEvent(QPaintEvent *event) QPushButton::paintEvent(event); } +bool LanItem::eventFilter(QObject *watched, QEvent *event) +{ + //菜单右边界与按钮右边界对齐 + if (event->type() == QEvent::Show && watched == m_moreMenu) { + int menuXPos = m_moreMenu->pos().x(); + int menuWidth = m_moreMenu->size().width(); + int btnWidth = m_moreButton->size().width(); + + QPoint pos = QPoint (menuXPos - menuWidth + btnWidth, m_moreMenu->pos().y()); + m_moreMenu->move(pos); + return true; + } + return false; +} + diff --git a/plugins/netconnect/lanitem.h b/plugins/netconnect/lanitem.h index ea1b1355..b27a9df2 100644 --- a/plugins/netconnect/lanitem.h +++ b/plugins/netconnect/lanitem.h @@ -29,12 +29,16 @@ #include #include #include +#include +#include +#include #include "fixlabel.h" //#include "infobutton.h" #include "../component/AddBtn/grayinfobutton.h" class LanItem : public QPushButton { + Q_OBJECT public: LanItem(bool isAcitve, QWidget *parent = nullptr); ~LanItem(); @@ -43,10 +47,15 @@ public: GrayInfoButton * infoLabel = nullptr; FixLabel * titileLabel = nullptr; QLabel * statusLabel = nullptr; + QToolButton* m_moreButton = nullptr; + QMenu* m_moreMenu = nullptr; + QAction* m_connectAction = nullptr; + QAction* m_deleteAction = nullptr; public: void startLoading(); void stopLoading(); + void setConnectActionText(bool isAcitve); bool loading = false; bool isAcitve = false; @@ -56,15 +65,23 @@ public: protected: void paintEvent(QPaintEvent *); + bool eventFilter(QObject *watched, QEvent *event); private: QTimer *waitTimer = nullptr; QGSettings *themeGsettings = nullptr; QList loadIcons; - int currentIconIndex=0; + int currentIconIndex=0; private slots: - void updateIcon(); + void updateIcon(); + void onConnectTriggered(); + void onDeletetTriggered(); + +Q_SIGNALS: + void connectActionTriggered(); + void disconnectActionTriggered(); + void deleteActionTriggered(); }; diff --git a/plugins/netconnect/netconnect.cpp b/plugins/netconnect/netconnect.cpp index 4bc29246..3a933302 100644 --- a/plugins/netconnect/netconnect.cpp +++ b/plugins/netconnect/netconnect.cpp @@ -235,6 +235,8 @@ void NetConnect::initComponent() { Q_UNUSED(checked) runExternalApp(); }); + + connect(this, SIGNAL(lanRemove(QString)), m_interface, SIGNAL(lanRemove(QString))); } //获取网卡列表 @@ -353,6 +355,12 @@ void NetConnect::runExternalApp() { process.startDetached(cmd); } +//刪除 +void NetConnect::deleteOneLan(QString ssid) +{ + Q_EMIT lanRemove(ssid); +} + //激活 void NetConnect::activeConnect(QString ssid, QString deviceName, int type) { qDebug() << "[NetConnect]call activateConnect" << __LINE__; @@ -421,12 +429,12 @@ void NetConnect::addLanItem(ItemFrame *frame, QString devName, QStringList infoL return; } - LanItem * lanItem = new LanItem(pluginWidget); + LanItem * lanItem = new LanItem(isActived, pluginWidget); QString iconPath = KLanSymbolic; if (isActived) { lanItem->statusLabel->setText(tr("connected")); } else { - lanItem->statusLabel->setText(""); + lanItem->statusLabel->setText("not connected"); } QIcon searchIcon = QIcon::fromTheme(iconPath); // if (iconPath != KLanSymbolic && iconPath != NoNetSymbolic) { @@ -458,6 +466,16 @@ void NetConnect::addLanItem(ItemFrame *frame, QString devName, QStringList infoL } }); + connect(lanItem, &LanItem::connectActionTriggered, this, [=] { + activeConnect(lanItem->uuid, devName, WIRED_TYPE); + }); + connect(lanItem, &LanItem::disconnectActionTriggered, this, [=] { + deActiveConnect(lanItem->uuid, devName, WIRED_TYPE); + }); + connect(lanItem, &LanItem::deleteActionTriggered, this, [=] { + deleteOneLan(lanItem->dbusPath); + }); + //记录到deviceFrame的itemMap中 deviceFrameMap[devName]->itemMap.insert(infoList.at(1), lanItem); qDebug()<<"insert " << infoList.at(1) << " to " << devName << " list"; @@ -695,7 +713,7 @@ void NetConnect::addOneLanFrame(ItemFrame *frame, QString deviceName, QStringLis QString iconPath; iconPath = KLanSymbolic; - lanItem->statusLabel->setText(""); + lanItem->statusLabel->setText("not connected"); QIcon searchIcon = QIcon::fromTheme(iconPath); // if (iconPath != KLanSymbolic && iconPath != NoNetSymbolic) { @@ -841,7 +859,9 @@ void NetConnect::itemActiveConnectionStatusChanged(LanItem *item, int status) item->statusLabel->setMaximumSize(16777215,16777215); item->statusLabel->clear(); item->isAcitve = false; + item->statusLabel->setText(tr("not connected")); } + item->setConnectActionText(item->isAcitve); // QIcon searchIcon = QIcon::fromTheme(iconPath); // item->iconLabel->setPixmap(searchIcon.pixmap(searchIcon.actualSize(QSize(24, 24)))); diff --git a/plugins/netconnect/netconnect.h b/plugins/netconnect/netconnect.h index f991540f..ac754d76 100644 --- a/plugins/netconnect/netconnect.h +++ b/plugins/netconnect/netconnect.h @@ -152,6 +152,9 @@ private slots: void onDeviceStatusChanged(); void onDeviceNameChanged(QString, QString, int); + +Q_SIGNALS: + void lanRemove(QString dbusPath); }; Q_DECLARE_METATYPE(QList);