From 45ccec54d62753bde0ca7515b5d3d7c77ff6f0de Mon Sep 17 00:00:00 2001 From: David Peng Date: Mon, 3 Aug 2020 10:51:39 +0800 Subject: [PATCH] Change switchbutton's style --- kylin-nm.pro | 8 +++- src/mainwindow.cpp | 81 ++++++++++++++++++++------------------ src/mainwindow.h | 2 + src/mainwindow.ui | 14 ------- src/switchbutton.cpp | 92 ++++++++++++++++++++++++++++++++++++++++++++ src/switchbutton.h | 32 +++++++++++++++ 6 files changed, 176 insertions(+), 53 deletions(-) create mode 100644 src/switchbutton.cpp create mode 100644 src/switchbutton.h diff --git a/kylin-nm.pro b/kylin-nm.pro index f97d0a5d..0108dd1d 100644 --- a/kylin-nm.pro +++ b/kylin-nm.pro @@ -4,7 +4,8 @@ # #------------------------------------------------- -QT += core gui x11extras dbus KWindowSystem +QT += core gui x11extras dbus KWindowSystem \ + widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -12,7 +13,8 @@ TARGET = kylin-nm TEMPLATE = app LANGUAGE = C++ -CONFIG += c++14 +CONFIG += c++14 \ + qt CONFIG += qt warn_on CONFIG += release @@ -57,6 +59,7 @@ SOURCES += \ src/mainwindow.cpp \ src/oneconnform.cpp \ src/onelancform.cpp \ + src/switchbutton.cpp \ src/utils.cpp \ wireless-security/dlgconnhidwifi.cpp \ wireless-security/dlgconnhidwifisecfast.cpp \ @@ -82,6 +85,7 @@ HEADERS += \ src/mainwindow.h \ src/oneconnform.h \ src/onelancform.h \ + src/switchbutton.h \ src/utils.h \ wireless-security/dlgconnhidwifi.h \ wireless-security/dlgconnhidwifisecfast.h \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 62b02138..64b0b842 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -98,7 +98,7 @@ MainWindow::MainWindow(QWidget *parent) : initTimer(); //初始化定时器 connect(ui->btnNetList, &QPushButton::clicked, this, &MainWindow::onBtnNetListClicked); - connect(ui->btnWifi, &QPushButton::clicked, this, &MainWindow::onBtnWifiClicked); + connect(btnWireless, &SwitchButton::clicked,this, &MainWindow::onBtnWifiClicked); //auto app = static_cast(QCoreApplication::instance()); //app->setStyle(new CustomStyle()); //设置自定义主题 @@ -334,6 +334,8 @@ void MainWindow::createListAreaUI() void MainWindow::createLeftAreaUI() { + btnWireless = new SwitchButton(this); + btnWireless->setStyleSheet("SwitchButton{border:none;background-color:rgba(255,255,255,0.12);}"); ui->btnNetList->setFocusPolicy(Qt::NoFocus); QString txtEthernet(tr("Ethernet")); ui->btnNetList->setToolTip(txtEthernet); @@ -347,12 +349,8 @@ void MainWindow::createLeftAreaUI() ui->lbWifiListImg->setStyleSheet("QLabel{background-image:url(:/res/x/wifi-list-bg.svg);}"); ui->btnNet->hide(); - ui->lbBtnNetBG->hide(); - ui->btnWifi->setFocusPolicy(Qt::NoFocus); - ui->btnWifi->setStyleSheet("QPushButton{border:none;background:transparent;}"); - ui->lbBtnWifiBall->setStyleSheet("QLabel{min-width: 16px; min-height: 16px;max-width:16px; max-height: 16px;" - "border-radius: 8px;background:white;}"); + btnWireless->setGeometry(412,20,50,24); ui->btnHotspot->setStyleSheet(leftBtnQss); ui->btnHotspot->setFocusPolicy(Qt::NoFocus); @@ -431,13 +429,15 @@ void MainWindow::initNetwork() syslog(LOG_DEBUG, "current network state: wired state =%d, wifi state =%d", iface->lstate, iface->wstate); qDebug()<<"==="; - ui->lbBtnNetBG->setStyleSheet(btnOnQss); + //ui->lbBtnNetBG->setStyleSheet(btnOnQss); if (iface->wstate == 0 || iface->wstate == 1) { - ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); - ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); + // ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); + //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(true); } else { - ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); - ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(false); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); + //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); } // 初始化网络列表 @@ -1059,6 +1059,7 @@ void MainWindow::onBtnWifiClicked(int flag) QThread *t = new QThread(); BackThread *bt = new BackThread(); bt->moveToThread(t); + btnWireless->setSwitchStatus(true); connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(started()), bt, SLOT(execDisWifi())); connect(bt, SIGNAL(disWifiDone()), this, SLOT(disWifiDone())); @@ -1077,6 +1078,7 @@ void MainWindow::onBtnWifiClicked(int flag) QThread *t = new QThread(); BackThread *bt = new BackThread(); + btnWireless->setSwitchStatus(true); bt->moveToThread(t); connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(started()), bt, SLOT(execEnWifi())); @@ -1096,6 +1098,7 @@ void MainWindow::onBtnWifiClicked(int flag) QThread *t = new QThread(); BackThread *bt = new BackThread(); + btnWireless->setSwitchStatus(true); bt->moveToThread(t); connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(started()), bt, SLOT(execEnWifi())); @@ -1112,6 +1115,7 @@ void MainWindow::onBtnWifiClicked(int flag) QThread *t = new QThread(); BackThread *bt = new BackThread(); + btnWireless->setSwitchStatus(true); bt->moveToThread(t); connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(started()), bt, SLOT(execDisWifi())); @@ -1171,9 +1175,9 @@ void MainWindow::onBtnNetListClicked(int flag) lbNoItemTip->hide(); ui->lbNetwork->setText(tr("Ethernet")); - ui->btnWifi->hide(); - ui->lbBtnWifiBG->hide(); - ui->lbBtnWifiBall->hide(); + btnWireless->hide(); + //ui->lbBtnWifiBG->hide(); + //ui->lbBtnWifiBall->hide(); // 强行设置为打开 if (flag == 1) { @@ -1230,22 +1234,23 @@ void MainWindow::on_btnWifiList_clicked() lbNoItemTip->hide(); ui->lbNetwork->setText(tr("Wifi")); - ui->btnWifi->show(); - ui->lbBtnWifiBG->show(); - ui->lbBtnWifiBall->show(); - + btnWireless->show(); + //ui->lbBtnWifiBG->show(); + //ui->lbBtnWifiBall->show(); if (iface->wstate == 0 || iface->wstate == 1) { - ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); - ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); + //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(true); } else { - ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); - ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); + //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(false); } if (iface->wstate != 2) { //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); - + btnWireless->setSwitchStatus(true); lbTopWifiList->show(); btnAddNet->show(); @@ -1254,7 +1259,7 @@ void MainWindow::on_btnWifiList_clicked() } else { //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); - + btnWireless->setSwitchStatus(false); delete topWifiListWidget; //清空top列表 createTopWifiUI(); //创建顶部无线网item lbTopWifiList->hide(); @@ -2154,8 +2159,9 @@ void MainWindow::enNetDone() // 打开网络开关时如果Wifi开关是打开的,设置其样式 if (checkWlOn()) { - ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); - ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(true); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); + //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); } onBtnNetListClicked(1); @@ -2173,9 +2179,9 @@ void MainWindow::disNetDone() ui->lbWifiListBG->setStyleSheet(btnOffQss); ui->lbNetwork->setText("有线网络"); - ui->btnWifi->hide(); - ui->lbBtnWifiBG->hide(); - ui->lbBtnWifiBall->hide(); + btnWireless->hide(); + //ui->lbBtnWifiBG->hide(); + //ui->lbBtnWifiBall->hide(); delete topLanListWidget; // 清空top列表 createTopLanUI(); //创建顶部有线网item @@ -2199,8 +2205,7 @@ void MainWindow::disNetDone() ui->lbBtnNetBG->setStyleSheet(btnOffQss); - ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); - ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(false); this->lanListWidget->show(); this->wifiListWidget->hide(); @@ -2223,8 +2228,8 @@ void MainWindow::launchLanDone() void MainWindow::enWifiDone() { - ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); - ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); + //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); is_update_wifi_list = 0; this->ksnm->execGetWifiList(); @@ -2247,8 +2252,9 @@ void MainWindow::disWifiDone() void MainWindow::disWifiStateKeep() { if (this->is_btnNetList_clicked == 1) { - ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); - ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(false); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); + //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); } if (this->is_btnWifiList_clicked== 1) { disWifiDoneChangeUI(); @@ -2288,8 +2294,9 @@ void MainWindow::disWifiDoneChangeUI() } } - ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); - ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); + btnWireless->setSwitchStatus(false); + //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); + //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); this->lanListWidget->hide(); this->topLanListWidget->hide(); diff --git a/src/mainwindow.h b/src/mainwindow.h index bdd647ab..548151f5 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -25,6 +25,7 @@ #include "kylin-dbus-interface.h" #include "kylin-network-interface.h" #include "utils.h" +#include "switchbutton.h" #include #include @@ -139,6 +140,7 @@ public: QString mwBandWidth; KylinDBus *objKyDBus = nullptr; NetworkSpeed *objNetSpeed = nullptr; + SwitchButton *btnWireless; //状态设置,0为假,1为真 int is_update_wifi_list = 0; //是否是update wifi列表,而不是load wifi列表 diff --git a/src/mainwindow.ui b/src/mainwindow.ui index c9492a52..f3803f32 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -53,19 +53,6 @@ - - - - 412 - 20 - 50 - 24 - - - - - - @@ -302,7 +289,6 @@ btnHotspot btnFlyMode lbBtnWifiBall - btnWifi lbNetListBG lbWifiListBG lbWifiListImg diff --git a/src/switchbutton.cpp b/src/switchbutton.cpp new file mode 100644 index 00000000..e3b2dbf7 --- /dev/null +++ b/src/switchbutton.cpp @@ -0,0 +1,92 @@ +#include "switchbutton.h" + +SwitchButton::SwitchButton(QWidget *parent) : QWidget(parent) +{ + + setFixedSize(50,24); + m_fWidth = (float)width(); + m_fHeight = (float)height(); + m_cTimer = new QTimer(this); + m_cTimer->setInterval(5); + + if(m_bIsOn == 1) { + m_fCurrentValue = m_fWidth - 16 - 4; + } + else { + m_fCurrentValue = 4; + } + connect(m_cTimer,SIGNAL(timeout()),this,SLOT(startAnimation())); + + +} + +void SwitchButton::setSwitchStatus(bool check) { + if(check == true) { + m_bIsOn = 1; + } else { + m_bIsOn = 0; + } + + m_cTimer->start(); //开始播放动画 +} + + +/* 播放按钮开启关闭动画 */ +void SwitchButton::startAnimation() { //滑动按钮动作播放 + int pos = 4; + int size = m_fWidth - 16; + if(m_bIsOn) { + m_fCurrentValue ++; //往右滑动 + if(m_fCurrentValue >= size - pos) { //到达边界停下来 + m_fCurrentValue = size - pos; + m_cTimer->stop(); + } + + } else { + m_fCurrentValue --; + if(m_fCurrentValue <= pos) { //到达最小值,停止继续前进 + m_fCurrentValue = pos; + m_cTimer->stop(); + } + } + update(); +} + +/* 按钮按下处理 */ +void SwitchButton::mousePressEvent(QMouseEvent *event) { + Q_UNUSED(event); + m_bIsOn = !m_bIsOn; + + Q_EMIT clicked(m_bIsOn); + + return QWidget::mousePressEvent(event); +} + +/* 绘制滑动按钮主体 */ +void SwitchButton::paintEvent(QPaintEvent *event) { + Q_UNUSED(event); + QPainter painter(this); + painter.setRenderHint(QPainter::SmoothPixmapTransform); + painter.setRenderHint(QPainter::Antialiasing); //抗锯齿效果 + painter.setPen(Qt::NoPen); + QColor colorActive(61,107,229); + QColor colorInactive(255,255,255); + colorInactive.setAlphaF(0.12); + if(m_bIsOn) { + painter.save(); + painter.setBrush(colorActive); + QRectF active_rect = QRectF(0,0,m_fWidth,m_fHeight); + painter.drawRoundedRect(active_rect, 0.5 * m_fHeight, 0.5 * m_fHeight); //画开启状态 + } else { + painter.save(); + painter.setBrush(colorInactive); + QRectF inactive_rect = QRectF(0 ,0,m_fWidth,m_fHeight); + painter.drawRoundedRect(inactive_rect, 0.5 * m_fHeight, 0.5 * m_fHeight); //画关闭状态 + } + painter.restore(); + painter.save(); + painter.setBrush(Qt::white); + painter.drawEllipse(m_fCurrentValue,4, 16, 16); + painter.restore(); +} + diff --git a/src/switchbutton.h b/src/switchbutton.h new file mode 100644 index 00000000..7c0f1e5d --- /dev/null +++ b/src/switchbutton.h @@ -0,0 +1,32 @@ +#ifndef SWITCHBUTTON_H +#define SWITCHBUTTON_H + +#include +#include +#include +#include + +class SwitchButton : public QWidget +{ + Q_OBJECT +public: + explicit SwitchButton(QWidget *parent = nullptr); + void setSwitchStatus(bool check); + +private: + int m_bIsOn = 1; + QTimer *m_cTimer; + float m_fWidth; + float m_fHeight; + float m_fCurrentValue; + void paintEvent(QPaintEvent *event); + void mousePressEvent(QMouseEvent *event); + +Q_SIGNALS: + void clicked(int check); +private Q_SLOTS: + void startAnimation(); + +}; + +#endif // SWITCHBUTTON_H