diff --git a/src/frontend/tab-pages/wlanpage.cpp b/src/frontend/tab-pages/wlanpage.cpp index 709f52d6..66edaa99 100644 --- a/src/frontend/tab-pages/wlanpage.cpp +++ b/src/frontend/tab-pages/wlanpage.cpp @@ -139,6 +139,7 @@ void WlanPage::initConnections() if (key == WIRELESS_SWITCH) { bool status = m_switchGsettings->get(WIRELESS_SWITCH).toBool(); m_wirelessConnectOpreation->setWirelessEnabled(status); + m_netSwitch->setSwitchStatus(m_switchGsettings->get(WIRELESS_SWITCH).toBool()); onWlanSwitchStatusChanged(m_switchGsettings->get(WIRELESS_SWITCH).toBool()); } }); @@ -171,10 +172,10 @@ void WlanPage::initDevice() deviceName = m_devList.at(0); m_settings->setValue(key, deviceName); } else { - qDebug() << "have no device to use " << Q_FUNC_INFO << __LINE__; - //检测不到无线网卡不再触发click信号 - m_netSwitch->setSwitchStatus(false); - m_netSwitch->setEnabled(false); + qDebug() << "have no device to use " << Q_FUNC_INFO << __LINE__; + //检测不到无线网卡不再触发click信号 + m_netSwitch->setSwitchStatus(false); + m_netSwitch->setEnabled(false); } updateDefaultDevice(deviceName); qDebug() << "[WlanPage] initDevice defaultDevice = " << deviceName; @@ -343,8 +344,7 @@ int WlanPage::getIndexByStrength(QString interface, QString ssid) { QList wlanList; m_resource->getDeviceWifiNetwork(interface, wlanList); - for (int i=0; iitemWidget((m_itemsMap.value(ssid)).first)->height(); - m_inactivatedNetListWidget->takeItem(m_inactivatedNetListWidget->row((m_itemsMap.value(ssid)).first)); - m_inactivatedNetListWidget->setFixedHeight(m_inactivatedNetListWidget->height() - height - NET_LIST_SPACING); - m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); + if (m_itemsMap.value(ssid).second) { + m_inactivatedNetListWidget->takeItem(m_inactivatedNetListWidget->row((m_itemsMap.value(ssid)).first)); + int height = m_itemsMap.value(ssid).second->height(); + m_inactivatedNetListWidget->setFixedHeight(m_inactivatedNetListWidget->height() - height - NET_LIST_SPACING); + m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); + } m_itemsMap.remove(ssid); } @@ -680,151 +682,51 @@ void WlanPage::onWifiEnabledChanged(bool isWifiOn) } } -void WlanPage::onRefreshIconTimer() -{ - //wifi页面打开时,每隔5s主动获取wifi信号强度对图标进行更新 - qDebug() << "refresh wifi icon" << Q_FUNC_INFO << __LINE__; -// QList wlanList; -// if (!m_resource->getDeviceWifiNetwork(m_defaultDevice, wlanList)) { -// return; -// } -// foreach (auto itemData, wlanList) { -// qDebug() << "pair" << itemData.m_NetSsid << m_itemsMap.value(itemData.m_NetSsid) << "refresh wifi icon secsess"<< Q_FUNC_INFO << __LINE__; -// WlanListItem *wlanListItem = nullptr; -// wlanListItem = (m_itemsMap.value(itemData.m_NetSsid)).second; -// if (wlanListItem) { -// qDebug() << "refresh wifi icon secsess"<< Q_FUNC_INFO << __LINE__; -// wlanListItem->setWlanSignal(itemData.m_signalStrength); -// } else { -// qWarning() << "Set wifi signal failed because of null pointer wlanListItem!" << Q_FUNC_INFO << __LINE__; -// } -// } -// updateByStrength(); - qDebug() << "updateByStrength"; - QList wlanList; - if (!m_resource->getDeviceWifiNetwork(m_defaultDevice, wlanList)) { - return; - } - int height = 0; - qDebug() << "tttttotal" << wlanList.count(); - - int row = 0; - int j=0; - for (int i=0; im_activatedWlanSSid) { - continue; - } - qDebug() << "onRefreshIconTimer-1" << i; - qDebug() << "onRefreshIconTimer0 wwwwwwwlan:" << wlanList.at(i).m_NetSsid; - QMap>::iterator iter; - for (iter = m_itemsMap.begin(); iter != m_itemsMap.constEnd(); ++iter,j++) { - if (wlanList.at(i).m_NetSsid == iter.key()) { - if (j == row) { - continue; - } - qDebug() << "onRefreshIconTimer1 " << row << m_itemsMap.value(iter.key()).second->m_data->m_signalStrength; - QListWidgetItem *tempItem = m_itemsMap.value(iter.key()).first; - WlanListItem *tempWidget = m_itemsMap.value(iter.key()).second; - QModelIndex index = m_inactivatedNetListWidget->model()->index(row, 0, QModelIndex()); //new - if (tempItem) { - QModelIndex lastIndex = m_inactivatedNetListWidget->model()->index(m_inactivatedNetListWidget->row(tempItem), 0, QModelIndex()); -// QModelIndex lastIndex = m_inactivatedNetListWidget->model()->index(1, 0, QModelIndex()); - qDebug() << "onRefreshIconTimer2 " << m_inactivatedNetListWidget->row(tempItem) << row; - m_inactivatedNetListWidget->setIndexWidget(lastIndex, m_inactivatedNetListWidget->indexWidget(index)); //把第0个放下来 - m_inactivatedNetListWidget->setIndexWidget(index, tempWidget); //把信号最强的放到0行 - - QString indexSsid = static_cast(m_inactivatedNetListWidget->indexWidget(lastIndex))->getSsid(); - QString lastSsid = static_cast(m_inactivatedNetListWidget->indexWidget(index))->getSsid(); - qDebug() << "onRefreshIconTimer3 " << indexSsid << lastSsid; - QPair curPair = m_itemsMap.value(iter.key()); - QPair lastPair = m_itemsMap.value(lastSsid); - qDebug() << "onRefreshIconTimern " << curPair.second->m_data->m_NetSsid << lastPair.second->m_data->m_NetSsid; - curPair.second = lastPair.second; - lastPair.second = tempWidget; - m_itemsMap[iter.key()] = curPair; - m_itemsMap[lastSsid] = lastPair; - row++; - } - } - - } - } -// m_inactivatedNetListWidget->setFixedHeight(height); -// m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); - - QMap>::iterator i; - qDebug() << "items:" << m_itemsMap.count(); - for (i = m_itemsMap.begin(); i != m_itemsMap.constEnd(); ++i) { - qDebug() << "item" <m_data; - } - qDebug() << "eeeeeeeeend"; -} - void WlanPage::updateByStrength() { - qDebug() << "update By Strength"; + qDebug() << "Will update Wlan list by strength." << Q_FUNC_INFO << __LINE__; QList wlanList; if (!m_resource->getDeviceWifiNetwork(m_defaultDevice, wlanList)) { return; } - int height = 0; - qDebug() << "tttttotal" << wlanList.count(); - - QMap>::iterator iters; - qDebug() << "items:" << m_itemsMap.count(); - for (iters = m_itemsMap.begin(); iters != m_itemsMap.constEnd(); ++iters) { - qDebug() << "item" <m_data; - } - - int row=0; - int j=0; - for (int i=0; im_activatedWlanSSid) { +// int height = 0; + int currentRow = 0; + for (int i = 0; i < wlanList.length(); i ++) { + if (wlanList.at(i).m_NetSsid == this->m_activatedWlanSSid) { //排除已连接WiFi continue; } - qDebug() << "i:" << i; - QMap>::iterator iter; - for (iter = m_itemsMap.begin(); iter != m_itemsMap.constEnd(); ++iter,j++) { - qDebug() << wlanList.at(i).m_NetSsid << wlanList.at(i).m_signalStrength; - if (wlanList.at(i).m_NetSsid == iter.key()) { - if (j==row) { - continue; - } - m_inactivatedNetListWidget->removeItemWidget(iter.value().first); - delete iter.value().first; - QListWidgetItem *listwidgetItem = new QListWidgetItem(); -// QListWidgetItem *listwidgetItem = iter.value().first; - qDebug() << "QListWidgetItem" << listwidgetItem; -// *(iter.value().second->m_data) = wlanList.at(i); - KyWirelessNetItem itemData = wlanList.at(i); - WlanListItem *wlanWidget = new WlanListItem(m_resource, &itemData, m_defaultDevice); - wlanWidget->setWlanSignal(itemData.m_signalStrength); - connect(wlanWidget, &WlanListItem::itemHeightChanged, this, &WlanPage::onItemHeightChanged); - connect(wlanWidget, &WlanListItem::connectButtonClicked, this, &WlanPage::onConnectButtonClicked); - listwidgetItem->setSizeHint(QSize(m_inactivatedNetListWidget->width(), wlanWidget->height())); - m_inactivatedNetListWidget->insertItem(row, listwidgetItem); - qDebug() << "iiinsert" << m_inactivatedNetListWidget->row(listwidgetItem); - m_inactivatedNetListWidget->setItemWidget(listwidgetItem, wlanWidget); - QPair pair (listwidgetItem, wlanWidget); - m_itemsMap.insert(iter.key(), pair); - row++; - - if (height == 0) { - height += m_itemsMap[iter.key()].second->height(); - } - height += m_itemsMap[iter.key()].second->height() + NET_LIST_SPACING; - } + QString currentSsid = wlanList.at(i).m_NetSsid; //应该在第currentRow行的新的WiFi名称 + QModelIndex currentIndex = m_inactivatedNetListWidget->model()->index(currentRow, 0, QModelIndex()); //当前行的index + WlanListItem * lastWlan = static_cast(m_inactivatedNetListWidget->indexWidget(currentIndex)); //原来在第currentRow行的WlanListItem + if (lastWlan && currentSsid == lastWlan->getSsid()) { //WiFi的排序未改变,不需要修改,继续遍历 + currentRow ++; + continue; + } else if (!lastWlan) { //已经超出原列表长度了,需要new一个放上去 + KyWirelessNetItem *data = new KyWirelessNetItem(wlanList.at(i)); + WlanListItem *wlanItemWidget = new WlanListItem(m_resource, data, m_defaultDevice); + QListWidgetItem *wlanItem = new QListWidgetItem(); + connect(wlanItemWidget, &WlanListItem::itemHeightChanged, this, &WlanPage::onItemHeightChanged); + connect(wlanItemWidget, &WlanListItem::connectButtonClicked, this, &WlanPage::onConnectButtonClicked); + QPair pair (wlanItem, wlanItemWidget); + m_itemsMap.insert(data->m_NetSsid, pair); + wlanItem->setSizeHint(QSize(m_inactivatedNetListWidget->width(), wlanItemWidget->height())); + m_inactivatedNetListWidget->addItem(wlanItem); + m_inactivatedNetListWidget->setItemWidget(wlanItem, wlanItemWidget); + int height = m_inactivatedNetListWidget->height() + wlanItemWidget->height() + NET_LIST_SPACING; + m_inactivatedNetListWidget->setFixedHeight(height); + m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); + } else {//找到了该位置的wifi而且与现在的排序不符,需要调整 + KyWirelessNetItem *data = new KyWirelessNetItem(wlanList.at(i)); + WlanListItem * currentWlan = new WlanListItem(m_resource, data, m_defaultDevice); + QPair newPair; + newPair.first = m_itemsMap.value(lastWlan->getSsid()).first; + newPair.second = currentWlan; + m_itemsMap[currentSsid] = newPair;//先把map中的当前行wlan替换掉,第currentRow行的ssid->currentRow行的控件 + m_itemsMap[lastWlan->getSsid()] = newPair;//临时使原来指向第currentRow行的的wlan依然指向这个value,防止访问空指针的情况发生,等遍历到该ssid时会赋予其正确的value + m_inactivatedNetListWidget->setIndexWidget(currentIndex, currentWlan); //最后把新的wlan填充到currentRow行 } - m_inactivatedNetListWidget->setFixedHeight(height); - m_inactivatedWlanListAreaCentralWidget->setFixedHeight(m_inactivatedNetListWidget->height() + m_hiddenWlanLabel->height()); + currentRow ++; } - - QMap>::iterator i; - qDebug() << "items:" << m_itemsMap.count(); - for (i = m_itemsMap.begin(); i != m_itemsMap.constEnd(); ++i) { - qDebug() << "item" <m_data; - } - qDebug() << "eeeeeeeeend"; } diff --git a/src/frontend/tab-pages/wlanpage.h b/src/frontend/tab-pages/wlanpage.h index 47a6ab48..f232e844 100644 --- a/src/frontend/tab-pages/wlanpage.h +++ b/src/frontend/tab-pages/wlanpage.h @@ -124,7 +124,7 @@ private slots: void showControlCenter(); void onWifiEnabledChanged(bool isWifiOn); - void onRefreshIconTimer(); +// void onRefreshIconTimer(); // void onsignalStrengthChange(QString, QString, int); diff --git a/src/main.cpp b/src/main.cpp index 080f03da..e97b2b9a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) // QApplication a(argc, argv); QString id = QString("kylin-nm"+ QLatin1String(getenv("DISPLAY"))); QtSingleApplication a(id, argc, argv); -// qInstallMessageHandler(messageOutput); + qInstallMessageHandler(messageOutput); if (a.isRunning()) { auto connection = QDBusConnection::sessionBus(); QDBusInterface iface("com.kylin.network",