Merge branch '0908-new' into 'dbus-interface'

Feature: Scan WiFi list periodically.

See merge request kylin-desktop/kylin-nm!240
This commit is contained in:
Zhang Jiaping 2021-09-10 06:27:09 +00:00
commit ab23eb4183
7 changed files with 116 additions and 16 deletions

View File

@ -30,6 +30,7 @@ void MainWindow::showMainwindow()
this->showNormal();
this->raise();
this->activateWindow();
emit this->mainWindowVisibleChanged(true);
}
/**
@ -38,6 +39,7 @@ void MainWindow::showMainwindow()
void MainWindow::hideMainwindow()
{
this->hide();
emit this->mainWindowVisibleChanged(false);
}
/**
@ -111,6 +113,7 @@ void MainWindow::initUI()
m_centralWidget->tabBar()->setFixedWidth(this->width());
m_lanWidget = new LanPage(m_centralWidget);
m_wlanWidget = new WlanPage(m_centralWidget);
connect(this, &MainWindow::mainWindowVisibleChanged, m_wlanWidget, &WlanPage::onMainWindowVisibleChanged);
m_centralWidget->addTab(m_lanWidget, QIcon::fromTheme("network-wired-connected-symbolic", QIcon::fromTheme("network-wired-symbolic", QIcon(":/res/l/network-online.svg"))), tr("LAN"));
m_centralWidget->addTab(m_wlanWidget, QIcon::fromTheme("network-wireless-signal-excellent-symbolic", QIcon(":/res/x/wifi-list-bg.svg")), tr("WLAN"));
}

View File

@ -54,6 +54,7 @@ signals:
//热点断开
void hotspotDeactivated(QString devName, QString ssid);
void hotspotActivated(QString devName, QString ssid);
void mainWindowVisibleChanged(const bool &visible);
public slots:
protected:

View File

@ -34,10 +34,11 @@ void TabPage::initUI()
//临时增加的下拉框选择网卡区域
m_deviceFrame = new QFrame(this);
m_deviceLayout = new QHBoxLayout(m_deviceFrame);
m_deviceLayout->setContentsMargins(TITLE_LAYOUT_MARGINS);
m_deviceLayout->setContentsMargins(DEVICE_LAYOUT_MARGINS);
m_deviceFrame->setLayout(m_deviceLayout);
m_deviceLabel = new QLabel(m_deviceFrame);
m_deviceComboBox = new QComboBox(m_deviceFrame);
m_deviceComboBox->setFixedWidth(DEVICE_COMBOBOX_WIDTH);
m_deviceLabel->setText(tr("Current Device"));
m_deviceLayout->addWidget(m_deviceLabel);
m_deviceLayout->addStretch();
@ -45,6 +46,7 @@ void TabPage::initUI()
connect(m_deviceComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &TabPage::onDeviceComboxIndexChanged);
m_activatedNetFrame = new QFrame(this);
m_activatedNetFrame->setMaximumHeight(ACTIVE_AREA_MAX_HEIGHT);
m_activatedNetLayout = new QVBoxLayout(m_activatedNetFrame);
m_activatedNetLayout->setContentsMargins(ACTIVE_NET_LAYOUT_MARGINS);
m_activatedNetLayout->setSpacing(NET_LAYOUT_SPACING);

View File

@ -16,6 +16,8 @@
#define MAIN_LAYOUT_SPACING 0
#define TITLE_FRAME_HEIGHT 52
#define TITLE_LAYOUT_MARGINS 24,0,24,0
#define DEVICE_LAYOUT_MARGINS 24,0,24,8
#define DEVICE_COMBOBOX_WIDTH 150
#define ACTIVE_NET_LAYOUT_MARGINS 8,8,8,8
#define NET_LAYOUT_MARGINS 8,8,0,8
#define NET_LAYOUT_SPACING 8
@ -24,7 +26,8 @@
//#define SCROLL_AREA_HEIGHT 200
#define SETTINGS_LAYOUT_MARGINS 24,16,24,16
#define TRANSPARENT_COLOR QColor(0,0,0,0)
#define INACTIVE_AREA_MIN_HEIGHT 200
#define INACTIVE_AREA_MIN_HEIGHT 150
#define ACTIVE_AREA_MAX_HEIGHT 100
enum KyDeviceType
{

View File

@ -13,13 +13,13 @@ WlanPage::WlanPage(QWidget *parent) : TabPage(parent)
m_networkResourceInstance = KyNetworkResourceManager::getInstance();
m_netDeviceResource=new KyNetworkDeviceResourse(this);
m_apConnectResource = new KyConnectResourse(this);
devList.empty();
m_devList.empty();
initDevice();
m_wirelessConnectOpreation = new KyWirelessConnectOperation(this);
initWlanUI();
initDeviceCombox();
//要在initUI之后调用保证UI的信号槽顺利绑定
initConnections();
initTimer();
getActiveWlan();
getAllWlan();
@ -122,6 +122,13 @@ void WlanPage::initConnections()
m_netSwitch->setSwitchStatus(m_wirelessConnectOpreation->getWirelessEnabled());
}
void WlanPage::initTimer()
{
m_scanTimer = new QTimer(this);
connect(m_scanTimer, &QTimer::timeout, this, &WlanPage::requestScan);
// m_scanTimer->start(10 * 1000);
}
/**
* @brief WlanPage::initDevice
*/
@ -131,11 +138,11 @@ void WlanPage::initDevice()
m_settings->beginGroup("DEFAULTCARD");
QString key("wireless");
QString deviceName = m_settings->value(key, "").toString();
m_netDeviceResource->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, devList);
m_netDeviceResource->getNetworkDeviceList(NetworkManager::Device::Type::Wifi, m_devList);
if (deviceName.isEmpty()) {
qDebug() << "initDevice but defalut wireless card is null";
if (!devList.isEmpty()) {
deviceName = devList.at(0);
if (!m_devList.isEmpty()) {
deviceName = m_devList.at(0);
m_settings->setValue(key, deviceName);
}
}
@ -145,12 +152,21 @@ void WlanPage::initDevice()
m_settings->sync();
delete m_settings;
m_settings = nullptr;
//获取完m_devList后调用减少重复获取
initDeviceCombox();
}
void WlanPage::initDeviceCombox()
{
//TODO 获取设备列表,单设备时隐藏下拉框,多设备时添加到下拉框
if (m_devList.length() <= 1) {
m_deviceFrame->hide();
} else {
m_deviceFrame->show();
foreach (QString device, m_devList) {
m_deviceComboBox->addItem(device, device);
}
}
}
/**
@ -158,6 +174,9 @@ void WlanPage::initDeviceCombox()
*/
void WlanPage::getActiveWlan()
{
if (!m_activatedNetListWidget) {
return;
}
QMap<QString,QStringList> actMap;
m_activatedNetListWidget->clear();
m_resource->getWirelessActiveConnection(NetworkManager::ActiveConnection::State::Activated, actMap);
@ -211,6 +230,9 @@ void WlanPage::appendActiveWlan(const QString &ssid, int &height)
*/
void WlanPage::getAllWlan()
{
if (!m_inactivatedNetListWidget) {
return;
}
qDebug() << "Started loading wlan list! time=" << QDateTime::currentDateTime().toString("hh:mm:ss.zzzz");
m_inactivatedNetListWidget->clear();
m_itemsMap.clear();
@ -290,13 +312,22 @@ void WlanPage::onDeviceAdd(QString deviceName, NetworkManager::Device::Type devi
if (deviceType != NetworkManager::Device::Type::Wifi) {
return;
}
devList << deviceName;
m_devList << deviceName;
if (getDefaultDevice().isEmpty())
{
updateDefaultDevice(deviceName);
setDefaultDevice(WIRELESS, deviceName);
}
//往下拉框添加新的网卡
if (m_deviceComboBox->findData(deviceName) == -1) {
if (m_devList.length() > 1 && !m_deviceFrame->isVisible()) {
m_deviceFrame->show();
}
m_deviceComboBox->addItem(deviceName, deviceName);
}
emit deviceStatusChanged();
}
@ -315,10 +346,20 @@ void WlanPage::onDeviceRemove(QString deviceName)
updateDefaultDevice(newDefaultDevice);
setDefaultDevice(WIRELESS, newDefaultDevice);
}
if (devList.contains(deviceName)) {
devList.removeOne(deviceName);
if (m_devList.contains(deviceName)) {
m_devList.removeOne(deviceName);
emit deviceStatusChanged();
}
//从下拉框删除已消失的网卡
if (m_deviceComboBox->findData(deviceName) != -1) {
if (m_devList.length() <= 1 && m_deviceFrame->isVisible()) {
m_deviceFrame->hide();
}
m_deviceComboBox->removeItem(m_deviceComboBox->findData(deviceName));
}
}
void WlanPage::onDeviceNameUpdate(QString oldName, QString newName)
@ -328,12 +369,17 @@ void WlanPage::onDeviceNameUpdate(QString oldName, QString newName)
setDefaultDevice(WIRELESS, newName);
}
if (devList.contains(oldName)) {
devList.removeOne(oldName);
devList.append(newName);
if (m_devList.contains(oldName)) {
m_devList.removeOne(oldName);
m_devList.append(newName);
qDebug() << "WlanPage emit deviceNameUpdate " << oldName << newName;
emit deviceNameChanged(oldName, newName);
}
if (m_deviceComboBox->findData(oldName) != -1) {
m_deviceComboBox->removeItem(m_deviceComboBox->findData(oldName));
m_deviceComboBox->addItem(newName, newName);
}
}
void WlanPage::onActivatedWlanChanged(QString uuid, NetworkManager::ActiveConnection::State state, NetworkManager::ActiveConnection::Reason reason)
@ -449,7 +495,24 @@ void WlanPage::onWlanSwitchStatusChanged(const bool &checked)
void WlanPage::onDeviceComboxIndexChanged(int currentIndex)
{
if (!m_deviceComboBox || currentIndex < 0) {
return;
}
//TODO 设备变更时更新设备和列表
QString currentDevice = m_deviceComboBox->itemText(currentIndex);
qDebug() << "Current device changed! device = " << currentDevice << Q_FUNC_INFO << __LINE__;
defaultDevice = currentDevice;
getActiveWlan();
getAllWlan();
}
//申请触发扫描,初始化执行&定时执行
void WlanPage::requestScan()
{
if (!m_wirelessConnectOpreation) {
qWarning() << "Scan failed! m_wirelessConnectOpreation is nullptr!" << Q_FUNC_INFO << __LINE__;
}
m_wirelessConnectOpreation->requestWirelessScan();
}
@ -552,3 +615,23 @@ void WlanPage::deactivateWireless(const QString& devName, const QString& ssid)
{
//todo
}
void WlanPage::onMainWindowVisibleChanged(const bool &visible)
{
qDebug() << "Received signal of mainwindow visible changed. cur_state = " << visible << Q_FUNC_INFO << __LINE__;
if (visible) {
//打开页面时先触发一次扫描
requestScan();
}
if (!m_scanTimer) {
qWarning() << "No QTimer!" << Q_FUNC_INFO << __LINE__;
return;
}
//若页面打开,开始扫描倒计时,若关闭,停止扫描倒计时
if (visible) {
m_scanTimer->start(10 * 1000);
} else {
m_scanTimer->stop();
}
}

View File

@ -30,12 +30,16 @@ public:
void getStoredApInfo(QStringList &list);
void activateWireless(const QString& devName, const QString& ssid);
void deactivateWireless(const QString& devName, const QString& ssid);
signals:
void oneItemExpanded(const QString &ssid);
void wirelessActivating(QString devName, QString ssid);
void hotspotDeactivated(QString devName, QString ssid);
void hotspotActivated(QString devName, QString ssid);
public slots:
void onMainWindowVisibleChanged(const bool &visible);
protected:
bool eventFilter(QObject *watched, QEvent *event);
@ -43,6 +47,10 @@ private:
void initWlanUI();
void initConnections();
//定时触发扫描的定时器
void initTimer();
QTimer * m_scanTimer = nullptr;
void initDevice();//初始化默认设备
void initDeviceCombox();
@ -59,7 +67,7 @@ private:
QLabel * m_hiddenWlanLabel = nullptr;
QString m_activatedWlanSSid;
QStringList devList;
QStringList m_devList;
KyWirelessNetResource *m_resource = nullptr;
KyActiveConnectResourse *m_connectResource = nullptr;
@ -86,6 +94,7 @@ private slots:
void onWlanSwitchClicked();
void onWlanSwitchStatusChanged(const bool &checked);
void onDeviceComboxIndexChanged(int currentIndex);
void requestScan();
};
#endif // WLANPAGE_H

View File

@ -42,7 +42,6 @@ void RadioItemButton::setButtonIcon(const QIcon &icon)
if (icon.isNull()) {
return;
}
//ZJP_TODO 绘制圆形按钮
m_iconLabel->setPixmap(icon.pixmap(ICON_SIZE));
}