This commit is contained in:
jzxc95 2022-10-31 17:51:35 +08:00 committed by chenxuechao
parent 1bf02f0d0d
commit f490fe0ff0
7 changed files with 394 additions and 61 deletions

View File

@ -20,7 +20,6 @@
#include "addnetbtn.h" #include "addnetbtn.h"
#include <QEvent> #include <QEvent>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel>
#include <QVariant> #include <QVariant>
#include <QPainter> #include <QPainter>
#include <QPainterPath> #include <QPainterPath>
@ -39,10 +38,10 @@ AddNetBtn::AddNetBtn(bool isWlan, QWidget *parent) : QPushButton(parent)
color.setAlphaF(0.5); color.setAlphaF(0.5);
pal.setColor(QPalette::Button, color); pal.setColor(QPalette::Button, color);
this->setPalette(pal); this->setPalette(pal);
QHBoxLayout *addLyt = new QHBoxLayout; QHBoxLayout *addLyt = new QHBoxLayout(this);
QLabel *iconLabel = new QLabel(); QLabel *iconLabel = new QLabel(this);
QLabel *textLabel = new QLabel(); textLabel = new QLabel(this);
if (isWlan) { if (isWlan) {
textLabel->setText(tr("Add Others")); textLabel->setText(tr("Add Others"));

View File

@ -25,6 +25,7 @@
#include <QPushButton> #include <QPushButton>
#include <QTranslator> #include <QTranslator>
#include <QApplication> #include <QApplication>
#include <QLabel>
class AddNetBtn : public QPushButton class AddNetBtn : public QPushButton
{ {
@ -33,11 +34,18 @@ public:
AddNetBtn(bool isWlan, QWidget *parent = nullptr); AddNetBtn(bool isWlan, QWidget *parent = nullptr);
~AddNetBtn(); ~AddNetBtn();
void setTextLabel(QString str) {
textLabel->setText(str);
}
protected: protected:
virtual void leaveEvent(QEvent * event); virtual void leaveEvent(QEvent * event);
virtual void enterEvent(QEvent * event); virtual void enterEvent(QEvent * event);
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
private:
QLabel* textLabel;
Q_SIGNALS: Q_SIGNALS:
void enterWidget(); void enterWidget();
void leaveWidget(); void leaveWidget();

View File

@ -34,7 +34,8 @@ ItemFrame::ItemFrame(QWidget *parent)
lanItemLayout = new QVBoxLayout(this); lanItemLayout = new QVBoxLayout(this);
lanItemLayout->setContentsMargins(LAYOUT_MARGINS); lanItemLayout->setContentsMargins(LAYOUT_MARGINS);
lanItemLayout->setSpacing(1); lanItemLayout->setSpacing(1);
addWlanWidget = new AddNetBtn(true, this); addWlanWidget = new AddNetBtn(false, this);
addWlanWidget->setTextLabel(tr("Add Vpn"));
deviceLanLayout->setSpacing(1); deviceLanLayout->setSpacing(1);
setLayout(deviceLanLayout); setLayout(deviceLanLayout);

View File

@ -23,6 +23,13 @@
#include <QProcess> #include <QProcess>
#include <QMouseEvent> #include <QMouseEvent>
#include <QDebug> #include <QDebug>
#include <QtDBus>
#include <QDBusArgument>
#define ACTIVATING 1
#define ACTIVATED 2
#define DEACTIVATING 3
#define DEACTIVATED 4
#define LABEL_RECT 17, 0, 105, 23 #define LABEL_RECT 17, 0, 105, 23
#define CONTENTS_MARGINS 0, 0, 0, 0 #define CONTENTS_MARGINS 0, 0, 0, 0
@ -45,6 +52,9 @@
#define KVpnSymbolic "ukui-vpn-symbolic" #define KVpnSymbolic "ukui-vpn-symbolic"
const QString VISIBLE = "visible";
const QByteArray GSETTINGS_SCHEMA = "org.ukui.kylin-nm.vpnicon";
Vpn::Vpn() : mFirstLoad(true) Vpn::Vpn() : mFirstLoad(true)
{ {
pluginName = tr("Vpn"); pluginName = tr("Vpn");
@ -56,6 +66,8 @@ Vpn::~Vpn()
if (!mFirstLoad) { if (!mFirstLoad) {
delete ui; delete ui;
ui = nullptr; ui = nullptr;
delete m_interface;
delete m_switchGsettings;
} }
} }
@ -75,7 +87,18 @@ QWidget *Vpn::pluginUi(){
pluginWidget->setAttribute(Qt::WA_DeleteOnClose); pluginWidget->setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(pluginWidget); ui->setupUi(pluginWidget);
qDBusRegisterMetaType<QVector<QStringList>>();
m_interface = new QDBusInterface("com.kylin.network",
"/com/kylin/vpnTool",
"com.kylin.vpnTool",
QDBusConnection::sessionBus());
if(!m_interface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
initComponent(); initComponent();
initConnect();
initNet();
} }
return pluginWidget; return pluginWidget;
} }
@ -117,7 +140,7 @@ void Vpn::initComponent(){
m_showFrame->setMinimumSize(FRAME_MIN_SIZE); m_showFrame->setMinimumSize(FRAME_MIN_SIZE);
m_showFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE); m_showFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE);
QHBoxLayout *showLayout = new QHBoxLayout(m_showFrame); QHBoxLayout *showLayout = new QHBoxLayout(m_showFrame);
m_showLabel = new QLabel(tr("Open"), m_showFrame); m_showLabel = new QLabel(tr("Show on Taskbar"), m_showFrame);
m_showLabel->setMinimumWidth(LABLE_MIN_WIDTH); m_showLabel->setMinimumWidth(LABLE_MIN_WIDTH);
m_showBtn = new KSwitchButton(m_showFrame); m_showBtn = new KSwitchButton(m_showFrame);
showLayout->setContentsMargins(ITEM_MARGINS); showLayout->setContentsMargins(ITEM_MARGINS);
@ -127,26 +150,26 @@ void Vpn::initComponent(){
m_showFrame->setLayout(showLayout); m_showFrame->setLayout(showLayout);
m_Line = myLine(); // m_Line = myLine();
m_timeFrame = new QFrame(m_topFrame); // m_timeFrame = new QFrame(m_topFrame);
m_timeFrame->setFrameShape(QFrame::Shape::NoFrame); // m_timeFrame->setFrameShape(QFrame::Shape::NoFrame);
m_timeFrame->setMinimumSize(FRAME_MIN_SIZE); // m_timeFrame->setMinimumSize(FRAME_MIN_SIZE);
m_timeFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE); // m_timeFrame->setMaximumSize(CONTECT_FRAME_MAX_SIZE);
QHBoxLayout *timeLayout = new QHBoxLayout(m_timeFrame); // QHBoxLayout *timeLayout = new QHBoxLayout(m_timeFrame);
m_timeLabel = new QLabel(tr("Open"), m_timeFrame); // m_timeLabel = new QLabel(tr("Open"), m_timeFrame);
m_timeLabel->setMinimumWidth(LABLE_MIN_WIDTH); // m_timeLabel->setMinimumWidth(LABLE_MIN_WIDTH);
m_timeBtn = new KSwitchButton(m_timeFrame); // m_timeBtn = new KSwitchButton(m_timeFrame);
timeLayout->setContentsMargins(ITEM_MARGINS); // timeLayout->setContentsMargins(ITEM_MARGINS);
timeLayout->addWidget(m_timeLabel); // timeLayout->addWidget(m_timeLabel);
timeLayout->addStretch(); // timeLayout->addStretch();
timeLayout->addWidget(m_timeBtn); // timeLayout->addWidget(m_timeBtn);
m_timeFrame->setLayout(timeLayout); // m_timeFrame->setLayout(timeLayout);
hotspotLyt->addWidget(m_showFrame); hotspotLyt->addWidget(m_showFrame);
hotspotLyt->addWidget(m_Line); // hotspotLyt->addWidget(m_Line);
hotspotLyt->addWidget(m_timeFrame); // hotspotLyt->addWidget(m_timeFrame);
hotspotLyt->setSpacing(0); hotspotLyt->setSpacing(0);
//列表 //列表
@ -158,6 +181,81 @@ void Vpn::initComponent(){
connect(m_listFrame->addWlanWidget, &AddNetBtn::clicked, this, [=]() { connect(m_listFrame->addWlanWidget, &AddNetBtn::clicked, this, [=]() {
runExternalApp(); runExternalApp();
}); });
if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
m_switchGsettings = new QGSettings(GSETTINGS_SCHEMA);
setShowSwitchStatus();
connect(m_switchGsettings, &QGSettings::changed, this, [=] (const QString &key) {
if (key == VISIBLE) {
setShowSwitchStatus();
}
});
} else {
m_showBtn->setChecked(false);
m_showBtn->setCheckable(false);
qDebug()<<"[Vpn] org.ukui.kylin-nm.visible is not installed!";
}
connect(m_showBtn, &KSwitchButton::stateChanged, this, [=](bool state){
if (m_switchGsettings != nullptr) {
m_switchGsettings->set(VISIBLE, state);
}
});
// connect(m_timeBtn, &KSwitchButton::stateChanged, this, [=](bool state){
// if (m_switchGsettings != nullptr) {
// m_switchGsettings->set(VISIBLE, state);
// }
// });
ui->pushButton->hide();
}
void Vpn::initConnect()
{
connect(m_interface, SIGNAL(vpnAdd(QStringList)), this, SLOT(onVpnAdd(QStringList)));
connect(m_interface, SIGNAL(vpnRemove(QString)), this, SLOT(onVpnRemove(QString)));
connect(m_interface, SIGNAL(vpnUpdate(QStringList)), this, SLOT(onVpnUpdate(QStringList)));
connect(m_interface, SIGNAL(vpnActiveConnectionStateChanged(QString, int)),
this, SLOT(onVpnActiveConnectionStateChanged(QString, int)));
}
//初始化列表
void Vpn::initNet()
{
qDebug() << "[Vpn]initNet";
if (!m_interface->isValid()) {
return;
}
QDBusMessage result = m_interface->call(QStringLiteral("getVirtualList"));
if(result.type() == QDBusMessage::ErrorMessage)
{
qWarning() << "getVirtualList error:" << result.errorMessage();
return;
}
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QVector<QStringList> variantList;
dbusArg >> variantList;
if (variantList.size() == 0) {
qDebug() << "[Vpn]initNet list empty";
return;
}
for (int i = 0; i < variantList.size(); ++i) {
QStringList vpnInfo = variantList.at(i);
addOneVirtualItem(vpnInfo);
}
return;
}
void Vpn::setShowSwitchStatus()
{
if (QGSettings::isSchemaInstalled(GSETTINGS_SCHEMA)) {
bool status = m_switchGsettings->get(VISIBLE).toBool();
m_showBtn->setChecked(status);
} else {
qDebug()<<"[Vpn] org.ukui.kylin-nm.switch is not installed!";
}
} }
void Vpn::runExternalApp(){ void Vpn::runExternalApp(){
@ -178,7 +276,21 @@ QFrame* Vpn::myLine()
return line; return line;
} }
//刪除
void Vpn::deleteVpn(QString uuid)
{
m_interface->call(QStringLiteral("deleteVpn"), uuid);
}
//激活
void Vpn::activeConnect(QString uuid) {
m_interface->call(QStringLiteral("activateVpn"), uuid);
}
//断开
void Vpn::deActiveConnect(QString uuid) {
m_interface->call(QStringLiteral("deactivateVpn"), uuid);
}
//增加一项 //增加一项
@ -189,22 +301,25 @@ void Vpn::addOneVirtualItem(QStringList infoList)
return; return;
} }
qDebug() << "[Vpn]addOneVitualItem" << infoList.at(0); qDebug() << "[Vpn]addOneVitualItem" << infoList.at(0) << infoList.at(3) ;
QString connName = infoList.at(0); QString connName = infoList.at(0);
QString connUuid = infoList.at(1); QString connUuid = infoList.at(1);
QString connDbusPath = infoList.at(2); QString connDbusPath = infoList.at(2);
VpnItem * lanItem = new VpnItem(pluginWidget); int status = infoList.at(3).toInt(); //1-连接中 2-已连接 3-断开中 4-已断开
VpnItem * item = new VpnItem(pluginWidget);
QString iconPath; item->statusLabel->setText(tr("not connected"));
iconPath = KVpnSymbolic;
lanItem->statusLabel->setText("");
QIcon searchIcon = QIcon::fromTheme(iconPath); QIcon searchIcon = QIcon::fromTheme(KVpnSymbolic);
lanItem->iconLabel->setPixmap(searchIcon.pixmap(searchIcon.actualSize(QSize(ICON_SIZE)))); item->iconLabel->setPixmap(searchIcon.pixmap(searchIcon.actualSize(QSize(ICON_SIZE))));
lanItem->titileLabel->setText(connName); item->titileLabel->setText(connName);
lanItem->uuid = connUuid; item->uuid = connUuid;
lanItem->dbusPath = connDbusPath; item->dbusPath = connDbusPath;
if (status == 1 || status == 3) {
item->startLoading();
}
// connect(lanItem->infoLabel, &GrayInfoButton::clicked, this, [=]{ // connect(lanItem->infoLabel, &GrayInfoButton::clicked, this, [=]{
// // open landetail page // // open landetail page
@ -216,33 +331,152 @@ void Vpn::addOneVirtualItem(QStringList infoList)
// qDebug() << "[NetConnect]call showPropertyWidget respond" << __LINE__; // qDebug() << "[NetConnect]call showPropertyWidget respond" << __LINE__;
// }); // });
lanItem->isAcitve = false; item->isAcitve = (status == 2);
item->setConnectActionText(item->isAcitve);
connect(lanItem, &QPushButton::clicked, this, [=] { connect(item, &QPushButton::clicked, this, [=] {
if (lanItem->isAcitve || lanItem->loading) { if (item->isAcitve || item->loading) {
deActiveConnect(lanItem->uuid, deviceName, WIRED_TYPE); deActiveConnect(item->uuid);
} else { } else {
activeConnect(lanItem->uuid, deviceName, WIRED_TYPE); activeConnect(item->uuid);
} }
}); });
connect(item, &VpnItem::connectActionTriggered, this, [=] {
activeConnect(item->uuid);
});
connect(item, &VpnItem::disconnectActionTriggered, this, [=] {
deActiveConnect(item->uuid);
});
connect(item, &VpnItem::deleteActionTriggered, this, [=] {
deleteVpn(item->uuid);
});
//记录到deviceFrame的itemMap中 //记录到deviceFrame的itemMap中
m_listFrame->itemMap.insert(connUuid, lanItem); m_listFrame->itemMap.insert(connUuid, item);
int index = getInsertPos(connName); int index = getInsertPos(connName);
qDebug()<<"[NetConnect]addOneVirtualItem " << connName << " to " << deviceName << " list at pos:" << index; qDebug()<<"[Vpn]addOneVirtualItem " << connName << " at pos:" << index;
m_listFrame->lanItemLayout->insertWidget(index, lanItem); m_listFrame->lanItemLayout->insertWidget(index, item);
} }
void Vpn::removeOneVirtualItem(QString uuid) void Vpn::removeOneVirtualItem(QString dbusPath)
{ {
if (!m_listFrame->itemMap.contains(uuid)) { qDebug()<<"[Vpn]vpn remove dbus path:" << dbusPath;
qDebug() << "[Vpn]not exist a virtual " << uuid;
return;
}
qDebug()<<"[Vpn]removeOneVirtualItem " << uuid; QMap<QString, VpnItem *>::iterator itemIter;
for (itemIter = m_listFrame->itemMap.begin(); itemIter != m_listFrame->itemMap.end(); itemIter++) {
m_listFrame->lanItemLayout->removeWidget(m_listFrame->itemMap[uuid]); if (itemIter.value()->dbusPath == dbusPath) {
delete m_listFrame->itemMap[uuid]; qDebug()<<"[Vpn]vpn remove " << dbusPath << " find in " << itemIter.value()->titileLabel->text();
m_listFrame->itemMap.remove(uuid); QString key = itemIter.key();
m_listFrame->lanItemLayout->removeWidget(itemIter.value());
delete itemIter.value();
m_listFrame->itemMap.remove(key);
break;
}
}
}
//增加
void Vpn::onVpnAdd(QStringList infoList)
{
addOneVirtualItem(infoList);
}
//移出
void Vpn::onVpnRemove(QString uuid)
{
removeOneVirtualItem(uuid);
}
//名称变化
void Vpn::onVpnUpdate(QStringList info)
{
if (m_listFrame->itemMap.contains(info.at(1))) {
qDebug() << "[Vpn]" << m_listFrame->itemMap[info.at(1)]->titileLabel->text() << "change to" << info.at(0);
if (m_listFrame->itemMap[info.at(1)]->titileLabel->text() != info.at(0)) {
m_listFrame->itemMap[info.at(1)]->titileLabel->setText(info.at(0));
}
}
}
void Vpn::onVpnActiveConnectionStateChanged(QString uuid, int status)
{
if (uuid.isEmpty()) {
qDebug() << "[Vpn]onActiveConnectionChanged but uuid is empty";
return;
}
qDebug() << "[Vpn]onActiveConnectionChanged " << uuid << status;
VpnItem * item= nullptr;
if (m_listFrame->itemMap.contains(uuid)) {
item = m_listFrame->itemMap[uuid];
if (status == ACTIVATED) {
//为已连接则放到第一个
m_listFrame->lanItemLayout->removeWidget(item);
m_listFrame->lanItemLayout->insertWidget(0,item);
} else if (status == DEACTIVATED) {
//为断开则重新插入
int index = getInsertPos(item->titileLabel->text());
qDebug() << "[Vpn]reinsert" << item->titileLabel->text() << "pos" << index << "because status changes to deactive";
m_listFrame->lanItemLayout->removeWidget(item);
m_listFrame->lanItemLayout->insertWidget(index,item);
}
itemActiveConnectionStatusChanged(item, status);
}
}
void Vpn::itemActiveConnectionStatusChanged(VpnItem *item, int status)
{
// QString iconPath = NoNetSymbolic;
if (status == ACTIVATING) {
item->startLoading();
} else if (status == ACTIVATED) {
item->stopLoading();
// iconPath = KLanSymbolic;
item->statusLabel->clear();
item->statusLabel->setMinimumSize(36,36);
item->statusLabel->setMaximumSize(16777215,16777215);
item->statusLabel->setText(tr("connected"));
item->isAcitve = true;
} else if (status == DEACTIVATING) {
item->startLoading();
} else {
item->stopLoading();
item->statusLabel->setMinimumSize(36,36);
item->statusLabel->setMaximumSize(16777215,16777215);
item->statusLabel->clear();
item->isAcitve = false;
item->statusLabel->setText(tr("not connected"));
}
item->setConnectActionText(item->isAcitve);
}
int Vpn::getInsertPos(QString connName)
{
qDebug() << "[Vpn]getInsertPos" << connName;
int index = 0;
if(!m_interface->isValid()) {
index = 0;
} else {
QDBusMessage result = m_interface->call(QStringLiteral("getVirtualList"));
if(result.type() == QDBusMessage::ErrorMessage)
{
qWarning() << "getVirtualList error:" << result.errorMessage();
return 0;
}
auto dbusArg = result.arguments().at(0).value<QDBusArgument>();
QVector<QStringList> variantList;
dbusArg >> variantList;
for (int i = 0; i < variantList.size(); ++i ) {
if (variantList.at(i).at(0) == connName) {
qDebug() << "pos in kylin-nm is " << i;
index = i;
break;
}
}
if (variantList.at(0).size() == 1) {
index--;
}
}
return index;
} }

View File

@ -59,6 +59,7 @@ public:
public: public:
void initComponent(); void initComponent();
void initConnect();
void runExternalApp(); void runExternalApp();
@ -72,6 +73,8 @@ private:
int pluginType; int pluginType;
QWidget * pluginWidget; QWidget * pluginWidget;
QDBusInterface *m_interface = nullptr;
QFrame *m_topFrame; QFrame *m_topFrame;
QFrame *m_showFrame; QFrame *m_showFrame;
QLabel *m_showLabel; QLabel *m_showLabel;
@ -83,19 +86,18 @@ private:
ItemFrame *m_listFrame; ItemFrame *m_listFrame;
bool mFirstLoad; bool mFirstLoad;
QGSettings *m_switchGsettings;
QFrame* myLine(); QFrame* myLine();
int getInsertPos(QString connName); int getInsertPos(QString connName);
void deleteOneLan(QString uuid); void deleteVpn(QString uuid);
void activeConnect(QString uuid); void activeConnect(QString uuid);
void deActiveConnect(QString uuid); void deActiveConnect(QString uuid);
//获取设备列表 //获取设备列表
void initNet(); void initNet();
//处理列表增加
void addLanItem(QStringList infoList, bool isActived);
//增加一项 //增加一项
void addOneVirtualItem(QStringList infoList); void addOneVirtualItem(QStringList infoList);
//减少一项 //减少一项
@ -103,6 +105,15 @@ private:
//单个lan连接状态变化 //单个lan连接状态变化
void itemActiveConnectionStatusChanged(VpnItem *item, int status); void itemActiveConnectionStatusChanged(VpnItem *item, int status);
void setShowSwitchStatus();
private slots:
void onVpnAdd(QStringList);
void onVpnRemove(QString);
void onVpnUpdate(QStringList);
void onVpnActiveConnectionStateChanged(QString, int);
}; };
#endif // VPN_H #endif // VPN_H

View File

@ -27,8 +27,8 @@
#define THEME_QT_SCHEMA "org.ukui.style" #define THEME_QT_SCHEMA "org.ukui.style"
#define MODE_QT_KEY "style-name" #define MODE_QT_KEY "style-name"
VpnItem::VpnItem(bool bAcitve, bool isLock, QWidget *parent) VpnItem::VpnItem(bool bAcitve, QWidget *parent)
: isAcitve(bAcitve), isLock(isLock), QPushButton(parent) : isAcitve(bAcitve), QPushButton(parent)
{ {
this->setMinimumSize(550, 58); this->setMinimumSize(550, 58);
this->setProperty("useButtonPalette", true); this->setProperty("useButtonPalette", true);
@ -42,16 +42,32 @@ VpnItem::VpnItem(bool bAcitve, bool isLock, QWidget *parent)
mLanLyt->setContentsMargins(16,0,16,0); mLanLyt->setContentsMargins(16,0,16,0);
mLanLyt->setSpacing(16); mLanLyt->setSpacing(16);
iconLabel = new QLabel(this); iconLabel = new QLabel(this);
iconLabel->setProperty("useIconHighlightEffect", 0x2);
titileLabel = new FixLabel(this); titileLabel = new FixLabel(this);
statusLabel = new QLabel(this); statusLabel = new QLabel(this);
statusLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter); statusLabel->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
// statusLabel->setMinimumSize(36,36);
// infoLabel = new GrayInfoButton(this); // 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(tr("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(iconLabel);
mLanLyt->addWidget(titileLabel,Qt::AlignLeft); mLanLyt->addWidget(titileLabel,Qt::AlignLeft);
mLanLyt->addStretch(); mLanLyt->addStretch();
mLanLyt->addWidget(statusLabel); mLanLyt->addWidget(statusLabel);
// mLanLyt->addWidget(infoLabel); // mLanLyt->addWidget(infoLabel);
mLanLyt->addWidget(m_moreButton);
loadIcons.append(QIcon::fromTheme("ukui-loading-1-symbolic")); loadIcons.append(QIcon::fromTheme("ukui-loading-1-symbolic"));
loadIcons.append(QIcon::fromTheme("ukui-loading-2-symbolic")); loadIcons.append(QIcon::fromTheme("ukui-loading-2-symbolic"));
@ -64,6 +80,9 @@ VpnItem::VpnItem(bool bAcitve, bool isLock, QWidget *parent)
waitTimer = new QTimer(this); waitTimer = new QTimer(this);
connect(waitTimer, &QTimer::timeout, this, &VpnItem::updateIcon); connect(waitTimer, &QTimer::timeout, this, &VpnItem::updateIcon);
connect(m_connectAction, &QAction::triggered, this, &VpnItem::onConnectTriggered);
connect(m_deleteAction, &QAction::triggered, this, &VpnItem::onDeletetTriggered);
m_moreMenu->installEventFilter(this);
} }
void VpnItem::updateIcon() void VpnItem::updateIcon()
@ -86,6 +105,35 @@ void VpnItem::stopLoading(){
loading = false; loading = false;
} }
void VpnItem::setConnectActionText(bool isAcitve)
{
if (isAcitve) {
m_connectAction->setText(tr("Disconnect"));
} else {
m_connectAction->setText(tr("Connect"));
}
}
void VpnItem::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 VpnItem::onDeletetTriggered()
{
if (!m_deleteAction) {
return;
}
Q_EMIT deleteActionTriggered();
}
void VpnItem::paintEvent(QPaintEvent *event) void VpnItem::paintEvent(QPaintEvent *event)
{ {
QPalette pal = this->palette(); QPalette pal = this->palette();
@ -122,3 +170,18 @@ void VpnItem::paintEvent(QPaintEvent *event)
painter.drawRect(rect); painter.drawRect(rect);
QPushButton::paintEvent(event); QPushButton::paintEvent(event);
} }
bool VpnItem::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;
}

View File

@ -28,33 +28,43 @@
#include <QDebug> #include <QDebug>
#include <QGSettings> #include <QGSettings>
#include <QImage> #include <QImage>
#include <QPainter>
#include <QToolButton>
#include <QMenu>
#include <QEvent>
#include "fixlabel.h" #include "fixlabel.h"
//#include "infobutton.h"
#include "../component/AddBtn/grayinfobutton.h" #include "../component/AddBtn/grayinfobutton.h"
class VpnItem : public QPushButton class VpnItem : public QPushButton
{ {
Q_OBJECT
public: public:
VpnItem(bool bAcitve, bool isLock, QWidget *parent = nullptr); VpnItem(bool bAcitve, QWidget *parent = nullptr);
public: public:
QLabel * iconLabel = nullptr; QLabel * iconLabel = nullptr;
// GrayInfoButton * infoLabel = nullptr; // GrayInfoButton * infoLabel = nullptr;
FixLabel * titileLabel = nullptr; FixLabel * titileLabel = nullptr;
QLabel * statusLabel = nullptr; QLabel * statusLabel = nullptr;
QToolButton* m_moreButton = nullptr;
QMenu* m_moreMenu = nullptr;
QAction* m_connectAction = nullptr;
QAction* m_deleteAction = nullptr;
QString uuid = ""; QString uuid = "";
QString dbusPath = "";
void setHalfFillet(bool flag) {useHalfFillet = flag; repaint();} void setHalfFillet(bool flag) {useHalfFillet = flag; repaint();}
public: public:
void startLoading(); void startLoading();
void stopLoading(); void stopLoading();
void setConnectActionText(bool isAcitve);
bool isAcitve = false; bool isAcitve = false;
bool loading = false; bool loading = false;
bool isLock = false;
protected: protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
bool eventFilter(QObject *watched, QEvent *event);
private: private:
QTimer *waitTimer = nullptr; QTimer *waitTimer = nullptr;
@ -65,6 +75,13 @@ private:
private slots: private slots:
void updateIcon(); void updateIcon();
void onConnectTriggered();
void onDeletetTriggered();
Q_SIGNALS:
void connectActionTriggered();
void disconnectActionTriggered();
void deleteActionTriggered();
}; };