Feat:Add wifi sort.

This commit is contained in:
zhangjiaping 2021-09-28 16:37:31 +08:00
parent ef365fa6bc
commit 0af291831c
3 changed files with 49 additions and 147 deletions

View File

@ -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<KyWirelessNetItem> wlanList;
m_resource->getDeviceWifiNetwork(interface, wlanList);
for (int i=0; i<wlanList.count(); i++) {
qDebug() << "aaaaaa" << wlanList.at(i).m_NetSsid << wlanList.at(i).m_signalStrength << i;
for (int i = 0; i< wlanList.count(); i++) {
if (wlanList.at(i).m_NetSsid == ssid) {
qDebug() << "append" << wlanList.at(i).m_NetSsid << i;
return i;
@ -360,10 +360,12 @@ void WlanPage::onWlanRemoved(QString interface, QString ssid)
if (interface != m_defaultDevice) {
qDebug() << "wlan remove interface not equal defaultdevice,ignore";
}
int height = m_inactivatedNetListWidget->itemWidget((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<KyWirelessNetItem> 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<KyWirelessNetItem> 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; i<wlanList.count();i++) {
if (wlanList.at(i).m_NetSsid == this->m_activatedWlanSSid) {
continue;
}
qDebug() << "onRefreshIconTimer-1" << i;
qDebug() << "onRefreshIconTimer0 wwwwwwwlan:" << wlanList.at(i).m_NetSsid;
QMap<QString, QPair<QListWidgetItem*, WlanListItem*>>::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<WlanListItem *>(m_inactivatedNetListWidget->indexWidget(lastIndex))->getSsid();
QString lastSsid = static_cast<WlanListItem *>(m_inactivatedNetListWidget->indexWidget(index))->getSsid();
qDebug() << "onRefreshIconTimer3 " << indexSsid << lastSsid;
QPair<QListWidgetItem*, WlanListItem*> curPair = m_itemsMap.value(iter.key());
QPair<QListWidgetItem*, WlanListItem*> 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<QString, QPair<QListWidgetItem*, WlanListItem*>>::iterator i;
qDebug() << "items:" << m_itemsMap.count();
for (i = m_itemsMap.begin(); i != m_itemsMap.constEnd(); ++i) {
qDebug() << "item" <<i.key() << i.value().first << m_itemsMap[i.key()].second->m_data;
}
qDebug() << "eeeeeeeeend";
}
void WlanPage::updateByStrength()
{
qDebug() << "update By Strength";
qDebug() << "Will update Wlan list by strength." << Q_FUNC_INFO << __LINE__;
QList<KyWirelessNetItem> wlanList;
if (!m_resource->getDeviceWifiNetwork(m_defaultDevice, wlanList)) {
return;
}
int height = 0;
qDebug() << "tttttotal" << wlanList.count();
QMap<QString, QPair<QListWidgetItem*, WlanListItem*>>::iterator iters;
qDebug() << "items:" << m_itemsMap.count();
for (iters = m_itemsMap.begin(); iters != m_itemsMap.constEnd(); ++iters) {
qDebug() << "item" <<iters.key() << iters.value().first << m_itemsMap[iters.key()].second->m_data;
}
int row=0;
int j=0;
for (int i=0; i<wlanList.count(); i++) {
if (wlanList.at(i).m_NetSsid == this->m_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<QString, QPair<QListWidgetItem*, WlanListItem*>>::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<QListWidgetItem*, WlanListItem*> 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<WlanListItem *>(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<QListWidgetItem*,WlanListItem*> 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<QListWidgetItem*, WlanListItem*> 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<QString, QPair<QListWidgetItem*, WlanListItem*>>::iterator i;
qDebug() << "items:" << m_itemsMap.count();
for (i = m_itemsMap.begin(); i != m_itemsMap.constEnd(); ++i) {
qDebug() << "item" <<i.key() << i.value().first << m_itemsMap[i.key()].second->m_data;
}
qDebug() << "eeeeeeeeend";
}

View File

@ -124,7 +124,7 @@ private slots:
void showControlCenter();
void onWifiEnabledChanged(bool isWifiOn);
void onRefreshIconTimer();
// void onRefreshIconTimer();
// void onsignalStrengthChange(QString, QString, int);

View File

@ -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",