Merge pull request #50 from xing9634/master

Change the SwitchButton's style
This commit is contained in:
chenlelin 2020-08-04 08:37:42 +08:00 committed by GitHub
commit 3ab21695b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 172 additions and 51 deletions

View File

@ -57,6 +57,7 @@ SOURCES += \
src/mainwindow.cpp \ src/mainwindow.cpp \
src/oneconnform.cpp \ src/oneconnform.cpp \
src/onelancform.cpp \ src/onelancform.cpp \
src/switchbutton.cpp \
src/utils.cpp \ src/utils.cpp \
wireless-security/dlgconnhidwifi.cpp \ wireless-security/dlgconnhidwifi.cpp \
wireless-security/dlgconnhidwifisecfast.cpp \ wireless-security/dlgconnhidwifisecfast.cpp \
@ -82,6 +83,7 @@ HEADERS += \
src/mainwindow.h \ src/mainwindow.h \
src/oneconnform.h \ src/oneconnform.h \
src/onelancform.h \ src/onelancform.h \
src/switchbutton.h \
src/utils.h \ src/utils.h \
wireless-security/dlgconnhidwifi.h \ wireless-security/dlgconnhidwifi.h \
wireless-security/dlgconnhidwifisecfast.h \ wireless-security/dlgconnhidwifisecfast.h \

View File

@ -98,7 +98,7 @@ MainWindow::MainWindow(QWidget *parent) :
initTimer(); //初始化定时器 initTimer(); //初始化定时器
connect(ui->btnNetList, &QPushButton::clicked, this, &MainWindow::onBtnNetListClicked); 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<QApplication*>(QCoreApplication::instance()); //auto app = static_cast<QApplication*>(QCoreApplication::instance());
//app->setStyle(new CustomStyle()); //设置自定义主题 //app->setStyle(new CustomStyle()); //设置自定义主题
@ -334,6 +334,8 @@ void MainWindow::createListAreaUI()
void MainWindow::createLeftAreaUI() 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); ui->btnNetList->setFocusPolicy(Qt::NoFocus);
QString txtEthernet(tr("Ethernet")); QString txtEthernet(tr("Ethernet"));
ui->btnNetList->setToolTip(txtEthernet); 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->lbWifiListImg->setStyleSheet("QLabel{background-image:url(:/res/x/wifi-list-bg.svg);}");
ui->btnNet->hide(); ui->btnNet->hide();
ui->lbBtnNetBG->hide();
ui->btnWifi->setFocusPolicy(Qt::NoFocus); btnWireless->setGeometry(412,20,50,24);
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;}");
ui->btnHotspot->setStyleSheet(leftBtnQss); ui->btnHotspot->setStyleSheet(leftBtnQss);
ui->btnHotspot->setFocusPolicy(Qt::NoFocus); 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); syslog(LOG_DEBUG, "current network state: wired state =%d, wifi state =%d", iface->lstate, iface->wstate);
qDebug()<<"==="; qDebug()<<"===";
ui->lbBtnNetBG->setStyleSheet(btnOnQss); //ui->lbBtnNetBG->setStyleSheet(btnOnQss);
if (iface->wstate == 0 || iface->wstate == 1) { if (iface->wstate == 0 || iface->wstate == 1) {
ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); // ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss);
ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL);
btnWireless->setSwitchStatus(true);
} else { } else {
ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); btnWireless->setSwitchStatus(false);
ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); //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(); QThread *t = new QThread();
BackThread *bt = new BackThread(); BackThread *bt = new BackThread();
bt->moveToThread(t); bt->moveToThread(t);
btnWireless->setSwitchStatus(true);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), bt, SLOT(execDisWifi())); connect(t, SIGNAL(started()), bt, SLOT(execDisWifi()));
connect(bt, SIGNAL(disWifiDone()), this, SLOT(disWifiDone())); connect(bt, SIGNAL(disWifiDone()), this, SLOT(disWifiDone()));
@ -1077,6 +1078,7 @@ void MainWindow::onBtnWifiClicked(int flag)
QThread *t = new QThread(); QThread *t = new QThread();
BackThread *bt = new BackThread(); BackThread *bt = new BackThread();
btnWireless->setSwitchStatus(true);
bt->moveToThread(t); bt->moveToThread(t);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), bt, SLOT(execEnWifi())); connect(t, SIGNAL(started()), bt, SLOT(execEnWifi()));
@ -1096,6 +1098,7 @@ void MainWindow::onBtnWifiClicked(int flag)
QThread *t = new QThread(); QThread *t = new QThread();
BackThread *bt = new BackThread(); BackThread *bt = new BackThread();
btnWireless->setSwitchStatus(true);
bt->moveToThread(t); bt->moveToThread(t);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), bt, SLOT(execEnWifi())); connect(t, SIGNAL(started()), bt, SLOT(execEnWifi()));
@ -1112,6 +1115,7 @@ void MainWindow::onBtnWifiClicked(int flag)
QThread *t = new QThread(); QThread *t = new QThread();
BackThread *bt = new BackThread(); BackThread *bt = new BackThread();
btnWireless->setSwitchStatus(true);
bt->moveToThread(t); bt->moveToThread(t);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), bt, SLOT(execDisWifi())); connect(t, SIGNAL(started()), bt, SLOT(execDisWifi()));
@ -1171,9 +1175,9 @@ void MainWindow::onBtnNetListClicked(int flag)
lbNoItemTip->hide(); lbNoItemTip->hide();
ui->lbNetwork->setText(tr("Ethernet")); ui->lbNetwork->setText(tr("Ethernet"));
ui->btnWifi->hide(); btnWireless->hide();
ui->lbBtnWifiBG->hide(); //ui->lbBtnWifiBG->hide();
ui->lbBtnWifiBall->hide(); //ui->lbBtnWifiBall->hide();
// 强行设置为打开 // 强行设置为打开
if (flag == 1) { if (flag == 1) {
@ -1230,22 +1234,23 @@ void MainWindow::on_btnWifiList_clicked()
lbNoItemTip->hide(); lbNoItemTip->hide();
ui->lbNetwork->setText(tr("Wifi")); ui->lbNetwork->setText(tr("Wifi"));
ui->btnWifi->show(); btnWireless->show();
ui->lbBtnWifiBG->show(); //ui->lbBtnWifiBG->show();
ui->lbBtnWifiBall->show(); //ui->lbBtnWifiBall->show();
if (iface->wstate == 0 || iface->wstate == 1) { if (iface->wstate == 0 || iface->wstate == 1) {
ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss);
ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL);
btnWireless->setSwitchStatus(true);
} else { } else {
ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss);
ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL);
btnWireless->setSwitchStatus(false);
} }
if (iface->wstate != 2) { if (iface->wstate != 2) {
//ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss);
//ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL);
btnWireless->setSwitchStatus(true);
lbTopWifiList->show(); lbTopWifiList->show();
btnAddNet->show(); btnAddNet->show();
@ -1254,7 +1259,7 @@ void MainWindow::on_btnWifiList_clicked()
} else { } else {
//ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss);
//ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL);
btnWireless->setSwitchStatus(false);
delete topWifiListWidget; //清空top列表 delete topWifiListWidget; //清空top列表
createTopWifiUI(); //创建顶部无线网item createTopWifiUI(); //创建顶部无线网item
lbTopWifiList->hide(); lbTopWifiList->hide();
@ -2154,8 +2159,9 @@ void MainWindow::enNetDone()
// 打开网络开关时如果Wifi开关是打开的设置其样式 // 打开网络开关时如果Wifi开关是打开的设置其样式
if (checkWlOn()) { if (checkWlOn()) {
ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); btnWireless->setSwitchStatus(true);
ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss);
//ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL);
} }
onBtnNetListClicked(1); onBtnNetListClicked(1);
@ -2173,9 +2179,9 @@ void MainWindow::disNetDone()
ui->lbWifiListBG->setStyleSheet(btnOffQss); ui->lbWifiListBG->setStyleSheet(btnOffQss);
ui->lbNetwork->setText("有线网络"); ui->lbNetwork->setText("有线网络");
ui->btnWifi->hide(); btnWireless->hide();
ui->lbBtnWifiBG->hide(); //ui->lbBtnWifiBG->hide();
ui->lbBtnWifiBall->hide(); //ui->lbBtnWifiBall->hide();
delete topLanListWidget; // 清空top列表 delete topLanListWidget; // 清空top列表
createTopLanUI(); //创建顶部有线网item createTopLanUI(); //创建顶部有线网item
@ -2199,8 +2205,7 @@ void MainWindow::disNetDone()
ui->lbBtnNetBG->setStyleSheet(btnOffQss); ui->lbBtnNetBG->setStyleSheet(btnOffQss);
ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); btnWireless->setSwitchStatus(false);
ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL);
this->lanListWidget->show(); this->lanListWidget->show();
this->wifiListWidget->hide(); this->wifiListWidget->hide();
@ -2223,8 +2228,8 @@ void MainWindow::launchLanDone()
void MainWindow::enWifiDone() void MainWindow::enWifiDone()
{ {
ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss); //ui->lbBtnWifiBG->setStyleSheet(btnBgOnQss);
ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBall->move(X_RIGHT_WIFI_BALL, Y_WIFI_BALL);
is_update_wifi_list = 0; is_update_wifi_list = 0;
this->ksnm->execGetWifiList(); this->ksnm->execGetWifiList();
@ -2247,8 +2252,9 @@ void MainWindow::disWifiDone()
void MainWindow::disWifiStateKeep() void MainWindow::disWifiStateKeep()
{ {
if (this->is_btnNetList_clicked == 1) { if (this->is_btnNetList_clicked == 1) {
ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); btnWireless->setSwitchStatus(false);
ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss);
//ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL);
} }
if (this->is_btnWifiList_clicked== 1) { if (this->is_btnWifiList_clicked== 1) {
disWifiDoneChangeUI(); disWifiDoneChangeUI();
@ -2288,8 +2294,9 @@ void MainWindow::disWifiDoneChangeUI()
} }
} }
ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss); btnWireless->setSwitchStatus(false);
ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL); //ui->lbBtnWifiBG->setStyleSheet(btnBgOffQss);
//ui->lbBtnWifiBall->move(X_LEFT_WIFI_BALL, Y_WIFI_BALL);
this->lanListWidget->hide(); this->lanListWidget->hide();
this->topLanListWidget->hide(); this->topLanListWidget->hide();

View File

@ -25,6 +25,7 @@
#include "kylin-dbus-interface.h" #include "kylin-dbus-interface.h"
#include "kylin-network-interface.h" #include "kylin-network-interface.h"
#include "utils.h" #include "utils.h"
#include "switchbutton.h"
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
@ -139,6 +140,7 @@ public:
QString mwBandWidth; QString mwBandWidth;
KylinDBus *objKyDBus = nullptr; KylinDBus *objKyDBus = nullptr;
NetworkSpeed *objNetSpeed = nullptr; NetworkSpeed *objNetSpeed = nullptr;
SwitchButton *btnWireless;
//状态设置,0为假1为真 //状态设置,0为假1为真
int is_update_wifi_list = 0; //是否是update wifi列表而不是load wifi列表 int is_update_wifi_list = 0; //是否是update wifi列表而不是load wifi列表

View File

@ -53,19 +53,6 @@
<string/> <string/>
</property> </property>
</widget> </widget>
<widget class="QPushButton" name="btnWifi">
<property name="geometry">
<rect>
<x>412</x>
<y>20</y>
<width>50</width>
<height>24</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="btnAdvConf"> <widget class="QPushButton" name="btnAdvConf">
<property name="geometry"> <property name="geometry">
<rect> <rect>
@ -302,7 +289,6 @@
<zorder>btnHotspot</zorder> <zorder>btnHotspot</zorder>
<zorder>btnFlyMode</zorder> <zorder>btnFlyMode</zorder>
<zorder>lbBtnWifiBall</zorder> <zorder>lbBtnWifiBall</zorder>
<zorder>btnWifi</zorder>
<zorder>lbNetListBG</zorder> <zorder>lbNetListBG</zorder>
<zorder>lbWifiListBG</zorder> <zorder>lbWifiListBG</zorder>
<zorder>lbWifiListImg</zorder> <zorder>lbWifiListImg</zorder>

92
src/switchbutton.cpp Normal file
View File

@ -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();
}

32
src/switchbutton.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef SWITCHBUTTON_H
#define SWITCHBUTTON_H
#include <QObject>
#include <QTimer>
#include <QWidget>
#include <QPainter>
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